aboutsummaryrefslogtreecommitdiffhomepage
path: root/gn
diff options
context:
space:
mode:
authorGravatar bungeman <bungeman@google.com>2016-09-23 08:16:04 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-09-23 08:16:04 -0700
commit623ef92e4fd7d79786f828844aade96bc3578e0e (patch)
treecf7b9ad3a2f2a9f636babb64839f3f155b056217 /gn
parent53c38087949252d27cde668368a3eeb59cc2eb00 (diff)
Json ide script for gn to output cmake.
Can be run like gn gen out/debug --ide=json --json-ide-script=../../gn/gn_to_cmake.py or gn gen out/debug --ide=json python gn/gn_to_cmake.py out/debug/project.json This first pass creates CMakeLists.txt with just enough information for an ide. Each target is just a static library with sources, include directories, and defines. NOTRY=true This isn't automatically tested. Review-Url: https://codereview.chromium.org/2363873002
Diffstat (limited to 'gn')
-rw-r--r--gn/gn_to_cmake.py110
1 files changed, 110 insertions, 0 deletions
diff --git a/gn/gn_to_cmake.py b/gn/gn_to_cmake.py
new file mode 100644
index 0000000000..07720c6e37
--- /dev/null
+++ b/gn/gn_to_cmake.py
@@ -0,0 +1,110 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 Google Inc.
+#
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""
+Usage: gn_to_cmake.py <json_file_name>
+
+gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py
+
+or
+
+gn gen out/config --ide=json
+python gn/gn_to_cmake.py out/config/project.json
+"""
+
+import json
+import posixpath
+import os
+import sys
+
+def write_project(project):
+
+ def get_base_name(target_name):
+ base_name = posixpath.basename(target_name)
+ sep = base_name.rfind(":")
+ if sep != -1:
+ base_name = base_name[sep+1:]
+ return base_name
+
+ def get_output_name(target_name, target_properties):
+ output_name = target_properties.get("output_name", None)
+ if output_name is None:
+ output_name = get_base_name(target_name)
+
+ output_extension = target_properties.get("output_extension", None)
+ if output_extension is not None:
+ output_name = posixpath.splitext(output_name)[0]
+ if len(output_extension):
+ output_name += "." + output_extension
+
+ return output_name
+
+ def get_absolute_path(root_path, path):
+ if path.startswith("//"):
+ return root_path + "/" + path[2:]
+ else:
+ return path
+
+ build_settings = project['build_settings']
+ root_path = build_settings['root_path']
+ build_path = os.path.join(root_path, build_settings['build_dir'][2:])
+
+ out = open(os.path.join(build_path, 'CMakeLists.txt'), 'w+')
+ out.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n')
+ out.write('cmake_policy(VERSION 2.8.8)\n')
+
+ for target_name, target_properties in project['targets'].items():
+ sources = target_properties.get('sources', [])
+ if not sources:
+ continue
+
+ target_output_name = get_output_name(target_name, target_properties)
+ out.write('\n')
+ out.write('add_library(')
+ out.write(target_output_name)
+ out.write(' STATIC\n')
+ for source in sources:
+ out.write(' "')
+ out.write(get_absolute_path(root_path, source))
+ out.write('"\n')
+ out.write(')\n')
+
+ out.write('set_target_properties(')
+ out.write(target_output_name)
+ out.write(' PROPERTIES EXCLUDE_FROM_ALL "TRUE")\n')
+
+ out.write('set_property(TARGET ')
+ out.write(target_output_name)
+ out.write(' APPEND PROPERTY INCLUDE_DIRECTORIES\n')
+ for include_dir in target_properties.get('include_dirs', []):
+ out.write(' "')
+ out.write(get_absolute_path(root_path, include_dir))
+ out.write('"\n')
+ out.write(')\n')
+
+ out.write('set_target_properties(')
+ out.write(target_output_name)
+ out.write(' PROPERTIES COMPILE_DEFINITIONS "')
+ for define in target_properties.get('defines', []):
+ out.write(define)
+ out.write(';')
+ out.write('")\n')
+
+def main():
+ if len(sys.argv) != 2:
+ print('Usage: ' + sys.argv[0] + ' <json_file_name>')
+ exit(1)
+
+ json_path = sys.argv[1]
+ project = None
+ with open(json_path, 'r') as json_file:
+ project = json.loads(json_file.read())
+
+ write_project(project)
+
+if __name__ == "__main__":
+ main()