| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2012 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 # Format for the JSON schema file: | |
| 7 # { | |
| 8 # "type_name": "DesiredCStructName", | |
| 9 # "headers": [ // Optional list of headers to be included by the .h. | |
| 10 # "path/to/header.h" | |
| 11 # ], | |
| 12 # "schema": [ // Fields of the generated structure. | |
| 13 # { | |
| 14 # "field": "my_enum_field", | |
| 15 # "type": "enum", // Either: int, string, string16, enum, array. | |
| 16 # "default": "RED", // Optional. Cannot be used for array. | |
| 17 # "ctype": "Color" // Only for enum, specify the C type. | |
| 18 # }, | |
| 19 # { | |
| 20 # "field": "my_int_array_field", // my_int_array_field_size will also | |
| 21 # "type": "array", // be generated. | |
| 22 # "contents": { | |
| 23 # "type": "int" // Either: int, string, string16, enum, array. | |
| 24 # } | |
| 25 # }, | |
| 26 # ... | |
| 27 # ] | |
| 28 # } | |
| 29 # | |
| 30 # Format for the JSON description file: | |
| 31 # { | |
| 32 # "int_variables": { // An optional list of constant int variables. | |
| 33 # "kDesiredConstantName": 45 | |
| 34 # }, | |
| 35 # "elements": { // All the elements for which to create static | |
| 36 # // initialization code in the .cc file. | |
| 37 # "my_const_variable": { | |
| 38 # "my_int_field": 10, | |
| 39 # "my_string_field": "foo bar", | |
| 40 # "my_enum_field": "BLACK", | |
| 41 # "my_int_array_field": [ 1, 2, 3, 5, 7 ], | |
| 42 # }, | |
| 43 # "my_other_const_variable": { | |
| 44 # ... | |
| 45 # } | |
| 46 # } | |
| 47 # } | |
| 48 | |
| 49 import json | |
| 50 from datetime import datetime | |
| 51 import os.path | |
| 52 import sys | |
| 53 import optparse | |
| 54 import copy | |
| 55 _script_path = os.path.realpath(__file__) | |
| 56 | |
| 57 sys.path.insert(0, os.path.normpath(_script_path + "/../../")) | |
| 58 try: | |
| 59 import json_comment_eater | |
| 60 finally: | |
| 61 sys.path.pop(0) | |
| 62 | |
| 63 import struct_generator | |
| 64 import element_generator | |
| 65 | |
| 66 HEAD = """// Copyright %d The Chromium Authors. All rights reserved. | |
| 67 // Use of this source code is governed by a BSD-style license that can be | |
| 68 // found in the LICENSE file. | |
| 69 | |
| 70 // GENERATED FROM THE SCHEMA DEFINITION AND DESCRIPTION IN | |
| 71 // %s | |
| 72 // %s | |
| 73 // DO NOT EDIT. | |
| 74 | |
| 75 """ | |
| 76 | |
| 77 def _GenerateHeaderGuard(h_filename): | |
| 78 """Generates the string used in #ifndef guarding the header file. | |
| 79 """ | |
| 80 return (('%s_' % h_filename) | |
| 81 .upper().replace(os.sep, '_').replace('/', '_').replace('.', '_')) | |
| 82 | |
| 83 def _GenerateH(fileroot, head, namespace, schema, description): | |
| 84 """Generates the .h file containing the definition of the structure specified | |
| 85 by the schema. | |
| 86 | |
| 87 Args: | |
| 88 fileroot: The filename and path of the file to create, without an extension. | |
| 89 head: The string to output as the header of the .h file. | |
| 90 namespace: A string corresponding to the C++ namespace to use. | |
| 91 schema: A dict containing the schema. See comment at the top of this file. | |
| 92 description: A dict containing the description. See comment at the top of | |
| 93 this file. | |
| 94 """ | |
| 95 | |
| 96 h_filename = fileroot + '.h' | |
| 97 with open(h_filename, 'w') as f: | |
| 98 f.write(head) | |
| 99 | |
| 100 f.write('#include <cstddef>\n') | |
| 101 f.write('\n') | |
| 102 | |
| 103 header_guard = _GenerateHeaderGuard(h_filename) | |
| 104 f.write('#ifndef %s\n' % header_guard) | |
| 105 f.write('#define %s\n' % header_guard) | |
| 106 f.write('\n') | |
| 107 | |
| 108 for header in schema.get('headers', []): | |
| 109 f.write('#include "%s"\n' % header) | |
| 110 f.write('\n') | |
| 111 | |
| 112 if namespace: | |
| 113 f.write('namespace %s {\n' % namespace) | |
| 114 f.write('\n') | |
| 115 | |
| 116 f.write(struct_generator.GenerateStruct( | |
| 117 schema['type_name'], schema['schema'])) | |
| 118 f.write('\n') | |
| 119 | |
| 120 for var_name, value in description.get('int_variables', []).items(): | |
| 121 f.write('extern const int %s;\n' % var_name) | |
| 122 f.write('\n') | |
| 123 | |
| 124 for element_name, element in description['elements'].items(): | |
| 125 f.write('extern const %s %s;\n' % (schema['type_name'], element_name)) | |
| 126 | |
| 127 if namespace: | |
| 128 f.write('\n') | |
| 129 f.write('} // namespace %s\n' % namespace) | |
| 130 | |
| 131 f.write('\n') | |
| 132 f.write( '#endif // %s\n' % header_guard) | |
| 133 | |
| 134 def _GenerateCC(fileroot, head, namespace, schema, description): | |
| 135 """Generates the .cc file containing the static initializers for the | |
| 136 of the elements specified in the description. | |
| 137 | |
| 138 Args: | |
| 139 fileroot: The filename and path of the file to create, without an extension. | |
| 140 head: The string to output as the header of the .cc file. | |
| 141 namespace: A string corresponding to the C++ namespace to use. | |
| 142 schema: A dict containing the schema. See comment at the top of this file. | |
| 143 description: A dict containing the description. See comment at the top of | |
| 144 this file. | |
| 145 """ | |
| 146 with open(fileroot + '.cc', 'w') as f: | |
| 147 f.write(head) | |
| 148 | |
| 149 f.write('#include "%s"\n' % (fileroot + '.h')) | |
| 150 f.write('\n') | |
| 151 | |
| 152 if namespace: | |
| 153 f.write('namespace %s {\n' % namespace) | |
| 154 f.write('\n') | |
| 155 | |
| 156 f.write(element_generator.GenerateElements(schema['type_name'], | |
| 157 schema['schema'], description)) | |
| 158 | |
| 159 if namespace: | |
| 160 f.write('\n') | |
| 161 f.write('} // namespace %s\n' % namespace) | |
| 162 | |
| 163 def _Load(filename): | |
| 164 """Loads a JSON file int a Python object and return this object. | |
| 165 """ | |
| 166 # TODO(beaudoin): When moving to Python 2.7 use object_pairs_hook=OrderedDict. | |
| 167 with open(filename, 'r') as handle: | |
| 168 result = json.loads(json_comment_eater.Nom(handle.read())) | |
| 169 return result | |
| 170 | |
| 171 if __name__ == '__main__': | |
| 172 parser = optparse.OptionParser( | |
| 173 description='Generates an C++ array of struct from a JSON description.', | |
| 174 usage='usage: %prog [option] -s schema description') | |
| 175 parser.add_option('-d', '--destdir', | |
| 176 help='root directory to output generated files.') | |
| 177 parser.add_option('-n', '--namespace', | |
| 178 help='C++ namespace for generated files. e.g search_providers.') | |
| 179 parser.add_option('-s', '--schema', help='path to the schema file, ' | |
| 180 'mandatory.') | |
| 181 (opts, args) = parser.parse_args() | |
| 182 | |
| 183 if not opts.schema: | |
| 184 parser.error('You must specify a --schema.') | |
| 185 | |
| 186 description_filename = os.path.normpath(args[0]) | |
| 187 root, ext = os.path.splitext(description_filename) | |
| 188 shortroot = os.path.split(root)[1] | |
| 189 if opts.destdir: | |
| 190 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) | |
| 191 else: | |
| 192 output_root = shortroot | |
| 193 | |
| 194 schema = _Load(opts.schema) | |
| 195 description = _Load(description_filename) | |
| 196 | |
| 197 head = HEAD % (datetime.now().year, opts.schema, description_filename) | |
| 198 _GenerateH(output_root, head, opts.namespace, schema, description) | |
| 199 _GenerateCC(output_root, head, opts.namespace, schema, description) | |
| OLD | NEW |