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 sys | 21 import sys |
22 | 22 |
23 from cpp_bundle_generator import CppBundleGenerator | 23 from cpp_bundle_generator import CppBundleGenerator |
24 from cpp_generator import CppGenerator | 24 from cpp_generator import CppGenerator |
25 from cpp_type_generator import CppTypeGenerator | 25 from cpp_type_generator import CppTypeGenerator |
26 from dart_generator import DartGenerator | 26 from dart_generator import DartGenerator |
| 27 from js_externs_generator import JsExternsGenerator |
27 import json_schema | 28 import json_schema |
28 from model import Model | 29 from model import Model |
29 from schema_loader import SchemaLoader | 30 from schema_loader import SchemaLoader |
30 | 31 |
31 # Names of supported code generators, as specified on the command-line. | 32 # Names of supported code generators, as specified on the command-line. |
32 # First is default. | 33 # First is default. |
33 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'dart'] | 34 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'dart', |
| 35 'externs'] |
34 | 36 |
35 def GenerateSchema(generator_name, | 37 def GenerateSchema(generator_name, |
36 file_paths, | 38 file_paths, |
37 root, | 39 root, |
38 destdir, | 40 destdir, |
39 cpp_namespace_pattern, | 41 cpp_namespace_pattern, |
40 dart_overrides_dir, | 42 dart_overrides_dir, |
41 impl_dir): | 43 impl_dir): |
42 # Merge the source files into a single list of schemas. | 44 # Merge the source files into a single list of schemas. |
43 api_defs = [] | 45 api_defs = [] |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 cpp_generator = CppGenerator(type_generator, cpp_namespace_pattern) | 110 cpp_generator = CppGenerator(type_generator, cpp_namespace_pattern) |
109 generators = [ | 111 generators = [ |
110 ('%s.h' % filename_base, cpp_generator.h_generator), | 112 ('%s.h' % filename_base, cpp_generator.h_generator), |
111 ('%s.cc' % filename_base, cpp_generator.cc_generator) | 113 ('%s.cc' % filename_base, cpp_generator.cc_generator) |
112 ] | 114 ] |
113 elif generator_name == 'dart': | 115 elif generator_name == 'dart': |
114 generators = [ | 116 generators = [ |
115 ('%s.dart' % namespace.unix_name, DartGenerator( | 117 ('%s.dart' % namespace.unix_name, DartGenerator( |
116 dart_overrides_dir)) | 118 dart_overrides_dir)) |
117 ] | 119 ] |
| 120 elif generator_name == 'externs': |
| 121 generators = [ |
| 122 ('%s_externs.js' % namespace.unix_name, JsExternsGenerator()) |
| 123 ] |
118 else: | 124 else: |
119 raise Exception('Unrecognised generator %s' % generator) | 125 raise Exception('Unrecognised generator %s' % generator_name) |
120 | 126 |
121 output_code = [] | 127 output_code = [] |
122 for filename, generator in generators: | 128 for filename, generator in generators: |
123 code = generator.Generate(namespace).Render() | 129 code = generator.Generate(namespace).Render() |
124 if destdir: | 130 if destdir: |
125 if generator_name == 'cpp-bundle-registration': | 131 if generator_name == 'cpp-bundle-registration': |
126 # Function registrations must be output to impl_dir, since they link in | 132 # Function registrations must be output to impl_dir, since they link in |
127 # API implementations. | 133 # API implementations. |
128 output_dir = os.path.join(destdir, impl_dir) | 134 output_dir = os.path.join(destdir, impl_dir) |
129 else: | 135 else: |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 len(file_paths) > 1): | 173 len(file_paths) > 1): |
168 # TODO(sashab): Could also just use file_paths[0] here and not complain. | 174 # TODO(sashab): Could also just use file_paths[0] here and not complain. |
169 raise Exception( | 175 raise Exception( |
170 "Unless in bundle mode, only one file can be specified at a time.") | 176 "Unless in bundle mode, only one file can be specified at a time.") |
171 | 177 |
172 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, | 178 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
173 opts.namespace, opts.dart_overrides_dir, | 179 opts.namespace, opts.dart_overrides_dir, |
174 opts.impl_dir) | 180 opts.impl_dir) |
175 if not opts.destdir: | 181 if not opts.destdir: |
176 print result | 182 print result |
OLD | NEW |