blob: 31975dd1167bfd4f20c748b568588cfffd711022 [file] [log] [blame]
Liviu Rau3f146242022-02-23 11:48:281#!/usr/bin/env vpython3
Tim van der Lippeb0d65f12020-03-05 12:15:242#
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 Lippeb571d432021-05-14 10:11:167# 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 Erdogmusf9e40332024-02-07 15:16:2841import argparse
Tim van der Lippeb0d65f12020-03-05 12:15:2442import os.path as path
Tim van der Lippeb0d65f12020-03-05 12:15:2443import os
Jack Franklin82978692020-03-12 14:06:4244import subprocess
Tim van der Lippeb0d65f12020-03-05 12:15:2445import sys
46
Jack Franklin82978692020-03-12 14:06:4247_CURRENT_DIR = path.join(path.dirname(__file__))
48
49try:
50 old_sys_path = sys.path[:]
51 sys.path.append(path.join(_CURRENT_DIR, '..', '..', 'scripts'))
52 import devtools_paths
53finally:
54 sys.path = old_sys_path
55
Tim van der Lippeb0d65f12020-03-05 12:15:2456ROOT_DIRECTORY = path.join(path.dirname(path.abspath(__file__)), '..', '..')
57
58V8_DIRECTORY_PATH = path.join(ROOT_DIRECTORY, 'v8')
Nikolay Vitkov106a04a2025-04-16 12:29:3859PROTOCOL_LOCATION = path.join(ROOT_DIRECTORY, 'third_party', 'blink', 'public',
60 'devtools_protocol')
Tim van der Lippeb0d65f12020-03-05 12:15:2461SCRIPTS_BUILD_PATH = path.join(ROOT_DIRECTORY, 'scripts', 'build')
62
63GENERATE_ARIA_SCRIPT = path.join(SCRIPTS_BUILD_PATH, 'generate_aria.py')
Nikolay Vitkov106a04a2025-04-16 12:29:3864GENERATE_SUPPORTED_CSS_SCRIPT = path.join(SCRIPTS_BUILD_PATH,
65 'generate_supported_css.py')
66GENERATE_PROTOCOL_DEFINITIONS_SCRIPT = path.join(SCRIPTS_BUILD_PATH,
67 'code_generator_frontend.py')
68CONCATENATE_PROTOCOL_SCRIPT = path.join(ROOT_DIRECTORY, 'third_party',
69 'inspector_protocol',
70 'concatenate_protocols.py')
Simon Zünd2ce67542023-02-07 10:15:1471GENERATE_DEPRECATIONS_SCRIPT = path.join(SCRIPTS_BUILD_PATH,
72 'generate_deprecations.py')
Tim van der Lippeb0d65f12020-03-05 12:15:2473
Jack Franklin82978692020-03-12 14:06:4274NODE_LOCATION = devtools_paths.node_path()
75TSC_LOCATION = devtools_paths.typescript_compiler_path()
76
Tim van der Lippeb0d65f12020-03-05 12:15:2477
Ergun Erdogmusf9e40332024-02-07 15:16:2878def 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 Vitkov106a04a2025-04-16 12:29:3886
Tim van der Lippeb0d65f12020-03-05 12:15:2487def popen(arguments, cwd=ROOT_DIRECTORY, env=os.environ.copy()):
Jack Franklin82978692020-03-12 14:06:4288 process = subprocess.Popen([sys.executable] + arguments, cwd=cwd, env=env)
Tim van der Lippeb0d65f12020-03-05 12:15:2489
90 process.communicate()
91
92 if process.returncode != 0:
93 sys.exit(process.returncode)
94
95
Ergun Erdogmusf9e40332024-02-07 15:16:2896def runTsc(file_to_compile, options):
Nikolay Vitkov106a04a2025-04-16 12:29:3897 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 Franklin82978692020-03-12 14:06:42108 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 Erdogmusf9e40332024-02-07 15:16:28113def runNode(file_to_execute, options):
114 process = subprocess.Popen([options.node_path, file_to_execute],
115 stdout=subprocess.PIPE,
116 stderr=subprocess.PIPE)
Jack Franklin82978692020-03-12 14:06:42117 stdout, stderr = process.communicate()
118 return process.returncode, stdout + stderr
119
120
Ergun Erdogmusf9e40332024-02-07 15:16:28121def generate_protocol_typescript_definitions(options):
Nikolay Vitkov106a04a2025-04-16 12:29:38122 generator_script_to_compile = path.join(ROOT_DIRECTORY, 'scripts',
123 'protocol_typescript',
124 'protocol_dts_generator.ts')
Jack Franklin82978692020-03-12 14:06:42125 # first run TSC to convert the script from TS to JS
Ergun Erdogmusf9e40332024-02-07 15:16:28126 typescript_found_errors, typescript_stderr = runTsc(
127 generator_script_to_compile, options)
Jack Franklin82978692020-03-12 14:06:42128
129 if typescript_found_errors:
130 print('')
Nikolay Vitkov106a04a2025-04-16 12:29:38131 print('TypeScript compilation failed on %s' %
132 generator_script_to_compile)
Jack Franklin82978692020-03-12 14:06:42133 print('')
134 print(typescript_stderr)
135 print('')
136 return 1
137
138 outputted_file_path = generator_script_to_compile.replace('.ts', '.js')
139
Ergun Erdogmusf9e40332024-02-07 15:16:28140 node_found_errors, node_stderr = runNode(outputted_file_path, options)
Jack Franklin82978692020-03-12 14:06:42141
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 Lippeb0d65f12020-03-05 12:15:24151# Generate the required `front_end/generated` files that are based on files living in Blink
152def main():
Ergun Erdogmusf9e40332024-02-07 15:16:28153 options = parse_options(sys.argv[1:])
154
Tim van der Lippeb0d65f12020-03-05 12:15:24155 popen([GENERATE_ARIA_SCRIPT])
156 popen([GENERATE_SUPPORTED_CSS_SCRIPT])
Simon Zünd2ce67542023-02-07 10:15:14157 popen([GENERATE_DEPRECATIONS_SCRIPT])
Tim van der Lippeb0d65f12020-03-05 12:15:24158
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 Erdogmusf9e40332024-02-07 15:16:28168 generate_protocol_typescript_definitions(options)
Jack Franklin82978692020-03-12 14:06:42169
Tim van der Lippeb0d65f12020-03-05 12:15:24170
171if __name__ == '__main__':
172 main()