From 14dfe7f2885cddf621ba107272a8ed2dfd7fed69 Mon Sep 17 00:00:00 2001 From: Yves Gerey Date: Thu, 22 Nov 2018 14:01:23 +0100 Subject: [PATCH] [GN] Fix dependency rebasing in BUILD.gn files. This CL ensures we properly points to deps shared with chromium, e.g. '//third_party/abseil-cpp...' and not '../third_party/abseil-cpp...' NB: This is only applied to dependencies which were missing, and doesn't fix existing ones. Bug: webrtc:10037 Change-Id: If4bbb00df39401c65def9d56e36e5feb5d67b9dd Reviewed-on: https://webrtc-review.googlesource.com/c/111600 Commit-Queue: Yves Gerey Reviewed-by: Mirko Bonadei Cr-Commit-Position: refs/heads/master@{#25762} --- tools_webrtc/gn_check_autofix.py | 55 ++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/tools_webrtc/gn_check_autofix.py b/tools_webrtc/gn_check_autofix.py index b939636d0c..e44f7122df 100644 --- a/tools_webrtc/gn_check_autofix.py +++ b/tools_webrtc/gn_check_autofix.py @@ -31,6 +31,9 @@ from collections import defaultdict SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) +CHROMIUM_DIRS = ['base', 'build', 'buildtools', + 'testing', 'third_party', 'tools'] + TARGET_RE = re.compile( r'(?P\s*)\w*\("(?P\w*)"\) {$') @@ -88,22 +91,48 @@ def FixErrors(filename, missing_deps, deleted_sources): Run(['gn', 'format', filename]) +def FirstNonEmpty(iterable): + """Return first item which evaluates to True, or fallback to None.""" + return next((x for x in iterable if x), None) + def Rebase(base_path, dependency_path, dependency): - base_path = base_path.split(os.path.sep) - dependency_path = dependency_path.split(os.path.sep) + """Adapt paths so they work both in stand-alone WebRTC and Chromium tree. - first_difference = None - shortest_length = min(len(dependency_path), len(base_path)) - for i in range(shortest_length): - if dependency_path[i] != base_path[i]: - first_difference = i - break + To cope with varying top-level directory (WebRTC VS Chromium), we use: + * relative paths for WebRTC modules. + * absolute paths for shared ones. + E.g. '//common_audio/...' -> '../../common_audio/' + '//third_party/...' remains as is. - first_difference = first_difference or shortest_length - base_path = base_path[first_difference:] - dependency_path = dependency_path[first_difference:] - return (os.path.sep.join((['..'] * len(base_path)) + dependency_path) + - ':' + dependency) + Args: + base_path: current module path (E.g. '//video') + dependency_path: path from root (E.g. '//rtc_base/time') + dependency: target itself (E.g. 'timestamp_extrapolator') + + Returns: + Full target path (E.g. '../rtc_base/time:timestamp_extrapolator'). + """ + + root = FirstNonEmpty(dependency_path.split('/')) + if root in CHROMIUM_DIRS: + # Chromium paths must remain absolute. E.g. //third_party//abseil-cpp... + rebased = dependency_path + else: + base_path = base_path.split(os.path.sep) + dependency_path = dependency_path.split(os.path.sep) + + first_difference = None + shortest_length = min(len(dependency_path), len(base_path)) + for i in range(shortest_length): + if dependency_path[i] != base_path[i]: + first_difference = i + break + + first_difference = first_difference or shortest_length + base_path = base_path[first_difference:] + dependency_path = dependency_path[first_difference:] + rebased = os.path.sep.join((['..'] * len(base_path)) + dependency_path) + return rebased + ':' + dependency def main(): deleted_sources = set()