Skip to content

Commit

Permalink
Add ToolchainTypeRequirement to ToolchainTestCase.
Browse files Browse the repository at this point in the history
Part of Optional Toolchains (bazelbuild#14726).
  • Loading branch information
katre committed Apr 5, 2022
1 parent 5f6a121 commit ff0088d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ public void load() throws Exception {
ToolchainContextKey toolchainContextKey =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.build();

// Create a static UnloadedToolchainContext.
UnloadedToolchainContext unloadedToolchainContext =
UnloadedToolchainContextImpl.builder(toolchainContextKey)
.setExecutionPlatform(linuxPlatform)
.setTargetPlatform(linuxPlatform)
.setRequiredToolchainTypes(ImmutableSet.of(testToolchainType))
.setToolchainTypes(ImmutableSet.of(testToolchainType))
.setRequestedLabelToToolchainType(
ImmutableMap.of(testToolchainTypeLabel, testToolchainType))
ImmutableMap.of(testToolchainTypeLabel, testToolchainTypeInfo))
.setToolchainTypeToResolved(
ImmutableSetMultimap.<ToolchainTypeInfo, Label>builder()
.put(
testToolchainType,
testToolchainTypeInfo,
Label.parseAbsoluteUnchecked("//extra:extra_toolchain_linux_impl"))
.build())
.build();
Expand Down Expand Up @@ -102,20 +102,20 @@ public void load_aliasedToolchain() throws Exception {
ToolchainContextKey toolchainContextKey =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.build();

// Create a static UnloadedToolchainContext.
UnloadedToolchainContext unloadedToolchainContext =
UnloadedToolchainContextImpl.builder(toolchainContextKey)
.setExecutionPlatform(linuxPlatform)
.setTargetPlatform(linuxPlatform)
.setRequiredToolchainTypes(ImmutableSet.of(testToolchainType))
.setToolchainTypes(ImmutableSet.of(testToolchainType))
.setRequestedLabelToToolchainType(
ImmutableMap.of(testToolchainTypeLabel, testToolchainType))
ImmutableMap.of(testToolchainTypeLabel, testToolchainTypeInfo))
.setToolchainTypeToResolved(
ImmutableSetMultimap.<ToolchainTypeInfo, Label>builder()
.put(testToolchainType, Label.parseAbsoluteUnchecked("//alias:toolchain"))
.put(testToolchainTypeInfo, Label.parseAbsoluteUnchecked("//alias:toolchain"))
.build())
.build();

Expand Down Expand Up @@ -144,20 +144,22 @@ public void load_notToolchain() throws Exception {
ToolchainContextKey toolchainContextKey =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.build();

// Create a static UnloadedToolchainContext.
UnloadedToolchainContext unloadedToolchainContext =
UnloadedToolchainContextImpl.builder(toolchainContextKey)
.setExecutionPlatform(linuxPlatform)
.setTargetPlatform(linuxPlatform)
.setRequiredToolchainTypes(ImmutableSet.of(testToolchainType))
.setToolchainTypes(ImmutableSet.of(testToolchainType))
.setRequestedLabelToToolchainType(
ImmutableMap.of(testToolchainTypeLabel, testToolchainType))
ImmutableMap.of(testToolchainTypeLabel, testToolchainTypeInfo))
.setToolchainTypeToResolved(
ImmutableSetMultimap.<ToolchainTypeInfo, Label>builder()
.put(testToolchainType, Label.parseAbsoluteUnchecked("//foo:not_a_toolchain"))
.put(
testToolchainTypeInfo,
Label.parseAbsoluteUnchecked("//foo:not_a_toolchain"))
.build())
.build();

Expand All @@ -179,7 +181,10 @@ public void load_withTemplateVariables() throws Exception {
// Add new toolchain rule that provides template variables.
Label variableToolchainTypeLabel =
Label.parseAbsoluteUnchecked("//variable:variable_toolchain_type");
ToolchainTypeInfo variableToolchainType = ToolchainTypeInfo.create(variableToolchainTypeLabel);
ToolchainTypeRequirement variableToolchainType =
ToolchainTypeRequirement.create(variableToolchainTypeLabel);
ToolchainTypeInfo variableToolchainTypeInfo =
ToolchainTypeInfo.create(variableToolchainTypeLabel);
scratch.file(
"variable/variable_toolchain_def.bzl",
"def _impl(ctx):",
Expand All @@ -203,21 +208,21 @@ public void load_withTemplateVariables() throws Exception {
ToolchainContextKey toolchainContextKey =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.build();

// Create a static UnloadedToolchainContext.
UnloadedToolchainContext unloadedToolchainContext =
UnloadedToolchainContextImpl.builder(toolchainContextKey)
.setExecutionPlatform(linuxPlatform)
.setTargetPlatform(linuxPlatform)
.setRequiredToolchainTypes(ImmutableSet.of(variableToolchainType))
.setToolchainTypes(ImmutableSet.of(variableToolchainType))
.setRequestedLabelToToolchainType(
ImmutableMap.of(variableToolchainTypeLabel, variableToolchainType))
ImmutableMap.of(variableToolchainTypeLabel, variableToolchainTypeInfo))
.setToolchainTypeToResolved(
ImmutableSetMultimap.<ToolchainTypeInfo, Label>builder()
.put(
variableToolchainType,
variableToolchainTypeInfo,
Label.parseAbsoluteUnchecked("//variable:variable_toolchain_impl"))
.build())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ java_library(
name = "testutil",
srcs = TESTUTIL_SRCS,
deps = [
"//src/main/java/com/google/devtools/build/lib/analysis:config/toolchain_type_requirement",
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.collect.ImmutableList;
import com.google.common.truth.IterableSubject;
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
import com.google.devtools.build.lib.analysis.platform.ConstraintSettingInfo;
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
import com.google.devtools.build.lib.analysis.platform.DeclaredToolchainInfo;
Expand Down Expand Up @@ -50,7 +51,8 @@ public abstract class ToolchainTestCase extends BuildViewTestCase {
public ConstraintValueInfo defaultedConstraint;

public Label testToolchainTypeLabel;
public ToolchainTypeInfo testToolchainType;
public ToolchainTypeRequirement testToolchainType;
public ToolchainTypeInfo testToolchainTypeInfo;

protected static IterableSubject assertToolchainLabels(
RegisteredToolchainsValue registeredToolchainsValue) {
Expand Down Expand Up @@ -195,7 +197,8 @@ public void createToolchains() throws Exception {
"bar");

testToolchainTypeLabel = Label.parseAbsoluteUnchecked("//toolchain:test_toolchain");
testToolchainType = ToolchainTypeInfo.create(testToolchainTypeLabel);
testToolchainType = ToolchainTypeRequirement.create(testToolchainTypeLabel);
testToolchainTypeInfo = ToolchainTypeInfo.create(testToolchainTypeLabel);
}

protected EvaluationResult<RegisteredToolchainsValue> requestToolchainsFromSkyframe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ public void testRegisteredToolchains() throws Exception {
// Check that the number of toolchains created for this test is correct.
assertThat(
value.registeredToolchains().stream()
.filter(toolchain -> toolchain.toolchainType().equals(testToolchainType))
.filter(toolchain -> toolchain.toolchainType().equals(testToolchainTypeInfo))
.collect(Collectors.toList()))
.hasSize(2);

assertThat(
value.registeredToolchains().stream()
.anyMatch(
toolchain ->
toolchain.toolchainType().equals(testToolchainType)
toolchain.toolchainType().equals(testToolchainTypeInfo)
&& toolchain.execConstraints().get(setting).equals(linuxConstraint)
&& toolchain.targetConstraints().get(setting).equals(macConstraint)
&& toolchain
Expand All @@ -108,7 +108,7 @@ public void testRegisteredToolchains() throws Exception {
value.registeredToolchains().stream()
.anyMatch(
toolchain ->
toolchain.toolchainType().equals(testToolchainType)
toolchain.toolchainType().equals(testToolchainTypeInfo)
&& toolchain.execConstraints().get(setting).equals(macConstraint)
&& toolchain.targetConstraints().get(setting).equals(linuxConstraint)
&& toolchain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,33 +143,33 @@ public void testToolchainResolutionValue_equalsAndHashCode() {
new EqualsTester()
.addEqualityGroup(
SingleToolchainResolutionValue.create(
testToolchainType,
testToolchainTypeInfo,
ImmutableMap.of(
linuxCtkey, Label.parseAbsoluteUnchecked("//test:toolchain_impl_1"))),
SingleToolchainResolutionValue.create(
testToolchainType,
testToolchainTypeInfo,
ImmutableMap.of(
linuxCtkey, Label.parseAbsoluteUnchecked("//test:toolchain_impl_1"))))
// Different execution platform, same label.
.addEqualityGroup(
SingleToolchainResolutionValue.create(
testToolchainType,
testToolchainTypeInfo,
ImmutableMap.of(macCtkey, Label.parseAbsoluteUnchecked("//test:toolchain_impl_1"))))
// Same execution platform, different label.
.addEqualityGroup(
SingleToolchainResolutionValue.create(
testToolchainType,
testToolchainTypeInfo,
ImmutableMap.of(
linuxCtkey, Label.parseAbsoluteUnchecked("//test:toolchain_impl_2"))))
// Different execution platform, different label.
.addEqualityGroup(
SingleToolchainResolutionValue.create(
testToolchainType,
testToolchainTypeInfo,
ImmutableMap.of(macCtkey, Label.parseAbsoluteUnchecked("//test:toolchain_impl_2"))))
// Multiple execution platforms.
.addEqualityGroup(
SingleToolchainResolutionValue.create(
testToolchainType,
testToolchainTypeInfo,
ImmutableMap.<ConfiguredTargetKey, Label>builder()
.put(linuxCtkey, Label.parseAbsoluteUnchecked("//test:toolchain_impl_1"))
.put(macCtkey, Label.parseAbsoluteUnchecked("//test:toolchain_impl_1"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void resolve() throws Exception {
ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);
Expand Down Expand Up @@ -187,7 +187,7 @@ public void resolve_unavailableToolchainType_single() throws Exception {
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(
ToolchainTypeRequirement.create(testToolchainTypeLabel),
testToolchainType,
ToolchainTypeRequirement.create(
Label.parseAbsoluteUnchecked("//fake/toolchain:type_1")))
.build();
Expand All @@ -210,7 +210,7 @@ public void resolve_unavailableToolchainType_multiple() throws Exception {
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(
ToolchainTypeRequirement.create(testToolchainTypeLabel),
testToolchainType,
ToolchainTypeRequirement.create(
Label.parseAbsoluteUnchecked("//fake/toolchain:type_1")),
ToolchainTypeRequirement.create(
Expand All @@ -233,7 +233,7 @@ public void resolve_invalidTargetPlatform_badTarget() throws Exception {
ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);
Expand All @@ -259,7 +259,7 @@ public void resolve_invalidTargetPlatform_badPackage() throws Exception {
ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);
Expand All @@ -283,7 +283,7 @@ public void resolve_invalidHostPlatform() throws Exception {
ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);
Expand All @@ -308,7 +308,7 @@ public void resolve_invalidExecutionPlatform() throws Exception {
ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.build();

EvaluationResult<UnloadedToolchainContext> result = invokeToolchainResolution(key);
Expand Down Expand Up @@ -349,7 +349,7 @@ public void resolve_execConstraints() throws Exception {
ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.execConstraintLabels(Label.parseAbsoluteUnchecked("//constraints:linux"))
.build();

Expand All @@ -370,7 +370,7 @@ public void resolve_execConstraints_invalid() throws Exception {
ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.execConstraintLabels(Label.parseAbsoluteUnchecked("//platforms:linux"))
.build();

Expand Down Expand Up @@ -461,7 +461,7 @@ public void resolve_forceExecutionPlatform() throws Exception {
ToolchainContextKey key =
ToolchainContextKey.key()
.configurationKey(targetConfigKey)
.toolchainTypes(ToolchainTypeRequirement.create(testToolchainTypeLabel))
.toolchainTypes(testToolchainType)
.forceExecutionPlatform(Label.parseAbsoluteUnchecked("//platforms:linux"))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void testToolchainTypeLookup() throws Exception {

Map<Label, ToolchainTypeInfo> toolchainTypes = result.get(key).toolchainTypes();
assertThat(toolchainTypes)
.containsExactlyEntriesIn(ImmutableMap.of(testToolchainTypeLabel, testToolchainType));
.containsExactlyEntriesIn(ImmutableMap.of(testToolchainTypeLabel, testToolchainTypeInfo));
}

@Test
Expand All @@ -111,9 +111,9 @@ public void testToolchainTypeLookup_toolchainAlias() throws Exception {
.containsExactlyEntriesIn(
ImmutableMap.of(
testToolchainTypeLabel,
testToolchainType,
testToolchainTypeInfo,
aliasToolchainTypeLabel,
testToolchainType));
testToolchainTypeInfo));
}

@Test
Expand Down

0 comments on commit ff0088d

Please sign in to comment.