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)) |
Christoffer Jansson | 3b393ec | 2022-02-23 09:39:10 +0100 | [diff] [blame] | 58 | sub = subprocess.Popen(cmd, |
| 59 | stdout=subprocess.PIPE, |
| 60 | stderr=subprocess.PIPE, |
| 61 | universal_newlines=True) |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 62 | return sub.communicate() |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 63 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 64 | |
| 65 | def FixErrors(filename, missing_deps, deleted_sources): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 66 | with open(filename) as f: |
| 67 | lines = f.readlines() |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 68 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 69 | fixed_file = '' |
| 70 | indentation_level = None |
| 71 | for line in lines: |
| 72 | match = TARGET_RE.match(line) |
| 73 | if match: |
| 74 | target = match.group('target_name') |
| 75 | if target in missing_deps: |
| 76 | indentation_level = match.group('indentation_level') |
| 77 | elif indentation_level is not None: |
| 78 | match = re.match(indentation_level + '}$', line) |
| 79 | if match: |
| 80 | line = ('deps = [\n' + ''.join(' "' + dep + '",\n' |
| 81 | for dep in missing_deps[target]) + |
| 82 | ']\n') + line |
| 83 | indentation_level = None |
| 84 | elif line.strip().startswith('deps'): |
| 85 | is_empty_deps = line.strip() == 'deps = []' |
| 86 | line = 'deps = [\n' if is_empty_deps else line |
| 87 | line += ''.join(' "' + dep + '",\n' for dep in missing_deps[target]) |
| 88 | line += ']\n' if is_empty_deps else '' |
| 89 | indentation_level = None |
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 | if line.strip() not in deleted_sources: |
| 92 | fixed_file += line |
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 | with open(filename, 'w') as f: |
| 95 | f.write(fixed_file) |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 96 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 97 | Run(['gn', 'format', filename]) |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 98 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 99 | |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 100 | def FirstNonEmpty(iterable): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 101 | """Return first item which evaluates to True, or fallback to None.""" |
| 102 | return next((x for x in iterable if x), None) |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 103 | |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 104 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 105 | def Rebase(base_path, dependency_path, dependency): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 106 | """Adapt paths so they work both in stand-alone WebRTC and Chromium tree. |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 107 | |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 108 | To cope with varying top-level directory (WebRTC VS Chromium), we use: |
| 109 | * relative paths for WebRTC modules. |
| 110 | * absolute paths for shared ones. |
| 111 | E.g. '//common_audio/...' -> '../../common_audio/' |
| 112 | '//third_party/...' remains as is. |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 113 | |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 114 | Args: |
| 115 | base_path: current module path (E.g. '//video') |
| 116 | dependency_path: path from root (E.g. '//rtc_base/time') |
| 117 | dependency: target itself (E.g. 'timestamp_extrapolator') |
| 118 | |
| 119 | Returns: |
| 120 | Full target path (E.g. '../rtc_base/time:timestamp_extrapolator'). |
| 121 | """ |
| 122 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 123 | root = FirstNonEmpty(dependency_path.split('/')) |
| 124 | if root in CHROMIUM_DIRS: |
| 125 | # Chromium paths must remain absolute. E.g. //third_party//abseil-cpp... |
| 126 | rebased = dependency_path |
| 127 | else: |
| 128 | base_path = base_path.split(os.path.sep) |
| 129 | dependency_path = dependency_path.split(os.path.sep) |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 130 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 131 | first_difference = None |
| 132 | shortest_length = min(len(dependency_path), len(base_path)) |
| 133 | for i in range(shortest_length): |
| 134 | if dependency_path[i] != base_path[i]: |
| 135 | first_difference = i |
| 136 | break |
Yves Gerey | 14dfe7f | 2018-11-22 14:01:23 +0100 | [diff] [blame] | 137 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 138 | first_difference = first_difference or shortest_length |
| 139 | base_path = base_path[first_difference:] |
| 140 | dependency_path = dependency_path[first_difference:] |
| 141 | rebased = os.path.sep.join((['..'] * len(base_path)) + dependency_path) |
| 142 | return rebased + ':' + dependency |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 143 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 144 | |
| 145 | def main(): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 146 | deleted_sources = set() |
| 147 | errors_by_file = defaultdict(lambda: defaultdict(set)) |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 148 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 149 | with TemporaryDirectory() as tmp_dir: |
| 150 | mb_script_path = os.path.join(SCRIPT_DIR, 'mb', 'mb.py') |
| 151 | mb_config_file_path = os.path.join(SCRIPT_DIR, 'mb', 'mb_config.pyl') |
| 152 | mb_gen_command = ([ |
| 153 | mb_script_path, |
| 154 | 'gen', |
| 155 | tmp_dir, |
| 156 | '--config-file', |
| 157 | mb_config_file_path, |
| 158 | ] + sys.argv[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 | mb_output = Run(mb_gen_command) |
Christoffer Jansson | 3b393ec | 2022-02-23 09:39:10 +0100 | [diff] [blame] | 161 | errors = mb_output[0].split('ERROR')[1:] |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 162 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 163 | if mb_output[1]: |
| 164 | print(mb_output[1]) |
| 165 | return 1 |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 166 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 167 | for error in errors: |
Christoffer Jansson | 3b393ec | 2022-02-23 09:39:10 +0100 | [diff] [blame] | 168 | error = error.split('\\n') |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 169 | target_msg = 'The target:' |
| 170 | if target_msg not in error: |
| 171 | target_msg = 'It is not in any dependency of' |
| 172 | if target_msg not in error: |
| 173 | print('\n'.join(error)) |
| 174 | continue |
| 175 | index = error.index(target_msg) + 1 |
| 176 | path, target = error[index].strip().split(':') |
| 177 | if error[index + 1] in ('is including a file from the target:', |
| 178 | 'The include file is in the target(s):'): |
| 179 | dep = error[index + 2].strip() |
| 180 | dep_path, dep = dep.split(':') |
| 181 | dep = Rebase(path, dep_path, dep) |
| 182 | # Replacing /target:target with /target |
| 183 | dep = re.sub(r'/(\w+):(\1)$', r'/\1', dep) |
| 184 | path = os.path.join(path[2:], 'BUILD.gn') |
| 185 | errors_by_file[path][target].add(dep) |
| 186 | elif error[index + 1] == 'has a source file:': |
| 187 | deleted_file = '"' + os.path.basename(error[index + 2].strip()) + '",' |
| 188 | deleted_sources.add(deleted_file) |
| 189 | else: |
| 190 | print('\n'.join(error)) |
| 191 | continue |
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 | for path, missing_deps in list(errors_by_file.items()): |
| 194 | FixErrors(path, missing_deps, deleted_sources) |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 195 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 196 | return 0 |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 197 | |
ehmaldonado | 01653b1 | 2016-12-08 07:27:37 -0800 | [diff] [blame] | 198 | |
| 199 | if __name__ == '__main__': |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 200 | sys.exit(main()) |