OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 """Generator for C++ structs from api json files. | 5 """Generator for C++ structs from api json files. |
6 | 6 |
7 The purpose of this tool is to remove the need for hand-written code that | 7 The purpose of this tool is to remove the need for hand-written code that |
8 converts to and from base::Value types when receiving javascript api calls. | 8 converts to and from base::Value types when receiving javascript api calls. |
9 Originally written for generating code for extension apis. Reference schemas | 9 Originally written for generating code for extension apis. Reference schemas |
10 are in chrome/common/extensions/api. | 10 are in chrome/common/extensions/api. |
11 | 11 |
12 Usage example: | 12 Usage example: |
13 compiler.py --root /home/Work/src --namespace extensions windows.json | 13 compiler.py --root /home/Work/src --namespace extensions windows.json |
14 tabs.json | 14 tabs.json |
15 compiler.py --destdir gen --root /home/Work/src | 15 compiler.py --destdir gen --root /home/Work/src |
16 --namespace extensions windows.json tabs.json | 16 --namespace extensions windows.json tabs.json |
17 """ | 17 """ |
18 | 18 |
19 from cpp_generator import CppGenerator | 19 from cpp_generator import CppGenerator |
20 from cpp_type_generator import CppTypeGenerator | 20 from cpp_type_generator import CppTypeGenerator |
21 from dart_generator import DartGenerator | 21 from dart_generator import DartGenerator |
22 from cpp_bundle_generator import CppBundleGenerator | 22 from cpp_bundle_generator import CppBundleGenerator |
23 from model import Model | 23 from model import Model, UnixName |
24 import idl_schema | 24 import idl_schema |
25 import json_schema | 25 import json_schema |
26 | 26 |
27 import optparse | 27 import optparse |
28 import os.path | 28 import os.path |
29 import sys | 29 import sys |
30 | 30 |
31 # Names of supported code generators, as specified on the command-line. | 31 # Names of supported code generators, as specified on the command-line. |
32 # First is default. | 32 # First is default. |
33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] | 33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 for target_namespace in api_defs: | 80 for target_namespace in api_defs: |
81 for referenced_schema in target_namespace.get('dependencies', []): | 81 for referenced_schema in target_namespace.get('dependencies', []): |
82 split_schema = referenced_schema.split(':', 1) | 82 split_schema = referenced_schema.split(':', 1) |
83 if len(split_schema) > 1: | 83 if len(split_schema) > 1: |
84 if split_schema[0] != 'api': | 84 if split_schema[0] != 'api': |
85 continue | 85 continue |
86 else: | 86 else: |
87 referenced_schema = split_schema[1] | 87 referenced_schema = split_schema[1] |
88 | 88 |
89 referenced_schema_path = os.path.join( | 89 referenced_schema_path = os.path.join( |
90 os.path.dirname(schema), referenced_schema + '.json') | 90 os.path.dirname(schema), '%s.json' % UnixName(referenced_schema)) |
91 referenced_api_defs = json_schema.Load(referenced_schema_path) | 91 referenced_api_defs = json_schema.Load(referenced_schema_path) |
92 | 92 |
93 for namespace in referenced_api_defs: | 93 for namespace in referenced_api_defs: |
94 api_model.AddNamespace( | 94 api_model.AddNamespace( |
95 namespace, | 95 namespace, |
96 os.path.relpath(referenced_schema_path, root), | 96 os.path.relpath(referenced_schema_path, root), |
97 include_compiler_options=True) | 97 include_compiler_options=True) |
98 | 98 |
99 # For single-schema compilation make sure that the first (i.e. only) schema | 99 # For single-schema compilation make sure that the first (i.e. only) schema |
100 # is the default one. | 100 # is the default one. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 # Unless in bundle mode, only one file should be specified. | 190 # Unless in bundle mode, only one file should be specified. |
191 if opts.generator != 'cpp-bundle' and len(filenames) > 1: | 191 if opts.generator != 'cpp-bundle' and len(filenames) > 1: |
192 # TODO(sashab): Could also just use filenames[0] here and not complain. | 192 # TODO(sashab): Could also just use filenames[0] here and not complain. |
193 raise Exception( | 193 raise Exception( |
194 "Unless in bundle mode, only one file can be specified at a time.") | 194 "Unless in bundle mode, only one file can be specified at a time.") |
195 | 195 |
196 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, | 196 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, |
197 opts.namespace, opts.dart_overrides_dir) | 197 opts.namespace, opts.dart_overrides_dir) |
198 if not opts.destdir: | 198 if not opts.destdir: |
199 print result | 199 print result |
OLD | NEW |