| 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_bundle_generator | |
| 22 import h_generator | 21 import h_generator |
| 23 import idl_schema | 22 import idl_schema |
| 24 import json_schema | 23 import json_schema |
| 25 import model | 24 import model |
| 25 import schema_bundle_generator |
| 26 |
| 26 import optparse | 27 import optparse |
| 27 import os.path | 28 import os.path |
| 28 import sys | 29 import sys |
| 29 | 30 |
| 30 def load_schema(schema): | 31 def load_schema(schema): |
| 31 schema_filename, schema_extension = os.path.splitext(schema) | 32 schema_filename, schema_extension = os.path.splitext(schema) |
| 32 | 33 |
| 33 if schema_extension == '.json': | 34 if schema_extension == '.json': |
| 34 api_defs = json_schema.Load(schema) | 35 api_defs = json_schema.Load(schema) |
| 35 elif schema_extension == '.idl': | 36 elif schema_extension == '.idl': |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 relpath = os.path.relpath(os.path.normpath(filenames[0]), root) | 113 relpath = os.path.relpath(os.path.normpath(filenames[0]), root) |
| 113 for target_namespace in api_defs: | 114 for target_namespace in api_defs: |
| 114 api_model.AddNamespace(target_namespace, relpath) | 115 api_model.AddNamespace(target_namespace, relpath) |
| 115 | 116 |
| 116 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) | 117 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) |
| 117 for referenced_namespace in api_model.namespaces.values(): | 118 for referenced_namespace in api_model.namespaces.values(): |
| 118 type_generator.AddNamespace( | 119 type_generator.AddNamespace( |
| 119 referenced_namespace, | 120 referenced_namespace, |
| 120 referenced_namespace.unix_name) | 121 referenced_namespace.unix_name) |
| 121 | 122 |
| 122 generator = h_bundle_generator.HBundleGenerator(api_model, type_generator) | 123 generator = schema_bundle_generator.SchemaBundleGenerator( |
| 123 h_bundle_code = generator.Generate().Render() | 124 api_model, api_defs, type_generator) |
| 125 api_h_code = generator.GenerateAPIHeader().Render() |
| 126 schemas_h_code = generator.GenerateSchemasHeader().Render() |
| 127 schemas_cc_code = generator.GenerateSchemasCC().Render() |
| 124 | 128 |
| 125 out_file = 'generated_api' | |
| 126 if dest_dir: | 129 if dest_dir: |
| 127 with open( | 130 basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') |
| 128 os.path.join(dest_dir, 'chrome/common/extensions/api/generated_api.h'), | 131 with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: |
| 129 'w') as h_file: | 132 h_file.write(api_h_code) |
| 130 h_file.write(h_bundle_code) | 133 with open(os.path.join(basedir, 'generated_schemas.h'), 'w') as h_file: |
| 134 h_file.write(schemas_h_code) |
| 135 with open(os.path.join(basedir, 'generated_schemas.cc'), 'w') as cc_file: |
| 136 cc_file.write(schemas_cc_code) |
| 131 else: | 137 else: |
| 132 print '%s.h' % out_file | 138 print 'generated_api.h' |
| 133 print | 139 print |
| 134 print h_bundle_code | 140 print api_h_code |
| 141 print |
| 142 print 'generated_schemas.h' |
| 143 print |
| 144 print schemas_h_code |
| 145 print |
| 146 print 'generated_schemas.cc' |
| 147 print |
| 148 print schemas_cc_code |
| 135 | 149 |
| 136 if __name__ == '__main__': | 150 if __name__ == '__main__': |
| 137 parser = optparse.OptionParser( | 151 parser = optparse.OptionParser( |
| 138 description='Generates a C++ model of an API from JSON schema', | 152 description='Generates a C++ model of an API from JSON schema', |
| 139 usage='usage: %prog [option]... schema') | 153 usage='usage: %prog [option]... schema') |
| 140 parser.add_option('-r', '--root', default='.', | 154 parser.add_option('-r', '--root', default='.', |
| 141 help='logical include root directory. Path to schema files from specified' | 155 help='logical include root directory. Path to schema files from specified' |
| 142 'dir will be the include path.') | 156 'dir will be the include path.') |
| 143 parser.add_option('-d', '--destdir', | 157 parser.add_option('-d', '--destdir', |
| 144 help='root directory to output generated files.') | 158 help='root directory to output generated files.') |
| 145 parser.add_option('-n', '--namespace', default='generated_api_schemas', | 159 parser.add_option('-n', '--namespace', default='generated_api_schemas', |
| 146 help='C++ namespace for generated files. e.g extensions::api.') | 160 help='C++ namespace for generated files. e.g extensions::api.') |
| 147 parser.add_option('-b', '--bundle', action="store_true", help= | 161 parser.add_option('-b', '--bundle', action="store_true", help= |
| 148 '''if supplied, causes compiler to generate bundle files for the given set of | 162 '''if supplied, causes compiler to generate bundle files for the given set of |
| 149 source files.''') | 163 source files.''') |
| 150 | 164 |
| 151 (opts, args) = parser.parse_args() | 165 (opts, args) = parser.parse_args() |
| 152 | 166 |
| 153 if not args: | 167 if not args: |
| 154 sys.exit(0) # This is OK as a no-op | 168 sys.exit(0) # This is OK as a no-op |
| 155 dest_dir = opts.destdir | 169 dest_dir = opts.destdir |
| 156 root_namespace = opts.namespace | 170 root_namespace = opts.namespace |
| 157 | 171 |
| 158 if opts.bundle: | 172 if opts.bundle: |
| 159 handle_bundle_schema(args, dest_dir, opts.root, root_namespace) | 173 handle_bundle_schema(args, dest_dir, opts.root, root_namespace) |
| 160 else: | 174 else: |
| 161 handle_single_schema(args[0], dest_dir, opts.root, root_namespace) | 175 handle_single_schema(args[0], dest_dir, opts.root, root_namespace) |
| OLD | NEW |