• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
11"""
12This tool tries to fix (some) errors reported by `gn gen --check` or
13`gn check`.
14It will run `mb gen` in a temporary directory and it is really useful to
15check for different configurations.
16
17Usage:
18    $ python tools_webrtc/gn_check_autofix.py -m some_mater -b some_bot
19    or
20    $ python tools_webrtc/gn_check_autofix.py -c some_mb_config
21"""
22
23import os
24import re
25import shutil
26import subprocess
27import sys
28import tempfile
29
30from collections import defaultdict
31
32SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
33
34CHROMIUM_DIRS = ['base', 'build', 'buildtools',
35                 'testing', 'third_party', 'tools']
36
37TARGET_RE = re.compile(
38    r'(?P<indentation_level>\s*)\w*\("(?P<target_name>\w*)"\) {$')
39
40class 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
49  def __exit__(self, exc, value, _tb):
50    if self._name and not self._closed:
51      shutil.rmtree(self._name)
52      self._closed = True
53
54
55def Run(cmd):
56  print 'Running:', ' '.join(cmd)
57  sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
58  return sub.communicate()
59
60def 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
94def 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
98def Rebase(base_path, dependency_path, dependency):
99  """Adapt paths so they work both in stand-alone WebRTC and Chromium tree.
100
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.
106
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
136
137def main():
138  deleted_sources = set()
139  errors_by_file = defaultdict(lambda: defaultdict(set))
140
141  with TemporaryDirectory() as tmp_dir:
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')
144    mb_gen_command = ([
145        mb_script_path, 'gen',
146        tmp_dir,
147        '--config-file', mb_config_file_path,
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)
172      # Replacing /target:target with /target
173      dep = re.sub(r'/(\w+):(\1)$', r'/\1', dep)
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
188if __name__ == '__main__':
189  sys.exit(main())
190