Add platform data into to the ToolchainContext unconditionally.

Part of #4128.

Change-Id: I1e043e7290912de5b246dbb8748cb2ad865ce38c
PiperOrigin-RevId: 176664440
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ToolchainContext.java b/src/main/java/com/google/devtools/build/lib/analysis/ToolchainContext.java
index 2334c6f..de1fc37 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/ToolchainContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/ToolchainContext.java
@@ -21,6 +21,7 @@
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
+import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
 import com.google.devtools.build.lib.analysis.platform.PlatformProviderUtils;
 import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
 import com.google.devtools.build.lib.cmdline.Label;
@@ -53,14 +54,24 @@
 public class ToolchainContext {
   public static ToolchainContext create(
       String targetDescription,
+      PlatformInfo executionPlatform,
+      PlatformInfo targetPlatform,
       Set<Label> requiredToolchains,
       ImmutableBiMap<Label, Label> resolvedLabels) {
-    ToolchainContext toolchainContext =
-        new ToolchainContext(
-            targetDescription, requiredToolchains, new ResolvedToolchainLabels(resolvedLabels));
-    return toolchainContext;
+    return new ToolchainContext(
+        targetDescription,
+        executionPlatform,
+        targetPlatform,
+        requiredToolchains,
+        new ResolvedToolchainLabels(resolvedLabels));
   }
 
+  /** The {@link PlatformInfo} describing where these toolchains can be executed. */
+  private final PlatformInfo executionPlatform;
+
+  /** The {@link PlatformInfo} describing the outputs of these toolchains. */
+  private final PlatformInfo targetPlatform;
+
   /** Description of the target the toolchain context applies to, for use in error messages. */
   private final String targetDescription;
 
@@ -75,15 +86,27 @@
 
   private ToolchainContext(
       String targetDescription,
+      PlatformInfo executionPlatform,
+      PlatformInfo targetPlatform,
       Set<Label> requiredToolchains,
       ResolvedToolchainLabels resolvedToolchainLabels) {
     this.targetDescription = targetDescription;
+    this.executionPlatform = executionPlatform;
+    this.targetPlatform = targetPlatform;
     this.requiredToolchains = ImmutableList.copyOf(requiredToolchains);
     this.resolvedToolchainLabels = resolvedToolchainLabels;
     this.resolvedToolchainProviders =
         new ResolvedToolchainProviders(ImmutableMap.<Label, ToolchainInfo>of());
   }
 
+  public PlatformInfo getExecutionPlatform() {
+    return executionPlatform;
+  }
+
+  public PlatformInfo getTargetPlatform() {
+    return targetPlatform;
+  }
+
   public ImmutableList<Label> getRequiredToolchains() {
     return requiredToolchains;
   }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ToolchainUtil.java b/src/main/java/com/google/devtools/build/lib/skyframe/ToolchainUtil.java
index ca311c8..a47e373 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ToolchainUtil.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ToolchainUtil.java
@@ -55,15 +55,36 @@
       Environment env,
       String targetDescription,
       Set<Label> requiredToolchains,
-      BuildConfiguration configuration)
+      @Nullable BuildConfiguration configuration)
       throws ToolchainContextException, InterruptedException {
+
+    // In some cases this is called with a missing configuration, so we skip toolchain context.
+    if (configuration == null) {
+      return null;
+    }
+
+    // TODO(katre): Load several possible execution platforms, and select one based on available
+    // toolchains.
+
+    // Load the execution and target platforms for the current configuration.
+    PlatformDescriptors platforms = loadPlatformDescriptors(env, configuration);
+    if (platforms == null) {
+      return null;
+    }
+
     ImmutableBiMap<Label, Label> resolvedLabels =
-        resolveToolchainLabels(env, requiredToolchains, configuration);
+        resolveToolchainLabels(env, requiredToolchains, configuration, platforms);
     if (resolvedLabels == null) {
       return null;
     }
+
     ToolchainContext toolchainContext =
-        ToolchainContext.create(targetDescription, requiredToolchains, resolvedLabels);
+        ToolchainContext.create(
+            targetDescription,
+            platforms.execPlatform(),
+            platforms.targetPlatform(),
+            requiredToolchains,
+            resolvedLabels);
     return toolchainContext;
   }
 
@@ -152,7 +173,10 @@
 
   @Nullable
   private static ImmutableBiMap<Label, Label> resolveToolchainLabels(
-      Environment env, Set<Label> requiredToolchains, BuildConfiguration configuration)
+      Environment env,
+      Set<Label> requiredToolchains,
+      BuildConfiguration configuration,
+      PlatformDescriptors platforms)
       throws InterruptedException, ToolchainContextException {
 
     // If there are no required toolchains, bail out early.
@@ -160,12 +184,6 @@
       return ImmutableBiMap.of();
     }
 
-    // Load the execution and target platforms for the current configuration.
-    PlatformDescriptors platforms = loadPlatformDescriptors(env, configuration);
-    if (platforms == null) {
-      return null;
-    }
-
     // Find the toolchains for the required toolchain types.
     List<SkyKey> registeredToolchainKeys = new ArrayList<>();
     for (Label toolchainType : requiredToolchains) {