OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import code | 5 import code |
6 import cpp_util | 6 import cpp_util |
7 from model import Platforms | 7 from model import Platforms |
8 from schema_util import CapitalizeFirstLetter | 8 from schema_util import CapitalizeFirstLetter |
9 from schema_util import JsFunctionNameToClassName | 9 from schema_util import JsFunctionNameToClassName |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... | |
24 # Some schemas actually have properties called "description", so only | 24 # Some schemas actually have properties called "description", so only |
25 # remove descriptions that have string values. | 25 # remove descriptions that have string values. |
26 if key == 'description' and isinstance(value, basestring): | 26 if key == 'description' and isinstance(value, basestring): |
27 continue | 27 continue |
28 result[key] = _RemoveDescriptions(value) | 28 result[key] = _RemoveDescriptions(value) |
29 return result | 29 return result |
30 if isinstance(node, list): | 30 if isinstance(node, list): |
31 return [_RemoveDescriptions(v) for v in node] | 31 return [_RemoveDescriptions(v) for v in node] |
32 return node | 32 return node |
33 | 33 |
34 class SchemaBundleGenerator(object): | 34 class CppBundleGenerator(object): |
35 """This class contains methods to generate code based on multiple schemas. | 35 """This class contains methods to generate code based on multiple schemas. |
36 """ | 36 """ |
37 | 37 |
38 def __init__(self, root, model, api_defs, cpp_type_generator): | 38 def __init__(self, root, model, api_defs, cpp_type_generator): |
39 self._root = root; | 39 self._root = root; |
40 self._model = model | 40 self._model = model |
41 self._api_defs = api_defs | 41 self._api_defs = api_defs |
42 self._cpp_type_generator = cpp_type_generator | 42 self._cpp_type_generator = cpp_type_generator |
43 | 43 |
44 self.api_h_generator = GenerateAPIHeader(self) | |
45 self.schemas_cc_generator = GenerateSchemasCC(self) | |
46 self.schemas_h_generator = GenerateSchemasHeader(self) | |
47 | |
44 def GenerateHeader(self, file_base, body_code): | 48 def GenerateHeader(self, file_base, body_code): |
45 """Generates a code.Code object for a header file | 49 """Generates a code.Code object for a header file |
46 | 50 |
47 Parameters: | 51 Parameters: |
48 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') | 52 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') |
49 - |body_code| - the code to put in between the multiple inclusion guards""" | 53 - |body_code| - the code to put in between the multiple inclusion guards""" |
50 c = code.Code() | 54 c = code.Code() |
51 c.Append(cpp_util.CHROMIUM_LICENSE) | 55 c.Append(cpp_util.CHROMIUM_LICENSE) |
52 c.Append() | 56 c.Append() |
53 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH) | 57 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH) |
(...skipping 15 matching lines...) Expand all Loading... | |
69 if model_object.platforms is None: | 73 if model_object.platforms is None: |
70 return None | 74 return None |
71 ifdefs = [] | 75 ifdefs = [] |
72 for platform in model_object.platforms: | 76 for platform in model_object.platforms: |
73 if platform == Platforms.CHROMEOS: | 77 if platform == Platforms.CHROMEOS: |
74 ifdefs.append('defined(OS_CHROMEOS)') | 78 ifdefs.append('defined(OS_CHROMEOS)') |
75 else: | 79 else: |
76 raise ValueError("Unsupported platform ifdef: %s" % platform.name) | 80 raise ValueError("Unsupported platform ifdef: %s" % platform.name) |
77 return ' and '.join(ifdefs) | 81 return ' and '.join(ifdefs) |
78 | 82 |
79 def GenerateAPIHeader(self): | |
80 """Generates the header for API registration / declaration""" | |
81 c = code.Code() | |
82 | |
83 c.Append('#include <string>') | |
84 c.Append() | |
85 c.Append('#include "base/basictypes.h"') | |
86 | |
87 for namespace in self._model.namespaces.values(): | |
88 namespace_name = namespace.unix_name.replace("experimental_", "") | |
89 implementation_header = namespace.compiler_options.get( | |
90 "implemented_in", | |
91 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, | |
92 namespace_name)) | |
93 if not os.path.exists( | |
94 os.path.join(self._root, os.path.normpath(implementation_header))): | |
95 if "implemented_in" in namespace.compiler_options: | |
96 raise ValueError('Header file for namespace "%s" specified in ' | |
97 'compiler_options not found: %s' % | |
98 (namespace.unix_name, implementation_header)) | |
99 continue | |
100 ifdefs = self._GetPlatformIfdefs(namespace) | |
101 if ifdefs is not None: | |
102 c.Append("#if %s" % ifdefs, indent_level=0) | |
103 | |
104 c.Append('#include "%s"' % implementation_header) | |
105 | |
106 if ifdefs is not None: | |
107 c.Append("#endif // %s" % ifdefs, indent_level=0) | |
108 | |
109 c.Append() | |
110 c.Append("class ExtensionFunctionRegistry;") | |
111 c.Append() | |
112 | |
113 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | |
114 for namespace in self._model.namespaces.values(): | |
115 c.Append("// TODO(miket): emit code for %s" % (namespace.unix_name)) | |
116 c.Append() | |
117 c.Concat(self.GenerateFunctionRegistry()) | |
118 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | |
119 c.Append() | |
120 return self.GenerateHeader('generated_api', c) | |
121 | |
122 def _GetNamespaceFunctions(self, namespace): | 83 def _GetNamespaceFunctions(self, namespace): |
123 functions = list(namespace.functions.values()) | 84 functions = list(namespace.functions.values()) |
124 if namespace.compiler_options.get("generate_type_functions", False): | 85 if namespace.compiler_options.get("generate_type_functions", False): |
125 for type_ in namespace.types.values(): | 86 for type_ in namespace.types.values(): |
126 functions += list(type_.functions.values()) | 87 functions += list(type_.functions.values()) |
127 return functions | 88 return functions |
128 | 89 |
129 def GenerateFunctionRegistry(self): | 90 def GenerateFunctionRegistry(self): |
130 c = code.Code() | 91 c = code.Code() |
131 c.Sblock("class GeneratedFunctionRegistry {") | 92 c.Sblock("class GeneratedFunctionRegistry {") |
(...skipping 20 matching lines...) Expand all Loading... | |
152 if function_ifdefs is not None: | 113 if function_ifdefs is not None: |
153 c.Append("#endif // %s" % function_ifdefs, indent_level=0) | 114 c.Append("#endif // %s" % function_ifdefs, indent_level=0) |
154 | 115 |
155 if namespace_ifdefs is not None: | 116 if namespace_ifdefs is not None: |
156 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) | 117 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) |
157 c.Eblock("}") | 118 c.Eblock("}") |
158 c.Eblock("};") | 119 c.Eblock("};") |
159 c.Append() | 120 c.Append() |
160 return c | 121 return c |
161 | 122 |
162 def GenerateSchemasHeader(self): | 123 class GenerateAPIHeader(object): |
not at google - send to devlin
2013/02/02 00:45:48
_APIHGenerator would be more consistent with namin
sashab
2013/02/04 05:09:27
Done.
| |
163 """Generates a code.Code object for the generated schemas .h file""" | 124 """Generates the header for API registration / declaration""" |
125 def __init__(self, cpp_bundle): | |
126 self._bundle = cpp_bundle | |
127 | |
128 def Generate(self, namespace): | |
129 c = code.Code() | |
130 | |
131 c.Append('#include <string>') | |
132 c.Append() | |
133 c.Append('#include "base/basictypes.h"') | |
134 | |
135 for namespace in self._bundle._model.namespaces.values(): | |
136 namespace_name = namespace.unix_name.replace("experimental_", "") | |
137 implementation_header = namespace.compiler_options.get( | |
138 "implemented_in", | |
139 "chrome/browser/extensions/api/%s/%s_api.h" % (namespace_name, | |
140 namespace_name)) | |
141 if not os.path.exists( | |
142 os.path.join(self._bundle._root, | |
143 os.path.normpath(implementation_header))): | |
144 if "implemented_in" in namespace.compiler_options: | |
145 raise ValueError('Header file for namespace "%s" specified in ' | |
146 'compiler_options not found: %s' % | |
147 (namespace.unix_name, implementation_header)) | |
148 continue | |
149 ifdefs = self._bundle._GetPlatformIfdefs(namespace) | |
150 if ifdefs is not None: | |
151 c.Append("#if %s" % ifdefs, indent_level=0) | |
152 | |
153 c.Append('#include "%s"' % implementation_header) | |
154 | |
155 if ifdefs is not None: | |
156 c.Append("#endif // %s" % ifdefs, indent_level=0) | |
157 | |
158 c.Append() | |
159 c.Append("class ExtensionFunctionRegistry;") | |
160 c.Append() | |
161 | |
162 c.Concat(self._bundle._cpp_type_generator.GetRootNamespaceStart()) | |
163 for namespace in self._bundle._model.namespaces.values(): | |
164 c.Append("// TODO(miket): emit code for %s" % (namespace.unix_name)) | |
165 c.Append() | |
166 c.Concat(self._bundle.GenerateFunctionRegistry()) | |
167 c.Concat(self._bundle._cpp_type_generator.GetRootNamespaceEnd()) | |
168 c.Append() | |
169 return self._bundle.GenerateHeader('generated_api', c) | |
170 | |
171 class GenerateSchemasHeader(object): | |
not at google - send to devlin
2013/02/02 00:45:48
_SchemasHGenerator
sashab
2013/02/04 05:09:27
Done.
| |
172 """Generates a code.Code object for the generated schemas .h file""" | |
173 def __init__(self, cpp_bundle): | |
174 self._bundle = cpp_bundle | |
175 | |
176 def Generate(self, namespace): | |
164 c = code.Code() | 177 c = code.Code() |
165 c.Append('#include <map>') | 178 c.Append('#include <map>') |
166 c.Append('#include <string>') | 179 c.Append('#include <string>') |
167 c.Append(); | 180 c.Append(); |
168 c.Append('#include "base/string_piece.h"') | 181 c.Append('#include "base/string_piece.h"') |
169 c.Append() | 182 c.Append() |
170 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | 183 c.Concat(self._bundle._cpp_type_generator.GetRootNamespaceStart()) |
171 c.Append() | 184 c.Append() |
172 c.Sblock('class GeneratedSchemas {') | 185 c.Sblock('class GeneratedSchemas {') |
173 c.Append(' public:') | 186 c.Append(' public:') |
174 c.Append('// Puts all API schemas in |schemas|.') | 187 c.Append('// Puts all API schemas in |schemas|.') |
175 c.Append('static void Get(' | 188 c.Append('static void Get(' |
176 'std::map<std::string, base::StringPiece>* schemas);') | 189 'std::map<std::string, base::StringPiece>* schemas);') |
177 c.Eblock('};'); | 190 c.Eblock('};'); |
178 c.Append() | 191 c.Append() |
179 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 192 c.Concat(self._bundle._cpp_type_generator.GetRootNamespaceEnd()) |
180 c.Append() | 193 c.Append() |
181 return self.GenerateHeader('generated_schemas', c) | 194 return self._bundle.GenerateHeader('generated_schemas', c) |
182 | 195 |
183 def GenerateSchemasCC(self): | 196 class GenerateSchemasCC(object): |
not at google - send to devlin
2013/02/02 00:45:48
_SchemasCCGenerator
sashab
2013/02/04 05:09:27
Done.
| |
184 """Generates a code.Code object for the generated schemas .cc file""" | 197 """Generates a code.Code object for the generated schemas .cc file""" |
198 | |
199 def __init__(self, cpp_bundle): | |
200 self._bundle = cpp_bundle | |
201 | |
202 def Generate(self, namespace): | |
185 c = code.Code() | 203 c = code.Code() |
186 c.Append(cpp_util.CHROMIUM_LICENSE) | 204 c.Append(cpp_util.CHROMIUM_LICENSE) |
187 c.Append() | 205 c.Append() |
188 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, | 206 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, |
189 'generated_schemas.h'))) | 207 'generated_schemas.h'))) |
190 c.Append() | 208 c.Append() |
191 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | 209 c.Concat(self._bundle._cpp_type_generator.GetRootNamespaceStart()) |
192 c.Append() | 210 c.Append() |
193 c.Append('// static') | 211 c.Append('// static') |
194 c.Sblock('void GeneratedSchemas::Get(' | 212 c.Sblock('void GeneratedSchemas::Get(' |
195 'std::map<std::string, base::StringPiece>* schemas) {') | 213 'std::map<std::string, base::StringPiece>* schemas) {') |
196 for api in self._api_defs: | 214 for api in self._bundle._api_defs: |
197 namespace = self._model.namespaces[api.get('namespace')] | 215 namespace = self._bundle._model.namespaces[api.get('namespace')] |
198 # JSON parsing code expects lists of schemas, so dump a singleton list. | 216 # JSON parsing code expects lists of schemas, so dump a singleton list. |
199 json_content = json.dumps([_RemoveDescriptions(api)], | 217 json_content = json.dumps([_RemoveDescriptions(api)], |
200 separators=(',', ':')) | 218 separators=(',', ':')) |
201 # Escape all double-quotes and backslashes. For this to output a valid | 219 # Escape all double-quotes and backslashes. For this to output a valid |
202 # JSON C string, we need to escape \ and ". | 220 # JSON C string, we need to escape \ and ". |
203 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') | 221 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') |
204 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) | 222 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) |
205 c.Eblock('}') | 223 c.Eblock('}') |
206 c.Append() | 224 c.Append() |
207 c.Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 225 c.Concat(self._bundle._cpp_type_generator.GetRootNamespaceEnd()) |
208 c.Append() | 226 c.Append() |
209 return c | 227 return c |
OLD | NEW |