Make build_helpers.py work in the chromium/src superproject

The script can't find the `find_depot_tools` module if this repo is
checked out under chromium/src.

Bug: b:364177019
Change-Id: I75997a41e1a9732d0e9f6567b00c86784c7b4ce9
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/361680
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42954}
diff --git a/tools_webrtc/autoroller/roll_deps.py b/tools_webrtc/autoroller/roll_deps.py
index cab0cc4..445b91d 100755
--- a/tools_webrtc/autoroller/roll_deps.py
+++ b/tools_webrtc/autoroller/roll_deps.py
@@ -21,6 +21,23 @@
 import urllib.request
 
 
+def FindRootPath():
+    """Returns the absolute path to the highest level repo root.
+
+    If this repo is checked out as a submodule of the chromium/src
+    superproject, this returns the superproect root. Otherwise, it returns the
+    webrtc/src repo root.
+    """
+    root_dir = os.path.dirname(os.path.abspath(__file__))
+    while os.path.basename(root_dir) not in ('src', 'chromium'):
+        par_dir = os.path.normpath(os.path.join(root_dir, os.pardir))
+        if par_dir == root_dir:
+            raise RuntimeError('Could not find the repo root.')
+        root_dir = par_dir
+    return root_dir
+
+
+
 # Skip these dependencies (list without solution name prefix).
 DONT_AUTOROLL_THESE = [
     'src/examples/androidtests/third_party/gradle',
@@ -59,9 +76,8 @@
 ROLL_BRANCH_NAME = 'roll_chromium_revision'
 
 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
-CHECKOUT_SRC_DIR = os.path.realpath(
-    os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
-CHECKOUT_ROOT_DIR = os.path.realpath(os.path.join(CHECKOUT_SRC_DIR, os.pardir))
+CHECKOUT_ROOT_DIR = FindRootPath()
+GCLIENT_ROOT_DIR = os.path.realpath(os.path.join(CHECKOUT_ROOT_DIR, os.pardir))
 
 # Copied from tools/android/roll/android_deps/.../BuildConfigGenerator.groovy.
 ANDROID_DEPS_START = r'=== ANDROID_DEPS Generated Code Start ==='
@@ -71,13 +87,13 @@
 
 NOTIFY_EMAIL = 'webrtc-trooper@grotations.appspotmail.com'
 
-sys.path.append(os.path.join(CHECKOUT_SRC_DIR, 'build'))
+sys.path.append(os.path.join(CHECKOUT_ROOT_DIR, 'build'))
 import find_depot_tools
 
 find_depot_tools.add_depot_tools_to_path()
 
 CLANG_UPDATE_SCRIPT_URL_PATH = 'tools/clang/scripts/update.py'
-CLANG_UPDATE_SCRIPT_LOCAL_PATH = os.path.join(CHECKOUT_SRC_DIR, 'tools',
+CLANG_UPDATE_SCRIPT_LOCAL_PATH = os.path.join(CHECKOUT_ROOT_DIR, 'tools',
                                               'clang', 'scripts', 'update.py')
 
 DepsEntry = collections.namedtuple('DepsEntry', 'path url revision')
@@ -147,7 +163,7 @@
     Returns:
       A tuple containing the stdout and stderr outputs as strings.
     """
-    working_dir = working_dir or CHECKOUT_SRC_DIR
+    working_dir = working_dir or CHECKOUT_ROOT_DIR
     logging.debug('CMD: %s CWD: %s', ' '.join(command), working_dir)
     env = os.environ.copy()
     if extra_env:
@@ -603,7 +619,7 @@
         # ChangedVersionEntry types are already been processed.
         if isinstance(dep, ChangedVersionEntry):
             continue
-        local_dep_dir = os.path.join(CHECKOUT_ROOT_DIR, dep.path)
+        local_dep_dir = os.path.join(GCLIENT_ROOT_DIR, dep.path)
         if not os.path.isdir(local_dep_dir):
             raise RollError(
                 'Cannot find local directory %s. Either run\n'
@@ -618,7 +634,7 @@
         else:
             update = '%s@%s' % (dep.path, dep.new_rev)
         _RunCommand(['gclient', 'setdep', '--revision', update],
-                    working_dir=CHECKOUT_SRC_DIR)
+                    working_dir=CHECKOUT_ROOT_DIR)
 
 
 def _IsTreeClean():
@@ -786,7 +802,7 @@
     if not opts.ignore_unclean_workdir:
         _EnsureUpdatedMainBranch(opts.dry_run)
 
-    deps_filename = os.path.join(CHECKOUT_SRC_DIR, 'DEPS')
+    deps_filename = os.path.join(CHECKOUT_ROOT_DIR, 'DEPS')
     webrtc_deps = ParseLocalDepsFile(deps_filename)
 
     rev_update = GetRollRevisionRanges(opts, webrtc_deps)
diff --git a/tools_webrtc/presubmit_checks_lib/build_helpers.py b/tools_webrtc/presubmit_checks_lib/build_helpers.py
index 9f08d26..42c919b 100644
--- a/tools_webrtc/presubmit_checks_lib/build_helpers.py
+++ b/tools_webrtc/presubmit_checks_lib/build_helpers.py
@@ -19,9 +19,24 @@
 import tempfile
 
 
-SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
-SRC_DIR = os.path.realpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
-sys.path.append(os.path.join(SRC_DIR, 'build'))
+def find_root_path():
+    """Returns the absolute path to the highest level repo root.
+
+    If this repo is checked out as a submodule of the chromium/src
+    superproject, this returns the superproect root. Otherwise, it returns the
+    webrtc/src repo root.
+    """
+    root_dir = os.path.dirname(os.path.abspath(__file__))
+    while os.path.basename(root_dir) not in ('src', 'chromium'):
+        par_dir = os.path.normpath(os.path.join(root_dir, os.pardir))
+        if par_dir == root_dir:
+            raise RuntimeError('Could not find the repo root.')
+        root_dir = par_dir
+    return root_dir
+
+
+ROOT_DIR = find_root_path()
+sys.path.append(os.path.join(ROOT_DIR, 'build'))
 import find_depot_tools
 
 
@@ -62,7 +77,7 @@
 def run_ninja_command(args, root_dir=None):
     """Runs ninja quietly. Any failure (e.g. clang not found) is
      silently discarded, since this is unlikely an error in submitted CL."""
-    command = [os.path.join(SRC_DIR, 'third_party', 'ninja', 'ninja')] + args
+    command = [os.path.join(ROOT_DIR, 'third_party', 'ninja', 'ninja')] + args
     proc = subprocess.Popen(command,
                             cwd=root_dir,
                             stdout=subprocess.PIPE,
@@ -75,7 +90,7 @@
     """POC/WIP! Use the one we have, even it doesn't match clang's version."""
     tidy = ('third_party/android_toolchain/toolchains/'
             'llvm/prebuilt/linux-x86_64/bin/clang-tidy')
-    return os.path.join(SRC_DIR, tidy)
+    return os.path.join(ROOT_DIR, tidy)
 
 
 def get_compilation_db(root_dir=None):