OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import model |
| 6 import os.path |
| 7 import json |
| 8 import generate_cc |
| 9 import generate_h |
| 10 import optparse |
| 11 import sys |
| 12 |
| 13 parser = optparse.OptionParser( |
| 14 description='Generate C++ boilerplate code for extension apis from json', |
| 15 usage='usage: %prog [options] target referenced_jsons') |
| 16 parser.add_option('-r', '--root', default='.', |
| 17 help='logical include root directory. Path to json files from specified' |
| 18 'dir will be the include path.') |
| 19 parser.add_option('-d', '--destdir', |
| 20 help='root directory to output generated files') |
| 21 parser.add_option('-n', '--namespace', |
| 22 help='C++ namespace for generated files') |
| 23 parser.add_option('-s', '--suffix', default='_api', |
| 24 help='C++ namespace suffix for generated files') |
| 25 |
| 26 (opts, args) = parser.parse_args() |
| 27 if not args: |
| 28 sys.exit('Error: No input json file') |
| 29 dest_dir = opts.destdir |
| 30 root_namespace = opts.namespace |
| 31 filename_suffix = opts.suffix |
| 32 |
| 33 target_json = os.path.normpath(args[0]) |
| 34 referenced_jsons = args[1:] |
| 35 |
| 36 json_model = model.Model() |
| 37 |
| 38 # Load type dependencies into the model |
| 39 for json_path in referenced_jsons: |
| 40 json_path = os.path.normpath(json_path) |
| 41 api_file = open(json_path, 'r') |
| 42 api_defs = json.loads(api_file.read()) |
| 43 api_file.close() |
| 44 |
| 45 for namespace in api_defs: |
| 46 json_model.add_namespace(namespace, root_namespace, |
| 47 os.path.relpath(json_path, opts.root), filename_suffix) |
| 48 |
| 49 # Actually generate for target file. |
| 50 target_api_file = open(target_json, 'r') |
| 51 target_api_defs = json.loads(target_api_file.read()) |
| 52 target_api_file.close() |
| 53 for target_namespace in target_api_defs: |
| 54 namespace = json_model.add_namespace(target_namespace, root_namespace, |
| 55 # Gets the relative path from opts.root to the target_json to correctly |
| 56 # determine the include path |
| 57 os.path.relpath(target_json, opts.root), filename_suffix) |
| 58 if not namespace: |
| 59 continue |
| 60 cc_generator = generate_cc.CC_Generator(namespace, json_model) |
| 61 cc_code = cc_generator.generate().render() |
| 62 h_generator = generate_h.H_Generator(namespace, json_model) |
| 63 h_code = h_generator.generate().render() |
| 64 if dest_dir: |
| 65 cc_file = open(os.path.join(dest_dir, namespace.parent_dir, |
| 66 namespace.filename + '.cc'), 'w') |
| 67 cc_file.write(cc_code) |
| 68 cc_file.close() |
| 69 h_file = open(os.path.join(dest_dir, namespace.parent_dir, |
| 70 namespace.filename + '.h'), 'w') |
| 71 h_file.write(h_code) |
| 72 h_file.close() |
| 73 else: |
| 74 print '%s.h' % namespace.filename |
| 75 print |
| 76 print h_code |
| 77 print |
| 78 print '%s.cc\n' % namespace.filename |
| 79 print |
| 80 print cc_code |
OLD | NEW |