| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 | 363 |
| 364 def main(): | 364 def main(): |
| 365 config_file, config = read_config() | 365 config_file, config = read_config() |
| 366 | 366 |
| 367 protocol = Protocol() | 367 protocol = Protocol() |
| 368 protocol.json_api = {"domains": []} | 368 protocol.json_api = {"domains": []} |
| 369 protocol.generate_domains = read_protocol_file(config.protocol.path, protoco
l.json_api) | 369 protocol.generate_domains = read_protocol_file(config.protocol.path, protoco
l.json_api) |
| 370 protocol.imported_domains = read_protocol_file(config.imported.path, protoco
l.json_api) if config.imported else [] | 370 protocol.imported_domains = read_protocol_file(config.imported.path, protoco
l.json_api) if config.imported else [] |
| 371 patch_full_qualified_refs(protocol) | 371 patch_full_qualified_refs(protocol) |
| 372 calculate_exports(protocol) | 372 calculate_exports(protocol) |
| 373 create_type_definitions(protocol, config.string.class_name) | 373 create_type_definitions(protocol, "::".join(config.imported.namespace) if co
nfig.imported else "") |
| 374 | 374 |
| 375 if not config.exported: | 375 if not config.exported: |
| 376 for domain_json in protocol.json_api["domains"]: | 376 for domain_json in protocol.json_api["domains"]: |
| 377 if domain_json["has_exports"] and domain_json["domain"] in protocol.
generate_domains: | 377 if domain_json["has_exports"] and domain_json["domain"] in protocol.
generate_domains: |
| 378 sys.stderr.write("Domain %s is exported, but config is missing e
xport entry\n\n" % domain_json["domain"]) | 378 sys.stderr.write("Domain %s is exported, but config is missing e
xport entry\n\n" % domain_json["domain"]) |
| 379 exit(1) | 379 exit(1) |
| 380 | 380 |
| 381 if not os.path.exists(config.protocol.output): | 381 if not os.path.exists(config.protocol.output): |
| 382 os.mkdir(config.protocol.output) | 382 os.mkdir(config.protocol.output) |
| 383 if protocol.json_api["has_exports"] and not os.path.exists(config.exported.o
utput): | 383 if protocol.json_api["has_exports"] and not os.path.exists(config.exported.o
utput): |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 outputs[os.path.join(config.protocol.output, class_name + ".h")] = i
mported_template.render(template_context) | 422 outputs[os.path.join(config.protocol.output, class_name + ".h")] = i
mported_template.render(template_context) |
| 423 | 423 |
| 424 if config.lib: | 424 if config.lib: |
| 425 template_context = { | 425 template_context = { |
| 426 "config": config | 426 "config": config |
| 427 } | 427 } |
| 428 | 428 |
| 429 # Note these should be sorted in the right order. | 429 # Note these should be sorted in the right order. |
| 430 # TODO(dgozman): sort them programmatically based on commented includes. | 430 # TODO(dgozman): sort them programmatically based on commented includes. |
| 431 lib_h_templates = [ | 431 lib_h_templates = [ |
| 432 "Allocator_h.template", | |
| 433 "Platform_h.template", | |
| 434 "Collections_h.template", | 432 "Collections_h.template", |
| 435 "String16_h.template", | |
| 436 "ErrorSupport_h.template", | 433 "ErrorSupport_h.template", |
| 437 "Values_h.template", | 434 "Values_h.template", |
| 438 "Object_h.template", | 435 "Object_h.template", |
| 439 "ValueConversions_h.template", | 436 "ValueConversions_h.template", |
| 440 "Maybe_h.template", | 437 "Maybe_h.template", |
| 441 "Array_h.template", | 438 "Array_h.template", |
| 442 "FrontendChannel_h.template", | |
| 443 "BackendCallback_h.template", | 439 "BackendCallback_h.template", |
| 444 "DispatcherBase_h.template", | 440 "DispatcherBase_h.template", |
| 445 "Parser_h.template", | 441 "Parser_h.template", |
| 446 ] | 442 ] |
| 447 | 443 |
| 448 lib_cpp_templates = [ | 444 lib_cpp_templates = [ |
| 449 "InspectorProtocol_cpp.template", | 445 "Protocol_cpp.template", |
| 450 "String16_cpp.template", | |
| 451 "ErrorSupport_cpp.template", | 446 "ErrorSupport_cpp.template", |
| 452 "Values_cpp.template", | 447 "Values_cpp.template", |
| 453 "Object_cpp.template", | 448 "Object_cpp.template", |
| 454 "DispatcherBase_cpp.template", | 449 "DispatcherBase_cpp.template", |
| 455 "Parser_cpp.template", | 450 "Parser_cpp.template", |
| 456 ] | 451 ] |
| 457 | 452 |
| 453 forward_h_templates = [ |
| 454 "Forward_h.template", |
| 455 "Allocator_h.template", |
| 456 "FrontendChannel_h.template", |
| 457 ] |
| 458 |
| 458 def generate_lib_file(file_name, template_files): | 459 def generate_lib_file(file_name, template_files): |
| 459 parts = [] | 460 parts = [] |
| 460 for template_file in template_files: | 461 for template_file in template_files: |
| 461 inputs.append(os.path.join(templates_dir, template_file)) | 462 inputs.append(os.path.join(templates_dir, template_file)) |
| 462 template = jinja_env.get_template(template_file) | 463 template = jinja_env.get_template(template_file) |
| 463 parts.append(template.render(template_context)) | 464 parts.append(template.render(template_context)) |
| 464 outputs[file_name] = "\n\n".join(parts) | 465 outputs[file_name] = "\n\n".join(parts) |
| 465 | 466 |
| 466 generate_lib_file(os.path.join(config.lib.output, "InspectorProtocol.h")
, lib_h_templates) | 467 generate_lib_file(os.path.join(config.lib.output, "Forward.h"), forward_
h_templates) |
| 467 generate_lib_file(os.path.join(config.lib.output, "InspectorProtocol.cpp
"), lib_cpp_templates) | 468 generate_lib_file(os.path.join(config.lib.output, "Protocol.h"), lib_h_t
emplates) |
| 469 generate_lib_file(os.path.join(config.lib.output, "Protocol.cpp"), lib_c
pp_templates) |
| 468 | 470 |
| 469 # Make gyp / make generatos happy, otherwise make rebuilds world. | 471 # Make gyp / make generatos happy, otherwise make rebuilds world. |
| 470 inputs_ts = max(map(os.path.getmtime, inputs)) | 472 inputs_ts = max(map(os.path.getmtime, inputs)) |
| 471 up_to_date = True | 473 up_to_date = True |
| 472 for output_file in outputs.iterkeys(): | 474 for output_file in outputs.iterkeys(): |
| 473 if not os.path.exists(output_file) or os.path.getmtime(output_file) < in
puts_ts: | 475 if not os.path.exists(output_file) or os.path.getmtime(output_file) < in
puts_ts: |
| 474 up_to_date = False | 476 up_to_date = False |
| 475 break | 477 break |
| 476 if up_to_date: | 478 if up_to_date: |
| 477 sys.exit() | 479 sys.exit() |
| 478 | 480 |
| 479 for file_name, content in outputs.iteritems(): | 481 for file_name, content in outputs.iteritems(): |
| 480 out_file = open(file_name, "w") | 482 out_file = open(file_name, "w") |
| 481 out_file.write(content) | 483 out_file.write(content) |
| 482 out_file.close() | 484 out_file.close() |
| 483 | 485 |
| 484 | 486 |
| 485 main() | 487 main() |
| OLD | NEW |