| 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 --suffix _api --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 cpp_util | |
| 20 import cc_generator | 19 import cc_generator |
| 21 import cpp_type_generator | 20 import cpp_type_generator |
| 22 import h_generator | 21 import h_generator |
| 23 import json | 22 import json |
| 24 import model | 23 import model |
| 25 import optparse | 24 import optparse |
| 26 import os.path | 25 import os.path |
| 27 import sys | 26 import sys |
| 28 | 27 |
| 29 if __name__ == '__main__': | 28 if __name__ == '__main__': |
| 30 parser = optparse.OptionParser( | 29 parser = optparse.OptionParser( |
| 31 description='Generates a C++ model of an API from JSON schema', | 30 description='Generates a C++ model of an API from JSON schema', |
| 32 usage='usage: %prog [option]... schema [referenced_schema]...') | 31 usage='usage: %prog [option]... schema [referenced_schema]...') |
| 33 parser.add_option('-r', '--root', default='.', | 32 parser.add_option('-r', '--root', default='.', |
| 34 help='logical include root directory. Path to schema files from specified' | 33 help='logical include root directory. Path to schema files from specified' |
| 35 'dir will be the include path.') | 34 'dir will be the include path.') |
| 36 parser.add_option('-d', '--destdir', | 35 parser.add_option('-d', '--destdir', |
| 37 help='root directory to output generated files.') | 36 help='root directory to output generated files.') |
| 38 parser.add_option('-n', '--namespace', default='generated_api_schemas', | 37 parser.add_option('-n', '--namespace', default='generated_api_schemas', |
| 39 help='C++ namespace for generated files. e.g extensions::api.') | 38 help='C++ namespace for generated files. e.g extensions::api.') |
| 40 parser.add_option('-s', '--suffix', default='', | |
| 41 help='Filename and C++ namespace suffix for generated files.') | |
| 42 | 39 |
| 43 (opts, args) = parser.parse_args() | 40 (opts, args) = parser.parse_args() |
| 44 if not args: | 41 if not args: |
| 45 sys.exit(parser.get_usage()) | 42 sys.exit(parser.get_usage()) |
| 46 dest_dir = opts.destdir | 43 dest_dir = opts.destdir |
| 47 root_namespace = opts.namespace | 44 root_namespace = opts.namespace |
| 48 filename_suffix = opts.suffix | |
| 49 | 45 |
| 50 schema = os.path.normpath(args[0]) | 46 schema = os.path.normpath(args[0]) |
| 51 referenced_schemas = args[1:] | 47 referenced_schemas = args[1:] |
| 52 | 48 |
| 53 api_model = model.Model() | 49 api_model = model.Model() |
| 54 | 50 |
| 55 # Load type dependencies into the model. | 51 # Load type dependencies into the model. |
| 56 for referenced_schema_path in referenced_schemas: | 52 for referenced_schema_path in referenced_schemas: |
| 57 with open(referenced_schema_path, 'r') as referenced_schema_file: | 53 with open(referenced_schema_path, 'r') as referenced_schema_file: |
| 58 referenced_api_defs = json.loads(referenced_schema_file.read()) | 54 referenced_api_defs = json.loads(referenced_schema_file.read()) |
| 59 | 55 |
| 60 for namespace in referenced_api_defs: | 56 for namespace in referenced_api_defs: |
| 61 api_model.AddNamespace(namespace, | 57 api_model.AddNamespace(namespace, |
| 62 os.path.relpath(referenced_schema_path, opts.root)) | 58 os.path.relpath(referenced_schema_path, opts.root)) |
| 63 | 59 |
| 64 # Actually generate for source file. | 60 # Actually generate for source file. |
| 65 with open(schema, 'r') as schema_file: | 61 with open(schema, 'r') as schema_file: |
| 66 api_defs = json.loads(schema_file.read()) | 62 api_defs = json.loads(schema_file.read()) |
| 67 | 63 |
| 68 for target_namespace in api_defs: | 64 for target_namespace in api_defs: |
| 69 # Gets the relative path from opts.root to the schema to correctly determine | 65 # Gets the relative path from opts.root to the schema to correctly determine |
| 70 # the include path. | 66 # the include path. |
| 71 relpath = os.path.relpath(schema, opts.root) | 67 relpath = os.path.relpath(schema, opts.root) |
| 72 namespace = api_model.AddNamespace(target_namespace, relpath) | 68 namespace = api_model.AddNamespace(target_namespace, relpath) |
| 73 if not namespace: | 69 if not namespace: |
| 74 continue | 70 continue |
| 75 | 71 |
| 76 out_file = namespace.name + filename_suffix | 72 # The output filename must match the input filename for gyp to deal with it |
| 73 # properly. |
| 74 out_file = namespace.name |
| 77 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace, | 75 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace, |
| 78 namespace, out_file) | 76 namespace, out_file) |
| 79 for referenced_namespace in api_model.namespaces.values(): | 77 for referenced_namespace in api_model.namespaces.values(): |
| 80 type_generator.AddNamespace( | 78 type_generator.AddNamespace( |
| 81 referenced_namespace, | 79 referenced_namespace, |
| 82 cpp_util.Classname(referenced_namespace.name).lower() + | 80 referenced_namespace.unix_name) |
| 83 filename_suffix) | |
| 84 cc_generator = cc_generator.CCGenerator(namespace, type_generator) | 81 cc_generator = cc_generator.CCGenerator(namespace, type_generator) |
| 85 cc_code = cc_generator.Generate().Render() | 82 cc_code = cc_generator.Generate().Render() |
| 86 h_generator = h_generator.HGenerator(namespace, type_generator) | 83 h_generator = h_generator.HGenerator(namespace, type_generator) |
| 87 h_code = h_generator.Generate().Render() | 84 h_code = h_generator.Generate().Render() |
| 88 | 85 |
| 89 if dest_dir: | 86 if dest_dir: |
| 90 with open( | 87 with open( |
| 91 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), | 88 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), |
| 92 'w') as cc_file: | 89 'w') as cc_file: |
| 93 cc_file.write(cc_code) | 90 cc_file.write(cc_code) |
| 94 with open( | 91 with open( |
| 95 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.h'), | 92 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.h'), |
| 96 'w') as h_file: | 93 'w') as h_file: |
| 97 h_file.write(h_code) | 94 h_file.write(h_code) |
| 98 else: | 95 else: |
| 99 print '%s.h' % out_file | 96 print '%s.h' % out_file |
| 100 print | 97 print |
| 101 print h_code | 98 print h_code |
| 102 print | 99 print |
| 103 print '%s.cc' % out_file | 100 print '%s.cc' % out_file |
| 104 print | 101 print |
| 105 print cc_code | 102 print cc_code |
| OLD | NEW |