WiFiServiceImpl (Windows): Fixed wrong authentication type with WEP-PSK.
[chromium-blink-merge.git] / tools / json_schema_compiler / compiler.py
blobf3c4009f8c5b549145557b33f1ea7d72c669787f
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 shlex
22 import sys
24 from cpp_bundle_generator import CppBundleGenerator
25 from cpp_generator import CppGenerator
26 from cpp_type_generator import CppTypeGenerator
27 from js_externs_generator import JsExternsGenerator
28 import json_schema
29 from cpp_namespace_environment import CppNamespaceEnvironment
30 from model import Model
31 from schema_loader import SchemaLoader
33 # Names of supported code generators, as specified on the command-line.
34 # First is default.
35 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs']
37 def GenerateSchema(generator_name,
38 file_paths,
39 root,
40 destdir,
41 cpp_namespace_pattern,
42 impl_dir,
43 include_rules):
44 # Merge the source files into a single list of schemas.
45 api_defs = []
46 for file_path in file_paths:
47 schema = os.path.relpath(file_path, root)
48 schema_loader = SchemaLoader(
49 root,
50 os.path.dirname(schema),
51 include_rules,
52 cpp_namespace_pattern)
53 api_def = schema_loader.LoadSchema(schema)
55 # If compiling the C++ model code, delete 'nocompile' nodes.
56 if generator_name == 'cpp':
57 api_def = json_schema.DeleteNodes(api_def, 'nocompile')
58 api_defs.extend(api_def)
60 api_model = Model(allow_inline_enums=False)
62 # For single-schema compilation make sure that the first (i.e. only) schema
63 # is the default one.
64 default_namespace = None
66 # If we have files from multiple source paths, we'll use the common parent
67 # path as the source directory.
68 src_path = None
70 # Load the actual namespaces into the model.
71 for target_namespace, file_path in zip(api_defs, file_paths):
72 relpath = os.path.relpath(os.path.normpath(file_path), root)
73 namespace = api_model.AddNamespace(target_namespace,
74 relpath,
75 include_compiler_options=True,
76 environment=CppNamespaceEnvironment(
77 cpp_namespace_pattern))
79 if default_namespace is None:
80 default_namespace = namespace
82 if src_path is None:
83 src_path = namespace.source_file_dir
84 else:
85 src_path = os.path.commonprefix((src_path, namespace.source_file_dir))
87 _, filename = os.path.split(file_path)
88 filename_base, _ = os.path.splitext(filename)
90 # Construct the type generator with all the namespaces in this model.
91 type_generator = CppTypeGenerator(api_model,
92 schema_loader,
93 default_namespace)
94 if generator_name in ('cpp-bundle-registration', 'cpp-bundle-schema'):
95 cpp_bundle_generator = CppBundleGenerator(root,
96 api_model,
97 api_defs,
98 type_generator,
99 cpp_namespace_pattern,
100 src_path,
101 impl_dir)
102 if generator_name == 'cpp-bundle-registration':
103 generators = [
104 ('generated_api_registration.cc',
105 cpp_bundle_generator.api_cc_generator),
106 ('generated_api_registration.h', cpp_bundle_generator.api_h_generator),
108 elif generator_name == 'cpp-bundle-schema':
109 generators = [
110 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator),
111 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator)
113 elif generator_name == 'cpp':
114 cpp_generator = CppGenerator(type_generator)
115 generators = [
116 ('%s.h' % filename_base, cpp_generator.h_generator),
117 ('%s.cc' % filename_base, cpp_generator.cc_generator)
119 elif generator_name == 'externs':
120 generators = [
121 ('%s_externs.js' % namespace.unix_name, JsExternsGenerator())
123 else:
124 raise Exception('Unrecognised generator %s' % generator_name)
126 output_code = []
127 for filename, generator in generators:
128 code = generator.Generate(namespace).Render()
129 if destdir:
130 if generator_name == 'cpp-bundle-registration':
131 # Function registrations must be output to impl_dir, since they link in
132 # API implementations.
133 output_dir = os.path.join(destdir, impl_dir)
134 else:
135 output_dir = os.path.join(destdir, src_path)
136 if not os.path.exists(output_dir):
137 os.makedirs(output_dir)
138 with open(os.path.join(output_dir, filename), 'w') as f:
139 f.write(code)
140 output_code += [filename, '', code, '']
142 return '\n'.join(output_code)
145 if __name__ == '__main__':
146 parser = optparse.OptionParser(
147 description='Generates a C++ model of an API from JSON schema',
148 usage='usage: %prog [option]... schema')
149 parser.add_option('-r', '--root', default='.',
150 help='logical include root directory. Path to schema files from specified'
151 ' dir will be the include path.')
152 parser.add_option('-d', '--destdir',
153 help='root directory to output generated files.')
154 parser.add_option('-n', '--namespace', default='generated_api_schemas',
155 help='C++ namespace for generated files. e.g extensions::api.')
156 parser.add_option('-g', '--generator', default=GENERATORS[0],
157 choices=GENERATORS,
158 help='The generator to use to build the output code. Supported values are'
159 ' %s' % GENERATORS)
160 parser.add_option('-i', '--impl-dir', dest='impl_dir',
161 help='The root path of all API implementations')
162 parser.add_option('-I', '--include-rules',
163 help='A list of paths to include when searching for referenced objects,'
164 ' with the namespace separated by a \':\'. Example: '
165 '/foo/bar:Foo::Bar::%(namespace)s')
167 (opts, file_paths) = parser.parse_args()
169 if not file_paths:
170 sys.exit(0) # This is OK as a no-op
172 # Unless in bundle mode, only one file should be specified.
173 if (opts.generator not in ('cpp-bundle-registration', 'cpp-bundle-schema') and
174 len(file_paths) > 1):
175 # TODO(sashab): Could also just use file_paths[0] here and not complain.
176 raise Exception(
177 "Unless in bundle mode, only one file can be specified at a time.")
179 def split_path_and_namespace(path_and_namespace):
180 if ':' not in path_and_namespace:
181 raise ValueError('Invalid include rule "%s". Rules must be of '
182 'the form path:namespace' % path_and_namespace)
183 return path_and_namespace.split(':', 1)
185 include_rules = []
186 if opts.include_rules:
187 include_rules = map(split_path_and_namespace,
188 shlex.split(opts.include_rules))
190 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir,
191 opts.namespace, opts.impl_dir, include_rules)
192 if not opts.destdir:
193 print result