OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright 2016 Google Inc. | |
4 # | |
5 # Use of this source code is governed by a BSD-style license that can be | |
6 # found in the LICENSE file. | |
7 | |
herb_g
2016/09/23 14:41:21
Comment showing typical use.
bungeman-skia
2016/09/23 15:12:07
Done.
| |
8 import json | |
9 import posixpath | |
10 import os | |
11 import sys | |
12 | |
13 def write_project(project): | |
14 | |
15 def get_base_name(target_name): | |
16 base_name = posixpath.basename(target_name) | |
17 sep = base_name.rfind(":") | |
18 if sep != -1: | |
19 base_name = base_name[sep+1:] | |
20 return base_name | |
21 | |
22 def get_output_name(target_name, target_properties): | |
23 output_name = target_properties.get("output_name", None) | |
24 if output_name is None: | |
25 output_name = get_base_name(target_name) | |
26 | |
27 output_extension = target_properties.get("output_extension", None) | |
28 if output_extension is not None: | |
29 output_name = posixpath.splitext(output_name)[0] | |
30 if len(output_extension): | |
31 output_name += "." + output_extension | |
32 | |
33 return output_name | |
34 | |
35 def get_absolute_path(root_path, path): | |
36 if path.startswith("//"): | |
37 return root_path + "/" + path[2:] | |
38 else: | |
39 return path | |
40 | |
41 build_settings = project['build_settings'] | |
42 root_path = build_settings['root_path'] | |
43 build_path = os.path.join(root_path, build_settings['build_dir'][2:]) | |
44 | |
45 out = open(os.path.join(build_path, 'CMakeLists.txt'), 'w+') | |
46 out.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n') | |
47 out.write('cmake_policy(VERSION 2.8.8)\n') | |
48 | |
49 for target_name, target_properties in project['targets'].items(): | |
50 sources = target_properties.get('sources', []) | |
51 if not sources: | |
52 continue | |
53 | |
54 target_output_name = get_output_name(target_name, target_properties) | |
55 out.write('\n') | |
56 out.write('add_library(') | |
57 out.write(target_output_name) | |
58 out.write(' STATIC\n') | |
59 for source in sources: | |
60 out.write(' "') | |
61 out.write(get_absolute_path(root_path, source)) | |
62 out.write('"\n') | |
63 out.write(')\n') | |
64 | |
65 out.write('set_target_properties(') | |
66 out.write(target_output_name) | |
67 out.write(' PROPERTIES EXCLUDE_FROM_ALL "TRUE")\n') | |
68 | |
69 out.write('set_property(TARGET ') | |
70 out.write(target_output_name) | |
71 out.write(' APPEND PROPERTY INCLUDE_DIRECTORIES\n') | |
72 for include_dir in target_properties.get('include_dirs', []): | |
73 out.write(' "') | |
74 out.write(get_absolute_path(root_path, include_dir)) | |
75 out.write('"\n') | |
76 out.write(')\n') | |
77 | |
78 out.write('set_target_properties(') | |
79 out.write(target_output_name) | |
80 out.write(' PROPERTIES COMPILE_DEFINITIONS "') | |
81 for define in target_properties.get('defines', []): | |
82 out.write(define) | |
83 out.write(';') | |
84 out.write('")\n') | |
85 | |
86 def main(): | |
87 if len(sys.argv) != 2: | |
88 print('Usage: ' + sys.argv[0] + ' <json_file_name>') | |
89 exit(1) | |
90 | |
91 json_path = sys.argv[1] | |
92 project = None | |
93 with open(json_path, 'r') as json_file: | |
94 project = json.loads(json_file.read()) | |
95 | |
96 write_project(project) | |
97 | |
98 if __name__ == "__main__": | |
99 main() | |
OLD | NEW |