crosperf: refactor chrome src search

Replace multiple levels of if/else of the chrome src search with a loop.
Add a missing search path .cache/distfiles/chrome-src.
Add checks for the Chrome source location and raise RuntimeError if it
does not exist.

BUG=None
TEST=Tested with crosperf and distfiles/target/chrome-src-internal.

Change-Id: I66dd7c802a4e80238e4d625586198bdf573621dc
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2453595
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Reviewed-by: Caroline Tice <cmtice@chromium.org>
Tested-by: Denis Nikitin <denik@chromium.org>
Commit-Queue: George Burgess <gbiv@chromium.org>
diff --git a/crosperf/label.py b/crosperf/label.py
index b14c4a7..a55d663 100644
--- a/crosperf/label.py
+++ b/crosperf/label.py
@@ -72,22 +72,31 @@
 
     self.chromeos_root = chromeos_root
     if not chrome_src:
-      self.chrome_src = os.path.join(
-          self.chromeos_root, '.cache/distfiles/target/chrome-src-internal')
-      if not os.path.exists(self.chrome_src):
-        self.chrome_src = os.path.join(self.chromeos_root,
-                                       '.cache/distfiles/target/chrome-src')
-      # Chrome source location has changed and we have to support
-      # old and new path.
-      if not os.path.exists(self.chrome_src):
-        self.chrome_src = os.path.join(self.chromeos_root,
-                                       '.cache/distfiles/chrome-src-internal')
+      # Old and new chroots may have different chrome src locations.
+      # The path also depends on the chrome build flags.
+      # Give priority to chrome-src-internal.
+      chrome_src_rel_paths = [
+          '.cache/distfiles/target/chrome-src-internal',
+          '.cache/distfiles/chrome-src-internal',
+          '.cache/distfiles/target/chrome-src',
+          '.cache/distfiles/chrome-src',
+      ]
+      for chrome_src_rel_path in chrome_src_rel_paths:
+        chrome_src_abs_path = os.path.join(self.chromeos_root,
+                                           chrome_src_rel_path)
+        if os.path.exists(chrome_src_abs_path):
+          chrome_src = chrome_src_abs_path
+          break
+      if not chrome_src:
+        raise RuntimeError('Can not find location of Chrome sources.\n'
+                           f'Checked paths: {chrome_src_rel_paths}')
     else:
-      chromeos_src = misc.CanonicalizePath(chrome_src)
-      if not chromeos_src:
+      chrome_src = misc.CanonicalizePath(chrome_src)
+      # Make sure the path exists.
+      if not os.path.exists(chrome_src):
         raise RuntimeError("Invalid Chrome src given for label '%s': '%s'." %
                            (name, chrome_src))
-      self.chrome_src = chromeos_src
+    self.chrome_src = chrome_src
 
     self._SetupChecksum()