blob: 3895dc9abeca0a608b61e70cc2e5a7f653393065 [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 09:01:12 +01001#!/usr/bin/env vpython3
ehmaldonado01653b12016-12-08 07:27:37 -08002
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.
mbonadei235d5cc2016-12-20 07:19:18 -080010"""
11This tool tries to fix (some) errors reported by `gn gen --check` or
12`gn check`.
13It will run `mb gen` in a temporary directory and it is really useful to
14check for different configurations.
15
16Usage:
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010017 $ vpython3 tools_webrtc/gn_check_autofix.py -m some_mater -b some_bot
mbonadei235d5cc2016-12-20 07:19:18 -080018 or
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010019 $ vpython3 tools_webrtc/gn_check_autofix.py -c some_mb_config
mbonadei235d5cc2016-12-20 07:19:18 -080020"""
21
ehmaldonado01653b12016-12-08 07:27:37 -080022import os
23import re
24import shutil
25import subprocess
26import sys
27import tempfile
28
29from collections import defaultdict
30
mbonadei235d5cc2016-12-20 07:19:18 -080031SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
32
Mirko Bonadei8cc66952020-10-30 10:13:45 +010033CHROMIUM_DIRS = [
34 'base', 'build', 'buildtools', 'testing', 'third_party', 'tools'
35]
Yves Gerey14dfe7f2018-11-22 14:01:23 +010036
ehmaldonado01653b12016-12-08 07:27:37 -080037TARGET_RE = re.compile(
38 r'(?P<indentation_level>\s*)\w*\("(?P<target_name>\w*)"\) {$')
39
Mirko Bonadei8cc66952020-10-30 10:13:45 +010040
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010041class TemporaryDirectory:
42 def __init__(self):
43 self._closed = False
44 self._name = None
45 self._name = tempfile.mkdtemp()
ehmaldonado01653b12016-12-08 07:27:37 -080046
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010047 def __enter__(self):
48 return self._name
ehmaldonado01653b12016-12-08 07:27:37 -080049
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010050 def __exit__(self, exc, value, _tb):
51 if self._name and not self._closed:
52 shutil.rmtree(self._name)
53 self._closed = True
ehmaldonado01653b12016-12-08 07:27:37 -080054
55
56def Run(cmd):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010057 print('Running:', ' '.join(cmd))
Christoffer Jansson3b393ec2022-02-23 09:39:10 +010058 sub = subprocess.Popen(cmd,
59 stdout=subprocess.PIPE,
60 stderr=subprocess.PIPE,
61 universal_newlines=True)
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010062 return sub.communicate()
Mirko Bonadei8cc66952020-10-30 10:13:45 +010063
ehmaldonado01653b12016-12-08 07:27:37 -080064
65def FixErrors(filename, missing_deps, deleted_sources):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010066 with open(filename) as f:
67 lines = f.readlines()
ehmaldonado01653b12016-12-08 07:27:37 -080068
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010069 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
Florent Castelli81acd032022-04-04 15:18:25 +020084 elif line.strip().startswith('deps = ['):
85 joined_deps = ''.join(' "' + dep + '",\n'
86 for dep in missing_deps[target])
87 line = line.replace('deps = [', 'deps = [' + joined_deps)
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010088 indentation_level = None
ehmaldonado01653b12016-12-08 07:27:37 -080089
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010090 if line.strip() not in deleted_sources:
91 fixed_file += line
ehmaldonado01653b12016-12-08 07:27:37 -080092
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010093 with open(filename, 'w') as f:
94 f.write(fixed_file)
ehmaldonado01653b12016-12-08 07:27:37 -080095
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010096 Run(['gn', 'format', filename])
Mirko Bonadei8cc66952020-10-30 10:13:45 +010097
ehmaldonado01653b12016-12-08 07:27:37 -080098
Yves Gerey14dfe7f2018-11-22 14:01:23 +010099def FirstNonEmpty(iterable):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100100 """Return first item which evaluates to True, or fallback to None."""
101 return next((x for x in iterable if x), None)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100102
Yves Gerey14dfe7f2018-11-22 14:01:23 +0100103
ehmaldonado01653b12016-12-08 07:27:37 -0800104def Rebase(base_path, dependency_path, dependency):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100105 """Adapt paths so they work both in stand-alone WebRTC and Chromium tree.
ehmaldonado01653b12016-12-08 07:27:37 -0800106
Yves Gerey14dfe7f2018-11-22 14:01:23 +0100107 To cope with varying top-level directory (WebRTC VS Chromium), we use:
108 * relative paths for WebRTC modules.
109 * absolute paths for shared ones.
110 E.g. '//common_audio/...' -> '../../common_audio/'
111 '//third_party/...' remains as is.
ehmaldonado01653b12016-12-08 07:27:37 -0800112
Yves Gerey14dfe7f2018-11-22 14:01:23 +0100113 Args:
114 base_path: current module path (E.g. '//video')
115 dependency_path: path from root (E.g. '//rtc_base/time')
116 dependency: target itself (E.g. 'timestamp_extrapolator')
117
118 Returns:
119 Full target path (E.g. '../rtc_base/time:timestamp_extrapolator').
120 """
121
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100122 root = FirstNonEmpty(dependency_path.split('/'))
123 if root in CHROMIUM_DIRS:
124 # Chromium paths must remain absolute. E.g. //third_party//abseil-cpp...
125 rebased = dependency_path
126 else:
127 base_path = base_path.split(os.path.sep)
128 dependency_path = dependency_path.split(os.path.sep)
Yves Gerey14dfe7f2018-11-22 14:01:23 +0100129
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100130 first_difference = None
131 shortest_length = min(len(dependency_path), len(base_path))
132 for i in range(shortest_length):
133 if dependency_path[i] != base_path[i]:
134 first_difference = i
135 break
Yves Gerey14dfe7f2018-11-22 14:01:23 +0100136
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100137 first_difference = first_difference or shortest_length
138 base_path = base_path[first_difference:]
139 dependency_path = dependency_path[first_difference:]
140 rebased = os.path.sep.join((['..'] * len(base_path)) + dependency_path)
141 return rebased + ':' + dependency
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100142
ehmaldonado01653b12016-12-08 07:27:37 -0800143
144def main():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100145 deleted_sources = set()
146 errors_by_file = defaultdict(lambda: defaultdict(set))
ehmaldonado01653b12016-12-08 07:27:37 -0800147
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100148 with TemporaryDirectory() as tmp_dir:
149 mb_script_path = os.path.join(SCRIPT_DIR, 'mb', 'mb.py')
150 mb_config_file_path = os.path.join(SCRIPT_DIR, 'mb', 'mb_config.pyl')
151 mb_gen_command = ([
152 mb_script_path,
153 'gen',
154 tmp_dir,
155 '--config-file',
156 mb_config_file_path,
157 ] + sys.argv[1:])
ehmaldonado01653b12016-12-08 07:27:37 -0800158
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100159 mb_output = Run(mb_gen_command)
Christoffer Jansson3b393ec2022-02-23 09:39:10 +0100160 errors = mb_output[0].split('ERROR')[1:]
ehmaldonado01653b12016-12-08 07:27:37 -0800161
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100162 if mb_output[1]:
163 print(mb_output[1])
164 return 1
ehmaldonado01653b12016-12-08 07:27:37 -0800165
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100166 for error in errors:
Florent Castelli48ad72e2022-04-04 14:55:16 +0200167 error = error.split('\n')
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100168 target_msg = 'The target:'
169 if target_msg not in error:
170 target_msg = 'It is not in any dependency of'
171 if target_msg not in error:
172 print('\n'.join(error))
173 continue
174 index = error.index(target_msg) + 1
175 path, target = error[index].strip().split(':')
176 if error[index + 1] in ('is including a file from the target:',
177 'The include file is in the target(s):'):
178 dep = error[index + 2].strip()
179 dep_path, dep = dep.split(':')
180 dep = Rebase(path, dep_path, dep)
181 # Replacing /target:target with /target
182 dep = re.sub(r'/(\w+):(\1)$', r'/\1', dep)
183 path = os.path.join(path[2:], 'BUILD.gn')
184 errors_by_file[path][target].add(dep)
185 elif error[index + 1] == 'has a source file:':
186 deleted_file = '"' + os.path.basename(error[index + 2].strip()) + '",'
187 deleted_sources.add(deleted_file)
188 else:
189 print('\n'.join(error))
190 continue
ehmaldonado01653b12016-12-08 07:27:37 -0800191
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100192 for path, missing_deps in list(errors_by_file.items()):
193 FixErrors(path, missing_deps, deleted_sources)
ehmaldonado01653b12016-12-08 07:27:37 -0800194
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100195 return 0
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100196
ehmaldonado01653b12016-12-08 07:27:37 -0800197
198if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100199 sys.exit(main())