compiler.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/env python3
  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.
  6. The purpose of this tool is to remove the need for hand-written code that
  7. converts to and from base::Value types when receiving javascript api calls.
  8. Originally written for generating code for extension apis. Reference schemas
  9. are in chrome/common/extensions/api.
  10. Usage example:
  11. compiler.py --root /home/Work/src --namespace extensions windows.json
  12. tabs.json
  13. compiler.py --destdir gen --root /home/Work/src
  14. --namespace extensions windows.json tabs.json
  15. """
  16. from __future__ import print_function
  17. import io
  18. import optparse
  19. import os
  20. import shlex
  21. import sys
  22. from cpp_bundle_generator import CppBundleGenerator
  23. from cpp_generator import CppGenerator
  24. from cpp_type_generator import CppTypeGenerator
  25. from js_externs_generator import JsExternsGenerator
  26. from js_interface_generator import JsInterfaceGenerator
  27. import json_schema
  28. from cpp_namespace_environment import CppNamespaceEnvironment
  29. from model import Model
  30. from namespace_resolver import NamespaceResolver
  31. from schema_loader import SchemaLoader
  32. # Names of supported code generators, as specified on the command-line.
  33. # First is default.
  34. GENERATORS = [
  35. 'cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs', 'interface'
  36. ]
  37. def GenerateSchema(generator_name,
  38. file_paths,
  39. root,
  40. destdir,
  41. cpp_namespace_pattern,
  42. bundle_name,
  43. impl_dir,
  44. include_rules):
  45. # Merge the source files into a single list of schemas.
  46. api_defs = []
  47. for file_path in file_paths:
  48. schema = os.path.relpath(file_path, root)
  49. api_def = SchemaLoader(root).LoadSchema(schema)
  50. # If compiling the C++ model code, delete 'nocompile' nodes.
  51. if generator_name == 'cpp':
  52. api_def = json_schema.DeleteNodes(api_def, 'nocompile')
  53. api_defs.extend(api_def)
  54. api_model = Model(allow_inline_enums=False)
  55. # For single-schema compilation make sure that the first (i.e. only) schema
  56. # is the default one.
  57. default_namespace = None
  58. # If we have files from multiple source paths, we'll use the common parent
  59. # path as the source directory.
  60. src_path = None
  61. # Load the actual namespaces into the model.
  62. for target_namespace, file_path in zip(api_defs, file_paths):
  63. relpath = os.path.relpath(os.path.normpath(file_path), root)
  64. namespace = api_model.AddNamespace(target_namespace,
  65. relpath,
  66. include_compiler_options=True,
  67. environment=CppNamespaceEnvironment(
  68. cpp_namespace_pattern))
  69. if default_namespace is None:
  70. default_namespace = namespace
  71. if src_path is None:
  72. src_path = namespace.source_file_dir
  73. else:
  74. src_path = os.path.commonprefix((src_path, namespace.source_file_dir))
  75. _, filename = os.path.split(file_path)
  76. filename_base, _ = os.path.splitext(filename)
  77. # Construct the type generator with all the namespaces in this model.
  78. schema_dir = os.path.dirname(os.path.relpath(file_paths[0], root))
  79. namespace_resolver = NamespaceResolver(root, schema_dir,
  80. include_rules, cpp_namespace_pattern)
  81. type_generator = CppTypeGenerator(api_model,
  82. namespace_resolver,
  83. default_namespace)
  84. if generator_name in ('cpp-bundle-registration', 'cpp-bundle-schema'):
  85. cpp_bundle_generator = CppBundleGenerator(root,
  86. api_model,
  87. api_defs,
  88. type_generator,
  89. cpp_namespace_pattern,
  90. bundle_name,
  91. src_path,
  92. impl_dir)
  93. if generator_name == 'cpp-bundle-registration':
  94. generators = [
  95. ('generated_api_registration.cc',
  96. cpp_bundle_generator.api_cc_generator),
  97. ('generated_api_registration.h', cpp_bundle_generator.api_h_generator),
  98. ]
  99. elif generator_name == 'cpp-bundle-schema':
  100. generators = [
  101. ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator),
  102. ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator)
  103. ]
  104. elif generator_name == 'cpp':
  105. cpp_generator = CppGenerator(type_generator)
  106. generators = [
  107. ('%s.h' % filename_base, cpp_generator.h_generator),
  108. ('%s.cc' % filename_base, cpp_generator.cc_generator)
  109. ]
  110. elif generator_name == 'externs':
  111. generators = [
  112. ('%s_externs.js' % namespace.unix_name, JsExternsGenerator())
  113. ]
  114. elif generator_name == 'interface':
  115. generators = [
  116. ('%s_interface.js' % namespace.unix_name, JsInterfaceGenerator())
  117. ]
  118. else:
  119. raise Exception('Unrecognised generator %s' % generator_name)
  120. output_code = []
  121. for filename, generator in generators:
  122. code = generator.Generate(namespace).Render()
  123. if destdir:
  124. if generator_name == 'cpp-bundle-registration':
  125. # Function registrations must be output to impl_dir, since they link in
  126. # API implementations.
  127. output_dir = os.path.join(destdir, impl_dir)
  128. else:
  129. output_dir = os.path.join(destdir, src_path)
  130. if not os.path.exists(output_dir):
  131. os.makedirs(output_dir)
  132. generator_filepath = os.path.join(output_dir, filename)
  133. with io.open(generator_filepath, 'w', encoding='utf-8') as f:
  134. f.write(code)
  135. # If multiple files are being output, add the filename for each file.
  136. if len(generators) > 1:
  137. output_code += [filename, '', code, '']
  138. else:
  139. output_code += [code]
  140. return '\n'.join(output_code)
  141. if __name__ == '__main__':
  142. parser = optparse.OptionParser(
  143. description='Generates a C++ model of an API from JSON schema',
  144. usage='usage: %prog [option]... schema')
  145. parser.add_option('-r', '--root', default='.',
  146. help='logical include root directory. Path to schema files from specified'
  147. ' dir will be the include path.')
  148. parser.add_option('-d', '--destdir',
  149. help='root directory to output generated files.')
  150. parser.add_option('-n', '--namespace', default='generated_api_schemas',
  151. help='C++ namespace for generated files. e.g extensions::api.')
  152. parser.add_option('-b', '--bundle-name', default='',
  153. help='A string to prepend to generated bundle class names, so that '
  154. 'multiple bundle rules can be used without conflicting. '
  155. 'Only used with one of the cpp-bundle generators.')
  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')
  166. (opts, file_paths) = parser.parse_args()
  167. if not file_paths:
  168. sys.exit(0) # This is OK as a no-op
  169. # Unless in bundle mode, only one file should be specified.
  170. if (opts.generator not in ('cpp-bundle-registration', 'cpp-bundle-schema') and
  171. len(file_paths) > 1):
  172. # TODO(sashab): Could also just use file_paths[0] here and not complain.
  173. raise Exception(
  174. "Unless in bundle mode, only one file can be specified at a time.")
  175. def split_path_and_namespace(path_and_namespace):
  176. if ':' not in path_and_namespace:
  177. raise ValueError('Invalid include rule "%s". Rules must be of '
  178. 'the form path:namespace' % path_and_namespace)
  179. return path_and_namespace.split(':', 1)
  180. include_rules = []
  181. if opts.include_rules:
  182. include_rules = list(
  183. map(split_path_and_namespace, shlex.split(opts.include_rules)))
  184. result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir,
  185. opts.namespace, opts.bundle_name, opts.impl_dir,
  186. include_rules)
  187. if not opts.destdir:
  188. print(result)