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 | 28 from model import Model |
29 from ppapi_generator import PpapiGenerator | |
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', 'dart'] | 34 GENERATORS = ['cpp', 'cpp-bundle', 'dart', 'ppapi'] |
34 | 35 |
35 def GenerateSchema(generator, | 36 def GenerateSchema(generator, |
36 filenames, | 37 filenames, |
37 root, | 38 root, |
38 destdir, | 39 destdir, |
39 root_namespace, | 40 root_namespace, |
40 dart_overrides_dir): | 41 dart_overrides_dir, |
42 is_ppapi_dev=False): | |
yzshen1
2013/12/10 21:43:02
FYI: very soon, pepper will switch to a new 'dev'
| |
41 schema_loader = SchemaLoader( | 43 schema_loader = SchemaLoader( |
42 os.path.dirname(os.path.relpath(os.path.normpath(filenames[0]), root)), | 44 os.path.dirname(os.path.relpath(os.path.normpath(filenames[0]), root)), |
43 os.path.dirname(filenames[0])) | 45 os.path.dirname(filenames[0])) |
44 # Merge the source files into a single list of schemas. | 46 # Merge the source files into a single list of schemas. |
45 api_defs = [] | 47 api_defs = [] |
46 for filename in filenames: | 48 for filename in filenames: |
47 schema = os.path.normpath(filename) | 49 schema = os.path.normpath(filename) |
48 api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) | 50 api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) |
49 | 51 |
50 # If compiling the C++ model code, delete 'nocompile' nodes. | 52 # If compiling the C++ model code, delete 'nocompile' nodes. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 cpp_generator = CppGenerator(type_generator, root_namespace) | 101 cpp_generator = CppGenerator(type_generator, root_namespace) |
100 generators = [ | 102 generators = [ |
101 ('%s.h' % namespace.unix_name, cpp_generator.h_generator), | 103 ('%s.h' % namespace.unix_name, cpp_generator.h_generator), |
102 ('%s.cc' % namespace.unix_name, cpp_generator.cc_generator) | 104 ('%s.cc' % namespace.unix_name, cpp_generator.cc_generator) |
103 ] | 105 ] |
104 elif generator == 'dart': | 106 elif generator == 'dart': |
105 generators = [ | 107 generators = [ |
106 ('%s.dart' % namespace.unix_name, DartGenerator( | 108 ('%s.dart' % namespace.unix_name, DartGenerator( |
107 dart_overrides_dir)) | 109 dart_overrides_dir)) |
108 ] | 110 ] |
111 elif generator == 'ppapi': | |
112 generator = PpapiGenerator(is_ppapi_dev) | |
113 if is_ppapi_dev: | |
114 dev_directory = 'dev' | |
115 dev_infix = '_dev' | |
116 else: | |
117 dev_directory = '' | |
118 dev_infix = '' | |
119 generators = [ | |
120 (os.path.join('api', dev_directory, 'ppb_%s%s.idl' % | |
121 (namespace.unix_name, dev_infix)), | |
122 generator.idl_generator), | |
123 ] | |
109 else: | 124 else: |
110 raise Exception('Unrecognised generator %s' % generator) | 125 raise Exception('Unrecognised generator %s' % generator) |
111 | 126 |
112 output_code = [] | 127 output_code = [] |
113 for filename, generator in generators: | 128 for filename, generator in generators: |
114 code = generator.Generate(namespace).Render() | 129 code = generator.Generate(namespace).Render() |
115 if destdir: | 130 if destdir: |
116 with open(os.path.join(destdir, namespace.source_file_dir, | 131 with open(os.path.join(destdir, namespace.source_file_dir, |
117 filename), 'w') as f: | 132 filename), 'w') as f: |
118 f.write(code) | 133 f.write(code) |
(...skipping 12 matching lines...) Expand all Loading... | |
131 parser.add_option('-d', '--destdir', | 146 parser.add_option('-d', '--destdir', |
132 help='root directory to output generated files.') | 147 help='root directory to output generated files.') |
133 parser.add_option('-n', '--namespace', default='generated_api_schemas', | 148 parser.add_option('-n', '--namespace', default='generated_api_schemas', |
134 help='C++ namespace for generated files. e.g extensions::api.') | 149 help='C++ namespace for generated files. e.g extensions::api.') |
135 parser.add_option('-g', '--generator', default=GENERATORS[0], | 150 parser.add_option('-g', '--generator', default=GENERATORS[0], |
136 choices=GENERATORS, | 151 choices=GENERATORS, |
137 help='The generator to use to build the output code. Supported values are' | 152 help='The generator to use to build the output code. Supported values are' |
138 ' %s' % GENERATORS) | 153 ' %s' % GENERATORS) |
139 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', | 154 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', |
140 help='Adds custom dart from files in the given directory (Dart only).') | 155 help='Adds custom dart from files in the given directory (Dart only).') |
156 parser.add_option( | |
157 '', '--ppapi_dev', action='store_true', dest='ppapi_dev', default=False, | |
158 help='Whether APIs are dev APIs (ppapi only).') | |
141 | 159 |
142 (opts, filenames) = parser.parse_args() | 160 (opts, filenames) = parser.parse_args() |
143 | 161 |
144 if not filenames: | 162 if not filenames: |
145 sys.exit(0) # This is OK as a no-op | 163 sys.exit(0) # This is OK as a no-op |
146 | 164 |
147 # Unless in bundle mode, only one file should be specified. | 165 # Unless in bundle mode, only one file should be specified. |
148 if opts.generator != 'cpp-bundle' and len(filenames) > 1: | 166 if opts.generator != 'cpp-bundle' and len(filenames) > 1: |
149 # TODO(sashab): Could also just use filenames[0] here and not complain. | 167 # TODO(sashab): Could also just use filenames[0] here and not complain. |
150 raise Exception( | 168 raise Exception( |
151 "Unless in bundle mode, only one file can be specified at a time.") | 169 "Unless in bundle mode, only one file can be specified at a time.") |
152 | 170 |
153 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, | 171 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, |
154 opts.namespace, opts.dart_overrides_dir) | 172 opts.namespace, opts.dart_overrides_dir, |
173 is_ppapi_dev=opts.ppapi_dev) | |
155 if not opts.destdir: | 174 if not opts.destdir: |
156 print result | 175 print result |
OLD | NEW |