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 js_externs_generator import JsExternsGenerator | 27 from js_externs_generator import JsExternsGenerator |
28 from js_interface_generator import JsInterfaceGenerator | |
28 import json_schema | 29 import json_schema |
29 from cpp_namespace_environment import CppNamespaceEnvironment | 30 from cpp_namespace_environment import CppNamespaceEnvironment |
30 from model import Model | 31 from model import Model |
31 from schema_loader import SchemaLoader | 32 from schema_loader import SchemaLoader |
32 | 33 |
33 # Names of supported code generators, as specified on the command-line. | 34 # Names of supported code generators, as specified on the command-line. |
34 # First is default. | 35 # First is default. |
35 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs'] | 36 GENERATORS = [ |
37 'cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs', 'interface' | |
38 ] | |
36 | 39 |
37 def GenerateSchema(generator_name, | 40 def GenerateSchema(generator_name, |
38 file_paths, | 41 file_paths, |
39 root, | 42 root, |
40 destdir, | 43 destdir, |
41 cpp_namespace_pattern, | 44 cpp_namespace_pattern, |
42 bundle_name, | 45 bundle_name, |
43 impl_dir, | 46 impl_dir, |
44 include_rules): | 47 include_rules): |
45 # Merge the source files into a single list of schemas. | 48 # Merge the source files into a single list of schemas. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 elif generator_name == 'cpp': | 118 elif generator_name == 'cpp': |
116 cpp_generator = CppGenerator(type_generator) | 119 cpp_generator = CppGenerator(type_generator) |
117 generators = [ | 120 generators = [ |
118 ('%s.h' % filename_base, cpp_generator.h_generator), | 121 ('%s.h' % filename_base, cpp_generator.h_generator), |
119 ('%s.cc' % filename_base, cpp_generator.cc_generator) | 122 ('%s.cc' % filename_base, cpp_generator.cc_generator) |
120 ] | 123 ] |
121 elif generator_name == 'externs': | 124 elif generator_name == 'externs': |
122 generators = [ | 125 generators = [ |
123 ('%s_externs.js' % namespace.unix_name, JsExternsGenerator()) | 126 ('%s_externs.js' % namespace.unix_name, JsExternsGenerator()) |
124 ] | 127 ] |
128 elif generator_name == 'interface': | |
129 generators = [ | |
130 ('%s_interface.js' % namespace.unix_name, JsInterfaceGenerator()) | |
131 ] | |
125 else: | 132 else: |
126 raise Exception('Unrecognised generator %s' % generator_name) | 133 raise Exception('Unrecognised generator %s' % generator_name) |
127 | 134 |
128 output_code = [] | 135 output_code = [] |
129 for filename, generator in generators: | 136 for filename, generator in generators: |
130 code = generator.Generate(namespace).Render() | 137 code = generator.Generate(namespace).Render() |
131 if destdir: | 138 if destdir: |
132 if generator_name == 'cpp-bundle-registration': | 139 if generator_name == 'cpp-bundle-registration': |
133 # Function registrations must be output to impl_dir, since they link in | 140 # Function registrations must be output to impl_dir, since they link in |
134 # API implementations. | 141 # API implementations. |
135 output_dir = os.path.join(destdir, impl_dir) | 142 output_dir = os.path.join(destdir, impl_dir) |
136 else: | 143 else: |
137 output_dir = os.path.join(destdir, src_path) | 144 output_dir = os.path.join(destdir, src_path) |
138 if not os.path.exists(output_dir): | 145 if not os.path.exists(output_dir): |
139 os.makedirs(output_dir) | 146 os.makedirs(output_dir) |
140 with open(os.path.join(output_dir, filename), 'w') as f: | 147 with open(os.path.join(output_dir, filename), 'w') as f: |
141 f.write(code) | 148 f.write(code) |
142 output_code += [filename, '', code, ''] | 149 if len(generators) > 1: |
150 output_code += [filename, '', code, ''] | |
michaelpg
2015/12/04 21:02:19
is this where the silly filename at the beginning
stevenjb
2015/12/07 18:31:24
It is. And this is correct: If we are writing a si
michaelpg
2015/12/07 18:46:53
ah, I didn't realize that's what |generators| repr
stevenjb
2015/12/07 19:49:32
So, I *think* that the intention is to specify an
| |
151 else: | |
152 output_code += [code] | |
143 | 153 |
144 return '\n'.join(output_code) | 154 return '\n'.join(output_code) |
145 | 155 |
146 | 156 |
147 if __name__ == '__main__': | 157 if __name__ == '__main__': |
148 parser = optparse.OptionParser( | 158 parser = optparse.OptionParser( |
149 description='Generates a C++ model of an API from JSON schema', | 159 description='Generates a C++ model of an API from JSON schema', |
150 usage='usage: %prog [option]... schema') | 160 usage='usage: %prog [option]... schema') |
151 parser.add_option('-r', '--root', default='.', | 161 parser.add_option('-r', '--root', default='.', |
152 help='logical include root directory. Path to schema files from specified' | 162 help='logical include root directory. Path to schema files from specified' |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
191 include_rules = [] | 201 include_rules = [] |
192 if opts.include_rules: | 202 if opts.include_rules: |
193 include_rules = map(split_path_and_namespace, | 203 include_rules = map(split_path_and_namespace, |
194 shlex.split(opts.include_rules)) | 204 shlex.split(opts.include_rules)) |
195 | 205 |
196 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, | 206 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
197 opts.namespace, opts.bundle_name, opts.impl_dir, | 207 opts.namespace, opts.bundle_name, opts.impl_dir, |
198 include_rules) | 208 include_rules) |
199 if not opts.destdir: | 209 if not opts.destdir: |
200 print result | 210 print result |
OLD | NEW |