blob: 282dc4fc0f7c8415c7e15a02e11e2d43db3cd3ac [file] [log] [blame]
ehmaldonado01653b12016-12-08 07:27:37 -08001#!/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.
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:
Henrik Kjellander90fd7d82017-05-09 08:30:10 +020017 $ python tools_webrtc/gn_check_autofix.py -m some_mater -b some_bot
mbonadei235d5cc2016-12-20 07:19:18 -080018 or
Henrik Kjellander90fd7d82017-05-09 08:30:10 +020019 $ python 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
ehmaldonado01653b12016-12-08 07:27:37 -080041class TemporaryDirectory(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010042 def __init__(self):
43 self._closed = False
44 self._name = None
45 self._name = tempfile.mkdtemp()
ehmaldonado01653b12016-12-08 07:27:37 -080046
Mirko Bonadei8cc66952020-10-30 10:13:45 +010047 def __enter__(self):
48 return self._name
ehmaldonado01653b12016-12-08 07:27:37 -080049
Mirko Bonadei8cc66952020-10-30 10:13:45 +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):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010057 print 'Running:', ' '.join(cmd)
58 sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
59 return sub.communicate()
60
ehmaldonado01653b12016-12-08 07:27:37 -080061
62def FixErrors(filename, missing_deps, deleted_sources):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010063 with open(filename) as f:
64 lines = f.readlines()
ehmaldonado01653b12016-12-08 07:27:37 -080065
Mirko Bonadei8cc66952020-10-30 10:13:45 +010066 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'
85 for dep in missing_deps[target])
86 line += ']\n' if is_empty_deps else ''
87 indentation_level = None
ehmaldonado01653b12016-12-08 07:27:37 -080088
Mirko Bonadei8cc66952020-10-30 10:13:45 +010089 if line.strip() not in deleted_sources:
90 fixed_file += line
ehmaldonado01653b12016-12-08 07:27:37 -080091
Mirko Bonadei8cc66952020-10-30 10:13:45 +010092 with open(filename, 'w') as f:
93 f.write(fixed_file)
ehmaldonado01653b12016-12-08 07:27:37 -080094
Mirko Bonadei8cc66952020-10-30 10:13:45 +010095 Run(['gn', 'format', filename])
96
ehmaldonado01653b12016-12-08 07:27:37 -080097
Yves Gerey14dfe7f2018-11-22 14:01:23 +010098def FirstNonEmpty(iterable):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010099 """Return first item which evaluates to True, or fallback to None."""
100 return next((x for x in iterable if x), None)
101
Yves Gerey14dfe7f2018-11-22 14:01:23 +0100102
ehmaldonado01653b12016-12-08 07:27:37 -0800103def Rebase(base_path, dependency_path, dependency):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100104 """Adapt paths so they work both in stand-alone WebRTC and Chromium tree.
ehmaldonado01653b12016-12-08 07:27:37 -0800105
Yves Gerey14dfe7f2018-11-22 14:01:23 +0100106 To cope with varying top-level directory (WebRTC VS Chromium), we use:
107 * relative paths for WebRTC modules.
108 * absolute paths for shared ones.
109 E.g. '//common_audio/...' -> '../../common_audio/'
110 '//third_party/...' remains as is.
ehmaldonado01653b12016-12-08 07:27:37 -0800111
Yves Gerey14dfe7f2018-11-22 14:01:23 +0100112 Args:
113 base_path: current module path (E.g. '//video')
114 dependency_path: path from root (E.g. '//rtc_base/time')
115 dependency: target itself (E.g. 'timestamp_extrapolator')
116
117 Returns:
118 Full target path (E.g. '../rtc_base/time:timestamp_extrapolator').
119 """
120
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100121 root = FirstNonEmpty(dependency_path.split('/'))
122 if root in CHROMIUM_DIRS:
123 # Chromium paths must remain absolute. E.g. //third_party//abseil-cpp...
124 rebased = dependency_path
125 else:
126 base_path = base_path.split(os.path.sep)
127 dependency_path = dependency_path.split(os.path.sep)
Yves Gerey14dfe7f2018-11-22 14:01:23 +0100128
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100129 first_difference = None
130 shortest_length = min(len(dependency_path), len(base_path))
131 for i in range(shortest_length):
132 if dependency_path[i] != base_path[i]:
133 first_difference = i
134 break
Yves Gerey14dfe7f2018-11-22 14:01:23 +0100135
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100136 first_difference = first_difference or shortest_length
137 base_path = base_path[first_difference:]
138 dependency_path = dependency_path[first_difference:]
139 rebased = os.path.sep.join((['..'] * len(base_path)) + dependency_path)
140 return rebased + ':' + dependency
141
ehmaldonado01653b12016-12-08 07:27:37 -0800142
143def main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100144 deleted_sources = set()
145 errors_by_file = defaultdict(lambda: defaultdict(set))
ehmaldonado01653b12016-12-08 07:27:37 -0800146
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100147 with TemporaryDirectory() as tmp_dir:
148 mb_script_path = os.path.join(SCRIPT_DIR, 'mb', 'mb.py')
149 mb_config_file_path = os.path.join(SCRIPT_DIR, 'mb', 'mb_config.pyl')
150 mb_gen_command = ([
151 mb_script_path,
152 'gen',
153 tmp_dir,
154 '--config-file',
155 mb_config_file_path,
156 ] + sys.argv[1:])
ehmaldonado01653b12016-12-08 07:27:37 -0800157
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100158 mb_output = Run(mb_gen_command)
159 errors = mb_output[0].split('ERROR')[1:]
ehmaldonado01653b12016-12-08 07:27:37 -0800160
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100161 if mb_output[1]:
162 print mb_output[1]
163 return 1
ehmaldonado01653b12016-12-08 07:27:37 -0800164
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100165 for error in errors:
166 error = error.splitlines()
167 target_msg = 'The target:'
168 if target_msg not in error:
169 target_msg = 'It is not in any dependency of'
170 if target_msg not in error:
171 print '\n'.join(error)
172 continue
173 index = error.index(target_msg) + 1
174 path, target = error[index].strip().split(':')
175 if error[index + 1] in ('is including a file from the target:',
176 'The include file is in the target(s):'):
177 dep = error[index + 2].strip()
178 dep_path, dep = dep.split(':')
179 dep = Rebase(path, dep_path, dep)
180 # Replacing /target:target with /target
181 dep = re.sub(r'/(\w+):(\1)$', r'/\1', dep)
182 path = os.path.join(path[2:], 'BUILD.gn')
183 errors_by_file[path][target].add(dep)
184 elif error[index + 1] == 'has a source file:':
185 deleted_file = '"' + os.path.basename(
186 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
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100192 for path, missing_deps in errors_by_file.items():
193 FixErrors(path, missing_deps, deleted_sources)
ehmaldonado01653b12016-12-08 07:27:37 -0800194
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100195 return 0
196
ehmaldonado01653b12016-12-08 07:27:37 -0800197
198if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100199 sys.exit(main())