Skip to content

Commit

Permalink
Check for allowed_toolchain_type in single toolchain resolution.
Browse files Browse the repository at this point in the history
This helps to check if an execution platform is usable with a specific toolchain type during exec platform migrations.

PiperOrigin-RevId: 665445311
Change-Id: I4954974d50dfe7d8c23bd7555d8d89609909bfcf
  • Loading branch information
katre authored and copybara-github committed Aug 20, 2024
1 parent d00b6f4 commit 8fb1933
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SingleToolchainResolutionValue> 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()
Expand Down

0 comments on commit 8fb1933

Please sign in to comment.