cpp_bundle_generator.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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.
  4. import code
  5. import cpp_util
  6. from model import Platforms
  7. from schema_util import CapitalizeFirstLetter
  8. from schema_util import JsFunctionNameToClassName
  9. import collections
  10. import copy
  11. import json
  12. import os
  13. import re
  14. import sys
  15. def _RemoveKey(node, key, type_restriction):
  16. if isinstance(node, dict):
  17. if key in node and isinstance(node[key], type_restriction):
  18. del node[key]
  19. for value in node.values():
  20. _RemoveKey(value, key, type_restriction)
  21. elif isinstance(node, list):
  22. for value in node:
  23. _RemoveKey(value, key, type_restriction)
  24. def _RemoveUnneededFields(schema):
  25. """Returns a copy of |schema| with fields that aren't necessary at runtime
  26. removed.
  27. """
  28. # Return a copy so that we don't pollute the global api object, which may be
  29. # used elsewhere.
  30. ret = copy.deepcopy(schema)
  31. if sys.version_info.major == 2:
  32. _RemoveKey(ret, 'description', basestring)
  33. else:
  34. _RemoveKey(ret, 'description', str)
  35. _RemoveKey(ret, 'compiler_options', dict)
  36. _RemoveKey(ret, 'nodoc', bool)
  37. _RemoveKey(ret, 'nocompile', bool)
  38. _RemoveKey(ret, 'noinline_doc', bool)
  39. _RemoveKey(ret, 'jsexterns', object)
  40. _RemoveKey(ret, 'manifest_keys', object)
  41. return ret
  42. def _PrefixSchemaWithNamespace(schema):
  43. """Modifies |schema| in place to prefix all types and references with a
  44. namespace, if they aren't already qualified. That is, in the tabs API, this
  45. will turn type Tab into tabs.Tab, but will leave the fully-qualified
  46. windows.Window as-is.
  47. """
  48. assert isinstance(schema, dict), "Schema is unexpected type"
  49. namespace = schema['namespace']
  50. def prefix(obj, key, mandatory):
  51. if not key in obj:
  52. assert not mandatory, (
  53. 'Required key "%s" is not present in object.' % key)
  54. return
  55. assert type(obj[key]) is str or (sys.version_info.major == 2 and
  56. isinstance(obj[key], basestring))
  57. if obj[key].find('.') == -1:
  58. obj[key] = '%s.%s' % (namespace, obj[key])
  59. if 'types' in schema:
  60. assert isinstance(schema['types'], list)
  61. for t in schema['types']:
  62. assert isinstance(t, dict), "Type entry is unexpected type"
  63. prefix(t, 'id', True)
  64. prefix(t, 'customBindings', False)
  65. def prefix_refs(val):
  66. if type(val) is list:
  67. for sub_val in val:
  68. prefix_refs(sub_val)
  69. elif type(val) is dict or type(val) is collections.OrderedDict:
  70. prefix(val, '$ref', False)
  71. for key, sub_val in val.items():
  72. prefix_refs(sub_val)
  73. prefix_refs(schema)
  74. return schema
  75. class CppBundleGenerator(object):
  76. """This class contains methods to generate code based on multiple schemas.
  77. """
  78. def __init__(self,
  79. root,
  80. model,
  81. api_defs,
  82. cpp_type_generator,
  83. cpp_namespace_pattern,
  84. bundle_name,
  85. source_file_dir,
  86. impl_dir):
  87. self._root = root
  88. self._model = model
  89. self._api_defs = api_defs
  90. self._cpp_type_generator = cpp_type_generator
  91. self._bundle_name = bundle_name
  92. self._source_file_dir = source_file_dir
  93. self._impl_dir = impl_dir
  94. # Hack: assume that the C++ namespace for the bundle is the namespace of the
  95. # files without the last component of the namespace. A cleaner way to do
  96. # this would be to make it a separate variable in the gyp file.
  97. self._cpp_namespace = cpp_namespace_pattern.rsplit('::', 1)[0]
  98. self.api_cc_generator = _APICCGenerator(self)
  99. self.api_h_generator = _APIHGenerator(self)
  100. self.schemas_cc_generator = _SchemasCCGenerator(self)
  101. self.schemas_h_generator = _SchemasHGenerator(self)
  102. def _GenerateHeader(self, file_base, body_code):
  103. """Generates a code.Code object for a header file
  104. Parameters:
  105. - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h')
  106. - |body_code| - the code to put in between the multiple inclusion guards"""
  107. c = code.Code()
  108. c.Append(cpp_util.CHROMIUM_LICENSE)
  109. c.Append()
  110. c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE %
  111. cpp_util.ToPosixPath(self._source_file_dir))
  112. ifndef_name = cpp_util.GenerateIfndefName(
  113. '%s/%s.h' % (cpp_util.ToPosixPath(self._source_file_dir), file_base))
  114. c.Append()
  115. c.Append('#ifndef %s' % ifndef_name)
  116. c.Append('#define %s' % ifndef_name)
  117. c.Append()
  118. c.Concat(body_code)
  119. c.Append()
  120. c.Append('#endif // %s' % ifndef_name)
  121. c.Append()
  122. return c
  123. def _GetPlatformIfdefs(self, model_object):
  124. """Generates the "defined" conditional for an #if check if |model_object|
  125. has platform restrictions. Returns None if there are no restrictions.
  126. """
  127. if model_object.platforms is None:
  128. return None
  129. ifdefs = []
  130. for platform in model_object.platforms:
  131. if platform == Platforms.CHROMEOS:
  132. ifdefs.append('BUILDFLAG(IS_CHROMEOS_ASH)')
  133. elif platform == Platforms.FUCHSIA:
  134. ifdefs.append('BUILDFLAG(IS_FUCHSIA)')
  135. elif platform == Platforms.LACROS:
  136. # TODO(https://crbug.com/1052397): For readability, this should become
  137. # BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(IS_CHROMEOS_LACROS).
  138. ifdefs.append('BUILDFLAG(IS_CHROMEOS_LACROS)')
  139. elif platform == Platforms.LINUX:
  140. ifdefs.append('BUILDFLAG(IS_LINUX)')
  141. elif platform == Platforms.MAC:
  142. ifdefs.append('BUILDFLAG(IS_MAC)')
  143. elif platform == Platforms.WIN:
  144. ifdefs.append('BUILDFLAG(IS_WIN)')
  145. else:
  146. raise ValueError("Unsupported platform ifdef: %s" % platform.name)
  147. return ' || '.join(ifdefs)
  148. def _GenerateRegistrationEntry(self, namespace_name, function):
  149. c = code.Code()
  150. function_ifdefs = self._GetPlatformIfdefs(function)
  151. if function_ifdefs is not None:
  152. c.Append("#if %s" % function_ifdefs, indent_level=0)
  153. function_name = '%sFunction' % JsFunctionNameToClassName(
  154. namespace_name, function.name)
  155. c.Sblock('{')
  156. c.Append('&NewExtensionFunction<%s>,' % function_name)
  157. c.Append('%s::static_function_name(),' % function_name)
  158. c.Append('%s::static_histogram_value(),' % function_name)
  159. c.Eblock('},')
  160. if function_ifdefs is not None:
  161. c.Append("#endif // %s" % function_ifdefs, indent_level=0)
  162. return c
  163. def _GenerateFunctionRegistryRegisterAll(self):
  164. c = code.Code()
  165. c.Append('// static')
  166. c.Sblock('void %s::RegisterAll(ExtensionFunctionRegistry* registry) {' %
  167. self._GenerateBundleClass('GeneratedFunctionRegistry'))
  168. c.Sblock('constexpr ExtensionFunctionRegistry::FactoryEntry kEntries[] = {')
  169. for namespace in self._model.namespaces.values():
  170. namespace_ifdefs = self._GetPlatformIfdefs(namespace)
  171. if namespace_ifdefs is not None:
  172. c.Append("#if %s" % namespace_ifdefs, indent_level=0)
  173. for function in namespace.functions.values():
  174. if function.nocompile:
  175. continue
  176. c.Concat(self._GenerateRegistrationEntry(namespace.name, function))
  177. for type_ in namespace.types.values():
  178. for function in type_.functions.values():
  179. if function.nocompile:
  180. continue
  181. namespace_types_name = JsFunctionNameToClassName(
  182. namespace.name, type_.name)
  183. c.Concat(self._GenerateRegistrationEntry(namespace_types_name,
  184. function))
  185. if namespace_ifdefs is not None:
  186. c.Append("#endif // %s" % namespace_ifdefs, indent_level=0)
  187. c.Eblock("};")
  188. c.Sblock("for (const auto& entry : kEntries) {")
  189. c.Append(" registry->Register(entry);")
  190. c.Eblock("}")
  191. c.Eblock("}")
  192. return c
  193. def _GenerateBundleClass(self, class_name):
  194. '''Generates the C++ class name to use for a bundle class, taking into
  195. account the bundle's name.
  196. '''
  197. return self._bundle_name + class_name
  198. class _APIHGenerator(object):
  199. """Generates the header for API registration / declaration"""
  200. def __init__(self, cpp_bundle):
  201. self._bundle = cpp_bundle
  202. def Generate(self, _): # namespace not relevant, this is a bundle
  203. c = code.Code()
  204. c.Append('#include <string>')
  205. c.Append()
  206. c.Append("class ExtensionFunctionRegistry;")
  207. c.Append()
  208. c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
  209. c.Append()
  210. c.Append('class %s {' %
  211. self._bundle._GenerateBundleClass('GeneratedFunctionRegistry'))
  212. c.Sblock(' public:')
  213. c.Append('static void RegisterAll('
  214. 'ExtensionFunctionRegistry* registry);')
  215. c.Eblock('};')
  216. c.Append()
  217. c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
  218. return self._bundle._GenerateHeader('generated_api', c)
  219. class _APICCGenerator(object):
  220. """Generates a code.Code object for the generated API .cc file"""
  221. def __init__(self, cpp_bundle):
  222. self._bundle = cpp_bundle
  223. def Generate(self, _): # namespace not relevant, this is a bundle
  224. c = code.Code()
  225. c.Append(cpp_util.CHROMIUM_LICENSE)
  226. c.Append()
  227. c.Append('#include "%s"' % (
  228. cpp_util.ToPosixPath(os.path.join(self._bundle._impl_dir,
  229. 'generated_api_registration.h'))))
  230. c.Append()
  231. c.Append('#include "build/build_config.h"')
  232. c.Append('#include "build/chromeos_buildflags.h"')
  233. c.Append()
  234. for namespace in self._bundle._model.namespaces.values():
  235. namespace_name = namespace.unix_name.replace("experimental_", "")
  236. implementation_header = namespace.compiler_options.get(
  237. "implemented_in",
  238. "%s/%s/%s_api.h" % (self._bundle._impl_dir,
  239. namespace_name,
  240. namespace_name))
  241. if not os.path.exists(
  242. os.path.join(self._bundle._root,
  243. os.path.normpath(implementation_header))):
  244. if "implemented_in" in namespace.compiler_options:
  245. raise ValueError('Header file for namespace "%s" specified in '
  246. 'compiler_options not found: %s' %
  247. (namespace.unix_name, implementation_header))
  248. continue
  249. ifdefs = self._bundle._GetPlatformIfdefs(namespace)
  250. if ifdefs is not None:
  251. c.Append("#if %s" % ifdefs, indent_level=0)
  252. c.Append('#include "%s"' % cpp_util.ToPosixPath(implementation_header))
  253. if ifdefs is not None:
  254. c.Append("#endif // %s" % ifdefs, indent_level=0)
  255. c.Append()
  256. c.Append('#include '
  257. '"extensions/browser/extension_function_registry.h"')
  258. c.Append()
  259. c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
  260. c.Append()
  261. c.Concat(self._bundle._GenerateFunctionRegistryRegisterAll())
  262. c.Append()
  263. c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
  264. c.Append()
  265. return c
  266. class _SchemasHGenerator(object):
  267. """Generates a code.Code object for the generated schemas .h file"""
  268. def __init__(self, cpp_bundle):
  269. self._bundle = cpp_bundle
  270. def Generate(self, _): # namespace not relevant, this is a bundle
  271. c = code.Code()
  272. c.Append('#include "base/strings/string_piece.h"')
  273. c.Append()
  274. c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
  275. c.Append()
  276. c.Append('class %s {' %
  277. self._bundle._GenerateBundleClass('GeneratedSchemas'))
  278. c.Sblock(' public:')
  279. c.Append('// Determines if schema named |name| is generated.')
  280. c.Append('static bool IsGenerated(base::StringPiece name);')
  281. c.Append()
  282. c.Append('// Gets the API schema named |name|.')
  283. c.Append('static base::StringPiece Get(base::StringPiece name);')
  284. c.Eblock('};')
  285. c.Append()
  286. c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
  287. return self._bundle._GenerateHeader('generated_schemas', c)
  288. def _FormatNameAsConstant(name):
  289. """Formats a name to be a C++ constant of the form kConstantName"""
  290. name = '%s%s' % (name[0].upper(), name[1:])
  291. return 'k%s' % re.sub('_[a-z]',
  292. lambda m: m.group(0)[1].upper(),
  293. name.replace('.', '_'))
  294. class _SchemasCCGenerator(object):
  295. """Generates a code.Code object for the generated schemas .cc file"""
  296. def __init__(self, cpp_bundle):
  297. self._bundle = cpp_bundle
  298. def Generate(self, _): # namespace not relevant, this is a bundle
  299. c = code.Code()
  300. c.Append(cpp_util.CHROMIUM_LICENSE)
  301. c.Append()
  302. c.Append('#include "%s"' % (
  303. cpp_util.ToPosixPath(os.path.join(self._bundle._source_file_dir,
  304. 'generated_schemas.h'))))
  305. c.Append()
  306. c.Append('#include <algorithm>')
  307. c.Append('#include <iterator>')
  308. c.Append()
  309. c.Append('#include "base/containers/fixed_flat_map.h"')
  310. c.Append('#include "base/strings/string_piece.h"')
  311. c.Append()
  312. c.Append('namespace {')
  313. for api in self._bundle._api_defs:
  314. namespace = self._bundle._model.namespaces[api.get('namespace')]
  315. json_content = json.dumps(_PrefixSchemaWithNamespace(
  316. _RemoveUnneededFields(api)),
  317. separators=(',', ':'))
  318. # This will output a valid JSON C string. Note that some schemas are
  319. # too large to compile on windows. Split the JSON up into several
  320. # strings, since apparently that helps.
  321. max_length = 8192
  322. segments = [
  323. json_content[i:i + max_length]
  324. for i in range(0, len(json_content), max_length)
  325. ]
  326. c.Append(
  327. 'constexpr char %s[] = R"R(%s)R";' %
  328. (_FormatNameAsConstant(namespace.name), ')R" R"R('.join(segments)))
  329. c.Append('} // namespace')
  330. c.Append()
  331. c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
  332. c.Append()
  333. c.Append('// static')
  334. c.Sblock('bool %s::IsGenerated(base::StringPiece name) {' %
  335. self._bundle._GenerateBundleClass('GeneratedSchemas'))
  336. c.Append('return !Get(name).empty();')
  337. c.Eblock('}')
  338. c.Append()
  339. c.Append('// static')
  340. c.Sblock('base::StringPiece %s::Get(base::StringPiece name) {' %
  341. self._bundle._GenerateBundleClass('GeneratedSchemas'))
  342. c.Append('static constexpr auto kSchemas = '
  343. 'base::MakeFixedFlatMap<base::StringPiece, base::StringPiece>({')
  344. c.Sblock()
  345. namespaces = [self._bundle._model.namespaces[api.get('namespace')].name
  346. for api in self._bundle._api_defs]
  347. for namespace in sorted(namespaces):
  348. schema_constant_name = _FormatNameAsConstant(namespace)
  349. c.Append('{"%s", %s},' % (namespace, schema_constant_name))
  350. c.Eblock('});')
  351. c.Append('auto it = kSchemas.find(name);')
  352. c.Append('return it != kSchemas.end() ? it->second : base::StringPiece();')
  353. c.Eblock('}')
  354. c.Append()
  355. c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
  356. c.Append()
  357. return c