diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java index 1d86033c4befad..a96c03a6ecf1e9 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java @@ -213,6 +213,24 @@ private static SingleToolchainResolutionValue resolveConstraints( } PlatformInfo executionPlatform = platforms.get(executionPlatformKey); + + // Check if the platform allows this toolchain type. + if (executionPlatform.checkToolchainTypes() + && !executionPlatform.allowedToolchainTypes().contains(toolchainType.toolchainType())) { + debugMessage( + resolutionTrace, + IndentLevel.EXECUTION_PLATFORM_LEVEL, + "Skipping execution platform %s; its allowed toolchain types does not contain the" + + " current toolchain type %s", + executionPlatformKey.getLabel(), + toolchainType.toolchainType()); + + // Keep looking for a valid toolchain for this exec platform + done = false; + continue; + } + + // Check if the execution constraints match. if (!checkConstraints( resolutionTrace, toolchain.execConstraints(), diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunctionTest.java index a02df36082f26e..f8d2c33f571c5e 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunctionTest.java @@ -135,6 +135,61 @@ public void testResolution_noneFound() throws Exception { assertThat(singleToolchainResolutionValue.availableToolchainLabels()).isEmpty(); } + @Test + public void testResolution_checkPlatformAllowedToolchains() throws Exception { + // Define two new execution platforms, only one of which is compatible with the test toolchain. + scratch.file( + "allowed/BUILD", + """ + platform( + name = "fails_match", + check_toolchain_types = True, + allowed_toolchain_types = [ + # Empty, so doesn't match anything. + ], + ) + + platform( + name = "matches", + check_toolchain_types = True, + allowed_toolchain_types = [ + "//toolchain:test_toolchain", + ], + ) + """); + ConfiguredTargetKey failsMatchPlatformCtKey = + ConfiguredTargetKey.builder() + .setLabel(Label.parseCanonicalUnchecked("//allowed:fails_match")) + .setConfiguration(getTargetConfiguration()) + .build(); + ConfiguredTargetKey allowedPlatformCtKey = + ConfiguredTargetKey.builder() + .setLabel(Label.parseCanonicalUnchecked("//allowed:matches")) + .setConfiguration(getTargetConfiguration()) + .build(); + + // Define the toolchains themselves. + addToolchain("extra", "extra_toolchain", ImmutableList.of(), ImmutableList.of(), "baz"); + rewriteWorkspace("register_toolchains('//extra:extra_toolchain')"); + + // Resolve toolchains. + SkyKey key = + SingleToolchainResolutionValue.key( + targetConfigKey, + testToolchainType, + testToolchainTypeInfo, + linuxCtkey, + ImmutableList.of(failsMatchPlatformCtKey, allowedPlatformCtKey)); + EvaluationResult result = invokeToolchainResolution(key); + + assertThatEvaluationResult(result).hasNoError(); + + SingleToolchainResolutionValue singleToolchainResolutionValue = result.get(key); + assertThat(singleToolchainResolutionValue.availableToolchainLabels()) + .containsExactly( + allowedPlatformCtKey, Label.parseCanonicalUnchecked("//extra:extra_toolchain_impl")); + } + @Test public void testToolchainResolutionValue_equalsAndHashCode() { new EqualsTester()