OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 os.path | 5 import os.path |
6 import sys | 6 import sys |
7 import optparse | 7 import optparse |
8 import collections | 8 import collections |
9 import functools | 9 import functools |
10 try: | 10 try: |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 has_exports = calculate_exports_in_json(json_value[key]) or has_
exports | 157 has_exports = calculate_exports_in_json(json_value[key]) or has_
exports |
158 return has_exports | 158 return has_exports |
159 | 159 |
160 protocol.json_api["has_exports"] = False | 160 protocol.json_api["has_exports"] = False |
161 for domain_json in protocol.json_api["domains"]: | 161 for domain_json in protocol.json_api["domains"]: |
162 domain_json["has_exports"] = calculate_exports_in_json(domain_json) | 162 domain_json["has_exports"] = calculate_exports_in_json(domain_json) |
163 if domain_json["has_exports"] and domain_json["domain"] in protocol.gene
rate_domains: | 163 if domain_json["has_exports"] and domain_json["domain"] in protocol.gene
rate_domains: |
164 protocol.json_api["has_exports"] = True | 164 protocol.json_api["has_exports"] = True |
165 | 165 |
166 | 166 |
167 def create_imported_type_definition(domain_name, type): | 167 def create_imported_type_definition(domain_name, type, imported_namespace): |
168 # pylint: disable=W0622 | 168 # pylint: disable=W0622 |
169 return { | 169 return { |
170 "return_type": "std::unique_ptr<protocol::%s::API::%s>" % (domain_name,
type["id"]), | 170 "return_type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace,
domain_name, type["id"]), |
171 "pass_type": "std::unique_ptr<protocol::%s::API::%s>" % (domain_name, ty
pe["id"]), | 171 "pass_type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, d
omain_name, type["id"]), |
172 "to_raw_type": "%s.get()", | 172 "to_raw_type": "%s.get()", |
173 "to_pass_type": "std::move(%s)", | 173 "to_pass_type": "std::move(%s)", |
174 "to_rvalue": "std::move(%s)", | 174 "to_rvalue": "std::move(%s)", |
175 "type": "std::unique_ptr<protocol::%s::API::%s>" % (domain_name, type["i
d"]), | 175 "type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, domain
_name, type["id"]), |
176 "raw_type": "protocol::%s::API::%s" % (domain_name, type["id"]), | 176 "raw_type": "%s::%s::API::%s" % (imported_namespace, domain_name, type["
id"]), |
177 "raw_pass_type": "protocol::%s::API::%s*" % (domain_name, type["id"]), | 177 "raw_pass_type": "%s::%s::API::%s*" % (imported_namespace, domain_name,
type["id"]), |
178 "raw_return_type": "protocol::%s::API::%s*" % (domain_name, type["id"]), | 178 "raw_return_type": "%s::%s::API::%s*" % (imported_namespace, domain_name
, type["id"]), |
179 } | 179 } |
180 | 180 |
181 | 181 |
182 def create_user_type_definition(domain_name, type): | 182 def create_user_type_definition(domain_name, type): |
183 # pylint: disable=W0622 | 183 # pylint: disable=W0622 |
184 return { | 184 return { |
185 "return_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type[
"id"]), | 185 "return_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type[
"id"]), |
186 "pass_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type["i
d"]), | 186 "pass_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type["i
d"]), |
187 "to_raw_type": "%s.get()", | 187 "to_raw_type": "%s.get()", |
188 "to_pass_type": "std::move(%s)", | 188 "to_pass_type": "std::move(%s)", |
(...skipping 28 matching lines...) Expand all Loading... |
217 "to_raw_type": "%s.get()", | 217 "to_raw_type": "%s.get()", |
218 "to_pass_type": "std::move(%s)", | 218 "to_pass_type": "std::move(%s)", |
219 "to_rvalue": "std::move(%s)", | 219 "to_rvalue": "std::move(%s)", |
220 "type": "std::unique_ptr<protocol::Value>", | 220 "type": "std::unique_ptr<protocol::Value>", |
221 "raw_type": "protocol::Value", | 221 "raw_type": "protocol::Value", |
222 "raw_pass_type": "protocol::Value*", | 222 "raw_pass_type": "protocol::Value*", |
223 "raw_return_type": "protocol::Value*", | 223 "raw_return_type": "protocol::Value*", |
224 } | 224 } |
225 | 225 |
226 | 226 |
227 def create_string_type_definition(string_type): | 227 def create_string_type_definition(): |
228 # pylint: disable=W0622 | 228 # pylint: disable=W0622 |
229 return { | 229 return { |
230 "return_type": string_type, | 230 "return_type": "String", |
231 "pass_type": ("const %s&" % string_type), | 231 "pass_type": "const String&", |
232 "to_pass_type": "%s", | 232 "to_pass_type": "%s", |
233 "to_raw_type": "%s", | 233 "to_raw_type": "%s", |
234 "to_rvalue": "%s", | 234 "to_rvalue": "%s", |
235 "type": string_type, | 235 "type": "String", |
236 "raw_type": string_type, | 236 "raw_type": "String", |
237 "raw_pass_type": ("const %s&" % string_type), | 237 "raw_pass_type": "const String&", |
238 "raw_return_type": string_type, | 238 "raw_return_type": "String", |
239 } | 239 } |
240 | 240 |
241 | 241 |
242 def create_primitive_type_definition(type): | 242 def create_primitive_type_definition(type): |
243 # pylint: disable=W0622 | 243 # pylint: disable=W0622 |
244 typedefs = { | 244 typedefs = { |
245 "number": "double", | 245 "number": "double", |
246 "integer": "int", | 246 "integer": "int", |
247 "boolean": "bool" | 247 "boolean": "bool" |
248 } | 248 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 "to_rvalue": "std::move(%s)", | 280 "to_rvalue": "std::move(%s)", |
281 "type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"], | 281 "type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"], |
282 "raw_type": "protocol::Array<%s>" % type["raw_type"], | 282 "raw_type": "protocol::Array<%s>" % type["raw_type"], |
283 "raw_pass_type": "protocol::Array<%s>*" % type["raw_type"], | 283 "raw_pass_type": "protocol::Array<%s>*" % type["raw_type"], |
284 "raw_return_type": "protocol::Array<%s>*" % type["raw_type"], | 284 "raw_return_type": "protocol::Array<%s>*" % type["raw_type"], |
285 "create_type": "wrapUnique(new protocol::Array<%s>())" % type["raw_type"
], | 285 "create_type": "wrapUnique(new protocol::Array<%s>())" % type["raw_type"
], |
286 "out_type": "protocol::Array<%s>&" % type["raw_type"], | 286 "out_type": "protocol::Array<%s>&" % type["raw_type"], |
287 } | 287 } |
288 | 288 |
289 | 289 |
290 def create_type_definitions(protocol, string_type): | 290 def create_type_definitions(protocol, imported_namespace): |
291 protocol.type_definitions = {} | 291 protocol.type_definitions = {} |
292 protocol.type_definitions["number"] = create_primitive_type_definition("numb
er") | 292 protocol.type_definitions["number"] = create_primitive_type_definition("numb
er") |
293 protocol.type_definitions["integer"] = create_primitive_type_definition("int
eger") | 293 protocol.type_definitions["integer"] = create_primitive_type_definition("int
eger") |
294 protocol.type_definitions["boolean"] = create_primitive_type_definition("boo
lean") | 294 protocol.type_definitions["boolean"] = create_primitive_type_definition("boo
lean") |
295 protocol.type_definitions["object"] = create_object_type_definition() | 295 protocol.type_definitions["object"] = create_object_type_definition() |
296 protocol.type_definitions["any"] = create_any_type_definition() | 296 protocol.type_definitions["any"] = create_any_type_definition() |
297 for domain in protocol.json_api["domains"]: | 297 for domain in protocol.json_api["domains"]: |
298 protocol.type_definitions[domain["domain"] + ".string"] = create_string_
type_definition(string_type) | 298 protocol.type_definitions[domain["domain"] + ".string"] = create_string_
type_definition() |
299 if not ("types" in domain): | 299 if not ("types" in domain): |
300 continue | 300 continue |
301 for type in domain["types"]: | 301 for type in domain["types"]: |
302 type_name = domain["domain"] + "." + type["id"] | 302 type_name = domain["domain"] + "." + type["id"] |
303 if type["type"] == "object" and domain["domain"] in protocol.importe
d_domains: | 303 if type["type"] == "object" and domain["domain"] in protocol.importe
d_domains: |
304 protocol.type_definitions[type_name] = create_imported_type_defi
nition(domain["domain"], type) | 304 protocol.type_definitions[type_name] = create_imported_type_defi
nition(domain["domain"], type, imported_namespace) |
305 elif type["type"] == "object": | 305 elif type["type"] == "object": |
306 protocol.type_definitions[type_name] = create_user_type_definiti
on(domain["domain"], type) | 306 protocol.type_definitions[type_name] = create_user_type_definiti
on(domain["domain"], type) |
307 elif type["type"] == "array": | 307 elif type["type"] == "array": |
308 items_type = type["items"]["type"] | 308 items_type = type["items"]["type"] |
309 protocol.type_definitions[type_name] = wrap_array_definition(pro
tocol.type_definitions[items_type]) | 309 protocol.type_definitions[type_name] = wrap_array_definition(pro
tocol.type_definitions[items_type]) |
310 elif type["type"] == domain["domain"] + ".string": | 310 elif type["type"] == domain["domain"] + ".string": |
311 protocol.type_definitions[type_name] = create_string_type_defini
tion(string_type) | 311 protocol.type_definitions[type_name] = create_string_type_defini
tion() |
312 else: | 312 else: |
313 protocol.type_definitions[type_name] = create_primitive_type_def
inition(type["type"]) | 313 protocol.type_definitions[type_name] = create_primitive_type_def
inition(type["type"]) |
314 | 314 |
315 | 315 |
316 def type_definition(protocol, name): | 316 def type_definition(protocol, name): |
317 return protocol.type_definitions[name] | 317 return protocol.type_definitions[name] |
318 | 318 |
319 | 319 |
320 def resolve_type(protocol, prop): | 320 def resolve_type(protocol, prop): |
321 if "$ref" in prop: | 321 if "$ref" in prop: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 | 361 |
362 def main(): | 362 def main(): |
363 config_file, config = read_config() | 363 config_file, config = read_config() |
364 | 364 |
365 protocol = Protocol() | 365 protocol = Protocol() |
366 protocol.json_api = {"domains": []} | 366 protocol.json_api = {"domains": []} |
367 protocol.generate_domains = read_protocol_file(config.protocol.path, protoco
l.json_api) | 367 protocol.generate_domains = read_protocol_file(config.protocol.path, protoco
l.json_api) |
368 protocol.imported_domains = read_protocol_file(config.imported.path, protoco
l.json_api) if config.imported else [] | 368 protocol.imported_domains = read_protocol_file(config.imported.path, protoco
l.json_api) if config.imported else [] |
369 patch_full_qualified_refs(protocol) | 369 patch_full_qualified_refs(protocol) |
370 calculate_exports(protocol) | 370 calculate_exports(protocol) |
371 create_type_definitions(protocol, config.string.class_name) | 371 create_type_definitions(protocol, "::".join(config.imported.namespace) if co
nfig.imported else "") |
372 | 372 |
373 if not config.exported: | 373 if not config.exported: |
374 for domain_json in protocol.json_api["domains"]: | 374 for domain_json in protocol.json_api["domains"]: |
375 if domain_json["has_exports"] and domain_json["domain"] in protocol.
generate_domains: | 375 if domain_json["has_exports"] and domain_json["domain"] in protocol.
generate_domains: |
376 sys.stderr.write("Domain %s is exported, but config is missing e
xport entry\n\n" % domain_json["domain"]) | 376 sys.stderr.write("Domain %s is exported, but config is missing e
xport entry\n\n" % domain_json["domain"]) |
377 exit(1) | 377 exit(1) |
378 | 378 |
379 if not os.path.exists(config.protocol.output): | 379 if not os.path.exists(config.protocol.output): |
380 os.mkdir(config.protocol.output) | 380 os.mkdir(config.protocol.output) |
381 if protocol.json_api["has_exports"] and not os.path.exists(config.exported.o
utput): | 381 if protocol.json_api["has_exports"] and not os.path.exists(config.exported.o
utput): |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 template_context = { | 423 template_context = { |
424 "config": config | 424 "config": config |
425 } | 425 } |
426 | 426 |
427 # Note these should be sorted in the right order. | 427 # Note these should be sorted in the right order. |
428 # TODO(dgozman): sort them programmatically based on commented includes. | 428 # TODO(dgozman): sort them programmatically based on commented includes. |
429 lib_h_templates = [ | 429 lib_h_templates = [ |
430 "Allocator_h.template", | 430 "Allocator_h.template", |
431 "Platform_h.template", | 431 "Platform_h.template", |
432 "Collections_h.template", | 432 "Collections_h.template", |
433 "String16_h.template", | |
434 "ErrorSupport_h.template", | 433 "ErrorSupport_h.template", |
435 "Values_h.template", | 434 "Values_h.template", |
436 "Object_h.template", | 435 "Object_h.template", |
437 "ValueConversions_h.template", | 436 "ValueConversions_h.template", |
438 "Maybe_h.template", | 437 "Maybe_h.template", |
439 "Array_h.template", | 438 "Array_h.template", |
440 "FrontendChannel_h.template", | 439 "FrontendChannel_h.template", |
441 "BackendCallback_h.template", | 440 "BackendCallback_h.template", |
442 "DispatcherBase_h.template", | 441 "DispatcherBase_h.template", |
443 "Parser_h.template", | 442 "Parser_h.template", |
444 ] | 443 ] |
445 | 444 |
446 lib_cpp_templates = [ | 445 lib_cpp_templates = [ |
447 "InspectorProtocol_cpp.template", | 446 "InspectorProtocol_cpp.template", |
448 "String16_cpp.template", | |
449 "ErrorSupport_cpp.template", | 447 "ErrorSupport_cpp.template", |
450 "Values_cpp.template", | 448 "Values_cpp.template", |
451 "Object_cpp.template", | 449 "Object_cpp.template", |
452 "DispatcherBase_cpp.template", | 450 "DispatcherBase_cpp.template", |
453 "Parser_cpp.template", | 451 "Parser_cpp.template", |
454 ] | 452 ] |
455 | 453 |
456 def generate_lib_file(file_name, template_files): | 454 def generate_lib_file(file_name, template_files): |
457 parts = [] | 455 parts = [] |
458 for template_file in template_files: | 456 for template_file in template_files: |
(...skipping 15 matching lines...) Expand all Loading... |
474 if up_to_date: | 472 if up_to_date: |
475 sys.exit() | 473 sys.exit() |
476 | 474 |
477 for file_name, content in outputs.iteritems(): | 475 for file_name, content in outputs.iteritems(): |
478 out_file = open(file_name, "w") | 476 out_file = open(file_name, "w") |
479 out_file.write(content) | 477 out_file.write(content) |
480 out_file.close() | 478 out_file.close() |
481 | 479 |
482 | 480 |
483 main() | 481 main() |
OLD | NEW |