| 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 |
| 8 """ |
| 9 Usage: gn_to_cmake.py <json_file_name> |
| 10 |
| 11 gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py |
| 12 |
| 13 or |
| 14 |
| 15 gn gen out/config --ide=json |
| 16 python gn/gn_to_cmake.py out/config/project.json |
| 17 """ |
| 18 |
| 19 import json |
| 20 import posixpath |
| 21 import os |
| 22 import sys |
| 23 |
| 24 def write_project(project): |
| 25 |
| 26 def get_base_name(target_name): |
| 27 base_name = posixpath.basename(target_name) |
| 28 sep = base_name.rfind(":") |
| 29 if sep != -1: |
| 30 base_name = base_name[sep+1:] |
| 31 return base_name |
| 32 |
| 33 def get_output_name(target_name, target_properties): |
| 34 output_name = target_properties.get("output_name", None) |
| 35 if output_name is None: |
| 36 output_name = get_base_name(target_name) |
| 37 |
| 38 output_extension = target_properties.get("output_extension", None) |
| 39 if output_extension is not None: |
| 40 output_name = posixpath.splitext(output_name)[0] |
| 41 if len(output_extension): |
| 42 output_name += "." + output_extension |
| 43 |
| 44 return output_name |
| 45 |
| 46 def get_absolute_path(root_path, path): |
| 47 if path.startswith("//"): |
| 48 return root_path + "/" + path[2:] |
| 49 else: |
| 50 return path |
| 51 |
| 52 build_settings = project['build_settings'] |
| 53 root_path = build_settings['root_path'] |
| 54 build_path = os.path.join(root_path, build_settings['build_dir'][2:]) |
| 55 |
| 56 out = open(os.path.join(build_path, 'CMakeLists.txt'), 'w+') |
| 57 out.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n') |
| 58 out.write('cmake_policy(VERSION 2.8.8)\n') |
| 59 |
| 60 for target_name, target_properties in project['targets'].items(): |
| 61 sources = target_properties.get('sources', []) |
| 62 if not sources: |
| 63 continue |
| 64 |
| 65 target_output_name = get_output_name(target_name, target_properties) |
| 66 out.write('\n') |
| 67 out.write('add_library(') |
| 68 out.write(target_output_name) |
| 69 out.write(' STATIC\n') |
| 70 for source in sources: |
| 71 out.write(' "') |
| 72 out.write(get_absolute_path(root_path, source)) |
| 73 out.write('"\n') |
| 74 out.write(')\n') |
| 75 |
| 76 out.write('set_target_properties(') |
| 77 out.write(target_output_name) |
| 78 out.write(' PROPERTIES EXCLUDE_FROM_ALL "TRUE")\n') |
| 79 |
| 80 out.write('set_property(TARGET ') |
| 81 out.write(target_output_name) |
| 82 out.write(' APPEND PROPERTY INCLUDE_DIRECTORIES\n') |
| 83 for include_dir in target_properties.get('include_dirs', []): |
| 84 out.write(' "') |
| 85 out.write(get_absolute_path(root_path, include_dir)) |
| 86 out.write('"\n') |
| 87 out.write(')\n') |
| 88 |
| 89 out.write('set_target_properties(') |
| 90 out.write(target_output_name) |
| 91 out.write(' PROPERTIES COMPILE_DEFINITIONS "') |
| 92 for define in target_properties.get('defines', []): |
| 93 out.write(define) |
| 94 out.write(';') |
| 95 out.write('")\n') |
| 96 |
| 97 def main(): |
| 98 if len(sys.argv) != 2: |
| 99 print('Usage: ' + sys.argv[0] + ' <json_file_name>') |
| 100 exit(1) |
| 101 |
| 102 json_path = sys.argv[1] |
| 103 project = None |
| 104 with open(json_path, 'r') as json_file: |
| 105 project = json.loads(json_file.read()) |
| 106 |
| 107 write_project(project) |
| 108 |
| 109 if __name__ == "__main__": |
| 110 main() |
| OLD | NEW |