scons: Create a small helper function for disecting a build target path.

This function does about half of the work of the loop which determines
the build root and the list of variants.

Change-Id: I4f44d1e2643244a4be889c677b25b83d41a39b19
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/40970
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
diff --git a/SConstruct b/SConstruct
index b63e2cc..beaf9ae 100755
--- a/SConstruct
+++ b/SConstruct
@@ -124,7 +124,7 @@
           help='Build systemc tests')
 
 from gem5_scons import Transform, error, warning, summarize_warnings
-from gem5_scons import TempFileSpawn
+from gem5_scons import TempFileSpawn, parse_build_path
 import gem5_scons
 
 ########################################################################
@@ -182,24 +182,20 @@
 
 # Generate a list of the unique build roots and configs that the
 # collected targets reference.
-variant_paths = []
+variant_paths = set()
 build_root = None
 for t in BUILD_TARGETS:
-    path_dirs = t.split('/')
-    try:
-        build_top = rfind(path_dirs, 'build', -2)
-    except:
-        error("No non-leaf 'build' dir found on target path.", t)
-    this_build_root = joinpath('/',*path_dirs[:build_top+1])
+    this_build_root, variant = parse_build_path(t)
+
+    # Make sure all targets use the same build root.
     if not build_root:
         build_root = this_build_root
-    else:
-        if this_build_root != build_root:
-            error("build targets not under same build root\n"
-                  "  %s\n  %s" % (build_root, this_build_root))
-    variant_path = joinpath('/',*path_dirs[:build_top+2])
-    if variant_path not in variant_paths:
-        variant_paths.append(variant_path)
+    elif this_build_root != build_root:
+        error("build targets not under same build root\n  %s\n  %s" %
+            (build_root, this_build_root))
+
+    # Collect all the variants into a set.
+    variant_paths.add(os.path.join('/', build_root, variant))
 
 # Make sure build_root exists (might not if this is the first build there)
 if not isdir(build_root):
diff --git a/site_scons/gem5_scons/__init__.py b/site_scons/gem5_scons/__init__.py
index 5b5777c..6b167af 100644
--- a/site_scons/gem5_scons/__init__.py
+++ b/site_scons/gem5_scons/__init__.py
@@ -221,4 +221,20 @@
     print_message('Error: ', termcap.Red, message, **kwargs)
     SCons.Script.Exit(1)
 
-__all__ = ['Configure', 'Transform', 'warning', 'error']
+def parse_build_path(target):
+    path_dirs = target.split('/')
+
+    # Pop off the target file.
+    path_dirs.pop()
+
+    # Search backwards for the "build" directory. Whatever was just before it
+    # was the name of the variant.
+    variant_dir = path_dirs.pop()
+    while path_dirs and path_dirs[-1] != 'build':
+        variant_dir = path_dirs.pop()
+    if not path_dirs:
+        error("No non-leaf 'build' dir found on target path.", t)
+
+    return os.path.join('/', *path_dirs), variant_dir
+
+__all__ = ['Configure', 'Transform', 'warning', 'error', 'parse_build_dir']