| OLD | NEW |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 loader=jinja2.FileSystemLoader(templates_dir), | 392 loader=jinja2.FileSystemLoader(templates_dir), |
| 393 # Bytecode cache is not concurrency-safe unless pre-cached: | 393 # Bytecode cache is not concurrency-safe unless pre-cached: |
| 394 # if pre-cached this is read-only, but writing creates a race condition. | 394 # if pre-cached this is read-only, but writing creates a race condition. |
| 395 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), | 395 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), |
| 396 keep_trailing_newline=True, # newline-terminate generated files | 396 keep_trailing_newline=True, # newline-terminate generated files |
| 397 lstrip_blocks=True, # so can indent control flow tags | 397 lstrip_blocks=True, # so can indent control flow tags |
| 398 trim_blocks=True) | 398 trim_blocks=True) |
| 399 jinja_env.filters.update({ | 399 jinja_env.filters.update({ |
| 400 'blink_capitalize': capitalize, | 400 'blink_capitalize': capitalize, |
| 401 'conditional': conditional_if_endif, | 401 'conditional': conditional_if_endif, |
| 402 'experimental_framework_runtime_enabled': experimental_framework_runtime
_enabled_if, |
| 402 'exposed': exposed_if, | 403 'exposed': exposed_if, |
| 403 'runtime_enabled': runtime_enabled_if, | 404 'runtime_enabled': runtime_enabled_if, |
| 404 }) | 405 }) |
| 405 return jinja_env | 406 return jinja_env |
| 406 | 407 |
| 407 | 408 |
| 408 def generate_indented_conditional(code, conditional): | 409 def generate_indented_conditional(code, conditional): |
| 409 # Indent if statement to level of original code | 410 # Indent if statement to level of original code |
| 410 indent = re.match(' *', code).group(0) | 411 indent = re.match(' *', code).group(0) |
| 411 return ('%sif (%s) {\n' % (indent, conditional) + | 412 return ('%sif (%s) {\n' % (indent, conditional) + |
| 412 ' %s\n' % '\n '.join(code.splitlines()) + | 413 ' %s\n' % '\n '.join(code.splitlines()) + |
| 413 '%s}\n' % indent) | 414 '%s}\n' % indent) |
| 414 | 415 |
| 415 | 416 |
| 416 # [Conditional] | 417 # [Conditional] |
| 417 def conditional_if_endif(code, conditional_string): | 418 def conditional_if_endif(code, conditional_string): |
| 418 # Jinja2 filter to generate if/endif directive blocks | 419 # Jinja2 filter to generate if/endif directive blocks |
| 419 if not conditional_string: | 420 if not conditional_string: |
| 420 return code | 421 return code |
| 421 return ('#if %s\n' % conditional_string + | 422 return ('#if %s\n' % conditional_string + |
| 422 code + | 423 code + |
| 423 '#endif // %s\n' % conditional_string) | 424 '#endif // %s\n' % conditional_string) |
| 424 | 425 |
| 425 | 426 |
| 427 def maybe_add_conditional(code, test, conditional): |
| 428 if not test: |
| 429 return code |
| 430 return generate_indented_conditional(code, conditional) |
| 431 |
| 426 # [Exposed] | 432 # [Exposed] |
| 427 def exposed_if(code, exposed_test): | 433 def exposed_if(code, exposed_test): |
| 428 if not exposed_test: | 434 return maybe_add_conditional(code, exposed_test, 'executionContext && (%s)'
% exposed_test) |
| 429 return code | |
| 430 return generate_indented_conditional(code, 'executionContext && (%s)' % expo
sed_test) | |
| 431 | 435 |
| 432 | 436 |
| 433 # [RuntimeEnabled] | 437 # [RuntimeEnabled] |
| 434 def runtime_enabled_if(code, runtime_enabled_function_name): | 438 def runtime_enabled_if(code, runtime_enabled_function_name): |
| 435 if not runtime_enabled_function_name: | 439 return maybe_add_conditional(code, runtime_enabled_function_name, '%s' % run
time_enabled_function_name) |
| 436 return code | |
| 437 return generate_indented_conditional(code, '%s()' % runtime_enabled_function
_name) | |
| 438 | 440 |
| 439 | 441 |
| 442 def experimental_framework_runtime_enabled_if(code, api_experiment_name): |
| 443 return maybe_add_conditional(code, api_experiment_name, "RuntimeEnabledFeatu
res::experimentalFrameworkEnabled()") |
| 444 |
| 440 ################################################################################ | 445 ################################################################################ |
| 441 | 446 |
| 442 def main(argv): | 447 def main(argv): |
| 443 # If file itself executed, cache templates | 448 # If file itself executed, cache templates |
| 444 try: | 449 try: |
| 445 cache_dir = argv[1] | 450 cache_dir = argv[1] |
| 446 dummy_filename = argv[2] | 451 dummy_filename = argv[2] |
| 447 except IndexError as err: | 452 except IndexError as err: |
| 448 print 'Usage: %s CACHE_DIR DUMMY_FILENAME' % argv[0] | 453 print 'Usage: %s CACHE_DIR DUMMY_FILENAME' % argv[0] |
| 449 return 1 | 454 return 1 |
| 450 | 455 |
| 451 # Cache templates | 456 # Cache templates |
| 452 jinja_env = initialize_jinja_env(cache_dir) | 457 jinja_env = initialize_jinja_env(cache_dir) |
| 453 template_filenames = [filename for filename in os.listdir(templates_dir) | 458 template_filenames = [filename for filename in os.listdir(templates_dir) |
| 454 # Skip .svn, directories, etc. | 459 # Skip .svn, directories, etc. |
| 455 if filename.endswith(('.cpp', '.h'))] | 460 if filename.endswith(('.cpp', '.h'))] |
| 456 for template_filename in template_filenames: | 461 for template_filename in template_filenames: |
| 457 jinja_env.get_template(template_filename) | 462 jinja_env.get_template(template_filename) |
| 458 | 463 |
| 459 # Create a dummy file as output for the build system, | 464 # Create a dummy file as output for the build system, |
| 460 # since filenames of individual cache files are unpredictable and opaque | 465 # since filenames of individual cache files are unpredictable and opaque |
| 461 # (they are hashes of the template path, which varies based on environment) | 466 # (they are hashes of the template path, which varies based on environment) |
| 462 with open(dummy_filename, 'w') as dummy_file: | 467 with open(dummy_filename, 'w') as dummy_file: |
| 463 pass # |open| creates or touches the file | 468 pass # |open| creates or touches the file |
| 464 | 469 |
| 465 | 470 |
| 466 if __name__ == '__main__': | 471 if __name__ == '__main__': |
| 467 sys.exit(main(sys.argv)) | 472 sys.exit(main(sys.argv)) |
| OLD | NEW |