Add ICU message format support
[chromium-blink-merge.git] / tools / json_schema_compiler / cpp_bundle_generator.py
bloba039368affd05a972d91897e8011047c0d9bab20
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import code
6 import cpp_util
7 from model import Platforms
8 from schema_util import CapitalizeFirstLetter
9 from schema_util import JsFunctionNameToClassName
11 import json
12 import os
13 import re
16 def _RemoveDescriptions(node):
17 """Returns a copy of |schema| with "description" fields removed.
18 """
19 if isinstance(node, dict):
20 result = {}
21 for key, value in node.items():
22 # Some schemas actually have properties called "description", so only
23 # remove descriptions that have string values.
24 if key == 'description' and isinstance(value, basestring):
25 continue
26 result[key] = _RemoveDescriptions(value)
27 return result
28 if isinstance(node, list):
29 return [_RemoveDescriptions(v) for v in node]
30 return node
33 class CppBundleGenerator(object):
34 """This class contains methods to generate code based on multiple schemas.
35 """
37 def __init__(self,
38 root,
39 model,
40 api_defs,
41 cpp_type_generator,
42 cpp_namespace_pattern,
43 bundle_name,
44 source_file_dir,
45 impl_dir):
46 self._root = root
47 self._model = model
48 self._api_defs = api_defs
49 self._cpp_type_generator = cpp_type_generator
50 self._bundle_name = bundle_name
51 self._source_file_dir = source_file_dir
52 self._impl_dir = impl_dir
54 # Hack: assume that the C++ namespace for the bundle is the namespace of the
55 # files without the last component of the namespace. A cleaner way to do
56 # this would be to make it a separate variable in the gyp file.
57 self._cpp_namespace = cpp_namespace_pattern.rsplit('::', 1)[0]
59 self.api_cc_generator = _APICCGenerator(self)
60 self.api_h_generator = _APIHGenerator(self)
61 self.schemas_cc_generator = _SchemasCCGenerator(self)
62 self.schemas_h_generator = _SchemasHGenerator(self)
64 def _GenerateHeader(self, file_base, body_code):
65 """Generates a code.Code object for a header file
67 Parameters:
68 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h')
69 - |body_code| - the code to put in between the multiple inclusion guards"""
70 c = code.Code()
71 c.Append(cpp_util.CHROMIUM_LICENSE)
72 c.Append()
73 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % self._source_file_dir)
74 ifndef_name = cpp_util.GenerateIfndefName(
75 '%s/%s.h' % (self._source_file_dir, file_base))
76 c.Append()
77 c.Append('#ifndef %s' % ifndef_name)
78 c.Append('#define %s' % ifndef_name)
79 c.Append()
80 c.Concat(body_code)
81 c.Append()
82 c.Append('#endif // %s' % ifndef_name)
83 c.Append()
84 return c
86 def _GetPlatformIfdefs(self, model_object):
87 """Generates the "defined" conditional for an #if check if |model_object|
88 has platform restrictions. Returns None if there are no restrictions.
89 """
90 if model_object.platforms is None:
91 return None
92 ifdefs = []
93 for platform in model_object.platforms:
94 if platform == Platforms.CHROMEOS:
95 ifdefs.append('defined(OS_CHROMEOS)')
96 elif platform == Platforms.LINUX:
97 ifdefs.append('defined(OS_LINUX)')
98 elif platform == Platforms.MAC:
99 ifdefs.append('defined(OS_MACOSX)')
100 elif platform == Platforms.WIN:
101 ifdefs.append('defined(OS_WIN)')
102 else:
103 raise ValueError("Unsupported platform ifdef: %s" % platform.name)
104 return ' || '.join(ifdefs)
106 def _GenerateRegisterFunctions(self, namespace_name, function):
107 c = code.Code()
108 function_ifdefs = self._GetPlatformIfdefs(function)
109 if function_ifdefs is not None:
110 c.Append("#if %s" % function_ifdefs, indent_level=0)
112 function_name = JsFunctionNameToClassName(namespace_name, function.name)
113 c.Append("registry->RegisterFunction<%sFunction>();" % (
114 function_name))
116 if function_ifdefs is not None:
117 c.Append("#endif // %s" % function_ifdefs, indent_level=0)
118 return c
120 def _GenerateFunctionRegistryRegisterAll(self):
121 c = code.Code()
122 c.Append('// static')
123 c.Sblock('void %s::RegisterAll(ExtensionFunctionRegistry* registry) {' %
124 self._GenerateBundleClass('GeneratedFunctionRegistry'))
125 for namespace in self._model.namespaces.values():
126 namespace_ifdefs = self._GetPlatformIfdefs(namespace)
127 if namespace_ifdefs is not None:
128 c.Append("#if %s" % namespace_ifdefs, indent_level=0)
130 for function in namespace.functions.values():
131 if function.nocompile:
132 continue
133 c.Concat(self._GenerateRegisterFunctions(namespace.name, function))
135 for type_ in namespace.types.values():
136 for function in type_.functions.values():
137 if function.nocompile:
138 continue
139 namespace_types_name = JsFunctionNameToClassName(
140 namespace.name, type_.name)
141 c.Concat(self._GenerateRegisterFunctions(namespace_types_name,
142 function))
144 if namespace_ifdefs is not None:
145 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0)
146 c.Eblock("}")
147 return c
149 def _GenerateBundleClass(self, class_name):
150 '''Generates the C++ class name to use for a bundle class, taking into
151 account the bundle's name.
153 return self._bundle_name + class_name
156 class _APIHGenerator(object):
157 """Generates the header for API registration / declaration"""
158 def __init__(self, cpp_bundle):
159 self._bundle = cpp_bundle
161 def Generate(self, _): # namespace not relevant, this is a bundle
162 c = code.Code()
164 c.Append('#include <string>')
165 c.Append()
166 c.Append('#include "base/basictypes.h"')
167 c.Append()
168 c.Append("class ExtensionFunctionRegistry;")
169 c.Append()
170 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
171 c.Append()
172 c.Append('class %s {' %
173 self._bundle._GenerateBundleClass('GeneratedFunctionRegistry'))
174 c.Sblock(' public:')
175 c.Append('static void RegisterAll('
176 'ExtensionFunctionRegistry* registry);')
177 c.Eblock('};')
178 c.Append()
179 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
180 return self._bundle._GenerateHeader('generated_api', c)
183 class _APICCGenerator(object):
184 """Generates a code.Code object for the generated API .cc file"""
186 def __init__(self, cpp_bundle):
187 self._bundle = cpp_bundle
189 def Generate(self, _): # namespace not relevant, this is a bundle
190 c = code.Code()
191 c.Append(cpp_util.CHROMIUM_LICENSE)
192 c.Append()
193 c.Append('#include "%s"' % (
194 os.path.join(self._bundle._impl_dir,
195 'generated_api_registration.h')))
196 c.Append()
197 for namespace in self._bundle._model.namespaces.values():
198 namespace_name = namespace.unix_name.replace("experimental_", "")
199 implementation_header = namespace.compiler_options.get(
200 "implemented_in",
201 "%s/%s/%s_api.h" % (self._bundle._impl_dir,
202 namespace_name,
203 namespace_name))
204 if not os.path.exists(
205 os.path.join(self._bundle._root,
206 os.path.normpath(implementation_header))):
207 if "implemented_in" in namespace.compiler_options:
208 raise ValueError('Header file for namespace "%s" specified in '
209 'compiler_options not found: %s' %
210 (namespace.unix_name, implementation_header))
211 continue
212 ifdefs = self._bundle._GetPlatformIfdefs(namespace)
213 if ifdefs is not None:
214 c.Append("#if %s" % ifdefs, indent_level=0)
216 c.Append('#include "%s"' % implementation_header)
218 if ifdefs is not None:
219 c.Append("#endif // %s" % ifdefs, indent_level=0)
220 c.Append()
221 c.Append('#include '
222 '"extensions/browser/extension_function_registry.h"')
223 c.Append()
224 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
225 c.Append()
226 c.Concat(self._bundle._GenerateFunctionRegistryRegisterAll())
227 c.Append()
228 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
229 c.Append()
230 return c
233 class _SchemasHGenerator(object):
234 """Generates a code.Code object for the generated schemas .h file"""
235 def __init__(self, cpp_bundle):
236 self._bundle = cpp_bundle
238 def Generate(self, _): # namespace not relevant, this is a bundle
239 c = code.Code()
240 c.Append('#include <map>')
241 c.Append('#include <string>')
242 c.Append()
243 c.Append('#include "base/strings/string_piece.h"')
244 c.Append()
245 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
246 c.Append()
247 c.Append('class %s {' %
248 self._bundle._GenerateBundleClass('GeneratedSchemas'))
249 c.Sblock(' public:')
250 c.Append('// Determines if schema named |name| is generated.')
251 c.Append('static bool IsGenerated(std::string name);')
252 c.Append()
253 c.Append('// Gets the API schema named |name|.')
254 c.Append('static base::StringPiece Get(const std::string& name);')
255 c.Eblock('};')
256 c.Append()
257 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
258 return self._bundle._GenerateHeader('generated_schemas', c)
261 def _FormatNameAsConstant(name):
262 """Formats a name to be a C++ constant of the form kConstantName"""
263 name = '%s%s' % (name[0].upper(), name[1:])
264 return 'k%s' % re.sub('_[a-z]',
265 lambda m: m.group(0)[1].upper(),
266 name.replace('.', '_'))
269 class _SchemasCCGenerator(object):
270 """Generates a code.Code object for the generated schemas .cc file"""
272 def __init__(self, cpp_bundle):
273 self._bundle = cpp_bundle
275 def Generate(self, _): # namespace not relevant, this is a bundle
276 c = code.Code()
277 c.Append(cpp_util.CHROMIUM_LICENSE)
278 c.Append()
279 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir,
280 'generated_schemas.h')))
281 c.Append()
282 c.Append('#include "base/lazy_instance.h"')
283 c.Append()
284 c.Append('namespace {')
285 for api in self._bundle._api_defs:
286 namespace = self._bundle._model.namespaces[api.get('namespace')]
287 # JSON parsing code expects lists of schemas, so dump a singleton list.
288 json_content = json.dumps([_RemoveDescriptions(api)],
289 separators=(',', ':'))
290 # Escape all double-quotes and backslashes. For this to output a valid
291 # JSON C string, we need to escape \ and ". Note that some schemas are
292 # too large to compile on windows. Split the JSON up into several
293 # strings, since apparently that helps.
294 max_length = 8192
295 segments = [json_content[i:i + max_length].replace('\\', '\\\\')
296 .replace('"', '\\"')
297 for i in xrange(0, len(json_content), max_length)]
298 c.Append('const char %s[] = "%s";' %
299 (_FormatNameAsConstant(namespace.name), '" "'.join(segments)))
300 c.Append()
301 c.Sblock('struct Static {')
302 c.Sblock('Static() {')
303 for api in self._bundle._api_defs:
304 namespace = self._bundle._model.namespaces[api.get('namespace')]
305 c.Append('schemas["%s"] = %s;' % (namespace.name,
306 _FormatNameAsConstant(namespace.name)))
307 c.Eblock('}')
308 c.Append()
309 c.Append('std::map<std::string, const char*> schemas;')
310 c.Eblock('};')
311 c.Append()
312 c.Append('base::LazyInstance<Static> g_lazy_instance;')
313 c.Append()
314 c.Append('} // namespace')
315 c.Append()
316 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
317 c.Append()
318 c.Append('// static')
319 c.Sblock('base::StringPiece %s::Get(const std::string& name) {' %
320 self._bundle._GenerateBundleClass('GeneratedSchemas'))
321 c.Append('return IsGenerated(name) ? '
322 'g_lazy_instance.Get().schemas[name] : "";')
323 c.Eblock('}')
324 c.Append()
325 c.Append('// static')
326 c.Sblock('bool %s::IsGenerated(std::string name) {' %
327 self._bundle._GenerateBundleClass('GeneratedSchemas'))
328 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;')
329 c.Eblock('}')
330 c.Append()
331 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
332 c.Append()
333 return c