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 100644 |
index 0000000000000000000000000000000000000000..103410d7f4a9961c754ed2a561945359e5db6cd2 |
--- /dev/null |
+++ b/mojo/public/tools/bindings/generate_type_mappings.py |
@@ -0,0 +1,57 @@ |
+# 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. |
+ |
+import argparse |
+import os |
+import json |
+ |
+ |
+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() |
+ parser.add_argument('--dependency', type=str, action='append', default=[]) |
+ parser.add_argument('--output', type=str, required=True) |
+ 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) |
+ |
+ |
+if __name__ == '__main__': |
+ main() |