Pull up calls to ToolchainContext.resolvedToolchainLabels and finish
ToolchainContext configuration earlier.

Change-Id: I034f56a8a181aac19ecb38e51558ccdc6c0c7a6a
PiperOrigin-RevId: 203835978
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ConfiguredTargetFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/ConfiguredTargetFunction.java
index 0598feb..938ce5e 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ConfiguredTargetFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ConfiguredTargetFunction.java
@@ -294,7 +294,9 @@
               ctgValue,
               ImmutableList.<Aspect>of(),
               configConditions,
-              toolchainContext,
+              toolchainContext == null
+                  ? ImmutableSet.of()
+                  : toolchainContext.resolvedToolchainLabels(),
               ruleClassProvider,
               view.getHostConfiguration(configuration),
               transitivePackagesForPackageRootResolution,
@@ -309,6 +311,12 @@
                 "Analysis failed", configuration, transitiveRootCauses.build()));
       }
       Preconditions.checkNotNull(depValueMap);
+
+      // Load the requested toolchains into the ToolchainContext, now that we have dependencies.
+      if (toolchainContext != null) {
+        toolchainContext.resolveToolchains(depValueMap);
+      }
+
       ConfiguredTargetValue ans =
           createConfiguredTarget(
               view,
@@ -416,7 +424,7 @@
    * @param ctgValue the label and the configuration of the node
    * @param aspects
    * @param configConditions the configuration conditions for evaluating the attributes of the node
-   * @param toolchainContext context information for required toolchains
+   * @param toolchainLabels labels of required toolchain dependencies
    * @param ruleClassProvider rule class provider for determining the right configuration fragments
    *     to apply to deps
    * @param hostConfiguration the host configuration. There's a noticeable performance hit from
@@ -433,7 +441,7 @@
       TargetAndConfiguration ctgValue,
       Iterable<Aspect> aspects,
       ImmutableMap<Label, ConfigMatchingProvider> configConditions,
-      @Nullable ToolchainContext toolchainContext,
+      ImmutableSet<Label> toolchainLabels,
       RuleClassProvider ruleClassProvider,
       BuildConfiguration hostConfiguration,
       @Nullable NestedSetBuilder<Package> transitivePackagesForPackageRootResolution,
@@ -450,9 +458,7 @@
               hostConfiguration,
               aspects,
               configConditions,
-              toolchainContext == null
-                  ? ImmutableSet.of()
-                  : toolchainContext.resolvedToolchainLabels(),
+              toolchainLabels,
               transitiveRootCauses,
               defaultBuildOptions,
               ((ConfiguredRuleClassProvider) ruleClassProvider).getTrimmingTransitionFactory());