Skip to content

Commit

Permalink
Add more tests for checking what toolchain types are allowed on exec …
Browse files Browse the repository at this point in the history
…platforms.

PiperOrigin-RevId: 691044548
Change-Id: Ibbd22b08c5df7fda1a224bc30d894e657807eea4
  • Loading branch information
katre authored and copybara-github committed Oct 29, 2024
1 parent dd343a3 commit 5ce6fa6
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ java_test(
java_test(
name = "ToolchainResolutionFunctionTest",
srcs = ["ToolchainResolutionFunctionTest.java"],
shard_count = 5,
deps = [
"//src/main/java/com/google/devtools/build/lib/analysis:config/build_options",
"//src/main/java/com/google/devtools/build/lib/analysis:config/toolchain_type_requirement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,43 +540,6 @@ public void resolve_noToolchainType() throws Exception {
assertThat(unloadedToolchainContext).hasTargetPlatform("//platforms:linux");
}

@Test
public void resolve_noToolchainType_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 = "allows_all",
)
""");
rewriteModuleDotBazel(
"""
register_execution_platforms("//allowed:fails_match", "//allowed:allows_all")
""");

useConfiguration("--host_platform=//allowed:fails_match");
ToolchainContextKey key = ToolchainContextKey.key().configurationKey(targetConfigKey).build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);

assertThatEvaluationResult(result).hasNoError();
UnloadedToolchainContext unloadedToolchainContext = result.get(key);
assertThat(unloadedToolchainContext).isNotNull();

assertThat(unloadedToolchainContext.toolchainTypes()).isEmpty();
// Even with no toolchains requested, should still select the first execution platform.
assertThat(unloadedToolchainContext).hasExecutionPlatform("//allowed:allows_all");
}

@Test
public void resolve_noToolchainType_hostNotAvailable() throws Exception {
scratch.file("host/BUILD", "platform(name = 'host')");
Expand Down Expand Up @@ -1338,4 +1301,202 @@ def _impl(ctx):
+ " //strange:strange_test_toolchain: For config_setting flagged, Feature flag"
+ " //strange:flag was accessed in a configuration it is not present in.");
}

@Test
public void resolve_checkPlatformAllowedToolchains_match() throws Exception {
// Define two new execution platforms, only one of which is compatible with the test toolchain.
scratch.file(
"allowed/BUILD",
"""
platform(
name = "allows_single_toolchain",
check_toolchain_types = True,
allowed_toolchain_types = [
"//toolchain:test_toolchain",
],
)
platform(
name = "allows_all",
)
""");
addToolchain("toolchain", "toolchain_impl", ImmutableList.of(), ImmutableList.of(), "baz");
rewriteModuleDotBazel(
"""
register_toolchains("//toolchain:toolchain_impl")
register_execution_platforms("//allowed:allows_single_toolchain", "//allowed:allows_all")
""");

ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(testToolchainType)
.build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);

assertThatEvaluationResult(result).hasNoError();
UnloadedToolchainContext unloadedToolchainContext = result.get(key);
assertThat(unloadedToolchainContext).isNotNull();

// The platform allows the required toolchain type, so it is selected.
assertThat(unloadedToolchainContext).hasExecutionPlatform("//allowed:allows_single_toolchain");
}

@Test
public void resolve_checkPlatformAllowedToolchains_failsMatch() throws Exception {
// Define two new execution platforms, only one of which is compatible with the test toolchain.
scratch.file(
"allowed/BUILD",
"""
toolchain_type(name = "other_toolchain")
platform(
name = "allows_single_toolchain",
check_toolchain_types = True,
allowed_toolchain_types = [
":other_toolchain",
],
)
platform(
name = "allows_all",
)
""");
addToolchain("toolchain", "toolchain_impl", ImmutableList.of(), ImmutableList.of(), "baz");
rewriteModuleDotBazel(
"""
register_toolchains("//toolchain:toolchain_impl")
register_execution_platforms("//allowed:allows_single_toolchain", "//allowed:allows_all")
""");

ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(testToolchainType)
.build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);

assertThatEvaluationResult(result).hasNoError();
UnloadedToolchainContext unloadedToolchainContext = result.get(key);
assertThat(unloadedToolchainContext).isNotNull();

// The platform does not allow the required toolchain type, so it is not selected.
assertThat(unloadedToolchainContext).hasExecutionPlatform("//allowed:allows_all");
}

@Test
public void resolve_checkPlatformAllowedToolchains_noneRequested_failsMatch() throws Exception {
// Define two new execution platforms, only one of which is compatible with the test toolchain.
scratch.file(
"allowed/BUILD",
"""
platform(
name = "allows_single_toolchain",
check_toolchain_types = True,
allowed_toolchain_types = [
"//toolchain:test_toolchain",
],
)
platform(
name = "allows_all",
)
""");
addToolchain("toolchain", "toolchain_impl", ImmutableList.of(), ImmutableList.of(), "baz");
rewriteModuleDotBazel(
"""
register_toolchains("//toolchain:toolchain_impl")
register_execution_platforms("//allowed:allows_single_toolchain", "//allowed:allows_all")
""");

ToolchainContextKey key = ToolchainContextKey.key().configurationKey(targetConfigKey).build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);

assertThatEvaluationResult(result).hasNoError();
UnloadedToolchainContext unloadedToolchainContext = result.get(key);
assertThat(unloadedToolchainContext).isNotNull();

// The platform requires a toolchain type, so it is not selected.
assertThat(unloadedToolchainContext).hasExecutionPlatform("//allowed:allows_all");
}

@Test
public void resolve_checkPlatformAllowedToolchains_noToolchainType_noneRequested()
throws Exception {
// Define two new execution platforms, only one of which is compatible with the test toolchain.
scratch.file(
"allowed/BUILD",
"""
platform(
name = "allows_none",
check_toolchain_types = True,
allowed_toolchain_types = [
# Empty, so doesn't match anything.
],
)
platform(
name = "allows_all",
)
""");
rewriteModuleDotBazel(
"""
register_execution_platforms("//allowed:allows_none", "//allowed:allows_all")
""");

ToolchainContextKey key = ToolchainContextKey.key().configurationKey(targetConfigKey).build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);

assertThatEvaluationResult(result).hasNoError();
UnloadedToolchainContext unloadedToolchainContext = result.get(key);
assertThat(unloadedToolchainContext).isNotNull();

// The platform doesn't have any toolchains specified.
assertThat(unloadedToolchainContext).hasExecutionPlatform("//allowed:allows_all");
}

@Test
public void resolve_checkPlatformAllowedToolchains_noToolchainType() throws Exception {
// Define two new execution platforms, only one of which is compatible with the test toolchain.
scratch.file(
"allowed/BUILD",
"""
platform(
name = "allows_none",
check_toolchain_types = True,
allowed_toolchain_types = [
# Empty, so doesn't match anything.
],
)
platform(
name = "allows_all",
)
""");
addToolchain("toolchain", "toolchain_impl", ImmutableList.of(), ImmutableList.of(), "baz");
rewriteModuleDotBazel(
"""
register_toolchains("//toolchain:toolchain_impl")
register_execution_platforms("//allowed:allows_none", "//allowed:allows_all")
""");

ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(testToolchainType)
.build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);

assertThatEvaluationResult(result).hasNoError();
UnloadedToolchainContext unloadedToolchainContext = result.get(key);
assertThat(unloadedToolchainContext).isNotNull();

// The platform doesn't have any toolchains specified, but the request does.
assertThat(unloadedToolchainContext).hasExecutionPlatform("//allowed:allows_all");
}
}

0 comments on commit 5ce6fa6

Please sign in to comment.