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