Chromium Code Reviews| 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 27 matching lines...) Expand all Loading... | |
| 38 'cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs', 'interface' | 38 'cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs', 'interface' |
| 39 ] | 39 ] |
| 40 | 40 |
| 41 def GenerateSchema(generator_name, | 41 def GenerateSchema(generator_name, |
| 42 file_paths, | 42 file_paths, |
| 43 root, | 43 root, |
| 44 destdir, | 44 destdir, |
| 45 cpp_namespace_pattern, | 45 cpp_namespace_pattern, |
| 46 bundle_name, | 46 bundle_name, |
| 47 impl_dir, | 47 impl_dir, |
| 48 include_rules): | 48 include_rules, |
| 49 no_normalize_enums): | |
| 49 # Merge the source files into a single list of schemas. | 50 # Merge the source files into a single list of schemas. |
| 50 api_defs = [] | 51 api_defs = [] |
| 51 for file_path in file_paths: | 52 for file_path in file_paths: |
| 52 schema = os.path.relpath(file_path, root) | 53 schema = os.path.relpath(file_path, root) |
| 53 api_def = SchemaLoader(root).LoadSchema(schema) | 54 api_def = SchemaLoader(root).LoadSchema(schema) |
| 54 | 55 |
| 55 # If compiling the C++ model code, delete 'nocompile' nodes. | 56 # If compiling the C++ model code, delete 'nocompile' nodes. |
| 56 if generator_name == 'cpp': | 57 if generator_name == 'cpp': |
| 57 api_def = json_schema.DeleteNodes(api_def, 'nocompile') | 58 api_def = json_schema.DeleteNodes(api_def, 'nocompile') |
| 58 | 59 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) | 120 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) |
| 120 ] | 121 ] |
| 121 elif generator_name == 'cpp': | 122 elif generator_name == 'cpp': |
| 122 cpp_generator = CppGenerator(type_generator) | 123 cpp_generator = CppGenerator(type_generator) |
| 123 generators = [ | 124 generators = [ |
| 124 ('%s.h' % filename_base, cpp_generator.h_generator), | 125 ('%s.h' % filename_base, cpp_generator.h_generator), |
| 125 ('%s.cc' % filename_base, cpp_generator.cc_generator) | 126 ('%s.cc' % filename_base, cpp_generator.cc_generator) |
| 126 ] | 127 ] |
| 127 elif generator_name == 'externs': | 128 elif generator_name == 'externs': |
| 128 generators = [ | 129 generators = [ |
| 129 ('%s_externs.js' % namespace.unix_name, JsExternsGenerator()) | 130 ('%s_externs.js' % namespace.unix_name, JsExternsGenerator( |
| 131 no_normalize_enums)) | |
| 130 ] | 132 ] |
| 131 elif generator_name == 'interface': | 133 elif generator_name == 'interface': |
| 132 generators = [ | 134 generators = [ |
| 133 ('%s_interface.js' % namespace.unix_name, JsInterfaceGenerator()) | 135 ('%s_interface.js' % namespace.unix_name, JsInterfaceGenerator()) |
| 134 ] | 136 ] |
| 135 else: | 137 else: |
| 136 raise Exception('Unrecognised generator %s' % generator_name) | 138 raise Exception('Unrecognised generator %s' % generator_name) |
| 137 | 139 |
| 138 output_code = [] | 140 output_code = [] |
| 139 for filename, generator in generators: | 141 for filename, generator in generators: |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 parser.add_option('-g', '--generator', default=GENERATORS[0], | 178 parser.add_option('-g', '--generator', default=GENERATORS[0], |
| 177 choices=GENERATORS, | 179 choices=GENERATORS, |
| 178 help='The generator to use to build the output code. Supported values are' | 180 help='The generator to use to build the output code. Supported values are' |
| 179 ' %s' % GENERATORS) | 181 ' %s' % GENERATORS) |
| 180 parser.add_option('-i', '--impl-dir', dest='impl_dir', | 182 parser.add_option('-i', '--impl-dir', dest='impl_dir', |
| 181 help='The root path of all API implementations') | 183 help='The root path of all API implementations') |
| 182 parser.add_option('-I', '--include-rules', | 184 parser.add_option('-I', '--include-rules', |
| 183 help='A list of paths to include when searching for referenced objects,' | 185 help='A list of paths to include when searching for referenced objects,' |
| 184 ' with the namespace separated by a \':\'. Example: ' | 186 ' with the namespace separated by a \':\'. Example: ' |
| 185 '/foo/bar:Foo::Bar::%(namespace)s') | 187 '/foo/bar:Foo::Bar::%(namespace)s') |
| 188 parser.add_option('-e', '--no-normalize-enums', action='store_true', | |
| 189 dest='no_normalize_enums', | |
|
Devlin
2017/01/04 23:54:21
nit: no_normalize_enums sounds funny - maybe we co
| |
| 190 help='Do not normalize enums to uppercase with underscores' | |
| 191 ' when generating externs') | |
| 186 | 192 |
| 187 (opts, file_paths) = parser.parse_args() | 193 (opts, file_paths) = parser.parse_args() |
| 188 | 194 |
| 189 if not file_paths: | 195 if not file_paths: |
| 190 sys.exit(0) # This is OK as a no-op | 196 sys.exit(0) # This is OK as a no-op |
| 191 | 197 |
| 192 # Unless in bundle mode, only one file should be specified. | 198 # Unless in bundle mode, only one file should be specified. |
| 193 if (opts.generator not in ('cpp-bundle-registration', 'cpp-bundle-schema') and | 199 if (opts.generator not in ('cpp-bundle-registration', 'cpp-bundle-schema') and |
| 194 len(file_paths) > 1): | 200 len(file_paths) > 1): |
| 195 # TODO(sashab): Could also just use file_paths[0] here and not complain. | 201 # TODO(sashab): Could also just use file_paths[0] here and not complain. |
| 196 raise Exception( | 202 raise Exception( |
| 197 "Unless in bundle mode, only one file can be specified at a time.") | 203 "Unless in bundle mode, only one file can be specified at a time.") |
| 198 | 204 |
| 199 def split_path_and_namespace(path_and_namespace): | 205 def split_path_and_namespace(path_and_namespace): |
| 200 if ':' not in path_and_namespace: | 206 if ':' not in path_and_namespace: |
| 201 raise ValueError('Invalid include rule "%s". Rules must be of ' | 207 raise ValueError('Invalid include rule "%s". Rules must be of ' |
| 202 'the form path:namespace' % path_and_namespace) | 208 'the form path:namespace' % path_and_namespace) |
| 203 return path_and_namespace.split(':', 1) | 209 return path_and_namespace.split(':', 1) |
| 204 | 210 |
| 205 include_rules = [] | 211 include_rules = [] |
| 206 if opts.include_rules: | 212 if opts.include_rules: |
| 207 include_rules = map(split_path_and_namespace, | 213 include_rules = map(split_path_and_namespace, |
| 208 shlex.split(opts.include_rules)) | 214 shlex.split(opts.include_rules)) |
| 209 | 215 |
| 210 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, | 216 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
| 211 opts.namespace, opts.bundle_name, opts.impl_dir, | 217 opts.namespace, opts.bundle_name, opts.impl_dir, |
| 212 include_rules) | 218 include_rules, opts.no_normalize_enums) |
| 213 if not opts.destdir: | 219 if not opts.destdir: |
| 214 print result | 220 print result |
| OLD | NEW |