Liviu Rau | 3f14624 | 2022-02-23 11:48:28 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
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 | |
Tim van der Lippe | b571d43 | 2021-05-14 10:11:16 | [diff] [blame] | 7 | # We are still generating PDL types as an out-of-process manual task, rather |
| 8 | # than in GN. There are some technical nuances here, but broadly speaking CDP |
| 9 | # is backwards compatible. That means that if you would update CDP, and |
| 10 | # DevTools was built with the expectation of an older version of CDP, that |
| 11 | # should compile and run. This is generally true, but sadly not always. As it |
| 12 | # turns out, the CDP owners regard some domains in CDP as "experimental", |
| 13 | # which is not covered by the backwards compatibility guarantee. |
| 14 | |
| 15 | # The concrete result of that is that, sometimes, the CDP owners update the |
| 16 | # CDP definitions in such a way that it would break compilation of DevTools. |
| 17 | # That would happen if the types change, methods or events are removed and all |
| 18 | # of that integrates with our TypeScript build, which would start failing. As |
| 19 | # such, if the CDP definitions change and DevTools would break on it, we |
| 20 | # currently have to manually patch these on DevTools side. This currently |
| 21 | # happens when DevTools engineers roll our deps, regenerate the types, run the |
| 22 | # build and figure out if manually patching is required. Most of the time, |
| 23 | # there are no functional changes required, but there are rare cases where it |
| 24 | # is required. (Generally speaking, those that introduced the CDP breakage tend |
| 25 | # to be the ones resolving the breakage as well.) |
| 26 | |
| 27 | # If we were to take the PDL definition from Chromium and compile DevTools with |
| 28 | # it, we effectively block any breaking change in CDP. The CDP owners indicated |
| 29 | # that they would like to maintain the freedom of making breaking changes in |
| 30 | # several domains, so that disallows us from integrating the PDL definition in |
| 31 | # the DevTools build. That's why we have to maintain a copy of CDP that we |
| 32 | # build DevTools with and ensure that we remain up-to-date on the Chromium side, |
| 33 | # resolving type breakages whenever they occur. |
| 34 | |
| 35 | # There is another reason why we generate the types manually: so that VS Code |
| 36 | # picks them up and makes them available for usage in the editor. If we |
| 37 | # wouldn't generate them, then VS Code (or any other IDE with a TypeScript |
| 38 | # language server) would not see the types and start complaining, |
| 39 | # even though the local build would succeed. |
| 40 | |
Ergun Erdogmus | f9e4033 | 2024-02-07 15:16:28 | [diff] [blame] | 41 | import argparse |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 42 | import os.path as path |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 43 | import os |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 44 | import subprocess |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 45 | import sys |
| 46 | |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 47 | _CURRENT_DIR = path.join(path.dirname(__file__)) |
| 48 | |
| 49 | try: |
| 50 | old_sys_path = sys.path[:] |
| 51 | sys.path.append(path.join(_CURRENT_DIR, '..', '..', 'scripts')) |
| 52 | import devtools_paths |
| 53 | finally: |
| 54 | sys.path = old_sys_path |
| 55 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 56 | ROOT_DIRECTORY = path.join(path.dirname(path.abspath(__file__)), '..', '..') |
| 57 | |
| 58 | V8_DIRECTORY_PATH = path.join(ROOT_DIRECTORY, 'v8') |
Nikolay Vitkov | 106a04a | 2025-04-16 12:29:38 | [diff] [blame] | 59 | PROTOCOL_LOCATION = path.join(ROOT_DIRECTORY, 'third_party', 'blink', 'public', |
| 60 | 'devtools_protocol') |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 61 | SCRIPTS_BUILD_PATH = path.join(ROOT_DIRECTORY, 'scripts', 'build') |
| 62 | |
| 63 | GENERATE_ARIA_SCRIPT = path.join(SCRIPTS_BUILD_PATH, 'generate_aria.py') |
Nikolay Vitkov | 106a04a | 2025-04-16 12:29:38 | [diff] [blame] | 64 | GENERATE_SUPPORTED_CSS_SCRIPT = path.join(SCRIPTS_BUILD_PATH, |
| 65 | 'generate_supported_css.py') |
| 66 | GENERATE_PROTOCOL_DEFINITIONS_SCRIPT = path.join(SCRIPTS_BUILD_PATH, |
| 67 | 'code_generator_frontend.py') |
| 68 | CONCATENATE_PROTOCOL_SCRIPT = path.join(ROOT_DIRECTORY, 'third_party', |
| 69 | 'inspector_protocol', |
| 70 | 'concatenate_protocols.py') |
Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 71 | GENERATE_DEPRECATIONS_SCRIPT = path.join(SCRIPTS_BUILD_PATH, |
| 72 | 'generate_deprecations.py') |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 73 | |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 74 | NODE_LOCATION = devtools_paths.node_path() |
| 75 | TSC_LOCATION = devtools_paths.typescript_compiler_path() |
| 76 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 77 | |
Ergun Erdogmus | f9e4033 | 2024-02-07 15:16:28 | [diff] [blame] | 78 | def parse_options(cli_args): |
| 79 | parser = argparse.ArgumentParser(description='Generate protocol resources') |
| 80 | parser.add_argument( |
| 81 | '--node-path', |
| 82 | default=NODE_LOCATION, |
| 83 | ) |
| 84 | return parser.parse_args(cli_args) |
| 85 | |
Nikolay Vitkov | 106a04a | 2025-04-16 12:29:38 | [diff] [blame] | 86 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 87 | def popen(arguments, cwd=ROOT_DIRECTORY, env=os.environ.copy()): |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 88 | process = subprocess.Popen([sys.executable] + arguments, cwd=cwd, env=env) |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 89 | |
| 90 | process.communicate() |
| 91 | |
| 92 | if process.returncode != 0: |
| 93 | sys.exit(process.returncode) |
| 94 | |
| 95 | |
Ergun Erdogmus | f9e4033 | 2024-02-07 15:16:28 | [diff] [blame] | 96 | def runTsc(file_to_compile, options): |
Nikolay Vitkov | 106a04a | 2025-04-16 12:29:38 | [diff] [blame] | 97 | process = subprocess.Popen([ |
| 98 | options.node_path, |
| 99 | TSC_LOCATION, |
| 100 | "--module", |
| 101 | "NodeNext", |
| 102 | "--moduleResolution", |
| 103 | "NodeNext", |
| 104 | file_to_compile, |
| 105 | ], |
| 106 | stdout=subprocess.PIPE, |
| 107 | stderr=subprocess.PIPE) |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 108 | stdout, stderr = process.communicate() |
| 109 | # TypeScript does not correctly write to stderr because of https://github.com/microsoft/TypeScript/issues/33849 |
| 110 | return process.returncode, stdout + stderr |
| 111 | |
| 112 | |
Ergun Erdogmus | f9e4033 | 2024-02-07 15:16:28 | [diff] [blame] | 113 | def runNode(file_to_execute, options): |
| 114 | process = subprocess.Popen([options.node_path, file_to_execute], |
| 115 | stdout=subprocess.PIPE, |
| 116 | stderr=subprocess.PIPE) |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 117 | stdout, stderr = process.communicate() |
| 118 | return process.returncode, stdout + stderr |
| 119 | |
| 120 | |
Ergun Erdogmus | f9e4033 | 2024-02-07 15:16:28 | [diff] [blame] | 121 | def generate_protocol_typescript_definitions(options): |
Nikolay Vitkov | 106a04a | 2025-04-16 12:29:38 | [diff] [blame] | 122 | generator_script_to_compile = path.join(ROOT_DIRECTORY, 'scripts', |
| 123 | 'protocol_typescript', |
| 124 | 'protocol_dts_generator.ts') |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 125 | # first run TSC to convert the script from TS to JS |
Ergun Erdogmus | f9e4033 | 2024-02-07 15:16:28 | [diff] [blame] | 126 | typescript_found_errors, typescript_stderr = runTsc( |
| 127 | generator_script_to_compile, options) |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 128 | |
| 129 | if typescript_found_errors: |
| 130 | print('') |
Nikolay Vitkov | 106a04a | 2025-04-16 12:29:38 | [diff] [blame] | 131 | print('TypeScript compilation failed on %s' % |
| 132 | generator_script_to_compile) |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 133 | print('') |
| 134 | print(typescript_stderr) |
| 135 | print('') |
| 136 | return 1 |
| 137 | |
| 138 | outputted_file_path = generator_script_to_compile.replace('.ts', '.js') |
| 139 | |
Ergun Erdogmus | f9e4033 | 2024-02-07 15:16:28 | [diff] [blame] | 140 | node_found_errors, node_stderr = runNode(outputted_file_path, options) |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 141 | |
| 142 | if node_found_errors: |
| 143 | print('') |
| 144 | print('Generating protocol typedefs failed') |
| 145 | print('') |
| 146 | print(node_stderr) |
| 147 | print('') |
| 148 | return 1 |
| 149 | |
| 150 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 151 | # Generate the required `front_end/generated` files that are based on files living in Blink |
| 152 | def main(): |
Ergun Erdogmus | f9e4033 | 2024-02-07 15:16:28 | [diff] [blame] | 153 | options = parse_options(sys.argv[1:]) |
| 154 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 155 | popen([GENERATE_ARIA_SCRIPT]) |
| 156 | popen([GENERATE_SUPPORTED_CSS_SCRIPT]) |
Simon Zünd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 157 | popen([GENERATE_DEPRECATIONS_SCRIPT]) |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 158 | |
| 159 | popen([CONCATENATE_PROTOCOL_SCRIPT] + [ |
| 160 | path.join(PROTOCOL_LOCATION, 'browser_protocol.pdl'), |
| 161 | path.join(V8_DIRECTORY_PATH, 'include', 'js_protocol.pdl'), |
| 162 | # output_file |
| 163 | path.join(PROTOCOL_LOCATION, 'browser_protocol.json'), |
| 164 | ]) |
| 165 | |
| 166 | popen([GENERATE_PROTOCOL_DEFINITIONS_SCRIPT]) |
| 167 | |
Ergun Erdogmus | f9e4033 | 2024-02-07 15:16:28 | [diff] [blame] | 168 | generate_protocol_typescript_definitions(options) |
Jack Franklin | 8297869 | 2020-03-12 14:06:42 | [diff] [blame] | 169 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 170 | |
| 171 | if __name__ == '__main__': |
| 172 | main() |