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 11 matching lines...) Expand all Loading... |
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 from js_interface_generator import JsInterfaceGenerator |
29 import json_schema | 29 import json_schema |
30 from cpp_namespace_environment import CppNamespaceEnvironment | 30 from cpp_namespace_environment import CppNamespaceEnvironment |
31 from model import Model | 31 from model import Model |
| 32 from namespace_resolver import NamespaceResolver |
32 from schema_loader import SchemaLoader | 33 from schema_loader import SchemaLoader |
33 | 34 |
34 # Names of supported code generators, as specified on the command-line. | 35 # Names of supported code generators, as specified on the command-line. |
35 # First is default. | 36 # First is default. |
36 GENERATORS = [ | 37 GENERATORS = [ |
37 'cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs', 'interface' | 38 'cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs', 'interface' |
38 ] | 39 ] |
39 | 40 |
40 def GenerateSchema(generator_name, | 41 def GenerateSchema(generator_name, |
41 file_paths, | 42 file_paths, |
42 root, | 43 root, |
43 destdir, | 44 destdir, |
44 cpp_namespace_pattern, | 45 cpp_namespace_pattern, |
45 bundle_name, | 46 bundle_name, |
46 impl_dir, | 47 impl_dir, |
47 include_rules): | 48 include_rules): |
48 # Merge the source files into a single list of schemas. | 49 # Merge the source files into a single list of schemas. |
49 api_defs = [] | 50 api_defs = [] |
50 for file_path in file_paths: | 51 for file_path in file_paths: |
51 schema = os.path.relpath(file_path, root) | 52 schema = os.path.relpath(file_path, root) |
52 schema_loader = SchemaLoader( | 53 api_def = SchemaLoader(root).LoadSchema(schema) |
53 root, | |
54 os.path.dirname(schema), | |
55 include_rules, | |
56 cpp_namespace_pattern) | |
57 api_def = schema_loader.LoadSchema(schema) | |
58 | 54 |
59 # If compiling the C++ model code, delete 'nocompile' nodes. | 55 # If compiling the C++ model code, delete 'nocompile' nodes. |
60 if generator_name == 'cpp': | 56 if generator_name == 'cpp': |
61 api_def = json_schema.DeleteNodes(api_def, 'nocompile') | 57 api_def = json_schema.DeleteNodes(api_def, 'nocompile') |
62 | 58 |
63 # Delete all 'nodefine' nodes. They are only for documentation. | 59 # Delete all 'nodefine' nodes. They are only for documentation. |
64 api_def = json_schema.DeleteNodes(api_def, 'nodefine') | 60 api_def = json_schema.DeleteNodes(api_def, 'nodefine') |
65 | 61 |
66 api_defs.extend(api_def) | 62 api_defs.extend(api_def) |
67 | 63 |
(...skipping 21 matching lines...) Expand all Loading... |
89 | 85 |
90 if src_path is None: | 86 if src_path is None: |
91 src_path = namespace.source_file_dir | 87 src_path = namespace.source_file_dir |
92 else: | 88 else: |
93 src_path = os.path.commonprefix((src_path, namespace.source_file_dir)) | 89 src_path = os.path.commonprefix((src_path, namespace.source_file_dir)) |
94 | 90 |
95 _, filename = os.path.split(file_path) | 91 _, filename = os.path.split(file_path) |
96 filename_base, _ = os.path.splitext(filename) | 92 filename_base, _ = os.path.splitext(filename) |
97 | 93 |
98 # Construct the type generator with all the namespaces in this model. | 94 # Construct the type generator with all the namespaces in this model. |
| 95 schema_dir = os.path.dirname(os.path.relpath(file_paths[0], root)) |
| 96 namespace_resolver = NamespaceResolver(root, schema_dir, |
| 97 include_rules, cpp_namespace_pattern) |
99 type_generator = CppTypeGenerator(api_model, | 98 type_generator = CppTypeGenerator(api_model, |
100 schema_loader, | 99 namespace_resolver, |
101 default_namespace) | 100 default_namespace) |
102 if generator_name in ('cpp-bundle-registration', 'cpp-bundle-schema'): | 101 if generator_name in ('cpp-bundle-registration', 'cpp-bundle-schema'): |
103 cpp_bundle_generator = CppBundleGenerator(root, | 102 cpp_bundle_generator = CppBundleGenerator(root, |
104 api_model, | 103 api_model, |
105 api_defs, | 104 api_defs, |
106 type_generator, | 105 type_generator, |
107 cpp_namespace_pattern, | 106 cpp_namespace_pattern, |
108 bundle_name, | 107 bundle_name, |
109 src_path, | 108 src_path, |
110 impl_dir) | 109 impl_dir) |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 include_rules = [] | 205 include_rules = [] |
207 if opts.include_rules: | 206 if opts.include_rules: |
208 include_rules = map(split_path_and_namespace, | 207 include_rules = map(split_path_and_namespace, |
209 shlex.split(opts.include_rules)) | 208 shlex.split(opts.include_rules)) |
210 | 209 |
211 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, | 210 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
212 opts.namespace, opts.bundle_name, opts.impl_dir, | 211 opts.namespace, opts.bundle_name, opts.impl_dir, |
213 include_rules) | 212 include_rules) |
214 if not opts.destdir: | 213 if not opts.destdir: |
215 print result | 214 print result |
OLD | NEW |