Use top-level targets' configurations for convenience symlinks.

This adds a new flag, --use_top_level_targets_for_symlinks.

Configuration-specific convenience symlinks (i.e., (prefix)bin,
(prefix)genfiles, (prefix)testlogs) previously pointed at the one top-level
target configuration. If there were multiple top-level target configurations,
which could only happen when --experimental_multi_cpu was used, then no symlinks
would be created, though they would not be deleted either.

With the flag on, the output directories of the configurations actually used by
the top-level targets (after rule class transitions etc.) are compared. If all
targets with non-null configurations have the same configuration (or, more
specifically, if all targets with non-null configurations have configurations
with the same output directory), then a symlink is created pointing at it.
Otherwise, the symlink is deleted, to avoid confusion with stale symlinks from
old builds.

Known changes in behavior WITHOUT the flag turned on:
* In the --experimental_multi_cpu case, non-configuration-specific
  convenience symlinks (i.e., (prefix)(workspace) and (prefix)out) will always
  be created, even though they previously wouldn't have been created at all in
  this case. (should be harmless)
* In the --experimental_multi_cpu case, configuration-specific convenience
  symlinks will be created if all configs have the same output directory.
  (should be safe, or specifically, should never happen because multi-cpu
  would necessarily change the output path, but shouldn't hurt anything even if
  it did)
* In the --experimental_multi_cpu case, configuration-based convenience
  symlinks will be deleted if some configs have a different output directory.
  (slightly more noticeable since that will be every build using
  experimental_multi_cpu, but then, it is experimental)

RELNOTES: None.
PiperOrigin-RevId: 168720843
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
index 83d259c..1285961 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
@@ -13,6 +13,7 @@
 // limitations under the License.
 package com.google.devtools.build.lib.buildtool;
 
+import static com.google.common.collect.ImmutableSet.toImmutableSet;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 
 import com.google.common.base.Joiner;
@@ -118,7 +119,7 @@
  *
  * <p>This is only intended for use by {@link BuildTool}.
  *
- * <p>This class contains an ActionCache, and refers to the BlazeRuntime's BuildView and
+ * <p>This class contains an ActionCache, and refers to the Blaze Runtime's BuildView and
  * PackageCache.
  *
  * @see BuildTool
@@ -357,17 +358,26 @@
     // Must be created after the output path is created above.
     createActionLogDirectory();
 
-    List<BuildConfiguration> targetConfigurations = configurations.getTargetConfigurations();
-    BuildConfiguration targetConfiguration = targetConfigurations.size() == 1
-        ? targetConfigurations.get(0) : null;
-    if (targetConfigurations.size() == 1) {
-      String productName = runtime.getProductName();
-      String workspaceName = env.getWorkspaceName();
-      OutputDirectoryLinksUtils.createOutputDirectoryLinks(
-          workspaceName, env.getWorkspace(), env.getDirectories().getExecRoot(workspaceName),
-          env.getDirectories().getOutputPath(workspaceName), getReporter(), targetConfiguration,
-          request.getBuildOptions().getSymlinkPrefix(productName), productName);
-    }
+    // Create convenience symlinks from the configurations actually used by the requested targets.
+    // Symlinks will be created if all such configurations would point the symlink to the same path;
+    // if this does not hold, stale symlinks (if present from a previous invocation) will be
+    // deleted instead.
+    Set<BuildConfiguration> targetConfigurations =
+        request.getBuildOptions().useTopLevelTargetsForSymlinks()
+        ? analysisResult
+            .getTargetsToBuild()
+            .stream()
+            .map(ConfiguredTarget::getConfiguration)
+            .filter(configuration -> configuration != null)
+            .distinct()
+            .collect(toImmutableSet())
+        : ImmutableSet.copyOf(configurations.getTargetConfigurations());
+    String productName = runtime.getProductName();
+    String workspaceName = env.getWorkspaceName();
+    OutputDirectoryLinksUtils.createOutputDirectoryLinks(
+        workspaceName, env.getWorkspace(), env.getDirectories().getExecRoot(workspaceName),
+        env.getDirectories().getOutputPath(workspaceName), getReporter(), targetConfigurations,
+        request.getBuildOptions().getSymlinkPrefix(productName), productName);
 
     ActionCache actionCache = getActionCache();
     SkyframeExecutor skyframeExecutor = env.getSkyframeExecutor();