| 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. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 def load_schema(schema): | 31 def load_schema(schema): |
| 32 schema_filename, schema_extension = os.path.splitext(schema) | 32 schema_filename, schema_extension = os.path.splitext(schema) |
| 33 | 33 |
| 34 if schema_extension == '.json': | 34 if schema_extension == '.json': |
| 35 api_defs = json_schema.Load(schema) | 35 api_defs = json_schema.Load(schema) |
| 36 elif schema_extension == '.idl': | 36 elif schema_extension == '.idl': |
| 37 api_defs = idl_schema.Load(schema) | 37 api_defs = idl_schema.Load(schema) |
| 38 else: | 38 else: |
| 39 sys.exit("Did not recognize file extension %s for schema %s" % | 39 sys.exit("Did not recognize file extension %s for schema %s" % |
| 40 (schema_extension, schema)) | 40 (schema_extension, schema)) |
| 41 if len(api_defs) != 1: |
| 42 sys.exit("File %s has multiple schemas. Files are only allowed to contain a" |
| 43 " single schema." % schema) |
| 41 | 44 |
| 42 return api_defs | 45 return api_defs |
| 43 | 46 |
| 44 def handle_single_schema(filename, dest_dir, root, root_namespace): | 47 def handle_single_schema(filename, dest_dir, root, root_namespace): |
| 45 schema = os.path.normpath(filename) | 48 schema = os.path.normpath(filename) |
| 46 schema_filename, schema_extension = os.path.splitext(schema) | 49 schema_filename, schema_extension = os.path.splitext(schema) |
| 50 path, short_filename = os.path.split(schema_filename) |
| 47 api_defs = load_schema(schema) | 51 api_defs = load_schema(schema) |
| 48 | 52 |
| 49 api_model = model.Model() | 53 api_model = model.Model() |
| 50 | 54 |
| 51 for target_namespace in api_defs: | 55 for target_namespace in api_defs: |
| 52 referenced_schemas = target_namespace.get('dependencies', []) | 56 referenced_schemas = target_namespace.get('dependencies', []) |
| 53 # Load type dependencies into the model. | 57 # Load type dependencies into the model. |
| 54 # TODO(miket): do we need this in IDL? | 58 # TODO(miket): do we need this in IDL? |
| 55 for referenced_schema in referenced_schemas: | 59 for referenced_schema in referenced_schemas: |
| 56 referenced_schema_path = os.path.join( | 60 referenced_schema_path = os.path.join( |
| 57 os.path.dirname(schema), referenced_schema + '.json') | 61 os.path.dirname(schema), referenced_schema + '.json') |
| 58 referenced_api_defs = json_schema.Load(referenced_schema_path) | 62 referenced_api_defs = json_schema.Load(referenced_schema_path) |
| 59 | 63 |
| 60 for namespace in referenced_api_defs: | 64 for namespace in referenced_api_defs: |
| 61 api_model.AddNamespace(namespace, | 65 api_model.AddNamespace(namespace, |
| 62 os.path.relpath(referenced_schema_path, opts.root)) | 66 os.path.relpath(referenced_schema_path, opts.root)) |
| 63 | 67 |
| 64 # Gets the relative path from opts.root to the schema to correctly determine | 68 # Gets the relative path from opts.root to the schema to correctly determine |
| 65 # the include path. | 69 # the include path. |
| 66 relpath = os.path.relpath(schema, opts.root) | 70 relpath = os.path.relpath(schema, opts.root) |
| 67 namespace = api_model.AddNamespace(target_namespace, relpath) | 71 namespace = api_model.AddNamespace(target_namespace, relpath) |
| 68 if not namespace: | 72 if not namespace: |
| 69 continue | 73 continue |
| 70 | 74 |
| 75 if short_filename != namespace.unix_name: |
| 76 sys.exit("Filename %s is illegal. Name files using unix_hacker style." % |
| 77 filename) |
| 78 |
| 71 # The output filename must match the input filename for gyp to deal with it | 79 # The output filename must match the input filename for gyp to deal with it |
| 72 # properly. | 80 # properly. |
| 73 out_file = namespace.name | 81 out_file = namespace.unix_name |
| 74 type_generator = cpp_type_generator.CppTypeGenerator( | 82 type_generator = cpp_type_generator.CppTypeGenerator( |
| 75 root_namespace, namespace, namespace.unix_name) | 83 root_namespace, namespace, namespace.unix_name) |
| 76 for referenced_namespace in api_model.namespaces.values(): | 84 for referenced_namespace in api_model.namespaces.values(): |
| 77 if referenced_namespace == namespace: | 85 if referenced_namespace == namespace: |
| 78 continue | 86 continue |
| 79 type_generator.AddNamespace( | 87 type_generator.AddNamespace( |
| 80 referenced_namespace, | 88 referenced_namespace, |
| 81 referenced_namespace.name) | 89 referenced_namespace.unix_name) |
| 82 | 90 |
| 83 h_code = (h_generator.HGenerator(namespace, type_generator) | 91 h_code = (h_generator.HGenerator(namespace, type_generator) |
| 84 .Generate().Render()) | 92 .Generate().Render()) |
| 85 cc_code = (cc_generator.CCGenerator(namespace, type_generator) | 93 cc_code = (cc_generator.CCGenerator(namespace, type_generator) |
| 86 .Generate().Render()) | 94 .Generate().Render()) |
| 87 | 95 |
| 88 if dest_dir: | 96 if dest_dir: |
| 89 with open( | 97 with open( |
| 90 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), | 98 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), |
| 91 'w') as cc_file: | 99 'w') as cc_file: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 106 def handle_bundle_schema(filenames, dest_dir, root, root_namespace): | 114 def handle_bundle_schema(filenames, dest_dir, root, root_namespace): |
| 107 # Merge the source files into a single list of schemas. | 115 # Merge the source files into a single list of schemas. |
| 108 api_defs = [] | 116 api_defs = [] |
| 109 for filename in filenames: | 117 for filename in filenames: |
| 110 schema = os.path.normpath(filename) | 118 schema = os.path.normpath(filename) |
| 111 schema_filename, schema_extension = os.path.splitext(schema) | 119 schema_filename, schema_extension = os.path.splitext(schema) |
| 112 api_defs.extend(load_schema(schema)) | 120 api_defs.extend(load_schema(schema)) |
| 113 | 121 |
| 114 api_model = model.Model() | 122 api_model = model.Model() |
| 115 relpath = os.path.relpath(os.path.normpath(filenames[0]), root) | 123 relpath = os.path.relpath(os.path.normpath(filenames[0]), root) |
| 116 for target_namespace in api_defs: | 124 |
| 117 api_model.AddNamespace(target_namespace, relpath) | 125 for target_namespace, schema_filename in zip(api_defs, filenames): |
| 126 namespace = api_model.AddNamespace(target_namespace, relpath) |
| 127 path, filename = os.path.split(schema_filename) |
| 128 short_filename, extension = os.path.splitext(filename) |
| 129 |
| 130 # Filenames are checked against the unix_names of the namespaces they |
| 131 # generate because the gyp uses the names of the JSON files to generate |
| 132 # the names of the .cc and .h files. We want these to be using unix_names. |
| 133 if namespace.unix_name != short_filename: |
| 134 sys.exit("Filename %s is illegal. Name files using unix_hacker style." % |
| 135 schema_filename) |
| 118 | 136 |
| 119 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) | 137 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) |
| 120 for referenced_namespace in api_model.namespaces.values(): | 138 for referenced_namespace in api_model.namespaces.values(): |
| 121 type_generator.AddNamespace( | 139 type_generator.AddNamespace( |
| 122 referenced_namespace, | 140 referenced_namespace, |
| 123 referenced_namespace.name) | 141 referenced_namespace.unix_name) |
| 124 | 142 |
| 125 generator = schema_bundle_generator.SchemaBundleGenerator( | 143 generator = schema_bundle_generator.SchemaBundleGenerator( |
| 126 api_model, api_defs, type_generator) | 144 api_model, api_defs, type_generator) |
| 127 api_h_code = generator.GenerateAPIHeader().Render() | 145 api_h_code = generator.GenerateAPIHeader().Render() |
| 128 schemas_h_code = generator.GenerateSchemasHeader().Render() | 146 schemas_h_code = generator.GenerateSchemasHeader().Render() |
| 129 schemas_cc_code = generator.GenerateSchemasCC().Render() | 147 schemas_cc_code = generator.GenerateSchemasCC().Render() |
| 130 | 148 |
| 131 if dest_dir: | 149 if dest_dir: |
| 132 basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') | 150 basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') |
| 133 with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: | 151 with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 186 |
| 169 if not args: | 187 if not args: |
| 170 sys.exit(0) # This is OK as a no-op | 188 sys.exit(0) # This is OK as a no-op |
| 171 dest_dir = opts.destdir | 189 dest_dir = opts.destdir |
| 172 root_namespace = opts.namespace | 190 root_namespace = opts.namespace |
| 173 | 191 |
| 174 if opts.bundle: | 192 if opts.bundle: |
| 175 handle_bundle_schema(args, dest_dir, opts.root, root_namespace) | 193 handle_bundle_schema(args, dest_dir, opts.root, root_namespace) |
| 176 else: | 194 else: |
| 177 handle_single_schema(args[0], dest_dir, opts.root, root_namespace) | 195 handle_single_schema(args[0], dest_dir, opts.root, root_namespace) |
| OLD | NEW |