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 sys | 22 import sys |
22 | 23 |
23 from cpp_bundle_generator import CppBundleGenerator | 24 from cpp_bundle_generator import CppBundleGenerator |
24 from cpp_generator import CppGenerator | 25 from cpp_generator import CppGenerator |
25 from cpp_type_generator import CppTypeGenerator | 26 from cpp_type_generator import CppTypeGenerator |
26 from dart_generator import DartGenerator | 27 from dart_generator import DartGenerator |
27 import json_schema | 28 import json_schema |
29 from cpp_namespace_environment import CppNamespaceEnvironment | |
28 from model import Model | 30 from model import Model |
29 from schema_loader import SchemaLoader | 31 from schema_loader import SchemaLoader |
30 | 32 |
31 # Names of supported code generators, as specified on the command-line. | 33 # Names of supported code generators, as specified on the command-line. |
32 # First is default. | 34 # First is default. |
33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] | 35 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] |
34 | 36 |
35 def GenerateSchema(generator, | 37 def GenerateSchema(generator, |
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, |
44 includes): | |
42 # Merge the source files into a single list of schemas. | 45 # Merge the source files into a single list of schemas. |
43 api_defs = [] | 46 api_defs = [] |
44 for file_path in file_paths: | 47 for file_path in file_paths: |
45 schema = os.path.normpath(file_path) | 48 schema = os.path.normpath(file_path) |
46 schema_loader = SchemaLoader( | 49 schema_loader = SchemaLoader( |
47 os.path.dirname(os.path.relpath(schema, root)), | 50 os.path.dirname(os.path.relpath(schema, root)), |
48 os.path.dirname(file_path)) | 51 os.path.dirname(file_path), |
52 includes, | |
53 cpp_namespace_pattern) | |
49 api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) | 54 api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) |
50 | 55 |
51 # If compiling the C++ model code, delete 'nocompile' nodes. | 56 # If compiling the C++ model code, delete 'nocompile' nodes. |
52 if generator == 'cpp': | 57 if generator == 'cpp': |
53 api_def = json_schema.DeleteNodes(api_def, 'nocompile') | 58 api_def = json_schema.DeleteNodes(api_def, 'nocompile') |
54 api_defs.extend(api_def) | 59 api_defs.extend(api_def) |
55 | 60 |
56 api_model = Model() | 61 api_model = Model() |
57 | 62 |
58 # For single-schema compilation make sure that the first (i.e. only) schema | 63 # For single-schema compilation make sure that the first (i.e. only) schema |
59 # is the default one. | 64 # is the default one. |
60 default_namespace = None | 65 default_namespace = None |
61 | 66 |
62 # If we have files from multiple source paths, we'll use the common parent | 67 # If we have files from multiple source paths, we'll use the common parent |
63 # path as the source directory. | 68 # path as the source directory. |
64 src_path = None | 69 src_path = None |
65 | 70 |
66 # Load the actual namespaces into the model. | 71 # Load the actual namespaces into the model. |
67 for target_namespace, file_path in zip(api_defs, file_paths): | 72 for target_namespace, file_path in zip(api_defs, file_paths): |
68 relpath = os.path.relpath(os.path.normpath(file_path), root) | 73 relpath = os.path.relpath(os.path.normpath(file_path), root) |
69 namespace = api_model.AddNamespace(target_namespace, | 74 namespace = api_model.AddNamespace(target_namespace, |
70 relpath, | 75 relpath, |
71 include_compiler_options=True) | 76 include_compiler_options=True, |
77 environment=CppNamespaceEnvironment( | |
78 cpp_namespace_pattern)) | |
72 | 79 |
73 if default_namespace is None: | 80 if default_namespace is None: |
74 default_namespace = namespace | 81 default_namespace = namespace |
75 | 82 |
76 if src_path is None: | 83 if src_path is None: |
77 src_path = namespace.source_file_dir | 84 src_path = namespace.source_file_dir |
78 else: | 85 else: |
79 src_path = os.path.commonprefix((src_path, namespace.source_file_dir)) | 86 src_path = os.path.commonprefix((src_path, namespace.source_file_dir)) |
80 | 87 |
81 path, filename = os.path.split(file_path) | 88 path, filename = os.path.split(file_path) |
(...skipping 11 matching lines...) Expand all Loading... | |
93 cpp_namespace_pattern, | 100 cpp_namespace_pattern, |
94 src_path, | 101 src_path, |
95 impl_dir) | 102 impl_dir) |
96 generators = [ | 103 generators = [ |
97 ('generated_api.cc', cpp_bundle_generator.api_cc_generator), | 104 ('generated_api.cc', cpp_bundle_generator.api_cc_generator), |
98 ('generated_api.h', cpp_bundle_generator.api_h_generator), | 105 ('generated_api.h', cpp_bundle_generator.api_h_generator), |
99 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), | 106 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), |
100 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) | 107 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) |
101 ] | 108 ] |
102 elif generator == 'cpp': | 109 elif generator == 'cpp': |
103 cpp_generator = CppGenerator(type_generator, cpp_namespace_pattern) | 110 cpp_generator = CppGenerator(type_generator) |
104 generators = [ | 111 generators = [ |
105 ('%s.h' % filename_base, cpp_generator.h_generator), | 112 ('%s.h' % filename_base, cpp_generator.h_generator), |
106 ('%s.cc' % filename_base, cpp_generator.cc_generator) | 113 ('%s.cc' % filename_base, cpp_generator.cc_generator) |
107 ] | 114 ] |
108 elif generator == 'dart': | 115 elif generator == 'dart': |
109 generators = [ | 116 generators = [ |
110 ('%s.dart' % namespace.unix_name, DartGenerator( | 117 ('%s.dart' % namespace.unix_name, DartGenerator( |
111 dart_overrides_dir)) | 118 dart_overrides_dir)) |
112 ] | 119 ] |
113 else: | 120 else: |
(...skipping 25 matching lines...) Expand all Loading... | |
139 parser.add_option('-n', '--namespace', default='generated_api_schemas', | 146 parser.add_option('-n', '--namespace', default='generated_api_schemas', |
140 help='C++ namespace for generated files. e.g extensions::api.') | 147 help='C++ namespace for generated files. e.g extensions::api.') |
141 parser.add_option('-g', '--generator', default=GENERATORS[0], | 148 parser.add_option('-g', '--generator', default=GENERATORS[0], |
142 choices=GENERATORS, | 149 choices=GENERATORS, |
143 help='The generator to use to build the output code. Supported values are' | 150 help='The generator to use to build the output code. Supported values are' |
144 ' %s' % GENERATORS) | 151 ' %s' % GENERATORS) |
145 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', | 152 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', |
146 help='Adds custom dart from files in the given directory (Dart only).') | 153 help='Adds custom dart from files in the given directory (Dart only).') |
147 parser.add_option('-i', '--impl-dir', dest='impl_dir', | 154 parser.add_option('-i', '--impl-dir', dest='impl_dir', |
148 help='The root path of all API implementations') | 155 help='The root path of all API implementations') |
156 parser.add_option('-I', '--include-rules', #dest='include', | |
not at google - send to devlin
2014/08/26 20:16:06
Remove #dest comment
lfg
2014/08/26 21:18:43
Done.
| |
157 help='A list of paths to include when searching for referenced objects,' | |
158 ' with the namespace separated by a \':\'. Example: ' | |
159 '/foo/bar:Foo::Bar::%(namespace)s') | |
149 | 160 |
150 (opts, file_paths) = parser.parse_args() | 161 (opts, file_paths) = parser.parse_args() |
151 | 162 |
152 if not file_paths: | 163 if not file_paths: |
153 sys.exit(0) # This is OK as a no-op | 164 sys.exit(0) # This is OK as a no-op |
154 | 165 |
155 # Unless in bundle mode, only one file should be specified. | 166 # Unless in bundle mode, only one file should be specified. |
156 if opts.generator != 'cpp-bundle' and len(file_paths) > 1: | 167 if opts.generator != 'cpp-bundle' and len(file_paths) > 1: |
157 # TODO(sashab): Could also just use file_paths[0] here and not complain. | 168 # TODO(sashab): Could also just use file_paths[0] here and not complain. |
158 raise Exception( | 169 raise Exception( |
159 "Unless in bundle mode, only one file can be specified at a time.") | 170 "Unless in bundle mode, only one file can be specified at a time.") |
160 | 171 |
172 def split_path_and_namespace(path_and_namespace): | |
173 if ':' not in path_and_namespace: | |
174 raise ValueError('Invalid include rule "%s". Rules must be of ' | |
175 'the form path:namespace' % path_and_namespace) | |
176 return path_and_namespace.split(':', 1) | |
177 | |
178 includes = [{'path': path, 'namespace': namespace} | |
179 for rule in shlex.split(opts.include_rules) | |
180 for path, namespace in [ split_path_and_namespace(rule) ]] | |
not at google - send to devlin
2014/08/26 20:16:06
Ah ok. So writing this singleton list thing is a b
lfg
2014/08/26 21:18:43
Done.
| |
181 | |
161 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, | 182 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
162 opts.namespace, opts.dart_overrides_dir, | 183 opts.namespace, opts.dart_overrides_dir, |
163 opts.impl_dir) | 184 opts.impl_dir, includes) |
not at google - send to devlin
2014/08/26 20:16:06
(call this include_rules)
lfg
2014/08/26 21:18:43
Done.
| |
164 if not opts.destdir: | 185 if not opts.destdir: |
165 print result | 186 print result |
OLD | NEW |