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 cc_generator | 19 import cc_generator |
20 import cpp_type_generator | 20 import cpp_type_generator |
21 import h_generator | 21 import h_generator |
22 import idl_schema | 22 import idl_schema |
23 import json_schema | 23 import json_schema |
24 import model | 24 import model |
25 import schema_bundle_generator | 25 import schema_bundle_generator |
26 | 26 |
27 import optparse | 27 import optparse |
28 import os.path | 28 import os.path |
29 import re | |
29 import sys | 30 import sys |
30 | 31 |
31 def load_schema(schema): | 32 def load_schema(schema): |
32 schema_filename, schema_extension = os.path.splitext(schema) | 33 schema_path, schema_extension = os.path.splitext(schema) |
34 schema_path, schema_filename = os.path.split(schema_path) | |
35 | |
36 if (schema_filename.find('.') != -1 or | |
37 re.search('[A-Z]', schema_filename) != None): | |
38 sys.exit("Filename %s%s is illegal. Name files using unix_hacker style." % | |
39 (schema_filename, schema_extension)) | |
not at google - send to devlin
2012/05/10 02:36:02
Instead of doing this can we load the Namespace th
cduvall
2012/05/10 03:09:01
I did it for single schemas, but I'm not quite sur
not at google - send to devlin
2012/05/10 03:26:52
Oh, that is a really good point. In fact none of t
| |
33 | 40 |
34 if schema_extension == '.json': | 41 if schema_extension == '.json': |
35 api_defs = json_schema.Load(schema) | 42 api_defs = json_schema.Load(schema) |
36 elif schema_extension == '.idl': | 43 elif schema_extension == '.idl': |
37 api_defs = idl_schema.Load(schema) | 44 api_defs = idl_schema.Load(schema) |
38 else: | 45 else: |
39 sys.exit("Did not recognize file extension %s for schema %s" % | 46 sys.exit("Did not recognize file extension %s for schema %s" % |
40 (schema_extension, schema)) | 47 (schema_extension, schema)) |
41 | 48 |
42 return api_defs | 49 return api_defs |
(...skipping 20 matching lines...) Expand all Loading... | |
63 | 70 |
64 # Gets the relative path from opts.root to the schema to correctly determine | 71 # Gets the relative path from opts.root to the schema to correctly determine |
65 # the include path. | 72 # the include path. |
66 relpath = os.path.relpath(schema, opts.root) | 73 relpath = os.path.relpath(schema, opts.root) |
67 namespace = api_model.AddNamespace(target_namespace, relpath) | 74 namespace = api_model.AddNamespace(target_namespace, relpath) |
68 if not namespace: | 75 if not namespace: |
69 continue | 76 continue |
70 | 77 |
71 # The output filename must match the input filename for gyp to deal with it | 78 # The output filename must match the input filename for gyp to deal with it |
72 # properly. | 79 # properly. |
73 out_file = namespace.name | 80 out_file = namespace.unix_name |
74 type_generator = cpp_type_generator.CppTypeGenerator( | 81 type_generator = cpp_type_generator.CppTypeGenerator( |
75 root_namespace, namespace, namespace.unix_name) | 82 root_namespace, namespace, namespace.unix_name) |
76 for referenced_namespace in api_model.namespaces.values(): | 83 for referenced_namespace in api_model.namespaces.values(): |
77 if referenced_namespace == namespace: | 84 if referenced_namespace == namespace: |
78 continue | 85 continue |
79 type_generator.AddNamespace( | 86 type_generator.AddNamespace( |
80 referenced_namespace, | 87 referenced_namespace, |
81 referenced_namespace.name) | 88 referenced_namespace.unix_name) |
82 | 89 |
83 h_code = (h_generator.HGenerator(namespace, type_generator) | 90 h_code = (h_generator.HGenerator(namespace, type_generator) |
84 .Generate().Render()) | 91 .Generate().Render()) |
85 cc_code = (cc_generator.CCGenerator(namespace, type_generator) | 92 cc_code = (cc_generator.CCGenerator(namespace, type_generator) |
86 .Generate().Render()) | 93 .Generate().Render()) |
87 | 94 |
88 if dest_dir: | 95 if dest_dir: |
89 with open( | 96 with open( |
90 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), | 97 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), |
91 'w') as cc_file: | 98 'w') as cc_file: |
(...skipping 21 matching lines...) Expand all Loading... | |
113 | 120 |
114 api_model = model.Model() | 121 api_model = model.Model() |
115 relpath = os.path.relpath(os.path.normpath(filenames[0]), root) | 122 relpath = os.path.relpath(os.path.normpath(filenames[0]), root) |
116 for target_namespace in api_defs: | 123 for target_namespace in api_defs: |
117 api_model.AddNamespace(target_namespace, relpath) | 124 api_model.AddNamespace(target_namespace, relpath) |
118 | 125 |
119 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) | 126 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) |
120 for referenced_namespace in api_model.namespaces.values(): | 127 for referenced_namespace in api_model.namespaces.values(): |
121 type_generator.AddNamespace( | 128 type_generator.AddNamespace( |
122 referenced_namespace, | 129 referenced_namespace, |
123 referenced_namespace.name) | 130 referenced_namespace.unix_name) |
124 | 131 |
125 generator = schema_bundle_generator.SchemaBundleGenerator( | 132 generator = schema_bundle_generator.SchemaBundleGenerator( |
126 api_model, api_defs, type_generator) | 133 api_model, api_defs, type_generator) |
127 api_h_code = generator.GenerateAPIHeader().Render() | 134 api_h_code = generator.GenerateAPIHeader().Render() |
128 schemas_h_code = generator.GenerateSchemasHeader().Render() | 135 schemas_h_code = generator.GenerateSchemasHeader().Render() |
129 schemas_cc_code = generator.GenerateSchemasCC().Render() | 136 schemas_cc_code = generator.GenerateSchemasCC().Render() |
130 | 137 |
131 if dest_dir: | 138 if dest_dir: |
132 basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') | 139 basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') |
133 with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: | 140 with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: |
(...skipping 30 matching lines...) Expand all Loading... | |
164 '''if supplied, causes compiler to generate bundle files for the given set of | 171 '''if supplied, causes compiler to generate bundle files for the given set of |
165 source files.''') | 172 source files.''') |
166 | 173 |
167 (opts, args) = parser.parse_args() | 174 (opts, args) = parser.parse_args() |
168 | 175 |
169 if not args: | 176 if not args: |
170 sys.exit(0) # This is OK as a no-op | 177 sys.exit(0) # This is OK as a no-op |
171 dest_dir = opts.destdir | 178 dest_dir = opts.destdir |
172 root_namespace = opts.namespace | 179 root_namespace = opts.namespace |
173 | 180 |
181 # TODO(cduvall) error on bad filename | |
not at google - send to devlin
2012/05/10 02:36:02
might as well do it now. sys.exit(...) (somewhere
cduvall
2012/05/10 03:09:01
Oops forgot to delete this.
| |
182 | |
174 if opts.bundle: | 183 if opts.bundle: |
175 handle_bundle_schema(args, dest_dir, opts.root, root_namespace) | 184 handle_bundle_schema(args, dest_dir, opts.root, root_namespace) |
176 else: | 185 else: |
177 handle_single_schema(args[0], dest_dir, opts.root, root_namespace) | 186 handle_single_schema(args[0], dest_dir, opts.root, root_namespace) |
OLD | NEW |