summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler/compiler.py
diff options
context:
space:
mode:
authormiket@chromium.org <miket@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-19 22:24:35 +0000
committermiket@chromium.org <miket@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-19 22:24:35 +0000
commitae33d3293bfb954bae710a31f401b0d833ba7be4 (patch)
tree7f7da014d0e47a89313f815d4cb42a80efba4144 /tools/json_schema_compiler/compiler.py
parent558a9746b8245f7bf8f162709755a373566bcf65 (diff)
downloadchromium_src-ae33d3293bfb954bae710a31f401b0d833ba7be4.zip
chromium_src-ae33d3293bfb954bae710a31f401b0d833ba7be4.tar.gz
chromium_src-ae33d3293bfb954bae710a31f401b0d833ba7be4.tar.bz2
Reapply 127159 (http://codereview.chromium.org/9666059) plus important fixes.
Refactor extension_function_dispatcher to extract ExtensionFunctionRegistry. This allows us to generate an additional code block that takes an ExtensionFunctionRegistry and asks it to register generated API functions. Then switch DnsResolve over to get registered this way. Along the way, notice that DNSResolve is capitalized using an untenable style. Fix that. Fixes since original 127159: - change name of build rule to gen_bundle_api - actually add per-file dependencies to .gyp (this was why clean builds failed) - change 'outputs' to match the emitted filename, fixing a needless rebuild problem. - split the top-level build action that was a part of json_schema_compile.gypi, and include it only in api.gyp, thus fixing the problem where json_schema_compiler_tests.gyp was asking it to create outputs in the tools/json_schema_compiler_test directory, again causing needless work during the build process. BUG=none (essential plumbing work) TEST=same as before. Review URL: https://chromiumcodereview.appspot.com/9716003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@127555 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler/compiler.py')
-rw-r--r--tools/json_schema_compiler/compiler.py95
1 files changed, 72 insertions, 23 deletions
diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py
index f782f72..bd9805d 100644
--- a/tools/json_schema_compiler/compiler.py
+++ b/tools/json_schema_compiler/compiler.py
@@ -18,6 +18,7 @@ Usage example:
import cc_generator
import cpp_type_generator
+import h_bundle_generator
import h_generator
import idl_schema
import json_schema
@@ -26,30 +27,9 @@ import optparse
import os.path
import sys
-if __name__ == '__main__':
- parser = optparse.OptionParser(
- description='Generates a C++ model of an API from JSON schema',
- usage='usage: %prog [option]... schema')
- parser.add_option('-r', '--root', default='.',
- help='logical include root directory. Path to schema files from specified'
- 'dir will be the include path.')
- parser.add_option('-d', '--destdir',
- help='root directory to output generated files.')
- parser.add_option('-n', '--namespace', default='generated_api_schemas',
- help='C++ namespace for generated files. e.g extensions::api.')
-
- (opts, args) = parser.parse_args()
- if not args:
- sys.exit(parser.get_usage())
- dest_dir = opts.destdir
- root_namespace = opts.namespace
-
- schema = os.path.normpath(args[0])
-
- api_model = model.Model()
-
- # Actually generate for source file.
+def load_schema(schema):
schema_filename, schema_extension = os.path.splitext(schema)
+
if schema_extension == '.json':
api_defs = json_schema.Load(schema)
elif schema_extension == '.idl':
@@ -58,6 +38,15 @@ if __name__ == '__main__':
sys.exit("Did not recognize file extension %s for schema %s" %
(schema_extension, schema))
+ return api_defs
+
+def handle_single_schema(filename, dest_dir, root, root_namespace):
+ schema = os.path.normpath(filename)
+ schema_filename, schema_extension = os.path.splitext(schema)
+ api_defs = load_schema(schema)
+
+ api_model = model.Model()
+
for target_namespace in api_defs:
referenced_schemas = target_namespace.get('dependencies', [])
# Load type dependencies into the model.
@@ -110,3 +99,63 @@ if __name__ == '__main__':
print '%s.cc' % out_file
print
print cc_code
+
+def handle_bundle_schema(filenames, dest_dir, root, root_namespace):
+ # Merge the source files into a single list of schemas.
+ api_defs = []
+ for filename in filenames:
+ schema = os.path.normpath(filename)
+ schema_filename, schema_extension = os.path.splitext(schema)
+ api_defs.extend(load_schema(schema))
+
+ api_model = model.Model()
+ relpath = os.path.relpath(os.path.normpath(filenames[0]), root)
+ for target_namespace in api_defs:
+ api_model.AddNamespace(target_namespace, relpath)
+
+ type_generator = cpp_type_generator.CppTypeGenerator(root_namespace)
+ for referenced_namespace in api_model.namespaces.values():
+ type_generator.AddNamespace(
+ referenced_namespace,
+ referenced_namespace.unix_name)
+
+ generator = h_bundle_generator.HBundleGenerator(api_model, type_generator)
+ h_bundle_code = generator.Generate().Render()
+
+ out_file = 'generated_api'
+ if dest_dir:
+ with open(
+ os.path.join(dest_dir, 'chrome/common/extensions/api/generated_api.h'),
+ 'w') as h_file:
+ h_file.write(h_bundle_code)
+ else:
+ print '%s.h' % out_file
+ print
+ print h_bundle_code
+
+if __name__ == '__main__':
+ parser = optparse.OptionParser(
+ description='Generates a C++ model of an API from JSON schema',
+ usage='usage: %prog [option]... schema')
+ parser.add_option('-r', '--root', default='.',
+ help='logical include root directory. Path to schema files from specified'
+ 'dir will be the include path.')
+ parser.add_option('-d', '--destdir',
+ help='root directory to output generated files.')
+ parser.add_option('-n', '--namespace', default='generated_api_schemas',
+ help='C++ namespace for generated files. e.g extensions::api.')
+ parser.add_option('-b', '--bundle', action="store_true", help=
+'''if supplied, causes compiler to generate bundle files for the given set of
+source files.''')
+
+ (opts, args) = parser.parse_args()
+
+ if not args:
+ sys.exit(0) # This is OK as a no-op
+ dest_dir = opts.destdir
+ root_namespace = opts.namespace
+
+ if opts.bundle:
+ handle_bundle_schema(args, dest_dir, opts.root, root_namespace)
+ else:
+ handle_single_schema(args[0], dest_dir, opts.root, root_namespace)