From 004099d17e92a09dc4f7c920e6d54bbe2e67d3fb Mon Sep 17 00:00:00 2001 From: John Cater Date: Fri, 17 May 2019 11:21:59 -0700 Subject: [PATCH] Update toolchain resolution to load requested toolchain types. This allows Bazel to validate toolchain types earleir, and to deal with aliases to toolchain types. Fixes #7404. Closes #8381. PiperOrigin-RevId: 248755418 --- .../analysis/ResolvedToolchainContext.java | 32 +-- .../skyframe/ToolchainResolutionFunction.java | 40 +++- .../lib/skyframe/ToolchainTypeLookupUtil.java | 152 ++++++++++++ .../skyframe/UnloadedToolchainContext.java | 24 +- .../ResolvedToolchainContextTest.java | 53 ++++- .../ToolchainResolutionFunctionTest.java | 54 ++++- .../skyframe/ToolchainTypeLookupUtilTest.java | 225 ++++++++++++++++++ src/test/shell/bazel/toolchain_test.sh | 94 ++++++++ 8 files changed, 650 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/google/devtools/build/lib/skyframe/ToolchainTypeLookupUtil.java create mode 100644 src/test/java/com/google/devtools/build/lib/skyframe/ToolchainTypeLookupUtilTest.java diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ResolvedToolchainContext.java b/src/main/java/com/google/devtools/build/lib/analysis/ResolvedToolchainContext.java index 9a19b105fe3353..9ecc4106d3094f 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/ResolvedToolchainContext.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/ResolvedToolchainContext.java @@ -38,7 +38,6 @@ import com.google.devtools.build.lib.skylarkinterface.StarlarkContext; import com.google.devtools.build.lib.syntax.EvalException; import com.google.devtools.build.lib.syntax.EvalUtils; -import java.util.Optional; import java.util.Set; import javax.annotation.Nullable; @@ -67,7 +66,9 @@ public static ResolvedToolchainContext load( .setExecutionPlatform(unloadedToolchainContext.executionPlatform()) .setTargetPlatform(unloadedToolchainContext.targetPlatform()) .setRequiredToolchainTypes(unloadedToolchainContext.requiredToolchainTypes()) - .setResolvedToolchainLabels(unloadedToolchainContext.resolvedToolchainLabels()); + .setResolvedToolchainLabels(unloadedToolchainContext.resolvedToolchainLabels()) + .setRequestedToolchainTypeLabels( + unloadedToolchainContext.requestedLabelToToolchainType()); ImmutableMap.Builder toolchains = new ImmutableMap.Builder<>(); @@ -126,6 +127,10 @@ interface Builder { /** Sets the toolchain types that were requested. */ Builder setRequiredToolchainTypes(Set requiredToolchainTypes); + /** Sets the map from requested {@link Label} to toolchain type provider. */ + Builder setRequestedToolchainTypeLabels( + ImmutableMap requestedToolchainTypeLabels); + /** Sets the map from toolchain type to toolchain provider. */ Builder setToolchains(ImmutableMap toolchains); @@ -142,6 +147,9 @@ interface Builder { /** Returns a description of the target being used, for error messaging. */ abstract String targetDescription(); + /** Sets the map from requested {@link Label} to toolchain type provider. */ + abstract ImmutableMap requestedToolchainTypeLabels(); + abstract ImmutableMap toolchains(); /** Returns the template variables that these toolchains provide. */ @@ -153,15 +161,11 @@ interface Builder { */ @Nullable public ToolchainInfo forToolchainType(Label toolchainTypeLabel) { - Optional toolchainType = - toolchains().keySet().stream() - .filter(info -> info.typeLabel().equals(toolchainTypeLabel)) - .findFirst(); - if (toolchainType.isPresent()) { - return forToolchainType(toolchainType.get()); - } else { + ToolchainTypeInfo toolchainTypeInfo = requestedToolchainTypeLabels().get(toolchainTypeLabel); + if (toolchainTypeInfo == null) { return null; } + return toolchains().get(toolchainTypeInfo); } @Nullable @@ -215,6 +219,9 @@ public ToolchainInfo getIndex(Object key, Location loc, StarlarkContext context) Label toolchainTypeLabel = transformKey(key, loc); if (!containsKey(key, loc, context)) { + // TODO(bazel-configurability): The list of available toolchain types is confusing in the + // presence of aliases, since it only contains the actual label, not the alias passed to the + // rule definition. throw new EvalException( loc, String.format( @@ -233,12 +240,7 @@ public ToolchainInfo getIndex(Object key, Location loc, StarlarkContext context) public boolean containsKey(Object key, Location loc, StarlarkContext context) throws EvalException { Label toolchainTypeLabel = transformKey(key, loc); - Optional