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 import json_schema | 27 import json_schema |
28 from model import Model, UnixName | 28 from model import Model |
29 from schema_loader import SchemaLoader | 29 from schema_loader import SchemaLoader |
30 | 30 |
31 # Names of supported code generators, as specified on the command-line. | 31 # Names of supported code generators, as specified on the command-line. |
32 # First is default. | 32 # First is default. |
33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] | 33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] |
34 | 34 |
35 def GenerateSchema(generator, | 35 def GenerateSchema(generator, |
36 filenames, | 36 filenames, |
37 root, | 37 root, |
38 destdir, | 38 destdir, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 path, filename = os.path.split(schema_filename) | 71 path, filename = os.path.split(schema_filename) |
72 short_filename, extension = os.path.splitext(filename) | 72 short_filename, extension = os.path.splitext(filename) |
73 | 73 |
74 # Filenames are checked against the unix_names of the namespaces they | 74 # Filenames are checked against the unix_names of the namespaces they |
75 # generate because the gyp uses the names of the JSON files to generate | 75 # generate because the gyp uses the names of the JSON files to generate |
76 # the names of the .cc and .h files. We want these to be using unix_names. | 76 # the names of the .cc and .h files. We want these to be using unix_names. |
77 if namespace.unix_name != short_filename: | 77 if namespace.unix_name != short_filename: |
78 sys.exit("Filename %s is illegal. Name files using unix_hacker style." % | 78 sys.exit("Filename %s is illegal. Name files using unix_hacker style." % |
79 schema_filename) | 79 schema_filename) |
80 | 80 |
81 # The output filename must match the input filename for gyp to deal with it | |
dhnishi (use Chromium)
2013/09/09 22:55:43
I removed this bit because the code was never used
not at google - send to devlin
2013/09/09 23:50:25
Eh who knows. Delete SGTM.
| |
82 # properly. | |
83 out_file = namespace.unix_name | |
84 | |
85 # Construct the type generator with all the namespaces in this model. | 81 # Construct the type generator with all the namespaces in this model. |
86 type_generator = CppTypeGenerator(api_model, | 82 type_generator = CppTypeGenerator(api_model, |
87 schema_loader, | 83 schema_loader, |
88 default_namespace=default_namespace) | 84 default_namespace=default_namespace) |
89 | 85 |
90 if generator == 'cpp-bundle': | 86 if generator == 'cpp-bundle': |
91 cpp_bundle_generator = CppBundleGenerator(root, | 87 cpp_bundle_generator = CppBundleGenerator(root, |
92 api_model, | 88 api_model, |
93 api_defs, | 89 api_defs, |
94 type_generator, | 90 type_generator, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 # Unless in bundle mode, only one file should be specified. | 146 # Unless in bundle mode, only one file should be specified. |
151 if opts.generator != 'cpp-bundle' and len(filenames) > 1: | 147 if opts.generator != 'cpp-bundle' and len(filenames) > 1: |
152 # TODO(sashab): Could also just use filenames[0] here and not complain. | 148 # TODO(sashab): Could also just use filenames[0] here and not complain. |
153 raise Exception( | 149 raise Exception( |
154 "Unless in bundle mode, only one file can be specified at a time.") | 150 "Unless in bundle mode, only one file can be specified at a time.") |
155 | 151 |
156 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, | 152 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, |
157 opts.namespace, opts.dart_overrides_dir) | 153 opts.namespace, opts.dart_overrides_dir) |
158 if not opts.destdir: | 154 if not opts.destdir: |
159 print result | 155 print result |
OLD | NEW |