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. |
(...skipping 21 matching lines...) Expand all Loading... |
32 | 32 |
33 # Names of supported code generators, as specified on the command-line. | 33 # Names of supported code generators, as specified on the command-line. |
34 # First is default. | 34 # First is default. |
35 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs'] | 35 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs'] |
36 | 36 |
37 def GenerateSchema(generator_name, | 37 def GenerateSchema(generator_name, |
38 file_paths, | 38 file_paths, |
39 root, | 39 root, |
40 destdir, | 40 destdir, |
41 cpp_namespace_pattern, | 41 cpp_namespace_pattern, |
| 42 bundle_name, |
42 impl_dir, | 43 impl_dir, |
43 include_rules): | 44 include_rules): |
44 # Merge the source files into a single list of schemas. | 45 # Merge the source files into a single list of schemas. |
45 api_defs = [] | 46 api_defs = [] |
46 for file_path in file_paths: | 47 for file_path in file_paths: |
47 schema = os.path.relpath(file_path, root) | 48 schema = os.path.relpath(file_path, root) |
48 schema_loader = SchemaLoader( | 49 schema_loader = SchemaLoader( |
49 root, | 50 root, |
50 os.path.dirname(schema), | 51 os.path.dirname(schema), |
51 include_rules, | 52 include_rules, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 # Construct the type generator with all the namespaces in this model. | 91 # Construct the type generator with all the namespaces in this model. |
91 type_generator = CppTypeGenerator(api_model, | 92 type_generator = CppTypeGenerator(api_model, |
92 schema_loader, | 93 schema_loader, |
93 default_namespace) | 94 default_namespace) |
94 if generator_name in ('cpp-bundle-registration', 'cpp-bundle-schema'): | 95 if generator_name in ('cpp-bundle-registration', 'cpp-bundle-schema'): |
95 cpp_bundle_generator = CppBundleGenerator(root, | 96 cpp_bundle_generator = CppBundleGenerator(root, |
96 api_model, | 97 api_model, |
97 api_defs, | 98 api_defs, |
98 type_generator, | 99 type_generator, |
99 cpp_namespace_pattern, | 100 cpp_namespace_pattern, |
| 101 bundle_name, |
100 src_path, | 102 src_path, |
101 impl_dir) | 103 impl_dir) |
102 if generator_name == 'cpp-bundle-registration': | 104 if generator_name == 'cpp-bundle-registration': |
103 generators = [ | 105 generators = [ |
104 ('generated_api_registration.cc', | 106 ('generated_api_registration.cc', |
105 cpp_bundle_generator.api_cc_generator), | 107 cpp_bundle_generator.api_cc_generator), |
106 ('generated_api_registration.h', cpp_bundle_generator.api_h_generator), | 108 ('generated_api_registration.h', cpp_bundle_generator.api_h_generator), |
107 ] | 109 ] |
108 elif generator_name == 'cpp-bundle-schema': | 110 elif generator_name == 'cpp-bundle-schema': |
109 generators = [ | 111 generators = [ |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 parser = optparse.OptionParser( | 148 parser = optparse.OptionParser( |
147 description='Generates a C++ model of an API from JSON schema', | 149 description='Generates a C++ model of an API from JSON schema', |
148 usage='usage: %prog [option]... schema') | 150 usage='usage: %prog [option]... schema') |
149 parser.add_option('-r', '--root', default='.', | 151 parser.add_option('-r', '--root', default='.', |
150 help='logical include root directory. Path to schema files from specified' | 152 help='logical include root directory. Path to schema files from specified' |
151 ' dir will be the include path.') | 153 ' dir will be the include path.') |
152 parser.add_option('-d', '--destdir', | 154 parser.add_option('-d', '--destdir', |
153 help='root directory to output generated files.') | 155 help='root directory to output generated files.') |
154 parser.add_option('-n', '--namespace', default='generated_api_schemas', | 156 parser.add_option('-n', '--namespace', default='generated_api_schemas', |
155 help='C++ namespace for generated files. e.g extensions::api.') | 157 help='C++ namespace for generated files. e.g extensions::api.') |
| 158 parser.add_option('-b', '--bundle-name', default='', |
| 159 help='A string to prepend to generated bundle class names, so that ' |
| 160 'multiple bundle rules can be used without conflicting. ' |
| 161 'Only used with one of the cpp-bundle generators.') |
156 parser.add_option('-g', '--generator', default=GENERATORS[0], | 162 parser.add_option('-g', '--generator', default=GENERATORS[0], |
157 choices=GENERATORS, | 163 choices=GENERATORS, |
158 help='The generator to use to build the output code. Supported values are' | 164 help='The generator to use to build the output code. Supported values are' |
159 ' %s' % GENERATORS) | 165 ' %s' % GENERATORS) |
160 parser.add_option('-i', '--impl-dir', dest='impl_dir', | 166 parser.add_option('-i', '--impl-dir', dest='impl_dir', |
161 help='The root path of all API implementations') | 167 help='The root path of all API implementations') |
162 parser.add_option('-I', '--include-rules', | 168 parser.add_option('-I', '--include-rules', |
163 help='A list of paths to include when searching for referenced objects,' | 169 help='A list of paths to include when searching for referenced objects,' |
164 ' with the namespace separated by a \':\'. Example: ' | 170 ' with the namespace separated by a \':\'. Example: ' |
165 '/foo/bar:Foo::Bar::%(namespace)s') | 171 '/foo/bar:Foo::Bar::%(namespace)s') |
(...skipping 15 matching lines...) Expand all Loading... |
181 raise ValueError('Invalid include rule "%s". Rules must be of ' | 187 raise ValueError('Invalid include rule "%s". Rules must be of ' |
182 'the form path:namespace' % path_and_namespace) | 188 'the form path:namespace' % path_and_namespace) |
183 return path_and_namespace.split(':', 1) | 189 return path_and_namespace.split(':', 1) |
184 | 190 |
185 include_rules = [] | 191 include_rules = [] |
186 if opts.include_rules: | 192 if opts.include_rules: |
187 include_rules = map(split_path_and_namespace, | 193 include_rules = map(split_path_and_namespace, |
188 shlex.split(opts.include_rules)) | 194 shlex.split(opts.include_rules)) |
189 | 195 |
190 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, | 196 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
191 opts.namespace, opts.impl_dir, include_rules) | 197 opts.namespace, opts.bundle_name, opts.impl_dir, |
| 198 include_rules) |
192 if not opts.destdir: | 199 if not opts.destdir: |
193 print result | 200 print result |
OLD | NEW |