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 import optparse | 19 import optparse |
20 import os | 20 import os |
21 import shlex | 21 import shlex |
22 import sys | 22 import sys |
23 | 23 |
24 from cpp_bundle_generator import CppBundleGenerator | 24 from cpp_bundle_generator import CppBundleGenerator |
25 from cpp_generator import CppGenerator | 25 from cpp_generator import CppGenerator |
26 from cpp_type_generator import CppTypeGenerator | 26 from cpp_type_generator import CppTypeGenerator |
27 from dart_generator import DartGenerator | |
28 import json_schema | 27 import json_schema |
29 from cpp_namespace_environment import CppNamespaceEnvironment | 28 from cpp_namespace_environment import CppNamespaceEnvironment |
30 from model import Model | 29 from model import Model |
31 from schema_loader import SchemaLoader | 30 from schema_loader import SchemaLoader |
32 | 31 |
33 # Names of supported code generators, as specified on the command-line. | 32 # Names of supported code generators, as specified on the command-line. |
34 # First is default. | 33 # First is default. |
35 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'dart'] | 34 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema'] |
36 | 35 |
37 def GenerateSchema(generator_name, | 36 def GenerateSchema(generator_name, |
38 file_paths, | 37 file_paths, |
39 root, | 38 root, |
40 destdir, | 39 destdir, |
41 cpp_namespace_pattern, | 40 cpp_namespace_pattern, |
42 dart_overrides_dir, | |
43 impl_dir, | 41 impl_dir, |
44 include_rules): | 42 include_rules): |
45 # Merge the source files into a single list of schemas. | 43 # Merge the source files into a single list of schemas. |
46 api_defs = [] | 44 api_defs = [] |
47 for file_path in file_paths: | 45 for file_path in file_paths: |
48 schema = os.path.relpath(file_path, root) | 46 schema = os.path.relpath(file_path, root) |
49 schema_loader = SchemaLoader( | 47 schema_loader = SchemaLoader( |
50 root, | 48 root, |
51 os.path.dirname(schema), | 49 os.path.dirname(schema), |
52 include_rules, | 50 include_rules, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 generators = [ | 108 generators = [ |
111 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), | 109 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), |
112 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) | 110 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) |
113 ] | 111 ] |
114 elif generator_name == 'cpp': | 112 elif generator_name == 'cpp': |
115 cpp_generator = CppGenerator(type_generator) | 113 cpp_generator = CppGenerator(type_generator) |
116 generators = [ | 114 generators = [ |
117 ('%s.h' % filename_base, cpp_generator.h_generator), | 115 ('%s.h' % filename_base, cpp_generator.h_generator), |
118 ('%s.cc' % filename_base, cpp_generator.cc_generator) | 116 ('%s.cc' % filename_base, cpp_generator.cc_generator) |
119 ] | 117 ] |
120 elif generator_name == 'dart': | |
121 generators = [ | |
122 ('%s.dart' % namespace.unix_name, DartGenerator( | |
123 dart_overrides_dir)) | |
124 ] | |
125 else: | 118 else: |
126 raise Exception('Unrecognised generator %s' % generator_name) | 119 raise Exception('Unrecognised generator %s' % generator_name) |
127 | 120 |
128 output_code = [] | 121 output_code = [] |
129 for filename, generator in generators: | 122 for filename, generator in generators: |
130 code = generator.Generate(namespace).Render() | 123 code = generator.Generate(namespace).Render() |
131 if destdir: | 124 if destdir: |
132 if generator_name == 'cpp-bundle-registration': | 125 if generator_name == 'cpp-bundle-registration': |
133 # Function registrations must be output to impl_dir, since they link in | 126 # Function registrations must be output to impl_dir, since they link in |
134 # API implementations. | 127 # API implementations. |
(...skipping 17 matching lines...) Expand all Loading... |
152 help='logical include root directory. Path to schema files from specified' | 145 help='logical include root directory. Path to schema files from specified' |
153 ' dir will be the include path.') | 146 ' dir will be the include path.') |
154 parser.add_option('-d', '--destdir', | 147 parser.add_option('-d', '--destdir', |
155 help='root directory to output generated files.') | 148 help='root directory to output generated files.') |
156 parser.add_option('-n', '--namespace', default='generated_api_schemas', | 149 parser.add_option('-n', '--namespace', default='generated_api_schemas', |
157 help='C++ namespace for generated files. e.g extensions::api.') | 150 help='C++ namespace for generated files. e.g extensions::api.') |
158 parser.add_option('-g', '--generator', default=GENERATORS[0], | 151 parser.add_option('-g', '--generator', default=GENERATORS[0], |
159 choices=GENERATORS, | 152 choices=GENERATORS, |
160 help='The generator to use to build the output code. Supported values are' | 153 help='The generator to use to build the output code. Supported values are' |
161 ' %s' % GENERATORS) | 154 ' %s' % GENERATORS) |
162 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', | |
163 help='Adds custom dart from files in the given directory (Dart only).') | |
164 parser.add_option('-i', '--impl-dir', dest='impl_dir', | 155 parser.add_option('-i', '--impl-dir', dest='impl_dir', |
165 help='The root path of all API implementations') | 156 help='The root path of all API implementations') |
166 parser.add_option('-I', '--include-rules', | 157 parser.add_option('-I', '--include-rules', |
167 help='A list of paths to include when searching for referenced objects,' | 158 help='A list of paths to include when searching for referenced objects,' |
168 ' with the namespace separated by a \':\'. Example: ' | 159 ' with the namespace separated by a \':\'. Example: ' |
169 '/foo/bar:Foo::Bar::%(namespace)s') | 160 '/foo/bar:Foo::Bar::%(namespace)s') |
170 | 161 |
171 (opts, file_paths) = parser.parse_args() | 162 (opts, file_paths) = parser.parse_args() |
172 | 163 |
173 if not file_paths: | 164 if not file_paths: |
(...skipping 11 matching lines...) Expand all Loading... |
185 raise ValueError('Invalid include rule "%s". Rules must be of ' | 176 raise ValueError('Invalid include rule "%s". Rules must be of ' |
186 'the form path:namespace' % path_and_namespace) | 177 'the form path:namespace' % path_and_namespace) |
187 return path_and_namespace.split(':', 1) | 178 return path_and_namespace.split(':', 1) |
188 | 179 |
189 include_rules = [] | 180 include_rules = [] |
190 if opts.include_rules: | 181 if opts.include_rules: |
191 include_rules = map(split_path_and_namespace, | 182 include_rules = map(split_path_and_namespace, |
192 shlex.split(opts.include_rules)) | 183 shlex.split(opts.include_rules)) |
193 | 184 |
194 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, | 185 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
195 opts.namespace, opts.dart_overrides_dir, | 186 opts.namespace, opts.impl_dir, include_rules) |
196 opts.impl_dir, include_rules) | |
197 if not opts.destdir: | 187 if not opts.destdir: |
198 print result | 188 print result |
OLD | NEW |