| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 """Generator for C++ structs from api json files. | 5 """Generator for C++ structs from api json files. |
| 6 | 6 |
| 7 The purpose of this tool is to remove the need for hand-written code that | 7 The purpose of this tool is to remove the need for hand-written code that |
| 8 converts to and from base::Value types when receiving javascript api calls. | 8 converts to and from base::Value types when receiving javascript api calls. |
| 9 Originally written for generating code for extension apis. Reference schemas | 9 Originally written for generating code for extension apis. Reference schemas |
| 10 are in chrome/common/extensions/api. | 10 are in chrome/common/extensions/api. |
| 11 | 11 |
| 12 Usage example: | 12 Usage example: |
| 13 compiler.py --root /home/Work/src --namespace extensions windows.json | 13 compiler.py --root /home/Work/src --namespace extensions windows.json |
| 14 tabs.json | 14 tabs.json |
| 15 compiler.py --destdir gen --root /home/Work/src | 15 compiler.py --destdir gen --root /home/Work/src |
| 16 --namespace extensions windows.json tabs.json | 16 --namespace extensions windows.json tabs.json |
| 17 """ | 17 """ |
| 18 | 18 |
| 19 import optparse | 19 import optparse |
| 20 import os | 20 import os |
| 21 import shlex | 21 import shlex |
| 22 import sys | 22 import sys |
| 23 | 23 |
| 24 from cpp_bundle_generator import CppBundleGenerator | 24 from cpp_bundle_generator import CppBundleGenerator |
| 25 from cpp_generator import CppGenerator | 25 from cpp_generator import CppGenerator |
| 26 from cpp_type_generator import CppTypeGenerator | 26 from cpp_type_generator import CppTypeGenerator |
| 27 from dart_generator import DartGenerator | 27 from dart_generator import DartGenerator |
| 28 from externs_js_generator import ExternsJsGenerator |
| 28 import json_schema | 29 import json_schema |
| 29 from cpp_namespace_environment import CppNamespaceEnvironment | 30 from cpp_namespace_environment import CppNamespaceEnvironment |
| 30 from model import Model | 31 from model import Model |
| 31 from schema_loader import SchemaLoader | 32 from schema_loader import SchemaLoader |
| 32 | 33 |
| 33 # Names of supported code generators, as specified on the command-line. | 34 # Names of supported code generators, as specified on the command-line. |
| 34 # First is default. | 35 # First is default. |
| 35 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'dart'] | 36 GENERATORS = [ |
| 37 'cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'dart', 'externs-js'] |
| 36 | 38 |
| 37 def GenerateSchema(generator_name, | 39 def GenerateSchema(generator_name, |
| 38 file_paths, | 40 file_paths, |
| 39 root, | 41 root, |
| 40 destdir, | 42 destdir, |
| 41 cpp_namespace_pattern, | 43 cpp_namespace_pattern, |
| 42 dart_overrides_dir, | 44 dart_overrides_dir, |
| 43 impl_dir, | 45 impl_dir, |
| 44 include_rules): | 46 include_rules): |
| 45 # Merge the source files into a single list of schemas. | 47 # Merge the source files into a single list of schemas. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 cpp_generator = CppGenerator(type_generator) | 117 cpp_generator = CppGenerator(type_generator) |
| 116 generators = [ | 118 generators = [ |
| 117 ('%s.h' % filename_base, cpp_generator.h_generator), | 119 ('%s.h' % filename_base, cpp_generator.h_generator), |
| 118 ('%s.cc' % filename_base, cpp_generator.cc_generator) | 120 ('%s.cc' % filename_base, cpp_generator.cc_generator) |
| 119 ] | 121 ] |
| 120 elif generator_name == 'dart': | 122 elif generator_name == 'dart': |
| 121 generators = [ | 123 generators = [ |
| 122 ('%s.dart' % namespace.unix_name, DartGenerator( | 124 ('%s.dart' % namespace.unix_name, DartGenerator( |
| 123 dart_overrides_dir)) | 125 dart_overrides_dir)) |
| 124 ] | 126 ] |
| 127 elif generator_name == 'externs-js': |
| 128 generators = [('externs.js', ExternsJsGenerator())] |
| 125 else: | 129 else: |
| 126 raise Exception('Unrecognised generator %s' % generator) | 130 raise Exception('Unrecognised generator %s' % generator) |
| 127 | 131 |
| 128 output_code = [] | 132 output_code = [] |
| 129 for filename, generator in generators: | 133 for filename, generator in generators: |
| 130 code = generator.Generate(namespace).Render() | 134 code = generator.Generate(namespace).Render() |
| 131 if destdir: | 135 if destdir: |
| 132 if generator_name == 'cpp-bundle-registration': | 136 if generator_name == 'cpp-bundle-registration': |
| 133 # Function registrations must be output to impl_dir, since they link in | 137 # Function registrations must be output to impl_dir, since they link in |
| 134 # API implementations. | 138 # API implementations. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 include_rules = [] | 193 include_rules = [] |
| 190 if opts.include_rules: | 194 if opts.include_rules: |
| 191 include_rules = map(split_path_and_namespace, | 195 include_rules = map(split_path_and_namespace, |
| 192 shlex.split(opts.include_rules)) | 196 shlex.split(opts.include_rules)) |
| 193 | 197 |
| 194 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, | 198 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
| 195 opts.namespace, opts.dart_overrides_dir, | 199 opts.namespace, opts.dart_overrides_dir, |
| 196 opts.impl_dir, include_rules) | 200 opts.impl_dir, include_rules) |
| 197 if not opts.destdir: | 201 if not opts.destdir: |
| 198 print result | 202 print result |
| OLD | NEW |