| 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 15 matching lines...) Expand all Loading... |
| 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 schema_loader import SchemaLoader | 29 from schema_loader import SchemaLoader |
| 30 | 30 |
| 31 # Names of supported code generators, as specified on the command-line. | 31 # Names of supported code generators, as specified on the command-line. |
| 32 # First is default. | 32 # First is default. |
| 33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] | 33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] |
| 34 | 34 |
| 35 def GenerateSchema(generator, | 35 def GenerateSchema(generator, |
| 36 filenames, | 36 file_paths, |
| 37 root, | 37 root, |
| 38 destdir, | 38 destdir, |
| 39 root_namespace, | 39 cpp_namespace_pattern, |
| 40 dart_overrides_dir, | 40 dart_overrides_dir, |
| 41 impl_dir): | 41 impl_dir): |
| 42 # Merge the source files into a single list of schemas. | 42 # Merge the source files into a single list of schemas. |
| 43 api_defs = [] | 43 api_defs = [] |
| 44 for filename in filenames: | 44 for file_path in file_paths: |
| 45 schema = os.path.normpath(filename) | 45 schema = os.path.normpath(file_path) |
| 46 schema_loader = SchemaLoader( | 46 schema_loader = SchemaLoader( |
| 47 os.path.dirname(os.path.relpath(os.path.normpath(filename), root)), | 47 os.path.dirname(os.path.relpath(schema, root)), |
| 48 os.path.dirname(filename)) | 48 os.path.dirname(file_path)) |
| 49 api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) | 49 api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) |
| 50 | 50 |
| 51 # If compiling the C++ model code, delete 'nocompile' nodes. | 51 # If compiling the C++ model code, delete 'nocompile' nodes. |
| 52 if generator == 'cpp': | 52 if generator == 'cpp': |
| 53 api_def = json_schema.DeleteNodes(api_def, 'nocompile') | 53 api_def = json_schema.DeleteNodes(api_def, 'nocompile') |
| 54 api_defs.extend(api_def) | 54 api_defs.extend(api_def) |
| 55 | 55 |
| 56 api_model = Model() | 56 api_model = Model() |
| 57 | 57 |
| 58 # For single-schema compilation make sure that the first (i.e. only) schema | 58 # For single-schema compilation make sure that the first (i.e. only) schema |
| 59 # is the default one. | 59 # is the default one. |
| 60 default_namespace = None | 60 default_namespace = None |
| 61 | 61 |
| 62 # If we have files from multiple source paths, we'll use the common parent | 62 # If we have files from multiple source paths, we'll use the common parent |
| 63 # path as the source directory. | 63 # path as the source directory. |
| 64 src_path = None | 64 src_path = None |
| 65 | 65 |
| 66 # Load the actual namespaces into the model. | 66 # Load the actual namespaces into the model. |
| 67 for target_namespace, schema_filename in zip(api_defs, filenames): | 67 for target_namespace, file_path in zip(api_defs, file_paths): |
| 68 relpath = os.path.relpath(os.path.normpath(schema_filename), root) | 68 relpath = os.path.relpath(os.path.normpath(file_path), root) |
| 69 namespace = api_model.AddNamespace(target_namespace, | 69 namespace = api_model.AddNamespace(target_namespace, |
| 70 relpath, | 70 relpath, |
| 71 include_compiler_options=True) | 71 include_compiler_options=True) |
| 72 | 72 |
| 73 if default_namespace is None: | 73 if default_namespace is None: |
| 74 default_namespace = namespace | 74 default_namespace = namespace |
| 75 | 75 |
| 76 if src_path is None: | 76 if src_path is None: |
| 77 src_path = namespace.source_file_dir | 77 src_path = namespace.source_file_dir |
| 78 else: | 78 else: |
| 79 src_path = os.path.commonprefix((src_path, namespace.source_file_dir)) | 79 src_path = os.path.commonprefix((src_path, namespace.source_file_dir)) |
| 80 | 80 |
| 81 path, filename = os.path.split(schema_filename) | 81 path, filename = os.path.split(file_path) |
| 82 short_filename, extension = os.path.splitext(filename) | 82 filename_base, _ = os.path.splitext(filename) |
| 83 | 83 |
| 84 # Construct the type generator with all the namespaces in this model. | 84 # Construct the type generator with all the namespaces in this model. |
| 85 type_generator = CppTypeGenerator(api_model, | 85 type_generator = CppTypeGenerator(api_model, |
| 86 schema_loader, | 86 schema_loader, |
| 87 default_namespace=default_namespace) | 87 default_namespace) |
| 88 if generator == 'cpp-bundle': | 88 if generator == 'cpp-bundle': |
| 89 cpp_bundle_generator = CppBundleGenerator(root, | 89 cpp_bundle_generator = CppBundleGenerator(root, |
| 90 api_model, | 90 api_model, |
| 91 api_defs, | 91 api_defs, |
| 92 type_generator, | 92 type_generator, |
| 93 root_namespace, | 93 cpp_namespace_pattern, |
| 94 src_path, | 94 src_path, |
| 95 impl_dir) | 95 impl_dir) |
| 96 generators = [ | 96 generators = [ |
| 97 ('generated_api.cc', cpp_bundle_generator.api_cc_generator), | 97 ('generated_api.cc', cpp_bundle_generator.api_cc_generator), |
| 98 ('generated_api.h', cpp_bundle_generator.api_h_generator), | 98 ('generated_api.h', cpp_bundle_generator.api_h_generator), |
| 99 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), | 99 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), |
| 100 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) | 100 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) |
| 101 ] | 101 ] |
| 102 elif generator == 'cpp': | 102 elif generator == 'cpp': |
| 103 cpp_generator = CppGenerator(type_generator, root_namespace) | 103 cpp_generator = CppGenerator(type_generator, cpp_namespace_pattern) |
| 104 generators = [ | 104 generators = [ |
| 105 ('%s.h' % short_filename, cpp_generator.h_generator), | 105 ('%s.h' % filename_base, cpp_generator.h_generator), |
| 106 ('%s.cc' % short_filename, cpp_generator.cc_generator) | 106 ('%s.cc' % filename_base, cpp_generator.cc_generator) |
| 107 ] | 107 ] |
| 108 elif generator == 'dart': | 108 elif generator == 'dart': |
| 109 generators = [ | 109 generators = [ |
| 110 ('%s.dart' % namespace.unix_name, DartGenerator( | 110 ('%s.dart' % namespace.unix_name, DartGenerator( |
| 111 dart_overrides_dir)) | 111 dart_overrides_dir)) |
| 112 ] | 112 ] |
| 113 else: | 113 else: |
| 114 raise Exception('Unrecognised generator %s' % generator) | 114 raise Exception('Unrecognised generator %s' % generator) |
| 115 | 115 |
| 116 output_code = [] | 116 output_code = [] |
| (...skipping 23 matching lines...) Expand all Loading... |
| 140 help='C++ namespace for generated files. e.g extensions::api.') | 140 help='C++ namespace for generated files. e.g extensions::api.') |
| 141 parser.add_option('-g', '--generator', default=GENERATORS[0], | 141 parser.add_option('-g', '--generator', default=GENERATORS[0], |
| 142 choices=GENERATORS, | 142 choices=GENERATORS, |
| 143 help='The generator to use to build the output code. Supported values are' | 143 help='The generator to use to build the output code. Supported values are' |
| 144 ' %s' % GENERATORS) | 144 ' %s' % GENERATORS) |
| 145 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', | 145 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', |
| 146 help='Adds custom dart from files in the given directory (Dart only).') | 146 help='Adds custom dart from files in the given directory (Dart only).') |
| 147 parser.add_option('-i', '--impl-dir', dest='impl_dir', | 147 parser.add_option('-i', '--impl-dir', dest='impl_dir', |
| 148 help='The root path of all API implementations') | 148 help='The root path of all API implementations') |
| 149 | 149 |
| 150 (opts, filenames) = parser.parse_args() | 150 (opts, file_paths) = parser.parse_args() |
| 151 | 151 |
| 152 if not filenames: | 152 if not file_paths: |
| 153 sys.exit(0) # This is OK as a no-op | 153 sys.exit(0) # This is OK as a no-op |
| 154 | 154 |
| 155 # Unless in bundle mode, only one file should be specified. | 155 # Unless in bundle mode, only one file should be specified. |
| 156 if opts.generator != 'cpp-bundle' and len(filenames) > 1: | 156 if opts.generator != 'cpp-bundle' and len(file_paths) > 1: |
| 157 # TODO(sashab): Could also just use filenames[0] here and not complain. | 157 # TODO(sashab): Could also just use file_paths[0] here and not complain. |
| 158 raise Exception( | 158 raise Exception( |
| 159 "Unless in bundle mode, only one file can be specified at a time.") | 159 "Unless in bundle mode, only one file can be specified at a time.") |
| 160 | 160 |
| 161 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, | 161 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
| 162 opts.namespace, opts.dart_overrides_dir, | 162 opts.namespace, opts.dart_overrides_dir, |
| 163 opts.impl_dir) | 163 opts.impl_dir) |
| 164 if not opts.destdir: | 164 if not opts.destdir: |
| 165 print result | 165 print result |
| OLD | NEW |