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