Tim van der Lippe | 8e12f92 | 2020-12-03 16:33:50 | [diff] [blame] | 1 | #!/usr/bin/env vpython |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2020 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | import os.path as path |
| 8 | import re |
| 9 | import os |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 10 | import subprocess |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 11 | import sys |
| 12 | |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 13 | _CURRENT_DIR = path.join(path.dirname(__file__)) |
| 14 | |
| 15 | try: |
| 16 | old_sys_path = sys.path[:] |
| 17 | sys.path.append(path.join(_CURRENT_DIR, '..', '..', 'scripts')) |
| 18 | import devtools_paths |
| 19 | finally: |
| 20 | sys.path = old_sys_path |
| 21 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 22 | ROOT_DIRECTORY = path.join(path.dirname(path.abspath(__file__)), '..', '..') |
| 23 | |
| 24 | V8_DIRECTORY_PATH = path.join(ROOT_DIRECTORY, 'v8') |
| 25 | PROTOCOL_LOCATION = path.join(ROOT_DIRECTORY, 'third_party', 'blink', 'public', 'devtools_protocol') |
| 26 | SCRIPTS_BUILD_PATH = path.join(ROOT_DIRECTORY, 'scripts', 'build') |
| 27 | |
| 28 | GENERATE_ARIA_SCRIPT = path.join(SCRIPTS_BUILD_PATH, 'generate_aria.py') |
| 29 | GENERATE_SUPPORTED_CSS_SCRIPT = path.join(SCRIPTS_BUILD_PATH, 'generate_supported_css.py') |
| 30 | GENERATE_PROTOCOL_DEFINITIONS_SCRIPT = path.join(SCRIPTS_BUILD_PATH, 'code_generator_frontend.py') |
| 31 | CONCATENATE_PROTOCOL_SCRIPT = path.join(ROOT_DIRECTORY, 'third_party', 'inspector_protocol', 'concatenate_protocols.py') |
| 32 | |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 33 | NODE_LOCATION = devtools_paths.node_path() |
| 34 | TSC_LOCATION = devtools_paths.typescript_compiler_path() |
| 35 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 36 | |
| 37 | def popen(arguments, cwd=ROOT_DIRECTORY, env=os.environ.copy()): |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 38 | process = subprocess.Popen([sys.executable] + arguments, cwd=cwd, env=env) |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 39 | |
| 40 | process.communicate() |
| 41 | |
| 42 | if process.returncode != 0: |
| 43 | sys.exit(process.returncode) |
| 44 | |
| 45 | |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 46 | def runTsc(file_to_compile): |
| 47 | process = subprocess.Popen([NODE_LOCATION, TSC_LOCATION, file_to_compile], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 48 | stdout, stderr = process.communicate() |
| 49 | # TypeScript does not correctly write to stderr because of https://github.com/microsoft/TypeScript/issues/33849 |
| 50 | return process.returncode, stdout + stderr |
| 51 | |
| 52 | |
| 53 | def runNode(file_to_execute): |
| 54 | process = subprocess.Popen([NODE_LOCATION, file_to_execute], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 55 | stdout, stderr = process.communicate() |
| 56 | return process.returncode, stdout + stderr |
| 57 | |
| 58 | |
| 59 | def generate_protocol_typescript_definitions(): |
| 60 | generator_script_to_compile = path.join(ROOT_DIRECTORY, 'scripts', 'protocol_typescript', 'protocol_dts_generator.ts') |
| 61 | |
| 62 | # first run TSC to convert the script from TS to JS |
| 63 | typescript_found_errors, typescript_stderr = runTsc(generator_script_to_compile) |
| 64 | |
| 65 | if typescript_found_errors: |
| 66 | print('') |
| 67 | print('TypeScript compilation failed on %s' % generator_script_to_compile) |
| 68 | print('') |
| 69 | print(typescript_stderr) |
| 70 | print('') |
| 71 | return 1 |
| 72 | |
| 73 | outputted_file_path = generator_script_to_compile.replace('.ts', '.js') |
| 74 | |
| 75 | node_found_errors, node_stderr = runNode(outputted_file_path) |
| 76 | |
| 77 | if node_found_errors: |
| 78 | print('') |
| 79 | print('Generating protocol typedefs failed') |
| 80 | print('') |
| 81 | print(node_stderr) |
| 82 | print('') |
| 83 | return 1 |
| 84 | |
| 85 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 86 | # Generate the required `front_end/generated` files that are based on files living in Blink |
| 87 | def main(): |
| 88 | popen([GENERATE_ARIA_SCRIPT]) |
| 89 | popen([GENERATE_SUPPORTED_CSS_SCRIPT]) |
| 90 | |
| 91 | popen([CONCATENATE_PROTOCOL_SCRIPT] + [ |
| 92 | path.join(PROTOCOL_LOCATION, 'browser_protocol.pdl'), |
| 93 | path.join(V8_DIRECTORY_PATH, 'include', 'js_protocol.pdl'), |
| 94 | # output_file |
| 95 | path.join(PROTOCOL_LOCATION, 'browser_protocol.json'), |
| 96 | ]) |
| 97 | |
| 98 | popen([GENERATE_PROTOCOL_DEFINITIONS_SCRIPT]) |
| 99 | |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 100 | generate_protocol_typescript_definitions() |
| 101 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 102 | |
| 103 | if __name__ == '__main__': |
| 104 | main() |