1 #!/usr/bin/env python 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 4 # found in the LICENSE file. 5 """Generator for C++ structs from api json files. 6 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. 9 Originally written for generating code for extension apis. Reference schemas 10 are in chrome/common/extensions/api. 11 12 Usage example: 13 compiler.py --root /home/Work/src --namespace extensions windows.json 14 tabs.json 15 compiler.py --destdir gen --root /home/Work/src 16 --namespace extensions windows.json tabs.json 17 """ 18 19 import optparse 20 import os 21 import sys 22 23 from cpp_bundle_generator import CppBundleGenerator 24 from cpp_generator import CppGenerator 25 from cpp_type_generator import CppTypeGenerator 26 from dart_generator import DartGenerator 27 import json_schema 28 from model import Model 29 from ppapi_generator import PpapiGenerator 30 from schema_loader import SchemaLoader 31 32 # Names of supported code generators, as specified on the command-line. 33 # First is default. 34 GENERATORS = ['cpp', 'cpp-bundle', 'dart', 'ppapi'] 35 36 def GenerateSchema(generator, 37 filenames, 38 root, 39 destdir, 40 root_namespace, 41 dart_overrides_dir): 42 schema_loader = SchemaLoader( 43 os.path.dirname(os.path.relpath(os.path.normpath(filenames[0]), root)), 44 os.path.dirname(filenames[0])) 45 # Merge the source files into a single list of schemas. 46 api_defs = [] 47 for filename in filenames: 48 schema = os.path.normpath(filename) 49 api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) 50 51 # If compiling the C++ model code, delete 'nocompile' nodes. 52 if generator == 'cpp': 53 api_def = json_schema.DeleteNodes(api_def, 'nocompile') 54 api_defs.extend(api_def) 55 56 api_model = Model() 57 58 # For single-schema compilation make sure that the first (i.e. only) schema 59 # is the default one. 60 default_namespace = None 61 62 # Load the actual namespaces into the model. 63 for target_namespace, schema_filename in zip(api_defs, filenames): 64 relpath = os.path.relpath(os.path.normpath(schema_filename), root) 65 namespace = api_model.AddNamespace(target_namespace, 66 relpath, 67 include_compiler_options=True) 68 if default_namespace is None: 69 default_namespace = namespace 70 71 path, filename = os.path.split(schema_filename) 72 short_filename, extension = os.path.splitext(filename) 73 74 # Filenames are checked against the unix_names of the namespaces they 75 # generate because the gyp uses the names of the JSON files to generate 76 # the names of the .cc and .h files. We want these to be using unix_names. 77 if namespace.unix_name != short_filename: 78 sys.exit("Filename %s is illegal. Name files using unix_hacker style." % 79 schema_filename) 80 81 # Construct the type generator with all the namespaces in this model. 82 type_generator = CppTypeGenerator(api_model, 83 schema_loader, 84 default_namespace=default_namespace) 85 86 if generator == 'cpp-bundle': 87 cpp_bundle_generator = CppBundleGenerator(root, 88 api_model, 89 api_defs, 90 type_generator, 91 root_namespace, 92 namespace.source_file_dir) 93 generators = [ 94 ('generated_api.cc', cpp_bundle_generator.api_cc_generator), 95 ('generated_api.h', cpp_bundle_generator.api_h_generator), 96 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), 97 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) 98 ] 99 elif generator == 'cpp': 100 cpp_generator = CppGenerator(type_generator, root_namespace) 101 generators = [ 102 ('%s.h' % namespace.unix_name, cpp_generator.h_generator), 103 ('%s.cc' % namespace.unix_name, cpp_generator.cc_generator) 104 ] 105 elif generator == 'dart': 106 generators = [ 107 ('%s.dart' % namespace.unix_name, DartGenerator( 108 dart_overrides_dir)) 109 ] 110 elif generator == 'ppapi': 111 generator = PpapiGenerator() 112 generators = [ 113 (os.path.join('api', 'ppb_%s.idl' % namespace.unix_name), 114 generator.idl_generator), 115 ] 116 else: 117 raise Exception('Unrecognised generator %s' % generator) 118 119 output_code = [] 120 for filename, generator in generators: 121 code = generator.Generate(namespace).Render() 122 if destdir: 123 with open(os.path.join(destdir, namespace.source_file_dir, 124 filename), 'w') as f: 125 f.write(code) 126 output_code += [filename, '', code, ''] 127 128 return '\n'.join(output_code) 129 130 131 if __name__ == '__main__': 132 parser = optparse.OptionParser( 133 description='Generates a C++ model of an API from JSON schema', 134 usage='usage: %prog [option]... schema') 135 parser.add_option('-r', '--root', default='.', 136 help='logical include root directory. Path to schema files from specified' 137 ' dir will be the include path.') 138 parser.add_option('-d', '--destdir', 139 help='root directory to output generated files.') 140 parser.add_option('-n', '--namespace', default='generated_api_schemas', 141 help='C++ namespace for generated files. e.g extensions::api.') 142 parser.add_option('-g', '--generator', default=GENERATORS[0], 143 choices=GENERATORS, 144 help='The generator to use to build the output code. Supported values are' 145 ' %s' % GENERATORS) 146 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', 147 help='Adds custom dart from files in the given directory (Dart only).') 148 149 (opts, filenames) = parser.parse_args() 150 151 if not filenames: 152 sys.exit(0) # This is OK as a no-op 153 154 # Unless in bundle mode, only one file should be specified. 155 if opts.generator != 'cpp-bundle' and len(filenames) > 1: 156 # TODO(sashab): Could also just use filenames[0] here and not complain. 157 raise Exception( 158 "Unless in bundle mode, only one file can be specified at a time.") 159 160 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, 161 opts.namespace, opts.dart_overrides_dir) 162 if not opts.destdir: 163 print result 164