diff options
author | 2017-08-30 20:07:32 +0200 | |
---|---|---|
committer | 2017-08-31 13:44:10 +0200 | |
commit | 5ee389c261c98854af9ed6a8142541d49b32cf74 (patch) | |
tree | ab7eabf879bba920b59b3fb94fd49bb880dcf1ce /src | |
parent | f3ad3034aee6bedecd7455fbf762d4acf2dace19 (diff) |
Add check that the exec and target platform labels actually provide a PlatformInfo provider.
Fixes #3631.
Change-Id: I78ed8905e18e3c11c01d6e30512c10491a5ba0f1
PiperOrigin-RevId: 167019469
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/skyframe/ToolchainUtil.java | 30 | ||||
-rwxr-xr-x | src/test/shell/bazel/toolchain_test.sh | 30 |
2 files changed, 58 insertions, 2 deletions
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 925c854c67..0f2a35238e 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 @@ -38,6 +38,7 @@ import com.google.devtools.build.skyframe.ValueOrException4; import java.util.ArrayList; import java.util.List; import java.util.Map; +import javax.annotation.Nullable; /** * Common code to create a {@link ToolchainContext} given a set of required toolchain type labels. @@ -48,6 +49,7 @@ public class ToolchainUtil { * Returns a new {@link ToolchainContext}, with the correct toolchain labels based on the results * of the {@link ToolchainResolutionFunction}. */ + @Nullable public static ToolchainContext createToolchainContext( Environment env, String targetDescription, @@ -56,6 +58,9 @@ public class ToolchainUtil { throws ToolchainContextException, InterruptedException { ImmutableBiMap<Label, Label> resolvedLabels = resolveToolchainLabels(env, requiredToolchains, configuration); + if (resolvedLabels == null) { + return null; + } ToolchainContext toolchainContext = ToolchainContext.create(targetDescription, requiredToolchains, resolvedLabels); return toolchainContext; @@ -76,6 +81,7 @@ public class ToolchainUtil { } } + @Nullable private static PlatformDescriptors loadPlatformDescriptors( Environment env, BuildConfiguration configuration) throws InterruptedException, ToolchainContextException { @@ -108,12 +114,22 @@ public class ToolchainUtil { PlatformInfo execPlatform = PlatformProviderUtils.platform(executionPlatformTarget); PlatformInfo targetPlatform = PlatformProviderUtils.platform(targetPlatformTarget); + if (execPlatform == null) { + throw new ToolchainContextException( + new InvalidPlatformException("execution platform", executionPlatformTarget)); + } + if (targetPlatform == null) { + throw new ToolchainContextException( + new InvalidPlatformException("target platform", targetPlatformTarget)); + } + return PlatformDescriptors.create(execPlatform, targetPlatform); } catch (ConfiguredValueCreationException e) { throw new ToolchainContextException(e); } } + @Nullable private static ImmutableBiMap<Label, Label> resolveToolchainLabels( Environment env, List<Label> requiredToolchains, BuildConfiguration configuration) throws InterruptedException, ToolchainContextException { @@ -186,6 +202,16 @@ public class ToolchainUtil { return builder.build(); } + /** Exception used when a platform label is not a valid platform. */ + public static final class InvalidPlatformException extends Exception { + public InvalidPlatformException(String platformType, ConfiguredTarget resolvedTarget) { + super( + String.format( + "Target %s was found as the %s, but does not provide PlatformInfo", + resolvedTarget.getTarget(), platformType)); + } + } + /** Exception used when a toolchain type is required but no matching toolchain is found. */ public static final class UnresolvedToolchainsException extends Exception { private final ImmutableList<Label> missingToolchainTypes; @@ -205,6 +231,10 @@ public class ToolchainUtil { /** Exception used to wrap exceptions during toolchain resolution. */ public static class ToolchainContextException extends Exception { + public ToolchainContextException(InvalidPlatformException e) { + super(e); + } + public ToolchainContextException(UnresolvedToolchainsException e) { super(e); } diff --git a/src/test/shell/bazel/toolchain_test.sh b/src/test/shell/bazel/toolchain_test.sh index c38072ec03..3688e4e5e1 100755 --- a/src/test/shell/bazel/toolchain_test.sh +++ b/src/test/shell/bazel/toolchain_test.sh @@ -478,11 +478,9 @@ EOF expect_log "While resolving toolchains for target //demo:use: invalid registered toolchain '//demo:invalid': target does not provide the DeclaredToolchainInfo provider" } - function test_toolchain_error_invalid_target() { write_test_toolchain write_test_rule -# write_toolchains # Write toolchain with an invalid target. mkdir -p invalid @@ -513,4 +511,32 @@ EOF expect_log "While resolving toolchains for target //demo:use: no such target '//toolchain:does_not_exist': target 'does_not_exist' not declared in package 'toolchain'" } + +function test_toolchain_error_invalid_target() { + write_test_toolchain + write_test_rule + write_toolchains + + mkdir -p demo + cat >> demo/BUILD <<EOF +load('//toolchain:rule.bzl', 'use_toolchain') +# Use the toolchain. +use_toolchain( + name = 'use', + message = 'this is the rule') +EOF + + # Write and invalid rule to be the platform. + mkdir -p platform + cat >> platform/BUILD <<EOF + filegroup(name = 'not_a_platform') +EOF + + bazel build --experimental_platforms=//platform:not_a_platform //demo:use &> $TEST_log && fail "Build failure expected" + expect_log "While resolving toolchains for target //demo:use: Target filegroup rule //platform:not_a_platform was found as the target platform, but does not provide PlatformInfo" + + bazel build --experimental_host_platform=//platform:not_a_platform //demo:use &> $TEST_log && fail "Build failure expected" + expect_log "While resolving toolchains for target //demo:use: Target filegroup rule //platform:not_a_platform was found as the execution platform, but does not provide PlatformInfo" +} + run_suite "toolchain tests" |