diff options
Diffstat (limited to 'src/test/java/com/google/devtools')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/packages/RuleClassTest.java | 143 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/skyframe/ToolchainUtilTest.java | 104 |
2 files changed, 240 insertions, 7 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/packages/RuleClassTest.java b/src/test/java/com/google/devtools/build/lib/packages/RuleClassTest.java index b9b2f4c33c..088e7db81e 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/RuleClassTest.java +++ b/src/test/java/com/google/devtools/build/lib/packages/RuleClassTest.java @@ -25,6 +25,7 @@ import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; import static com.google.devtools.build.lib.syntax.Type.INTEGER; import static com.google.devtools.build.lib.syntax.Type.STRING; import static com.google.devtools.build.lib.syntax.Type.STRING_LIST; +import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; import static org.junit.Assert.fail; import com.google.common.base.Function; @@ -50,6 +51,7 @@ import com.google.devtools.build.lib.packages.Attribute.ValidityPredicate; import com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy.MissingFragmentPolicy; import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType; import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory; +import com.google.devtools.build.lib.packages.RuleClass.ExecutionPlatformConstraintsAllowed; import com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap; import com.google.devtools.build.lib.packages.util.PackageLoadingTestCase; import com.google.devtools.build.lib.syntax.BaseFunction; @@ -892,9 +894,12 @@ public class RuleClassTest extends PackageLoadingTestCase { .setMissingFragmentPolicy(missingFragmentPolicy) .build(), supportsConstraintChecking, - /*requiredToolchains=*/ ImmutableSet.<Label>of(), + /*requiredToolchains=*/ ImmutableSet.of(), /*supportsPlatforms=*/ true, - OutputFile.Kind.FILE, ImmutableList.copyOf(attributes)); + ExecutionPlatformConstraintsAllowed.PER_RULE, + /* executionPlatformConstraints= */ ImmutableSet.of(), + OutputFile.Kind.FILE, + ImmutableList.copyOf(attributes)); } private static RuleClass createParentRuleClass() { @@ -1035,8 +1040,7 @@ public class RuleClassTest extends PackageLoadingTestCase { .add(attr("tags", STRING_LIST)); ruleClassBuilder.addRequiredToolchains( - ImmutableList.of( - Label.parseAbsolute("//toolchain:tc1"), Label.parseAbsolute("//toolchain:tc2"))); + Label.parseAbsolute("//toolchain:tc1"), Label.parseAbsolute("//toolchain:tc2")); RuleClass ruleClass = ruleClassBuilder.build(); @@ -1044,4 +1048,135 @@ public class RuleClassTest extends PackageLoadingTestCase { .containsExactly( Label.parseAbsolute("//toolchain:tc1"), Label.parseAbsolute("//toolchain:tc2")); } + + @Test + public void testExecutionPlatformConstraints_perRule() throws Exception { + RuleClass.Builder ruleClassBuilder = + new RuleClass.Builder("ruleClass", RuleClassType.NORMAL, false) + .factory(DUMMY_CONFIGURED_TARGET_FACTORY) + .add(attr("tags", STRING_LIST)) + .executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_RULE); + + ruleClassBuilder.addExecutionPlatformConstraints( + Label.parseAbsolute("//constraints:cv1"), Label.parseAbsolute("//constraints:cv2")); + + RuleClass ruleClass = ruleClassBuilder.build(); + + assertThat(ruleClass.getExecutionPlatformConstraints()) + .containsExactly( + Label.parseAbsolute("//constraints:cv1"), Label.parseAbsolute("//constraints:cv2")); + assertThat(ruleClass.hasAttr("exec_compatible_with", LABEL_LIST)).isFalse(); + } + + @Test + public void testExecutionPlatformConstraints_perTarget() { + RuleClass.Builder ruleClassBuilder = + new RuleClass.Builder("ruleClass", RuleClassType.NORMAL, false) + .factory(DUMMY_CONFIGURED_TARGET_FACTORY) + .add(attr("tags", STRING_LIST)); + + ruleClassBuilder.executionPlatformConstraintsAllowed( + ExecutionPlatformConstraintsAllowed.PER_TARGET); + + RuleClass ruleClass = ruleClassBuilder.build(); + + assertThat(ruleClass.executionPlatformConstraintsAllowed()) + .isEqualTo(ExecutionPlatformConstraintsAllowed.PER_TARGET); + assertThat(ruleClass.hasAttr("exec_compatible_with", LABEL_LIST)).isTrue(); + } + + @Test + public void testExecutionPlatformConstraints_inheritConstraintsFromParent() throws Exception { + RuleClass parentRuleClass = + new RuleClass.Builder("$parentRuleClass", RuleClassType.ABSTRACT, false) + .add(attr("tags", STRING_LIST)) + .executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_RULE) + .addExecutionPlatformConstraints( + Label.parseAbsolute("//constraints:cv1"), Label.parseAbsolute("//constraints:cv2")) + .build(); + + RuleClass childRuleClass = + new RuleClass.Builder("childRuleClass", RuleClassType.NORMAL, false, parentRuleClass) + .factory(DUMMY_CONFIGURED_TARGET_FACTORY) + .build(); + + assertThat(childRuleClass.getExecutionPlatformConstraints()) + .containsExactly( + Label.parseAbsolute("//constraints:cv1"), Label.parseAbsolute("//constraints:cv2")); + assertThat(childRuleClass.hasAttr("exec_compatible_with", LABEL_LIST)).isFalse(); + } + + @Test + public void testExecutionPlatformConstraints_inheritAndAddConstraints() throws Exception { + RuleClass parentRuleClass = + new RuleClass.Builder("$parentRuleClass", RuleClassType.ABSTRACT, false) + .add(attr("tags", STRING_LIST)) + .build(); + + RuleClass.Builder childRuleClassBuilder = + new RuleClass.Builder("childRuleClass", RuleClassType.NORMAL, false, parentRuleClass) + .factory(DUMMY_CONFIGURED_TARGET_FACTORY) + .executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_RULE) + .addExecutionPlatformConstraints( + Label.parseAbsolute("//constraints:cv1"), Label.parseAbsolute("//constraints:cv2")); + + RuleClass childRuleClass = childRuleClassBuilder.build(); + + assertThat(childRuleClass.getExecutionPlatformConstraints()) + .containsExactly( + Label.parseAbsolute("//constraints:cv1"), Label.parseAbsolute("//constraints:cv2")); + assertThat(childRuleClass.hasAttr("exec_compatible_with", LABEL_LIST)).isFalse(); + } + + @Test + public void testExecutionPlatformConstraints_inherit_parentAllowsPerTarget() { + RuleClass parentRuleClass = + new RuleClass.Builder("parentRuleClass", RuleClassType.NORMAL, false) + .factory(DUMMY_CONFIGURED_TARGET_FACTORY) + .add(attr("tags", STRING_LIST)) + .executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_TARGET) + .build(); + + RuleClass.Builder childRuleClassBuilder = + new RuleClass.Builder("childRuleClass", RuleClassType.NORMAL, false, parentRuleClass) + .factory(DUMMY_CONFIGURED_TARGET_FACTORY) + .executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_RULE); + + RuleClass childRuleClass = childRuleClassBuilder.build(); + + assertThat(childRuleClass.hasAttr("exec_compatible_with", LABEL_LIST)).isFalse(); + } + + @Test + public void testExecutionPlatformConstraints_inherit_childAllowsPerTarget() { + RuleClass parentRuleClass = + new RuleClass.Builder("$parentRuleClass", RuleClassType.ABSTRACT, false) + .add(attr("tags", STRING_LIST)) + .executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_RULE) + .build(); + + RuleClass.Builder childRuleClassBuilder = + new RuleClass.Builder("childRuleClass", RuleClassType.NORMAL, false, parentRuleClass) + .factory(DUMMY_CONFIGURED_TARGET_FACTORY) + .executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_TARGET); + + RuleClass childRuleClass = childRuleClassBuilder.build(); + + assertThat(childRuleClass.hasAttr("exec_compatible_with", LABEL_LIST)).isTrue(); + } + + @Test + public void testExecutionPlatformConstraints_extraExecCompatibleWithAttribute() { + RuleClass.Builder ruleClassBuilder = + new RuleClass.Builder("ruleClass", RuleClassType.NORMAL, false) + .factory(DUMMY_CONFIGURED_TARGET_FACTORY) + .add(attr("tags", STRING_LIST)); + + ruleClassBuilder.add( + attr("exec_compatible_with", LABEL_LIST).allowedFileTypes().value(ImmutableList.of())); + ruleClassBuilder.executionPlatformConstraintsAllowed( + ExecutionPlatformConstraintsAllowed.PER_TARGET); + + assertThrows(IllegalStateException.class, () -> ruleClassBuilder.build()); + } } diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainUtilTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainUtilTest.java index 7b2f76cb2c..9c3d251cc1 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainUtilTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainUtilTest.java @@ -26,6 +26,7 @@ import com.google.devtools.build.lib.analysis.ToolchainContext; import com.google.devtools.build.lib.analysis.util.AnalysisMock; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.rules.platform.ToolchainTestCase; +import com.google.devtools.build.lib.skyframe.ToolchainUtil.InvalidConstraintValueException; import com.google.devtools.build.lib.skyframe.ToolchainUtil.InvalidPlatformException; import com.google.devtools.build.lib.skyframe.ToolchainUtil.ToolchainContextException; import com.google.devtools.build.lib.skyframe.ToolchainUtil.UnresolvedToolchainsException; @@ -258,6 +259,82 @@ public class ToolchainUtilTest extends ToolchainTestCase { .contains("//invalid:not_a_platform"); } + @Test + public void createToolchainContext_execConstraints() throws Exception { + // This should select platform linux, toolchain extra_toolchain_linux, due to extra constraints, + // even though platform mac is registered first. + addToolchain( + /* packageName= */ "extra", + /* toolchainName= */ "extra_toolchain_linux", + /* execConstraints= */ ImmutableList.of("//constraints:linux"), + /* targetConstraints= */ ImmutableList.of("//constraints:linux"), + /* data= */ "baz"); + addToolchain( + /* packageName= */ "extra", + /* toolchainName= */ "extra_toolchain_mac", + /* execConstraints= */ ImmutableList.of("//constraints:mac"), + /* targetConstraints= */ ImmutableList.of("//constraints:linux"), + /* data= */ "baz"); + rewriteWorkspace( + "register_toolchains('//extra:extra_toolchain_linux', '//extra:extra_toolchain_mac')", + "register_execution_platforms('//platforms:mac', '//platforms:linux')"); + + useConfiguration("--platforms=//platforms:linux"); + CreateToolchainContextKey key = + CreateToolchainContextKey.create( + "test", + ImmutableSet.of(testToolchainType), + ImmutableSet.of(Label.parseAbsoluteUnchecked("//constraints:linux")), + targetConfigKey); + + EvaluationResult<CreateToolchainContextValue> result = createToolchainContext(key); + + assertThatEvaluationResult(result).hasNoError(); + ToolchainContext toolchainContext = result.get(key).toolchainContext(); + assertThat(toolchainContext).isNotNull(); + + assertThat(toolchainContext.getRequiredToolchains()).containsExactly(testToolchainType); + assertThat(toolchainContext.getResolvedToolchainLabels()) + .containsExactly(Label.parseAbsoluteUnchecked("//extra:extra_toolchain_linux_impl")); + + assertThat(toolchainContext.getExecutionPlatform()).isNotNull(); + assertThat(toolchainContext.getExecutionPlatform().label()) + .isEqualTo(Label.parseAbsoluteUnchecked("//platforms:linux")); + + assertThat(toolchainContext.getTargetPlatform()).isNotNull(); + assertThat(toolchainContext.getTargetPlatform().label()) + .isEqualTo(Label.parseAbsoluteUnchecked("//platforms:linux")); + } + + @Test + public void createToolchainContext_execConstraints_invalid() throws Exception { + CreateToolchainContextKey key = + CreateToolchainContextKey.create( + "test", + ImmutableSet.of(testToolchainType), + ImmutableSet.of(Label.parseAbsoluteUnchecked("//platforms:linux")), + targetConfigKey); + + EvaluationResult<CreateToolchainContextValue> result = createToolchainContext(key); + + assertThatEvaluationResult(result).hasError(); + assertThatEvaluationResult(result) + .hasErrorEntryForKeyThat(key) + .hasExceptionThat() + .isInstanceOf(ToolchainContextException.class); + assertThatEvaluationResult(result) + .hasErrorEntryForKeyThat(key) + .hasExceptionThat() + .hasCauseThat() + .isInstanceOf(InvalidConstraintValueException.class); + assertThatEvaluationResult(result) + .hasErrorEntryForKeyThat(key) + .hasExceptionThat() + .hasCauseThat() + .hasMessageThat() + .contains("//platforms:linux"); + } + // Calls ToolchainUtil.createToolchainContext. private static final SkyFunctionName CREATE_TOOLCHAIN_CONTEXT_FUNCTION = SkyFunctionName.create("CREATE_TOOLCHAIN_CONTEXT_FUNCTION"); @@ -271,7 +348,9 @@ public class ToolchainUtilTest extends ToolchainTestCase { abstract String targetDescription(); - abstract Set<Label> requiredToolchains(); + abstract ImmutableSet<Label> requiredToolchains(); + + abstract ImmutableSet<Label> execConstraintLabels(); abstract BuildConfigurationValue.Key configurationKey(); @@ -279,8 +358,23 @@ public class ToolchainUtilTest extends ToolchainTestCase { String targetDescription, Set<Label> requiredToolchains, BuildConfigurationValue.Key configurationKey) { + return create( + targetDescription, + requiredToolchains, + /* execConstraintLabels= */ ImmutableSet.of(), + configurationKey); + } + + public static CreateToolchainContextKey create( + String targetDescription, + Set<Label> requiredToolchains, + Set<Label> execConstraintLabels, + BuildConfigurationValue.Key configurationKey) { return new AutoValue_ToolchainUtilTest_CreateToolchainContextKey( - targetDescription, requiredToolchains, configurationKey); + targetDescription, + ImmutableSet.copyOf(requiredToolchains), + ImmutableSet.copyOf(execConstraintLabels), + configurationKey); } } @@ -325,7 +419,11 @@ public class ToolchainUtilTest extends ToolchainTestCase { try { toolchainContext = ToolchainUtil.createToolchainContext( - env, key.targetDescription(), key.requiredToolchains(), key.configurationKey()); + env, + key.targetDescription(), + key.requiredToolchains(), + key.execConstraintLabels(), + key.configurationKey()); if (toolchainContext == null) { return null; } |