Revert safe-browsing PrefixSet size_t handling.
[chromium-blink-merge.git] / tools / json_schema_compiler / compiler.py
blobc79beaec767644b1913c30fc7d4cfbddfb79e407
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.
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.
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 """
19 import optparse
20 import os
21 import sys
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
32 # Names of supported code generators, as specified on the command-line.
33 # First is default.
34 GENERATORS = ['cpp', 'cpp-bundle', 'dart', 'ppapi']
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])
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)
56 api_model = Model()
58 # For single-schema compilation make sure that the first (i.e. only) schema
59 # is the default one.
60 default_namespace = None
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
71 path, filename = os.path.split(schema_filename)
72 short_filename, extension = os.path.splitext(filename)
74 # Construct the type generator with all the namespaces in this model.
75 type_generator = CppTypeGenerator(api_model,
76 schema_loader,
77 default_namespace=default_namespace)
79 if generator == 'cpp-bundle':
80 cpp_bundle_generator = CppBundleGenerator(root,
81 api_model,
82 api_defs,
83 type_generator,
84 root_namespace,
85 namespace.source_file_dir)
86 generators = [
87 ('generated_api.cc', cpp_bundle_generator.api_cc_generator),
88 ('generated_api.h', cpp_bundle_generator.api_h_generator),
89 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator),
90 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator)
92 elif generator == 'cpp':
93 cpp_generator = CppGenerator(type_generator, root_namespace)
94 generators = [
95 ('%s.h' % short_filename, cpp_generator.h_generator),
96 ('%s.cc' % short_filename, cpp_generator.cc_generator)
98 elif generator == 'dart':
99 generators = [
100 ('%s.dart' % namespace.unix_name, DartGenerator(
101 dart_overrides_dir))
103 elif generator == 'ppapi':
104 generator = PpapiGenerator()
105 generators = [
106 (os.path.join('api', 'ppb_%s.idl' % namespace.unix_name),
107 generator.idl_generator),
109 else:
110 raise Exception('Unrecognised generator %s' % generator)
112 output_code = []
113 for filename, generator in generators:
114 code = generator.Generate(namespace).Render()
115 if destdir:
116 with open(os.path.join(destdir, namespace.source_file_dir,
117 filename), 'w') as f:
118 f.write(code)
119 output_code += [filename, '', code, '']
121 return '\n'.join(output_code)
124 if __name__ == '__main__':
125 parser = optparse.OptionParser(
126 description='Generates a C++ model of an API from JSON schema',
127 usage='usage: %prog [option]... schema')
128 parser.add_option('-r', '--root', default='.',
129 help='logical include root directory. Path to schema files from specified'
130 ' dir will be the include path.')
131 parser.add_option('-d', '--destdir',
132 help='root directory to output generated files.')
133 parser.add_option('-n', '--namespace', default='generated_api_schemas',
134 help='C++ namespace for generated files. e.g extensions::api.')
135 parser.add_option('-g', '--generator', default=GENERATORS[0],
136 choices=GENERATORS,
137 help='The generator to use to build the output code. Supported values are'
138 ' %s' % GENERATORS)
139 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir',
140 help='Adds custom dart from files in the given directory (Dart only).')
142 (opts, filenames) = parser.parse_args()
144 if not filenames:
145 sys.exit(0) # This is OK as a no-op
147 # Unless in bundle mode, only one file should be specified.
148 if opts.generator != 'cpp-bundle' and len(filenames) > 1:
149 # TODO(sashab): Could also just use filenames[0] here and not complain.
150 raise Exception(
151 "Unless in bundle mode, only one file can be specified at a time.")
153 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir,
154 opts.namespace, opts.dart_overrides_dir)
155 if not opts.destdir:
156 print result