| 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 js_externs_generator import JsExternsGenerator |
| 27 import json_schema | 28 import json_schema |
| 28 from cpp_namespace_environment import CppNamespaceEnvironment | 29 from cpp_namespace_environment import CppNamespaceEnvironment |
| 29 from model import Model | 30 from model import Model |
| 30 from schema_loader import SchemaLoader | 31 from schema_loader import SchemaLoader |
| 31 | 32 |
| 32 # Names of supported code generators, as specified on the command-line. | 33 # Names of supported code generators, as specified on the command-line. |
| 33 # First is default. | 34 # First is default. |
| 34 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema'] | 35 GENERATORS = ['cpp', 'cpp-bundle-registration', 'cpp-bundle-schema', 'externs'] |
| 35 | 36 |
| 36 def GenerateSchema(generator_name, | 37 def GenerateSchema(generator_name, |
| 37 file_paths, | 38 file_paths, |
| 38 root, | 39 root, |
| 39 destdir, | 40 destdir, |
| 40 cpp_namespace_pattern, | 41 cpp_namespace_pattern, |
| 41 impl_dir, | 42 impl_dir, |
| 42 include_rules): | 43 include_rules): |
| 43 # Merge the source files into a single list of schemas. | 44 # Merge the source files into a single list of schemas. |
| 44 api_defs = [] | 45 api_defs = [] |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 generators = [ | 109 generators = [ |
| 109 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), | 110 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), |
| 110 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) | 111 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) |
| 111 ] | 112 ] |
| 112 elif generator_name == 'cpp': | 113 elif generator_name == 'cpp': |
| 113 cpp_generator = CppGenerator(type_generator) | 114 cpp_generator = CppGenerator(type_generator) |
| 114 generators = [ | 115 generators = [ |
| 115 ('%s.h' % filename_base, cpp_generator.h_generator), | 116 ('%s.h' % filename_base, cpp_generator.h_generator), |
| 116 ('%s.cc' % filename_base, cpp_generator.cc_generator) | 117 ('%s.cc' % filename_base, cpp_generator.cc_generator) |
| 117 ] | 118 ] |
| 119 elif generator_name == 'externs': |
| 120 generators = [ |
| 121 ('%s_externs.js' % namespace.unix_name, JsExternsGenerator()) |
| 122 ] |
| 118 else: | 123 else: |
| 119 raise Exception('Unrecognised generator %s' % generator_name) | 124 raise Exception('Unrecognised generator %s' % generator_name) |
| 120 | 125 |
| 121 output_code = [] | 126 output_code = [] |
| 122 for filename, generator in generators: | 127 for filename, generator in generators: |
| 123 code = generator.Generate(namespace).Render() | 128 code = generator.Generate(namespace).Render() |
| 124 if destdir: | 129 if destdir: |
| 125 if generator_name == 'cpp-bundle-registration': | 130 if generator_name == 'cpp-bundle-registration': |
| 126 # Function registrations must be output to impl_dir, since they link in | 131 # Function registrations must be output to impl_dir, since they link in |
| 127 # API implementations. | 132 # API implementations. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 | 184 |
| 180 include_rules = [] | 185 include_rules = [] |
| 181 if opts.include_rules: | 186 if opts.include_rules: |
| 182 include_rules = map(split_path_and_namespace, | 187 include_rules = map(split_path_and_namespace, |
| 183 shlex.split(opts.include_rules)) | 188 shlex.split(opts.include_rules)) |
| 184 | 189 |
| 185 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, | 190 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
| 186 opts.namespace, opts.impl_dir, include_rules) | 191 opts.namespace, opts.impl_dir, include_rules) |
| 187 if not opts.destdir: | 192 if not opts.destdir: |
| 188 print result | 193 print result |
| OLD | NEW |