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 21 matching lines...) Expand all Loading... |
32 | 32 |
33 class CppBundleGenerator(object): | 33 class CppBundleGenerator(object): |
34 """This class contains methods to generate code based on multiple schemas. | 34 """This class contains methods to generate code based on multiple schemas. |
35 """ | 35 """ |
36 | 36 |
37 def __init__(self, | 37 def __init__(self, |
38 root, | 38 root, |
39 model, | 39 model, |
40 api_defs, | 40 api_defs, |
41 cpp_type_generator, | 41 cpp_type_generator, |
42 cpp_namespace, | 42 cpp_namespace_pattern, |
43 source_file_dir, | 43 source_file_dir, |
44 impl_dir): | 44 impl_dir): |
45 self._root = root | 45 self._root = root |
46 self._model = model | 46 self._model = model |
47 self._api_defs = api_defs | 47 self._api_defs = api_defs |
48 self._cpp_type_generator = cpp_type_generator | 48 self._cpp_type_generator = cpp_type_generator |
49 self._cpp_namespace = cpp_namespace | |
50 self._source_file_dir = source_file_dir | 49 self._source_file_dir = source_file_dir |
51 self._impl_dir = impl_dir | 50 self._impl_dir = impl_dir |
52 | 51 |
| 52 # Hack: assume that the C++ namespace for the bundle is the namespace of the |
| 53 # files without the last component of the namespace. A cleaner way to do |
| 54 # this would be to make it a separate variable in the gyp file. |
| 55 self._cpp_namespace = cpp_namespace_pattern.rsplit('::', 1)[0] |
| 56 |
53 self.api_cc_generator = _APICCGenerator(self) | 57 self.api_cc_generator = _APICCGenerator(self) |
54 self.api_h_generator = _APIHGenerator(self) | 58 self.api_h_generator = _APIHGenerator(self) |
55 self.schemas_cc_generator = _SchemasCCGenerator(self) | 59 self.schemas_cc_generator = _SchemasCCGenerator(self) |
56 self.schemas_h_generator = _SchemasHGenerator(self) | 60 self.schemas_h_generator = _SchemasHGenerator(self) |
57 | 61 |
58 def _GenerateHeader(self, file_base, body_code): | 62 def _GenerateHeader(self, file_base, body_code): |
59 """Generates a code.Code object for a header file | 63 """Generates a code.Code object for a header file |
60 | 64 |
61 Parameters: | 65 Parameters: |
62 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') | 66 - |file_base| - the base of the filename, e.g. 'foo' (for 'foo.h') |
63 - |body_code| - the code to put in between the multiple inclusion guards""" | 67 - |body_code| - the code to put in between the multiple inclusion guards""" |
64 c = code.Code() | 68 c = code.Code() |
65 c.Append(cpp_util.CHROMIUM_LICENSE) | 69 c.Append(cpp_util.CHROMIUM_LICENSE) |
66 c.Append() | 70 c.Append() |
67 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % self._source_file_dir) | 71 c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % self._source_file_dir) |
68 ifndef_name = cpp_util.GenerateIfndefName(self._source_file_dir, file_base) | 72 ifndef_name = cpp_util.GenerateIfndefName( |
| 73 '%s/%s.h' % (self._source_file_dir, file_base)) |
69 c.Append() | 74 c.Append() |
70 c.Append('#ifndef %s' % ifndef_name) | 75 c.Append('#ifndef %s' % ifndef_name) |
71 c.Append('#define %s' % ifndef_name) | 76 c.Append('#define %s' % ifndef_name) |
72 c.Append() | 77 c.Append() |
73 c.Concat(body_code) | 78 c.Concat(body_code) |
74 c.Append() | 79 c.Append() |
75 c.Append('#endif // %s' % ifndef_name) | 80 c.Append('#endif // %s' % ifndef_name) |
76 c.Append() | 81 c.Append() |
77 return c | 82 return c |
78 | 83 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) | 145 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) |
141 c.Eblock("}") | 146 c.Eblock("}") |
142 return c | 147 return c |
143 | 148 |
144 | 149 |
145 class _APIHGenerator(object): | 150 class _APIHGenerator(object): |
146 """Generates the header for API registration / declaration""" | 151 """Generates the header for API registration / declaration""" |
147 def __init__(self, cpp_bundle): | 152 def __init__(self, cpp_bundle): |
148 self._bundle = cpp_bundle | 153 self._bundle = cpp_bundle |
149 | 154 |
150 def Generate(self, namespace): | 155 def Generate(self, _): # namespace not relevant, this is a bundle |
151 c = code.Code() | 156 c = code.Code() |
152 | 157 |
153 c.Append('#include <string>') | 158 c.Append('#include <string>') |
154 c.Append() | 159 c.Append() |
155 c.Append('#include "base/basictypes.h"') | 160 c.Append('#include "base/basictypes.h"') |
156 c.Append() | 161 c.Append() |
157 c.Append("class ExtensionFunctionRegistry;") | 162 c.Append("class ExtensionFunctionRegistry;") |
158 c.Append() | 163 c.Append() |
159 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | 164 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
160 c.Append() | 165 c.Append() |
161 c.Append('class GeneratedFunctionRegistry {') | 166 c.Append('class GeneratedFunctionRegistry {') |
162 c.Sblock(' public:') | 167 c.Sblock(' public:') |
163 c.Append('static void RegisterAll(' | 168 c.Append('static void RegisterAll(' |
164 'ExtensionFunctionRegistry* registry);') | 169 'ExtensionFunctionRegistry* registry);') |
165 c.Eblock('};') | 170 c.Eblock('};') |
166 c.Append() | 171 c.Append() |
167 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 172 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
168 return self._bundle._GenerateHeader('generated_api', c) | 173 return self._bundle._GenerateHeader('generated_api', c) |
169 | 174 |
170 | 175 |
171 class _APICCGenerator(object): | 176 class _APICCGenerator(object): |
172 """Generates a code.Code object for the generated API .cc file""" | 177 """Generates a code.Code object for the generated API .cc file""" |
173 | 178 |
174 def __init__(self, cpp_bundle): | 179 def __init__(self, cpp_bundle): |
175 self._bundle = cpp_bundle | 180 self._bundle = cpp_bundle |
176 | 181 |
177 def Generate(self, namespace): | 182 def Generate(self, _): # namespace not relevant, this is a bundle |
178 c = code.Code() | 183 c = code.Code() |
179 c.Append(cpp_util.CHROMIUM_LICENSE) | 184 c.Append(cpp_util.CHROMIUM_LICENSE) |
180 c.Append() | 185 c.Append() |
181 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, | 186 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, |
182 'generated_api.h'))) | 187 'generated_api.h'))) |
183 c.Append() | 188 c.Append() |
184 for namespace in self._bundle._model.namespaces.values(): | 189 for namespace in self._bundle._model.namespaces.values(): |
185 namespace_name = namespace.unix_name.replace("experimental_", "") | 190 namespace_name = namespace.unix_name.replace("experimental_", "") |
186 implementation_header = namespace.compiler_options.get( | 191 implementation_header = namespace.compiler_options.get( |
187 "implemented_in", | 192 "implemented_in", |
(...skipping 27 matching lines...) Expand all Loading... |
215 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 220 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
216 c.Append() | 221 c.Append() |
217 return c | 222 return c |
218 | 223 |
219 | 224 |
220 class _SchemasHGenerator(object): | 225 class _SchemasHGenerator(object): |
221 """Generates a code.Code object for the generated schemas .h file""" | 226 """Generates a code.Code object for the generated schemas .h file""" |
222 def __init__(self, cpp_bundle): | 227 def __init__(self, cpp_bundle): |
223 self._bundle = cpp_bundle | 228 self._bundle = cpp_bundle |
224 | 229 |
225 def Generate(self, namespace): | 230 def Generate(self, _): # namespace not relevant, this is a bundle |
226 c = code.Code() | 231 c = code.Code() |
227 c.Append('#include <map>') | 232 c.Append('#include <map>') |
228 c.Append('#include <string>') | 233 c.Append('#include <string>') |
229 c.Append() | 234 c.Append() |
230 c.Append('#include "base/strings/string_piece.h"') | 235 c.Append('#include "base/strings/string_piece.h"') |
231 c.Append() | 236 c.Append() |
232 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) | 237 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) |
233 c.Append() | 238 c.Append() |
234 c.Append('class GeneratedSchemas {') | 239 c.Append('class GeneratedSchemas {') |
235 c.Sblock(' public:') | 240 c.Sblock(' public:') |
(...skipping 15 matching lines...) Expand all Loading... |
251 lambda m: m.group(0)[1].upper(), | 256 lambda m: m.group(0)[1].upper(), |
252 name.replace('.', '_')) | 257 name.replace('.', '_')) |
253 | 258 |
254 | 259 |
255 class _SchemasCCGenerator(object): | 260 class _SchemasCCGenerator(object): |
256 """Generates a code.Code object for the generated schemas .cc file""" | 261 """Generates a code.Code object for the generated schemas .cc file""" |
257 | 262 |
258 def __init__(self, cpp_bundle): | 263 def __init__(self, cpp_bundle): |
259 self._bundle = cpp_bundle | 264 self._bundle = cpp_bundle |
260 | 265 |
261 def Generate(self, namespace): | 266 def Generate(self, _): # namespace not relevant, this is a bundle |
262 c = code.Code() | 267 c = code.Code() |
263 c.Append(cpp_util.CHROMIUM_LICENSE) | 268 c.Append(cpp_util.CHROMIUM_LICENSE) |
264 c.Append() | 269 c.Append() |
265 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, | 270 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, |
266 'generated_schemas.h'))) | 271 'generated_schemas.h'))) |
267 c.Append() | 272 c.Append() |
268 c.Append('#include "base/lazy_instance.h"') | 273 c.Append('#include "base/lazy_instance.h"') |
269 c.Append() | 274 c.Append() |
270 c.Append('namespace {') | 275 c.Append('namespace {') |
271 for api in self._bundle._api_defs: | 276 for api in self._bundle._api_defs: |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 c.Eblock('}') | 312 c.Eblock('}') |
308 c.Append() | 313 c.Append() |
309 c.Append('// static') | 314 c.Append('// static') |
310 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {') | 315 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {') |
311 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') | 316 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') |
312 c.Eblock('}') | 317 c.Eblock('}') |
313 c.Append() | 318 c.Append() |
314 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 319 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
315 c.Append() | 320 c.Append() |
316 return c | 321 return c |
OLD | NEW |