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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 api_model.AddNamespace(namespace, | 61 api_model.AddNamespace(namespace, |
62 os.path.relpath(referenced_schema_path, opts.root)) | 62 os.path.relpath(referenced_schema_path, opts.root)) |
63 | 63 |
64 # Gets the relative path from opts.root to the schema to correctly determine | 64 # Gets the relative path from opts.root to the schema to correctly determine |
65 # the include path. | 65 # the include path. |
66 relpath = os.path.relpath(schema, opts.root) | 66 relpath = os.path.relpath(schema, opts.root) |
67 namespace = api_model.AddNamespace(target_namespace, relpath) | 67 namespace = api_model.AddNamespace(target_namespace, relpath) |
68 if not namespace: | 68 if not namespace: |
69 continue | 69 continue |
70 | 70 |
71 path, schema_filename = os.path.split(schema_filename) | |
72 if schema_filename != namespace.unix_name: | |
73 sys.exit("Filename %s%s is illegal. Name files using unix_hacker style." % | |
74 (schema_filename, schema_extension)) | |
75 | |
71 # The output filename must match the input filename for gyp to deal with it | 76 # The output filename must match the input filename for gyp to deal with it |
72 # properly. | 77 # properly. |
73 out_file = namespace.name | 78 out_file = namespace.unix_name |
74 type_generator = cpp_type_generator.CppTypeGenerator( | 79 type_generator = cpp_type_generator.CppTypeGenerator( |
75 root_namespace, namespace, namespace.unix_name) | 80 root_namespace, namespace, namespace.unix_name) |
76 for referenced_namespace in api_model.namespaces.values(): | 81 for referenced_namespace in api_model.namespaces.values(): |
77 if referenced_namespace == namespace: | 82 if referenced_namespace == namespace: |
78 continue | 83 continue |
79 type_generator.AddNamespace( | 84 type_generator.AddNamespace( |
80 referenced_namespace, | 85 referenced_namespace, |
81 referenced_namespace.name) | 86 referenced_namespace.unix_name) |
82 | 87 |
83 h_code = (h_generator.HGenerator(namespace, type_generator) | 88 h_code = (h_generator.HGenerator(namespace, type_generator) |
84 .Generate().Render()) | 89 .Generate().Render()) |
85 cc_code = (cc_generator.CCGenerator(namespace, type_generator) | 90 cc_code = (cc_generator.CCGenerator(namespace, type_generator) |
86 .Generate().Render()) | 91 .Generate().Render()) |
87 | 92 |
88 if dest_dir: | 93 if dest_dir: |
89 with open( | 94 with open( |
90 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), | 95 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), |
91 'w') as cc_file: | 96 'w') as cc_file: |
(...skipping 15 matching lines...) Expand all Loading... | |
107 # Merge the source files into a single list of schemas. | 112 # Merge the source files into a single list of schemas. |
108 api_defs = [] | 113 api_defs = [] |
109 for filename in filenames: | 114 for filename in filenames: |
110 schema = os.path.normpath(filename) | 115 schema = os.path.normpath(filename) |
111 schema_filename, schema_extension = os.path.splitext(schema) | 116 schema_filename, schema_extension = os.path.splitext(schema) |
112 api_defs.extend(load_schema(schema)) | 117 api_defs.extend(load_schema(schema)) |
113 | 118 |
114 api_model = model.Model() | 119 api_model = model.Model() |
115 relpath = os.path.relpath(os.path.normpath(filenames[0]), root) | 120 relpath = os.path.relpath(os.path.normpath(filenames[0]), root) |
116 for target_namespace in api_defs: | 121 for target_namespace in api_defs: |
117 api_model.AddNamespace(target_namespace, relpath) | 122 api_model.AddNamespace(target_namespace, relpath) |
cduvall
2012/05/10 03:09:02
Not quite sure the best way to check the filenames
| |
118 | 123 |
119 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) | 124 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) |
120 for referenced_namespace in api_model.namespaces.values(): | 125 for referenced_namespace in api_model.namespaces.values(): |
121 type_generator.AddNamespace( | 126 type_generator.AddNamespace( |
122 referenced_namespace, | 127 referenced_namespace, |
123 referenced_namespace.name) | 128 referenced_namespace.unix_name) |
124 | 129 |
125 generator = schema_bundle_generator.SchemaBundleGenerator( | 130 generator = schema_bundle_generator.SchemaBundleGenerator( |
126 api_model, api_defs, type_generator) | 131 api_model, api_defs, type_generator) |
127 api_h_code = generator.GenerateAPIHeader().Render() | 132 api_h_code = generator.GenerateAPIHeader().Render() |
128 schemas_h_code = generator.GenerateSchemasHeader().Render() | 133 schemas_h_code = generator.GenerateSchemasHeader().Render() |
129 schemas_cc_code = generator.GenerateSchemasCC().Render() | 134 schemas_cc_code = generator.GenerateSchemasCC().Render() |
130 | 135 |
131 if dest_dir: | 136 if dest_dir: |
132 basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') | 137 basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') |
133 with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: | 138 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 | 173 |
169 if not args: | 174 if not args: |
170 sys.exit(0) # This is OK as a no-op | 175 sys.exit(0) # This is OK as a no-op |
171 dest_dir = opts.destdir | 176 dest_dir = opts.destdir |
172 root_namespace = opts.namespace | 177 root_namespace = opts.namespace |
173 | 178 |
174 if opts.bundle: | 179 if opts.bundle: |
175 handle_bundle_schema(args, dest_dir, opts.root, root_namespace) | 180 handle_bundle_schema(args, dest_dir, opts.root, root_namespace) |
176 else: | 181 else: |
177 handle_single_schema(args[0], dest_dir, opts.root, root_namespace) | 182 handle_single_schema(args[0], dest_dir, opts.root, root_namespace) |
OLD | NEW |