| 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 sys | 21 import sys |
| 22 | 22 |
| 23 from cpp_bundle_generator import CppBundleGenerator | 23 from cpp_bundle_generator import CppBundleGenerator |
| 24 from cpp_generator import CppGenerator | 24 from cpp_generator import CppGenerator |
| 25 from cpp_type_generator import CppTypeGenerator | 25 from cpp_type_generator import CppTypeGenerator |
| 26 from dart_generator import DartGenerator | 26 from dart_generator import DartGenerator |
| 27 import json_schema | 27 import json_schema |
| 28 from model import Model | 28 from model import Model |
| 29 from ppapi_generator import PpapiGenerator |
| 29 from schema_loader import SchemaLoader | 30 from schema_loader import SchemaLoader |
| 30 | 31 |
| 31 # Names of supported code generators, as specified on the command-line. | 32 # Names of supported code generators, as specified on the command-line. |
| 32 # First is default. | 33 # First is default. |
| 33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] | 34 GENERATORS = ['cpp', 'cpp-bundle', 'dart', 'ppapi'] |
| 34 | 35 |
| 35 def GenerateSchema(generator, | 36 def GenerateSchema(generator, |
| 36 filenames, | 37 filenames, |
| 37 root, | 38 root, |
| 38 destdir, | 39 destdir, |
| 39 root_namespace, | 40 root_namespace, |
| 40 dart_overrides_dir): | 41 dart_overrides_dir): |
| 41 schema_loader = SchemaLoader( | 42 schema_loader = SchemaLoader( |
| 42 os.path.dirname(os.path.relpath(os.path.normpath(filenames[0]), root)), | 43 os.path.dirname(os.path.relpath(os.path.normpath(filenames[0]), root)), |
| 43 os.path.dirname(filenames[0])) | 44 os.path.dirname(filenames[0])) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 cpp_generator = CppGenerator(type_generator, root_namespace) | 100 cpp_generator = CppGenerator(type_generator, root_namespace) |
| 100 generators = [ | 101 generators = [ |
| 101 ('%s.h' % namespace.unix_name, cpp_generator.h_generator), | 102 ('%s.h' % namespace.unix_name, cpp_generator.h_generator), |
| 102 ('%s.cc' % namespace.unix_name, cpp_generator.cc_generator) | 103 ('%s.cc' % namespace.unix_name, cpp_generator.cc_generator) |
| 103 ] | 104 ] |
| 104 elif generator == 'dart': | 105 elif generator == 'dart': |
| 105 generators = [ | 106 generators = [ |
| 106 ('%s.dart' % namespace.unix_name, DartGenerator( | 107 ('%s.dart' % namespace.unix_name, DartGenerator( |
| 107 dart_overrides_dir)) | 108 dart_overrides_dir)) |
| 108 ] | 109 ] |
| 110 elif generator == 'ppapi': |
| 111 generator = PpapiGenerator() |
| 112 generators = [ |
| 113 (os.path.join('api', 'ppb_%s.idl' % namespace.unix_name), |
| 114 generator.idl_generator), |
| 115 ] |
| 109 else: | 116 else: |
| 110 raise Exception('Unrecognised generator %s' % generator) | 117 raise Exception('Unrecognised generator %s' % generator) |
| 111 | 118 |
| 112 output_code = [] | 119 output_code = [] |
| 113 for filename, generator in generators: | 120 for filename, generator in generators: |
| 114 code = generator.Generate(namespace).Render() | 121 code = generator.Generate(namespace).Render() |
| 115 if destdir: | 122 if destdir: |
| 116 with open(os.path.join(destdir, namespace.source_file_dir, | 123 with open(os.path.join(destdir, namespace.source_file_dir, |
| 117 filename), 'w') as f: | 124 filename), 'w') as f: |
| 118 f.write(code) | 125 f.write(code) |
| (...skipping 28 matching lines...) Expand all Loading... |
| 147 # Unless in bundle mode, only one file should be specified. | 154 # Unless in bundle mode, only one file should be specified. |
| 148 if opts.generator != 'cpp-bundle' and len(filenames) > 1: | 155 if opts.generator != 'cpp-bundle' and len(filenames) > 1: |
| 149 # TODO(sashab): Could also just use filenames[0] here and not complain. | 156 # TODO(sashab): Could also just use filenames[0] here and not complain. |
| 150 raise Exception( | 157 raise Exception( |
| 151 "Unless in bundle mode, only one file can be specified at a time.") | 158 "Unless in bundle mode, only one file can be specified at a time.") |
| 152 | 159 |
| 153 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, | 160 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, |
| 154 opts.namespace, opts.dart_overrides_dir) | 161 opts.namespace, opts.dart_overrides_dir) |
| 155 if not opts.destdir: | 162 if not opts.destdir: |
| 156 print result | 163 print result |
| OLD | NEW |