1#!/usr/bin/env python 2# Copyright 2016 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import os.path 7import sys 8import argparse 9import collections 10import functools 11import re 12import copy 13try: 14 import json 15except ImportError: 16 import simplejson as json 17 18import pdl 19 20try: 21 unicode 22except NameError: 23 # Define unicode for Py3 24 def unicode(s, *_): 25 return s 26 27# Path handling for libraries and templates 28# Paths have to be normalized because Jinja uses the exact template path to 29# determine the hash used in the cache filename, and we need a pre-caching step 30# to be concurrency-safe. Use absolute path because __file__ is absolute if 31# module is imported, and relative if executed directly. 32# If paths differ between pre-caching and individual file compilation, the cache 33# is regenerated, which causes a race condition and breaks concurrent build, 34# since some compile processes will try to read the partially written cache. 35module_path, module_filename = os.path.split(os.path.realpath(__file__)) 36 37def read_config(): 38 # pylint: disable=W0703 39 def json_to_object(data, output_base): 40 def json_object_hook(object_dict): 41 items = [(k, os.path.join(output_base, v) if k == "path" else v) for (k, v) in object_dict.items()] 42 items = [(k, os.path.join(output_base, v) if k == "output" else v) for (k, v) in items] 43 keys, values = list(zip(*items)) 44 # 'async' is a python 3.7 keyword. Don't use namedtuple(rename=True) 45 # because that only renames it in python 3 but not python 2. 46 keys = tuple('async_' if k == 'async' else k for k in keys) 47 return collections.namedtuple('X', keys)(*values) 48 return json.loads(data, object_hook=json_object_hook) 49 50 def init_defaults(config_tuple, path, defaults): 51 keys = list(config_tuple._fields) # pylint: disable=E1101 52 values = [getattr(config_tuple, k) for k in keys] 53 for i in range(len(keys)): 54 if hasattr(values[i], "_fields"): 55 values[i] = init_defaults(values[i], path + "." + keys[i], defaults) 56 for optional in defaults: 57 if optional.find(path + ".") != 0: 58 continue 59 optional_key = optional[len(path) + 1:] 60 if optional_key.find(".") == -1 and optional_key not in keys: 61 keys.append(optional_key) 62 values.append(defaults[optional]) 63 return collections.namedtuple('X', keys)(*values) 64 65 try: 66 cmdline_parser = argparse.ArgumentParser() 67 cmdline_parser.add_argument("--output_base", type=unicode, required=True) 68 cmdline_parser.add_argument("--jinja_dir", type=unicode, required=True) 69 cmdline_parser.add_argument("--config", type=unicode, required=True) 70 cmdline_parser.add_argument("--config_value", default=[], action="append") 71 arg_options = cmdline_parser.parse_args() 72 jinja_dir = arg_options.jinja_dir 73 output_base = arg_options.output_base 74 config_file = arg_options.config 75 config_values = arg_options.config_value 76 except Exception: 77 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.html 78 exc = sys.exc_info()[1] 79 sys.stderr.write("Failed to parse command-line arguments: %s\n\n" % exc) 80 exit(1) 81 82 try: 83 config_json_file = open(config_file, "r") 84 config_json_string = config_json_file.read() 85 config_partial = json_to_object(config_json_string, output_base) 86 config_json_file.close() 87 defaults = { 88 ".use_snake_file_names": False, 89 ".use_title_case_methods": False, 90 ".imported": False, 91 ".imported.export_macro": "", 92 ".imported.export_header": False, 93 ".imported.header": False, 94 ".imported.package": False, 95 ".imported.options": False, 96 ".protocol.export_macro": "", 97 ".protocol.export_header": False, 98 ".protocol.options": False, 99 ".protocol.file_name_prefix": "", 100 ".exported": False, 101 ".exported.export_macro": "", 102 ".exported.export_header": False, 103 ".lib": False, 104 ".lib.export_macro": "", 105 ".lib.export_header": False, 106 } 107 for key_value in config_values: 108 parts = key_value.split("=") 109 if len(parts) == 2: 110 defaults["." + parts[0]] = parts[1] 111 return (jinja_dir, config_file, init_defaults(config_partial, "", defaults)) 112 except Exception: 113 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.html 114 exc = sys.exc_info()[1] 115 sys.stderr.write("Failed to parse config file: %s\n\n" % exc) 116 exit(1) 117 118 119# ---- Begin of utilities exposed to generator ---- 120 121 122def to_title_case(name): 123 return name[:1].upper() + name[1:] 124 125 126def dash_to_camelcase(word): 127 prefix = "" 128 if word[0] == "-": 129 prefix = "Negative" 130 word = word[1:] 131 return prefix + "".join(to_title_case(x) or "-" for x in word.split("-")) 132 133 134def to_snake_case(name): 135 return re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", name, sys.maxsize).lower() 136 137 138def to_method_case(config, name): 139 if config.use_title_case_methods: 140 return to_title_case(name) 141 return name 142 143 144def join_arrays(dict, keys): 145 result = [] 146 for key in keys: 147 if key in dict: 148 result += dict[key] 149 return result 150 151 152def format_include(config, header, file_name=None): 153 if file_name is not None: 154 header = header + "/" + file_name + ".h" 155 header = "\"" + header + "\"" if header[0] not in "<\"" else header 156 if config.use_snake_file_names: 157 header = to_snake_case(header) 158 return header 159 160 161def format_domain_include(config, header, file_name): 162 return format_include(config, header, config.protocol.file_name_prefix + file_name) 163 164 165def to_file_name(config, file_name): 166 if config.use_snake_file_names: 167 return to_snake_case(file_name).replace(".cpp", ".cc") 168 return file_name 169 170 171# ---- End of utilities exposed to generator ---- 172 173 174def initialize_jinja_env(jinja_dir, cache_dir, config): 175 # pylint: disable=F0401 176 sys.path.insert(1, os.path.abspath(jinja_dir)) 177 import jinja2 178 179 jinja_env = jinja2.Environment( 180 loader=jinja2.FileSystemLoader(module_path), 181 # Bytecode cache is not concurrency-safe unless pre-cached: 182 # if pre-cached this is read-only, but writing creates a race condition. 183 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), 184 keep_trailing_newline=True, # newline-terminate generated files 185 lstrip_blocks=True, # so can indent control flow tags 186 trim_blocks=True) 187 jinja_env.filters.update({"to_title_case": to_title_case, "dash_to_camelcase": dash_to_camelcase, "to_method_case": functools.partial(to_method_case, config)}) 188 jinja_env.add_extension("jinja2.ext.loopcontrols") 189 return jinja_env 190 191 192def create_imported_type_definition(domain_name, type, imported_namespace): 193 # pylint: disable=W0622 194 return { 195 "return_type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, domain_name, type["id"]), 196 "pass_type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, domain_name, type["id"]), 197 "to_raw_type": "%s.get()", 198 "to_pass_type": "std::move(%s)", 199 "to_rvalue": "std::move(%s)", 200 "type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, domain_name, type["id"]), 201 "raw_type": "%s::%s::API::%s" % (imported_namespace, domain_name, type["id"]), 202 "raw_pass_type": "%s::%s::API::%s*" % (imported_namespace, domain_name, type["id"]), 203 "raw_return_type": "%s::%s::API::%s*" % (imported_namespace, domain_name, type["id"]), 204 } 205 206 207def create_user_type_definition(domain_name, type): 208 # pylint: disable=W0622 209 return { 210 "return_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type["id"]), 211 "pass_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type["id"]), 212 "to_raw_type": "%s.get()", 213 "to_pass_type": "std::move(%s)", 214 "to_rvalue": "std::move(%s)", 215 "type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type["id"]), 216 "raw_type": "protocol::%s::%s" % (domain_name, type["id"]), 217 "raw_pass_type": "protocol::%s::%s*" % (domain_name, type["id"]), 218 "raw_return_type": "protocol::%s::%s*" % (domain_name, type["id"]), 219 } 220 221 222def create_object_type_definition(): 223 # pylint: disable=W0622 224 return { 225 "return_type": "std::unique_ptr<protocol::DictionaryValue>", 226 "pass_type": "std::unique_ptr<protocol::DictionaryValue>", 227 "to_raw_type": "%s.get()", 228 "to_pass_type": "std::move(%s)", 229 "to_rvalue": "std::move(%s)", 230 "type": "std::unique_ptr<protocol::DictionaryValue>", 231 "raw_type": "protocol::DictionaryValue", 232 "raw_pass_type": "protocol::DictionaryValue*", 233 "raw_return_type": "protocol::DictionaryValue*", 234 } 235 236 237def create_any_type_definition(): 238 # pylint: disable=W0622 239 return { 240 "return_type": "std::unique_ptr<protocol::Value>", 241 "pass_type": "std::unique_ptr<protocol::Value>", 242 "to_raw_type": "%s.get()", 243 "to_pass_type": "std::move(%s)", 244 "to_rvalue": "std::move(%s)", 245 "type": "std::unique_ptr<protocol::Value>", 246 "raw_type": "protocol::Value", 247 "raw_pass_type": "protocol::Value*", 248 "raw_return_type": "protocol::Value*", 249 } 250 251 252def create_string_type_definition(): 253 # pylint: disable=W0622 254 return { 255 "return_type": "String", 256 "pass_type": "const String&", 257 "to_pass_type": "%s", 258 "to_raw_type": "%s", 259 "to_rvalue": "%s", 260 "type": "String", 261 "raw_type": "String", 262 "raw_pass_type": "const String&", 263 "raw_return_type": "String", 264 } 265 266 267def create_binary_type_definition(): 268 # pylint: disable=W0622 269 return { 270 "return_type": "Binary", 271 "pass_type": "const Binary&", 272 "to_pass_type": "%s", 273 "to_raw_type": "%s", 274 "to_rvalue": "%s", 275 "type": "Binary", 276 "raw_type": "Binary", 277 "raw_pass_type": "const Binary&", 278 "raw_return_type": "Binary", 279 } 280 281 282def create_primitive_type_definition(type): 283 # pylint: disable=W0622 284 typedefs = { 285 "number": "double", 286 "integer": "int", 287 "boolean": "bool" 288 } 289 defaults = { 290 "number": "0", 291 "integer": "0", 292 "boolean": "false" 293 } 294 jsontypes = { 295 "number": "TypeDouble", 296 "integer": "TypeInteger", 297 "boolean": "TypeBoolean", 298 } 299 return { 300 "return_type": typedefs[type], 301 "pass_type": typedefs[type], 302 "to_pass_type": "%s", 303 "to_raw_type": "%s", 304 "to_rvalue": "%s", 305 "type": typedefs[type], 306 "raw_type": typedefs[type], 307 "raw_pass_type": typedefs[type], 308 "raw_return_type": typedefs[type], 309 "default_value": defaults[type] 310 } 311 312 313def wrap_array_definition(type): 314 # pylint: disable=W0622 315 return { 316 "return_type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"], 317 "pass_type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"], 318 "to_raw_type": "%s.get()", 319 "to_pass_type": "std::move(%s)", 320 "to_rvalue": "std::move(%s)", 321 "type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"], 322 "raw_type": "protocol::Array<%s>" % type["raw_type"], 323 "raw_pass_type": "protocol::Array<%s>*" % type["raw_type"], 324 "raw_return_type": "protocol::Array<%s>*" % type["raw_type"], 325 "out_type": "protocol::Array<%s>&" % type["raw_type"], 326 } 327 328 329class Protocol(object): 330 def __init__(self, config): 331 self.config = config 332 self.json_api = {"domains": []} 333 self.imported_domains = [] 334 self.exported_domains = [] 335 self.generate_domains = self.read_protocol_file(config.protocol.path) 336 337 if config.protocol.options: 338 self.generate_domains = [rule.domain for rule in config.protocol.options] 339 self.exported_domains = [rule.domain for rule in config.protocol.options if hasattr(rule, "exported")] 340 341 if config.imported: 342 self.imported_domains = self.read_protocol_file(config.imported.path) 343 if config.imported.options: 344 self.imported_domains = [rule.domain for rule in config.imported.options] 345 346 self.patch_full_qualified_refs() 347 self.create_notification_types() 348 self.create_type_definitions() 349 self.generate_used_types() 350 351 352 def read_protocol_file(self, file_name): 353 input_file = open(file_name, "r") 354 parsed_json = pdl.loads(input_file.read(), file_name) 355 input_file.close() 356 version = parsed_json["version"]["major"] + "." + parsed_json["version"]["minor"] 357 domains = [] 358 for domain in parsed_json["domains"]: 359 domains.append(domain["domain"]) 360 domain["version"] = version 361 self.json_api["domains"] += parsed_json["domains"] 362 return domains 363 364 365 def patch_full_qualified_refs(self): 366 def patch_full_qualified_refs_in_domain(json, domain_name): 367 if isinstance(json, list): 368 for item in json: 369 patch_full_qualified_refs_in_domain(item, domain_name) 370 if not isinstance(json, dict): 371 return 372 for key in json: 373 if key == "type" and json[key] == "string": 374 json[key] = domain_name + ".string" 375 if key != "$ref": 376 patch_full_qualified_refs_in_domain(json[key], domain_name) 377 continue 378 if json["$ref"].find(".") == -1: 379 json["$ref"] = domain_name + "." + json["$ref"] 380 return 381 382 for domain in self.json_api["domains"]: 383 patch_full_qualified_refs_in_domain(domain, domain["domain"]) 384 385 386 def all_references(self, json): 387 refs = set() 388 if isinstance(json, list): 389 for item in json: 390 refs |= self.all_references(item) 391 if not isinstance(json, dict): 392 return refs 393 for key in json: 394 if key != "$ref": 395 refs |= self.all_references(json[key]) 396 else: 397 refs.add(json["$ref"]) 398 return refs 399 400 def generate_used_types(self): 401 all_refs = set() 402 for domain in self.json_api["domains"]: 403 domain_name = domain["domain"] 404 if "commands" in domain: 405 for command in domain["commands"]: 406 if self.generate_command(domain_name, command["name"]): 407 all_refs |= self.all_references(command) 408 if "events" in domain: 409 for event in domain["events"]: 410 if self.generate_event(domain_name, event["name"]): 411 all_refs |= self.all_references(event) 412 all_refs.add(domain_name + "." + to_title_case(event["name"]) + "Notification") 413 414 dependencies = self.generate_type_dependencies() 415 queue = set(all_refs) 416 while len(queue): 417 ref = queue.pop() 418 if ref in dependencies: 419 queue |= dependencies[ref] - all_refs 420 all_refs |= dependencies[ref] 421 self.used_types = all_refs 422 423 424 def generate_type_dependencies(self): 425 dependencies = dict() 426 domains_with_types = (x for x in self.json_api["domains"] if "types" in x) 427 for domain in domains_with_types: 428 domain_name = domain["domain"] 429 for type in domain["types"]: 430 related_types = self.all_references(type) 431 if len(related_types): 432 dependencies[domain_name + "." + type["id"]] = related_types 433 return dependencies 434 435 436 def create_notification_types(self): 437 for domain in self.json_api["domains"]: 438 if "events" in domain: 439 for event in domain["events"]: 440 event_type = dict() 441 event_type["description"] = "Wrapper for notification params" 442 event_type["type"] = "object" 443 event_type["id"] = to_title_case(event["name"]) + "Notification" 444 if "parameters" in event: 445 event_type["properties"] = copy.deepcopy(event["parameters"]) 446 if "types" not in domain: 447 domain["types"] = list() 448 domain["types"].append(event_type) 449 450 451 def create_type_definitions(self): 452 imported_namespace = "::".join(self.config.imported.namespace) if self.config.imported else "" 453 self.type_definitions = {} 454 self.type_definitions["number"] = create_primitive_type_definition("number") 455 self.type_definitions["integer"] = create_primitive_type_definition("integer") 456 self.type_definitions["boolean"] = create_primitive_type_definition("boolean") 457 self.type_definitions["object"] = create_object_type_definition() 458 self.type_definitions["any"] = create_any_type_definition() 459 self.type_definitions["binary"] = create_binary_type_definition() 460 for domain in self.json_api["domains"]: 461 self.type_definitions[domain["domain"] + ".string"] = create_string_type_definition() 462 self.type_definitions[domain["domain"] + ".binary"] = create_binary_type_definition() 463 if not ("types" in domain): 464 continue 465 for type in domain["types"]: 466 type_name = domain["domain"] + "." + type["id"] 467 if type["type"] == "object" and domain["domain"] in self.imported_domains: 468 self.type_definitions[type_name] = create_imported_type_definition(domain["domain"], type, imported_namespace) 469 elif type["type"] == "object": 470 self.type_definitions[type_name] = create_user_type_definition(domain["domain"], type) 471 elif type["type"] == "array": 472 self.type_definitions[type_name] = self.resolve_type(type) 473 elif type["type"] == domain["domain"] + ".string": 474 self.type_definitions[type_name] = create_string_type_definition() 475 elif type["type"] == domain["domain"] + ".binary": 476 self.type_definitions[type_name] = create_binary_type_definition() 477 else: 478 self.type_definitions[type_name] = create_primitive_type_definition(type["type"]) 479 480 481 def check_options(self, options, domain, name, include_attr, exclude_attr, default): 482 for rule in options: 483 if rule.domain != domain: 484 continue 485 if include_attr and hasattr(rule, include_attr): 486 return name in getattr(rule, include_attr) 487 if exclude_attr and hasattr(rule, exclude_attr): 488 return name not in getattr(rule, exclude_attr) 489 return default 490 return False 491 492 493 # ---- Begin of methods exposed to generator 494 495 496 def type_definition(self, name): 497 return self.type_definitions[name] 498 499 500 def resolve_type(self, prop): 501 if "$ref" in prop: 502 return self.type_definitions[prop["$ref"]] 503 if prop["type"] == "array": 504 return wrap_array_definition(self.resolve_type(prop["items"])) 505 return self.type_definitions[prop["type"]] 506 507 508 def generate_command(self, domain, command): 509 if not self.config.protocol.options: 510 return domain in self.generate_domains 511 return self.check_options(self.config.protocol.options, domain, command, "include", "exclude", True) 512 513 514 def generate_event(self, domain, event): 515 if not self.config.protocol.options: 516 return domain in self.generate_domains 517 return self.check_options(self.config.protocol.options, domain, event, "include_events", "exclude_events", True) 518 519 520 def generate_type(self, domain, typename): 521 return domain + "." + typename in self.used_types 522 523 524 def is_async_command(self, domain, command): 525 if not self.config.protocol.options: 526 return False 527 return self.check_options(self.config.protocol.options, domain, command, "async_", None, False) 528 529 530 def is_exported(self, domain, name): 531 if not self.config.protocol.options: 532 return False 533 return self.check_options(self.config.protocol.options, domain, name, "exported", None, False) 534 535 536 def is_imported(self, domain, name): 537 if not self.config.imported: 538 return False 539 if not self.config.imported.options: 540 return domain in self.imported_domains 541 return self.check_options(self.config.imported.options, domain, name, "imported", None, False) 542 543 544 def is_exported_domain(self, domain): 545 return domain in self.exported_domains 546 547 548 def generate_disable(self, domain): 549 if "commands" not in domain: 550 return True 551 for command in domain["commands"]: 552 if command["name"] == "disable" and self.generate_command(domain["domain"], "disable"): 553 return False 554 return True 555 556 557 def is_imported_dependency(self, domain): 558 return domain in self.generate_domains or domain in self.imported_domains 559 560 561def main(): 562 jinja_dir, config_file, config = read_config() 563 564 protocol = Protocol(config) 565 566 if not config.exported and len(protocol.exported_domains): 567 sys.stderr.write("Domains [%s] are exported, but config is missing export entry\n\n" % ", ".join(protocol.exported_domains)) 568 exit(1) 569 570 if not os.path.exists(config.protocol.output): 571 os.mkdir(config.protocol.output) 572 if len(protocol.exported_domains) and not os.path.exists(config.exported.output): 573 os.mkdir(config.exported.output) 574 jinja_env = initialize_jinja_env(jinja_dir, config.protocol.output, config) 575 576 inputs = [] 577 inputs.append(__file__) 578 inputs.append(config_file) 579 inputs.append(config.protocol.path) 580 if config.imported: 581 inputs.append(config.imported.path) 582 templates_dir = os.path.join(module_path, "templates") 583 inputs.append(os.path.join(templates_dir, "TypeBuilder_h.template")) 584 inputs.append(os.path.join(templates_dir, "TypeBuilder_cpp.template")) 585 inputs.append(os.path.join(templates_dir, "Exported_h.template")) 586 inputs.append(os.path.join(templates_dir, "Imported_h.template")) 587 588 h_template = jinja_env.get_template("templates/TypeBuilder_h.template") 589 cpp_template = jinja_env.get_template("templates/TypeBuilder_cpp.template") 590 exported_template = jinja_env.get_template("templates/Exported_h.template") 591 imported_template = jinja_env.get_template("templates/Imported_h.template") 592 593 outputs = dict() 594 595 for domain in protocol.json_api["domains"]: 596 class_name = domain["domain"] 597 file_name = config.protocol.file_name_prefix + class_name 598 template_context = { 599 "protocol": protocol, 600 "config": config, 601 "domain": domain, 602 "join_arrays": join_arrays, 603 "format_include": functools.partial(format_include, config), 604 "format_domain_include": functools.partial(format_domain_include, config), 605 } 606 607 if domain["domain"] in protocol.generate_domains: 608 outputs[os.path.join(config.protocol.output, to_file_name(config, file_name + ".h"))] = h_template.render(template_context) 609 outputs[os.path.join(config.protocol.output, to_file_name(config, file_name + ".cpp"))] = cpp_template.render(template_context) 610 if domain["domain"] in protocol.exported_domains: 611 outputs[os.path.join(config.exported.output, to_file_name(config, file_name + ".h"))] = exported_template.render(template_context) 612 if domain["domain"] in protocol.imported_domains: 613 outputs[os.path.join(config.protocol.output, to_file_name(config, file_name + ".h"))] = imported_template.render(template_context) 614 615 if config.lib: 616 template_context = { 617 "config": config, 618 "format_include": functools.partial(format_include, config), 619 } 620 621 lib_templates_dir = os.path.join(module_path, "lib") 622 # Note these should be sorted in the right order. 623 # TODO(dgozman): sort them programmatically based on commented includes. 624 protocol_h_templates = [ 625 "ErrorSupport_h.template", 626 "Values_h.template", 627 "Object_h.template", 628 "ValueConversions_h.template", 629 "Maybe_h.template", 630 "Array_h.template", 631 "DispatcherBase_h.template", 632 "Parser_h.template", 633 "encoding_h.template", 634 ] 635 636 protocol_cpp_templates = [ 637 "Protocol_cpp.template", 638 "ErrorSupport_cpp.template", 639 "Values_cpp.template", 640 "Object_cpp.template", 641 "DispatcherBase_cpp.template", 642 "Parser_cpp.template", 643 "encoding_cpp.template", 644 ] 645 646 forward_h_templates = [ 647 "Forward_h.template", 648 "Allocator_h.template", 649 "FrontendChannel_h.template", 650 ] 651 652 base_string_adapter_h_templates = [ 653 "base_string_adapter_h.template", 654 ] 655 656 base_string_adapter_cc_templates = [ 657 "base_string_adapter_cc.template", 658 ] 659 660 def generate_lib_file(file_name, template_files): 661 parts = [] 662 for template_file in template_files: 663 inputs.append(os.path.join(lib_templates_dir, template_file)) 664 template = jinja_env.get_template("lib/" + template_file) 665 parts.append(template.render(template_context)) 666 outputs[file_name] = "\n\n".join(parts) 667 668 generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "Forward.h")), forward_h_templates) 669 generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "Protocol.h")), protocol_h_templates) 670 generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "Protocol.cpp")), protocol_cpp_templates) 671 generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "base_string_adapter.h")), base_string_adapter_h_templates) 672 generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "base_string_adapter.cc")), base_string_adapter_cc_templates) 673 674 # Make gyp / make generatos happy, otherwise make rebuilds world. 675 inputs_ts = max(map(os.path.getmtime, inputs)) 676 up_to_date = True 677 for output_file in outputs.keys(): 678 if not os.path.exists(output_file) or os.path.getmtime(output_file) < inputs_ts: 679 up_to_date = False 680 break 681 if up_to_date: 682 sys.exit() 683 684 for file_name, content in outputs.items(): 685 out_file = open(file_name, "w") 686 out_file.write(content) 687 out_file.close() 688 689 690main() 691