Index: mojo/public/tools/bindings/generate_type_mappings.py |
diff --git a/mojo/public/tools/bindings/generate_type_mappings.py b/mojo/public/tools/bindings/generate_type_mappings.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..20d0c7229387e4c8a37f956e9140d7eba0672253 |
--- /dev/null |
+++ b/mojo/public/tools/bindings/generate_type_mappings.py |
@@ -0,0 +1,123 @@ |
+#!/usr/bin/env python |
+# Copyright 2016 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+"""Generates a JSON typemap from its command-line arguments and dependencies. |
+ |
+Each typemap should be specified in an command-line argument of the form |
+key=value, with an argument of "--start-typemap" preceding each typemap. |
+ |
+For example, |
+generate_type_mappings.py --output=foo.typemap --start-typemap \\ |
+ public_headers=foo.h traits_headers=foo_traits.h \\ |
+ type_mappings=mojom.Foo=FooImpl |
+ |
+generates a foo.typemap containing |
+{ |
+ "c++": { |
+ "mojom.Foo": { |
+ "typename": "FooImpl", |
+ "traits_headers": [ |
+ "foo_traits.h" |
+ ], |
+ "public_headers": [ |
+ "foo.h" |
+ ] |
+ } |
+ } |
+} |
+ |
+Then, |
+generate_type_mappings.py --dependency foo.typemap --output=bar.typemap \\ |
+ --start-typemap public_headers=bar.h traits_headers=bar_traits.h \\ |
+ type_mappings=mojom.Bar=BarImpl |
+ |
+generates a bar.typemap containing |
+{ |
+ "c++": { |
+ "mojom.Bar": { |
+ "typename": "BarImpl", |
+ "traits_headers": [ |
+ "bar_traits.h" |
+ ], |
+ "public_headers": [ |
+ "bar.h" |
+ ] |
+ }, |
+ "mojom.Foo": { |
+ "typename": "FooImpl", |
+ "traits_headers": [ |
+ "foo_traits.h" |
+ ], |
+ "public_headers": [ |
+ "foo.h" |
+ ] |
+ } |
+ } |
+} |
+""" |
+ |
+import argparse |
+import json |
+import os |
+ |
+ |
+def ReadTypemap(path): |
+ with open(path) as f: |
+ return json.load(f)['c++'] |
+ |
+ |
+def ParseTypemapArgs(args): |
+ typemaps = [s for s in '\n'.join(args).split('--start-typemap\n') if s] |
+ result = {} |
+ for typemap in typemaps: |
+ result.update(ParseTypemap(typemap)) |
+ return result |
+ |
+ |
+def ParseTypemap(typemap): |
+ values = {'type_mappings': [], 'public_headers': [], 'traits_headers': []} |
+ for line in typemap.split('\n'): |
+ if not line: |
+ continue |
+ key, _, value = line.partition('=') |
+ values[key].append(value.lstrip('/')) |
+ result = {} |
+ for typename in values['type_mappings']: |
+ mojom_type, _, native_type = typename.partition('=') |
+ result[mojom_type] = { |
+ 'typename': native_type, |
+ 'public_headers': values['public_headers'], |
+ 'traits_headers': values['traits_headers'], |
+ } |
+ return result |
+ |
+ |
+def main(): |
+ parser = argparse.ArgumentParser( |
+ description=__doc__, |
+ formatter_class=argparse.RawDescriptionHelpFormatter) |
+ parser.add_argument( |
+ '--dependency', |
+ type=str, |
+ action='append', |
+ default=[], |
+ help=('A path to another JSON typemap to merge into the output. ' |
+ 'This may be repeated to merge multiple typemaps.')) |
+ parser.add_argument('--output', |
+ type=str, |
+ required=True, |
+ help='The path to which to write the generated JSON.') |
+ params, typemap_params = parser.parse_known_args() |
+ typemaps = ParseTypemapArgs(typemap_params) |
+ missing = [path for path in params.dependency if not os.path.exists(path)] |
+ if missing: |
+ raise IOError('Missing dependencies: %s' % ', '.join(missing)) |
+ for path in params.dependency: |
+ typemaps.update(ReadTypemap(path)) |
+ with open(params.output, 'w') as f: |
+ json.dump({'c++': typemaps}, f, indent=2) |
+ |
+ |
+if __name__ == '__main__': |
+ main() |