Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env vpython3 |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 2 | |
| 3 | # Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license |
| 6 | # that can be found in the LICENSE file in the root of the source |
| 7 | # tree. An additional intellectual property rights grant can be found |
| 8 | # in the file PATENTS. All contributing project authors may |
| 9 | # be found in the AUTHORS file in the root of the source tree. |
mbonadei | 235d5cc | 2016-12-20 07:19:18 -0800 | [diff] [blame] | 10 | """ |
| 11 | This tool tries to fix (some) errors reported by `gn gen --check` or |
| 12 | `gn check`. |
| 13 | It will run `mb gen` in a temporary directory and it is really useful to |
| 14 | check for different configurations. |
| 15 | |
| 16 | Usage: |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 17 | $ vpython3 tools_webrtc/gn_check_autofix.py -m some_mater -b some_bot |
mbonadei | 235d5cc | 2016-12-20 07:19:18 -0800 | [diff] [blame] | 18 | or |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 19 | $ vpython3 tools_webrtc/gn_check_autofix.py -c some_mb_config |
mbonadei | 235d5cc | 2016-12-20 07:19:18 -0800 | [diff] [blame] | 20 | """ |
| 21 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 22 | import os |
| 23 | import re |
| 24 | import shutil |
| 25 | import subprocess |
| 26 | import sys |
| 27 | import tempfile |
| 28 | |
| 29 | from collections import defaultdict |
| 30 | |
mbonadei | 235d5cc | 2016-12-20 07:19:18 -0800 | [diff] [blame] | 31 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 32 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 33 | CHROMIUM_DIRS = [ |
| 34 | 'base', 'build', 'buildtools', 'testing', 'third_party', 'tools' |
| 35 | ] |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 36 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 37 | TARGET_RE = re.compile( |
| 38 | r'(?P<indentation_level>\s*)\w*\("(?P<target_name>\w*)"\) {$') |
| 39 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 40 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 41 | class TemporaryDirectory: |
| 42 | def __init__(self): |
| 43 | self._closed = False |
| 44 | self._name = None |
| 45 | self._name = tempfile.mkdtemp() |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 46 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 47 | def __enter__(self): |
| 48 | return self._name |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 49 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 50 | def __exit__(self, exc, value, _tb): |
| 51 | if self._name and not self._closed: |
| 52 | shutil.rmtree(self._name) |
| 53 | self._closed = True |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def Run(cmd): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 57 | print('Running:', ' '.join(cmd)) |
| 58 | sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 59 | return sub.communicate() |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 60 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 61 | |
| 62 | def FixErrors(filename, missing_deps, deleted_sources): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 63 | with open(filename) as f: |
| 64 | lines = f.readlines() |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 65 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 66 | fixed_file = '' |
| 67 | indentation_level = None |
| 68 | for line in lines: |
| 69 | match = TARGET_RE.match(line) |
| 70 | if match: |
| 71 | target = match.group('target_name') |
| 72 | if target in missing_deps: |
| 73 | indentation_level = match.group('indentation_level') |
| 74 | elif indentation_level is not None: |
| 75 | match = re.match(indentation_level + '}$', line) |
| 76 | if match: |
| 77 | line = ('deps = [\n' + ''.join(' "' + dep + '",\n' |
| 78 | for dep in missing_deps[target]) + |
| 79 | ']\n') + line |
| 80 | indentation_level = None |
| 81 | elif line.strip().startswith('deps'): |
| 82 | is_empty_deps = line.strip() == 'deps = []' |
| 83 | line = 'deps = [\n' if is_empty_deps else line |
| 84 | line += ''.join(' "' + dep + '",\n' for dep in missing_deps[target]) |
| 85 | line += ']\n' if is_empty_deps else '' |
| 86 | indentation_level = None |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 87 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 88 | if line.strip() not in deleted_sources: |
| 89 | fixed_file += line |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 90 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 91 | with open(filename, 'w') as f: |
| 92 | f.write(fixed_file) |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 93 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 94 | Run(['gn', 'format', filename]) |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 95 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 96 | |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 97 | def FirstNonEmpty(iterable): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 98 | """Return first item which evaluates to True, or fallback to None.""" |
| 99 | return next((x for x in iterable if x), None) |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 100 | |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 101 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 102 | def Rebase(base_path, dependency_path, dependency): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 103 | """Adapt paths so they work both in stand-alone WebRTC and Chromium tree. |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 104 | |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 105 | To cope with varying top-level directory (WebRTC VS Chromium), we use: |
| 106 | * relative paths for WebRTC modules. |
| 107 | * absolute paths for shared ones. |
| 108 | E.g. '//common_audio/...' -> '../../common_audio/' |
| 109 | '//third_party/...' remains as is. |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 110 | |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 111 | Args: |
| 112 | base_path: current module path (E.g. '//video') |
| 113 | dependency_path: path from root (E.g. '//rtc_base/time') |
| 114 | dependency: target itself (E.g. 'timestamp_extrapolator') |
| 115 | |
| 116 | Returns: |
| 117 | Full target path (E.g. '../rtc_base/time:timestamp_extrapolator'). |
| 118 | """ |
| 119 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 120 | root = FirstNonEmpty(dependency_path.split('/')) |
| 121 | if root in CHROMIUM_DIRS: |
| 122 | # Chromium paths must remain absolute. E.g. //third_party//abseil-cpp... |
| 123 | rebased = dependency_path |
| 124 | else: |
| 125 | base_path = base_path.split(os.path.sep) |
| 126 | dependency_path = dependency_path.split(os.path.sep) |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 127 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 128 | first_difference = None |
| 129 | shortest_length = min(len(dependency_path), len(base_path)) |
| 130 | for i in range(shortest_length): |
| 131 | if dependency_path[i] != base_path[i]: |
| 132 | first_difference = i |
| 133 | break |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 134 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 135 | first_difference = first_difference or shortest_length |
| 136 | base_path = base_path[first_difference:] |
| 137 | dependency_path = dependency_path[first_difference:] |
| 138 | rebased = os.path.sep.join((['..'] * len(base_path)) + dependency_path) |
| 139 | return rebased + ':' + dependency |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 140 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 141 | |
| 142 | def main(): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 143 | deleted_sources = set() |
| 144 | errors_by_file = defaultdict(lambda: defaultdict(set)) |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 145 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 146 | with TemporaryDirectory() as tmp_dir: |
| 147 | mb_script_path = os.path.join(SCRIPT_DIR, 'mb', 'mb.py') |
| 148 | mb_config_file_path = os.path.join(SCRIPT_DIR, 'mb', 'mb_config.pyl') |
| 149 | mb_gen_command = ([ |
| 150 | mb_script_path, |
| 151 | 'gen', |
| 152 | tmp_dir, |
| 153 | '--config-file', |
| 154 | mb_config_file_path, |
| 155 | ] + sys.argv[1:]) |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 156 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 157 | mb_output = Run(mb_gen_command) |
| 158 | errors = mb_output[0].decode('utf-8').split('ERROR')[1:] |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 159 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 160 | if mb_output[1]: |
| 161 | print(mb_output[1]) |
| 162 | return 1 |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 163 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 164 | for error in errors: |
| 165 | error = error.splitlines() |
| 166 | target_msg = 'The target:' |
| 167 | if target_msg not in error: |
| 168 | target_msg = 'It is not in any dependency of' |
| 169 | if target_msg not in error: |
| 170 | print('\n'.join(error)) |
| 171 | continue |
| 172 | index = error.index(target_msg) + 1 |
| 173 | path, target = error[index].strip().split(':') |
| 174 | if error[index + 1] in ('is including a file from the target:', |
| 175 | 'The include file is in the target(s):'): |
| 176 | dep = error[index + 2].strip() |
| 177 | dep_path, dep = dep.split(':') |
| 178 | dep = Rebase(path, dep_path, dep) |
| 179 | # Replacing /target:target with /target |
| 180 | dep = re.sub(r'/(\w+):(\1)$', r'/\1', dep) |
| 181 | path = os.path.join(path[2:], 'BUILD.gn') |
| 182 | errors_by_file[path][target].add(dep) |
| 183 | elif error[index + 1] == 'has a source file:': |
| 184 | deleted_file = '"' + os.path.basename(error[index + 2].strip()) + '",' |
| 185 | deleted_sources.add(deleted_file) |
| 186 | else: |
| 187 | print('\n'.join(error)) |
| 188 | continue |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 189 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 190 | for path, missing_deps in list(errors_by_file.items()): |
| 191 | FixErrors(path, missing_deps, deleted_sources) |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 192 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 193 | return 0 |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 194 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 195 | |
| 196 | if __name__ == '__main__': |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 197 | sys.exit(main()) |