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. |
| 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 from cpp_generator import CppGenerator |
| 20 import cpp_type_generator | 20 from cpp_type_generator import CppTypeGenerator |
| 21 import h_generator | 21 from dart_generator import DartGenerator |
| 22 from cpp_bundle_generator import CppBundleGenerator | |
| 23 from model import Model | |
| 22 import idl_schema | 24 import idl_schema |
| 23 import json_schema | 25 import json_schema |
| 24 import model | |
| 25 import schema_bundle_generator | |
| 26 | 26 |
| 27 import optparse | 27 import optparse |
| 28 import os.path | 28 import os.path |
| 29 import sys | 29 import sys |
| 30 | 30 |
| 31 # Names of supported code generators, as specified on the command-line. | |
| 32 # First is default. | |
| 33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] | |
| 34 | |
| 31 def load_schema(schema): | 35 def load_schema(schema): |
| 32 schema_filename, schema_extension = os.path.splitext(schema) | 36 schema_filename, schema_extension = os.path.splitext(schema) |
| 33 | 37 |
| 34 if schema_extension == '.json': | 38 if schema_extension == '.json': |
| 35 api_defs = json_schema.Load(schema) | 39 api_defs = json_schema.Load(schema) |
| 36 elif schema_extension == '.idl': | 40 elif schema_extension == '.idl': |
| 37 api_defs = idl_schema.Load(schema) | 41 api_defs = idl_schema.Load(schema) |
| 38 else: | 42 else: |
| 39 sys.exit("Did not recognize file extension %s for schema %s" % | 43 sys.exit('Did not recognize file extension %s for schema %s' % |
| 40 (schema_extension, schema)) | 44 (schema_extension, schema)) |
| 41 if len(api_defs) != 1: | 45 if len(api_defs) != 1: |
| 42 sys.exit("File %s has multiple schemas. Files are only allowed to contain a" | 46 sys.exit('File %s has multiple schemas. Files are only allowed to contain a' |
| 43 " single schema." % schema) | 47 ' single schema.' % schema) |
| 44 | 48 |
| 45 return api_defs | 49 return api_defs |
| 46 | 50 |
| 47 def handle_single_schema(filename, dest_dir, root, root_namespace): | 51 if __name__ == '__main__': |
| 48 schema = os.path.normpath(filename) | 52 parser = optparse.OptionParser( |
| 49 schema_filename, schema_extension = os.path.splitext(schema) | 53 description='Generates a C++ model of an API from JSON schema', |
| 50 path, short_filename = os.path.split(schema_filename) | 54 usage='usage: %prog [option]... schema') |
| 51 api_defs = json_schema.DeleteNocompileNodes(load_schema(schema)) | 55 parser.add_option('-r', '--root', default='.', |
| 56 help='logical include root directory. Path to schema files from specified' | |
| 57 'dir will be the include path.') | |
| 58 parser.add_option('-d', '--destdir', | |
| 59 help='root directory to output generated files.') | |
| 60 parser.add_option('-n', '--namespace', default='generated_api_schemas', | |
| 61 help='C++ namespace for generated files. e.g extensions::api.') | |
| 62 parser.add_option('-g', '--generator', default=GENERATORS[0], | |
| 63 choices=GENERATORS, | |
| 64 help='The generator to use to build the output code. Supported values are' | |
| 65 ' %s' % GENERATORS) | |
| 66 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', | |
| 67 help='Adds custom dart from files in the given directory (Dart only).') | |
| 52 | 68 |
| 53 api_model = model.Model() | 69 (opts, filenames) = parser.parse_args() |
| 54 | 70 |
| 71 if not filenames: | |
| 72 sys.exit(0) # This is OK as a no-op | |
| 73 | |
| 74 # Unless in bundle mode, only one file should be specified. | |
| 75 if opts.generator != 'cpp-bundle' and len(filenames) > 1: | |
| 76 # TODO(sashab): Could also just use filenames[0] here and not complain. | |
| 77 raise Exception( | |
| 78 "Unless in bundle mode, only one file can be specified at a time.") | |
| 79 | |
| 80 # Merge the source files into a single list of schemas. | |
| 81 api_defs = [] | |
| 82 for filename in filenames: | |
| 83 schema = os.path.normpath(filename) | |
| 84 schema_filename, schema_extension = os.path.splitext(schema) | |
| 85 path, short_filename = os.path.split(schema_filename) | |
| 86 api_def = load_schema(schema) | |
| 87 | |
| 88 # If in a c++-related mode, delete 'nocompile' nodes. | |
| 89 if opts.generator in ['cpp', 'cpp-bundle']: | |
| 90 api_def = json_schema.DeleteNodes(api_def, 'nocompile') | |
| 91 api_defs.extend(api_def) | |
| 92 | |
| 93 api_model = Model() | |
| 94 | |
| 95 # Load type dependencies into the model. | |
| 55 for target_namespace in api_defs: | 96 for target_namespace in api_defs: |
| 56 referenced_schemas = target_namespace.get('dependencies', []) | |
| 57 # Load type dependencies into the model. | |
| 58 # TODO(miket): do we need this in IDL? | 97 # TODO(miket): do we need this in IDL? |
| 59 for referenced_schema in referenced_schemas: | 98 for referenced_schema in target_namespace.get('dependencies', []): |
| 60 split_schema = referenced_schema.split(':', 1) | 99 split_schema = referenced_schema.split(':', 1) |
| 61 if len(split_schema) > 1: | 100 if len(split_schema) > 1: |
| 62 if split_schema[0] != 'api': | 101 if split_schema[0] != 'api': |
| 63 continue | 102 continue |
| 64 else: | 103 else: |
| 65 referenced_schema = split_schema[1] | 104 referenced_schema = split_schema[1] |
| 66 | 105 |
| 67 referenced_schema_path = os.path.join( | 106 referenced_schema_path = os.path.join( |
| 68 os.path.dirname(schema), referenced_schema + '.json') | 107 os.path.dirname(schema), referenced_schema + '.json') |
| 69 referenced_api_defs = json_schema.Load(referenced_schema_path) | 108 referenced_api_defs = json_schema.Load(referenced_schema_path) |
| 70 | 109 |
| 71 for namespace in referenced_api_defs: | 110 for namespace in referenced_api_defs: |
| 72 api_model.AddNamespace( | 111 api_model.AddNamespace( |
| 73 namespace, | 112 namespace, |
| 74 os.path.relpath(referenced_schema_path, opts.root)) | 113 os.path.relpath(referenced_schema_path, opts.root)) |
| 75 | 114 |
| 76 # Gets the relative path from opts.root to the schema to correctly determine | 115 # Load the actual namespaces into the model. |
| 77 # the include path. | 116 for target_namespace, schema_filename in zip(api_defs, filenames): |
| 78 relpath = os.path.relpath(schema, opts.root) | 117 relpath = os.path.relpath(os.path.normpath(schema_filename), opts.root) |
| 79 namespace = api_model.AddNamespace(target_namespace, | 118 namespace = api_model.AddNamespace(target_namespace, |
| 80 relpath, | 119 relpath, |
| 81 include_compiler_options=True) | 120 include_compiler_options=True) |
| 82 if not namespace: | |
| 83 continue | |
| 84 | |
| 85 if short_filename != namespace.unix_name: | |
| 86 sys.exit("Filename %s is illegal. Name files using unix_hacker style." % | |
| 87 filename) | |
| 88 | |
| 89 # The output filename must match the input filename for gyp to deal with it | |
| 90 # properly. | |
| 91 out_file = namespace.unix_name | |
| 92 type_generator = cpp_type_generator.CppTypeGenerator( | |
| 93 root_namespace, namespace, namespace.unix_name) | |
| 94 for referenced_namespace in api_model.namespaces.values(): | |
| 95 if referenced_namespace == namespace: | |
| 96 continue | |
| 97 type_generator.AddNamespace( | |
| 98 referenced_namespace, | |
| 99 referenced_namespace.unix_name) | |
| 100 | |
| 101 h_code = (h_generator.HGenerator(namespace, type_generator) | |
| 102 .Generate().Render()) | |
| 103 cc_code = (cc_generator.CCGenerator(namespace, type_generator) | |
| 104 .Generate().Render()) | |
| 105 | |
| 106 if dest_dir: | |
| 107 with open( | |
| 108 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), | |
| 109 'w') as cc_file: | |
| 110 cc_file.write(cc_code) | |
| 111 with open( | |
| 112 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.h'), | |
| 113 'w') as h_file: | |
| 114 h_file.write(h_code) | |
| 115 else: | |
| 116 print '%s.h' % out_file | |
| 117 print | |
| 118 print h_code | |
| 119 print | |
| 120 print '%s.cc' % out_file | |
| 121 print | |
| 122 print cc_code | |
| 123 | |
| 124 def handle_bundle_schema(filenames, dest_dir, root, root_namespace): | |
| 125 # Merge the source files into a single list of schemas. | |
| 126 api_defs = [] | |
| 127 for filename in filenames: | |
| 128 schema = os.path.normpath(filename) | |
| 129 schema_filename, schema_extension = os.path.splitext(schema) | |
| 130 api_defs.extend(load_schema(schema)) | |
| 131 | |
| 132 api_model = model.Model() | |
| 133 relpath = os.path.relpath(os.path.normpath(filenames[0]), root) | |
| 134 | |
| 135 for target_namespace, schema_filename in zip(api_defs, filenames): | |
| 136 namespace = api_model.AddNamespace(target_namespace, | |
| 137 relpath, | |
| 138 include_compiler_options=True) | |
| 139 path, filename = os.path.split(schema_filename) | 121 path, filename = os.path.split(schema_filename) |
| 140 short_filename, extension = os.path.splitext(filename) | 122 short_filename, extension = os.path.splitext(filename) |
| 141 | 123 |
| 142 # Filenames are checked against the unix_names of the namespaces they | 124 # Filenames are checked against the unix_names of the namespaces they |
| 143 # generate because the gyp uses the names of the JSON files to generate | 125 # generate because the gyp uses the names of the JSON files to generate |
| 144 # the names of the .cc and .h files. We want these to be using unix_names. | 126 # the names of the .cc and .h files. We want these to be using unix_names. |
| 145 if namespace.unix_name != short_filename: | 127 if namespace.unix_name != short_filename: |
| 146 sys.exit("Filename %s is illegal. Name files using unix_hacker style." % | 128 sys.exit("Filename %s is illegal. Name files using unix_hacker style." % |
| 147 schema_filename) | 129 schema_filename) |
| 148 | 130 |
| 149 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) | 131 # The output filename must match the input filename for gyp to deal with it |
| 132 # properly. | |
| 133 out_file = namespace.unix_name | |
| 134 | |
| 135 # Construct the type generator with all the namespaces in this model. | |
| 136 type_generator = CppTypeGenerator(opts.namespace) | |
| 150 for referenced_namespace in api_model.namespaces.values(): | 137 for referenced_namespace in api_model.namespaces.values(): |
| 151 type_generator.AddNamespace( | 138 type_generator.AddNamespace( |
|
not at google - send to devlin
2013/02/04 17:21:06
You know what? I think it's odd that this happens
sashab
2013/02/04 23:12:45
Done.
| |
| 152 referenced_namespace, | 139 referenced_namespace, |
| 153 referenced_namespace.unix_name) | 140 referenced_namespace.unix_name) |
| 154 | 141 |
| 155 generator = schema_bundle_generator.SchemaBundleGenerator( | 142 # Generate the code for each namespace. |
| 156 root, api_model, api_defs, type_generator) | 143 for namespace in api_model.namespaces.values(): |
| 157 api_h_code = generator.GenerateAPIHeader().Render() | 144 if opts.generator == 'cpp-bundle': |
| 158 schemas_h_code = generator.GenerateSchemasHeader().Render() | 145 cpp_bundle_generator = CppBundleGenerator(opts.root, api_model, api_defs, |
| 159 schemas_cc_code = generator.GenerateSchemasCC().Render() | 146 type_generator) |
| 147 generators = [ | |
| 148 ('generated_api.h', cpp_bundle_generator.api_h_generator), | |
| 149 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), | |
| 150 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) | |
| 151 ] | |
| 152 elif opts.generator == 'cpp': | |
| 153 cpp_generator = CppGenerator(type_generator) | |
| 154 generators = [ | |
| 155 ('%s.h' % namespace.unix_name, cpp_generator.h_generator), | |
| 156 ('%s.cc' % namespace.unix_name, cpp_generator.cc_generator) | |
| 157 ] | |
| 158 elif opts.generator == 'dart': | |
| 159 generators = [ | |
| 160 ('%s.dart' % namespace.unix_name, DartGenerator( | |
| 161 opts.dart_overrides_dir)) | |
| 162 ] | |
| 163 else: | |
| 164 raise Exception('Unrecognised generator %s' % opts.generator) | |
| 160 | 165 |
| 161 if dest_dir: | 166 for filename, generator in generators: |
| 162 basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') | 167 code = generator.Generate(namespace).Render() |
| 163 with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: | 168 if opts.destdir: |
| 164 h_file.write(api_h_code) | 169 with open(os.path.join(opts.destdir, namespace.source_file_dir, |
| 165 with open(os.path.join(basedir, 'generated_schemas.h'), 'w') as h_file: | 170 filename)) as f: |
| 166 h_file.write(schemas_h_code) | 171 f.write(code) |
| 167 with open(os.path.join(basedir, 'generated_schemas.cc'), 'w') as cc_file: | 172 else: |
| 168 cc_file.write(schemas_cc_code) | 173 print filename |
| 169 else: | 174 print |
| 170 print 'generated_api.h' | 175 print code |
| 171 print | 176 print |
| 172 print api_h_code | |
| 173 print | |
| 174 print 'generated_schemas.h' | |
| 175 print | |
| 176 print schemas_h_code | |
| 177 print | |
| 178 print 'generated_schemas.cc' | |
| 179 print | |
| 180 print schemas_cc_code | |
| 181 | |
| 182 if __name__ == '__main__': | |
| 183 parser = optparse.OptionParser( | |
| 184 description='Generates a C++ model of an API from JSON schema', | |
| 185 usage='usage: %prog [option]... schema') | |
| 186 parser.add_option('-r', '--root', default='.', | |
| 187 help='logical include root directory. Path to schema files from specified' | |
| 188 'dir will be the include path.') | |
| 189 parser.add_option('-d', '--destdir', | |
| 190 help='root directory to output generated files.') | |
| 191 parser.add_option('-n', '--namespace', default='generated_api_schemas', | |
| 192 help='C++ namespace for generated files. e.g extensions::api.') | |
| 193 parser.add_option('-b', '--bundle', action="store_true", help= | |
| 194 '''if supplied, causes compiler to generate bundle files for the given set of | |
| 195 source files.''') | |
| 196 | |
| 197 (opts, args) = parser.parse_args() | |
| 198 | |
| 199 if not args: | |
| 200 sys.exit(0) # This is OK as a no-op | |
| 201 dest_dir = opts.destdir | |
| 202 root_namespace = opts.namespace | |
| 203 | |
| 204 if opts.bundle: | |
| 205 handle_bundle_schema(args, dest_dir, opts.root, root_namespace) | |
| 206 else: | |
| 207 handle_single_schema(args[0], dest_dir, opts.root, root_namespace) | |
| OLD | NEW |