diff options
author | 2017-05-25 22:31:39 +0200 | |
---|---|---|
committer | 2017-05-26 09:37:53 +0200 | |
commit | 04c090adfafd5d6715c4cb17446b7d62607e9a0b (patch) | |
tree | dacb0b00f0479e9b13614197df1b7cc63d4a0f95 /src | |
parent | 3a035d0d76cb46f9ac5fe314b42cfda123ddd9b9 (diff) |
Make platform providers creatable from Skylark.
This will allow custom rules to interact more fully with the platform system.
Change-Id: I22dd2efab55b1c6e6129b1ba99fb5f0aa9c2d6b2
PiperOrigin-RevId: 157145828
Diffstat (limited to 'src')
11 files changed, 516 insertions, 178 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfo.java b/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfo.java index 817580e8fa..daab127de5 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfo.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfo.java @@ -14,15 +14,20 @@ package com.google.devtools.build.lib.analysis.platform; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.ClassObjectConstructor; import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; import com.google.devtools.build.lib.packages.SkylarkClassObject; import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; +import com.google.devtools.build.lib.syntax.EvalException; +import com.google.devtools.build.lib.syntax.FunctionSignature; +import com.google.devtools.build.lib.syntax.SkylarkType; /** Provider for a platform constraint setting that is available to be fulfilled. */ @SkylarkModule( @@ -36,9 +41,29 @@ public class ConstraintSettingInfo extends SkylarkClassObject { /** Name used in Skylark for accessing this provider. */ public static final String SKYLARK_NAME = "ConstraintSettingInfo"; + private static final FunctionSignature.WithValues<Object, SkylarkType> SIGNATURE = + FunctionSignature.WithValues.create( + FunctionSignature.of( + /*numMandatoryPositionals=*/ 1, + /*numOptionalPositionals=*/ 0, + /*numMandatoryNamedOnly*/ 0, + /*starArg=*/ false, + /*kwArg=*/ false, + /*names=*/ "label"), + /*defaultValues=*/ null, + /*types=*/ ImmutableList.<SkylarkType>of(SkylarkType.of(Label.class))); + /** Skylark constructor and identifier for this provider. */ public static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = - new NativeClassObjectConstructor(SKYLARK_NAME) {}; + new NativeClassObjectConstructor(SKYLARK_NAME, SIGNATURE) { + @Override + protected ConstraintSettingInfo createInstanceFromSkylark(Object[] args, Location loc) + throws EvalException { + // Based on SIGNATURE above, the args are label. + Label label = (Label) args[0]; + return ConstraintSettingInfo.create(label, loc); + } + }; /** Identifier used to retrieve this provider from rules which export it. */ public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = @@ -46,8 +71,8 @@ public class ConstraintSettingInfo extends SkylarkClassObject { private final Label label; - private ConstraintSettingInfo(Label label) { - super(SKYLARK_CONSTRUCTOR, ImmutableMap.<String, Object>of("label", label)); + private ConstraintSettingInfo(Label label, Location location) { + super(SKYLARK_CONSTRUCTOR, ImmutableMap.<String, Object>of("label", label), location); this.label = label; } @@ -58,6 +83,11 @@ public class ConstraintSettingInfo extends SkylarkClassObject { /** Returns a new {@link ConstraintSettingInfo} with the given data. */ public static ConstraintSettingInfo create(Label constraintSetting) { - return new ConstraintSettingInfo(constraintSetting); + return create(constraintSetting, Location.BUILTIN); + } + + /** Returns a new {@link ConstraintSettingInfo} with the given data. */ + public static ConstraintSettingInfo create(Label constraintSetting, Location location) { + return new ConstraintSettingInfo(constraintSetting, location); } } diff --git a/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfo.java b/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfo.java index 9e58126264..ca6ad9b2dd 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfo.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfo.java @@ -14,15 +14,20 @@ package com.google.devtools.build.lib.analysis.platform; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.ClassObjectConstructor; import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; import com.google.devtools.build.lib.packages.SkylarkClassObject; import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; +import com.google.devtools.build.lib.syntax.EvalException; +import com.google.devtools.build.lib.syntax.FunctionSignature; +import com.google.devtools.build.lib.syntax.SkylarkType; /** Provider for a platform constraint value that fulfills a {@link ConstraintSettingInfo}. */ @SkylarkModule( @@ -36,9 +41,32 @@ public class ConstraintValueInfo extends SkylarkClassObject { /** Name used in Skylark for accessing this provider. */ public static final String SKYLARK_NAME = "ConstraintValueInfo"; + private static final FunctionSignature.WithValues<Object, SkylarkType> SIGNATURE = + FunctionSignature.WithValues.create( + FunctionSignature.of( + /*numMandatoryPositionals=*/ 2, + /*numOptionalPositionals=*/ 0, + /*numMandatoryNamedOnly*/ 0, + /*starArg=*/ false, + /*kwArg=*/ false, + /*names=*/ "label", + "constraint_setting"), + /*defaultValues=*/ null, + /*types=*/ ImmutableList.<SkylarkType>of( + SkylarkType.of(Label.class), SkylarkType.of(ConstraintSettingInfo.class))); + /** Skylark constructor and identifier for this provider. */ public static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = - new NativeClassObjectConstructor(SKYLARK_NAME) {}; + new NativeClassObjectConstructor(SKYLARK_NAME, SIGNATURE) { + @Override + protected ConstraintValueInfo createInstanceFromSkylark(Object[] args, Location loc) + throws EvalException { + // Based on SIGNATURE above, the args are label, constraint_setting. + Label label = (Label) args[0]; + ConstraintSettingInfo constraint = (ConstraintSettingInfo) args[1]; + return ConstraintValueInfo.create(constraint, label, loc); + } + }; /** Identifier used to retrieve this provider from rules which export it. */ public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = @@ -47,12 +75,13 @@ public class ConstraintValueInfo extends SkylarkClassObject { private final ConstraintSettingInfo constraint; private final Label label; - private ConstraintValueInfo(ConstraintSettingInfo constraint, Label label) { + private ConstraintValueInfo(ConstraintSettingInfo constraint, Label label, Location location) { super( SKYLARK_CONSTRUCTOR, ImmutableMap.<String, Object>of( "constraint", constraint, - "label", label)); + "label", label), + location); this.constraint = constraint; this.label = label; @@ -68,6 +97,12 @@ public class ConstraintValueInfo extends SkylarkClassObject { /** Returns a new {@link ConstraintValueInfo} with the given data. */ public static ConstraintValueInfo create(ConstraintSettingInfo constraint, Label value) { - return new ConstraintValueInfo(constraint, value); + return create(constraint, value, Location.BUILTIN); + } + + /** Returns a new {@link ConstraintValueInfo} with the given data. */ + public static ConstraintValueInfo create( + ConstraintSettingInfo constraint, Label value, Location location) { + return new ConstraintValueInfo(constraint, value, location); } } diff --git a/src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformInfo.java b/src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformInfo.java index 64b9262adb..c800415020 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformInfo.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformInfo.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.ClassObjectConstructor; import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; import com.google.devtools.build.lib.packages.SkylarkClassObject; @@ -27,6 +28,10 @@ import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; +import com.google.devtools.build.lib.syntax.EvalException; +import com.google.devtools.build.lib.syntax.FunctionSignature; +import com.google.devtools.build.lib.syntax.SkylarkList; +import com.google.devtools.build.lib.syntax.SkylarkType; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -44,9 +49,38 @@ public class PlatformInfo extends SkylarkClassObject { /** Name used in Skylark for accessing this provider. */ public static final String SKYLARK_NAME = "PlatformInfo"; + private static final FunctionSignature.WithValues<Object, SkylarkType> SIGNATURE = + FunctionSignature.WithValues.create( + FunctionSignature.of( + /*numMandatoryPositionals=*/ 1, + /*numOptionalPositionals=*/ 0, + /*numMandatoryNamedOnly*/ 0, + /*starArg=*/ false, + /*kwArg=*/ false, + /*names=*/ "constraint_values"), + /*defaultValues=*/ null, + /*types=*/ ImmutableList.<SkylarkType>of( + SkylarkType.Combination.of( + SkylarkType.LIST, SkylarkType.of(ConstraintValueInfo.class)))); + /** Skylark constructor and identifier for this provider. */ public static final ClassObjectConstructor SKYLARK_CONSTRUCTOR = - new NativeClassObjectConstructor(SKYLARK_NAME) {}; + new NativeClassObjectConstructor(SKYLARK_NAME, SIGNATURE) { + @Override + protected PlatformInfo createInstanceFromSkylark(Object[] args, Location loc) + throws EvalException { + // Based on SIGNATURE above, the args are constraint_values. + + SkylarkList<ConstraintValueInfo> constraintValues = + (SkylarkList<ConstraintValueInfo>) args[0]; + try { + return builder().addConstraints(constraintValues).setLocation(loc).build(); + } catch (DuplicateConstraintException dce) { + throw new EvalException( + loc, String.format("Cannot create PlatformInfo: %s", dce.getMessage())); + } + } + }; /** Identifier used to retrieve this provider from rules which export it. */ public static final SkylarkProviderIdentifier SKYLARK_IDENTIFIER = @@ -57,8 +91,10 @@ public class PlatformInfo extends SkylarkClassObject { private PlatformInfo( ImmutableList<ConstraintValueInfo> constraints, - ImmutableMap<String, String> remoteExecutionProperties) { - super(SKYLARK_CONSTRUCTOR, ImmutableMap.<String, Object>of("constraints", constraints)); + ImmutableMap<String, String> remoteExecutionProperties, + Location location) { + super( + SKYLARK_CONSTRUCTOR, ImmutableMap.<String, Object>of("constraints", constraints), location); this.constraints = constraints; this.remoteExecutionProperties = remoteExecutionProperties; @@ -86,6 +122,7 @@ public class PlatformInfo extends SkylarkClassObject { public static class Builder { private final List<ConstraintValueInfo> constraints = new ArrayList<>(); private final Map<String, String> remoteExecutionProperties = new HashMap<>(); + private Location location = Location.BUILTIN; /** * Adds the given constraint value to the constraints that define this {@link PlatformInfo}. @@ -140,6 +177,17 @@ public class PlatformInfo extends SkylarkClassObject { } /** + * Sets the {@link Location} where this {@link PlatformInfo} was created. + * + * @param location the location where the instance was created + * @return the {@link Builder} instance for method chaining + */ + public Builder setLocation(Location location) { + this.location = location; + return this; + } + + /** * Returns the new {@link PlatformInfo} instance. * * @throws DuplicateConstraintException if more than one constraint value exists for the same @@ -147,7 +195,8 @@ public class PlatformInfo extends SkylarkClassObject { */ public PlatformInfo build() throws DuplicateConstraintException { ImmutableList<ConstraintValueInfo> validatedConstraints = validateConstraints(constraints); - return new PlatformInfo(validatedConstraints, ImmutableMap.copyOf(remoteExecutionProperties)); + return new PlatformInfo( + validatedConstraints, ImmutableMap.copyOf(remoteExecutionProperties), location); } private ImmutableList<ConstraintValueInfo> validateConstraints( diff --git a/src/test/java/com/google/devtools/build/lib/analysis/platform/BUILD b/src/test/java/com/google/devtools/build/lib/analysis/platform/BUILD index 1e752eeab8..6bacfd18c5 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/platform/BUILD +++ b/src/test/java/com/google/devtools/build/lib/analysis/platform/BUILD @@ -9,6 +9,7 @@ java_test( srcs = glob(["*.java"]), test_class = "com.google.devtools.build.lib.AllTests", deps = [ + "//src/main/java/com/google/devtools/build/lib:build-base", "//src/main/java/com/google/devtools/build/lib:events", "//src/main/java/com/google/devtools/build/lib:os_util", "//src/main/java/com/google/devtools/build/lib:packages-internal", @@ -16,6 +17,7 @@ java_test( "//src/main/java/com/google/devtools/build/lib:util", "//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/rules/platform", "//src/test/java/com/google/devtools/build/lib:analysis_testutil", "//src/test/java/com/google/devtools/build/lib:packages_testutil", "//src/test/java/com/google/devtools/build/lib/skylark:testutil", diff --git a/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfoTest.java b/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfoTest.java new file mode 100644 index 0000000000..ddc66d71b3 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfoTest.java @@ -0,0 +1,65 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.analysis.platform; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.testing.EqualsTester; +import com.google.devtools.build.lib.analysis.ConfiguredTarget; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.rules.platform.ConstraintSetting; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests of {@link ConstraintSettingInfo}. */ +@RunWith(JUnit4.class) +public class ConstraintSettingInfoTest extends BuildViewTestCase { + + @Test + public void constraintSetting_equalsTester() { + new EqualsTester() + .addEqualityGroup( + ConstraintSettingInfo.create(makeLabel("//constraint:basic")), + ConstraintSettingInfo.create(makeLabel("//constraint:basic"))) + .addEqualityGroup(ConstraintSettingInfo.create(makeLabel("//constraint:other"))) + .testEquals(); + } + + @Test + public void constraintSettingInfoConstructor() throws Exception { + scratch.file( + "test/platform/my_constraint_setting.bzl", + "def _impl(ctx):", + " constraint_setting = platform_common.ConstraintSettingInfo(label = ctx.label)", + " return [constraint_setting]", + "my_constraint_setting = rule(", + " implementation = _impl,", + " attrs = {", + " }", + ")"); + scratch.file( + "test/platform/BUILD", + "load('//test/platform:my_constraint_setting.bzl', 'my_constraint_setting')", + "my_constraint_setting(name = 'custom')"); + + ConfiguredTarget setting = getConfiguredTarget("//test/platform:custom"); + assertThat(setting).isNotNull(); + assertThat(ConstraintSetting.constraintSetting(setting)).isNotNull(); + assertThat(ConstraintSetting.constraintSetting(setting).label()) + .isEqualTo(Label.parseAbsolute("//test/platform:custom")); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfoTest.java b/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfoTest.java new file mode 100644 index 0000000000..9a6e6d21c3 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfoTest.java @@ -0,0 +1,82 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.analysis.platform; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.testing.EqualsTester; +import com.google.devtools.build.lib.analysis.ConfiguredTarget; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.rules.platform.ConstraintValue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests of {@link ConstraintValueInfo}. */ +@RunWith(JUnit4.class) +public class ConstraintValueInfoTest extends BuildViewTestCase { + + @Test + public void constraintValue_equalsTester() { + ConstraintSettingInfo setting1 = ConstraintSettingInfo.create(makeLabel("//constraint:basic")); + ConstraintSettingInfo setting2 = ConstraintSettingInfo.create(makeLabel("//constraint:other")); + new EqualsTester() + .addEqualityGroup( + // Base case. + ConstraintValueInfo.create(setting1, makeLabel("//constraint:value")), + ConstraintValueInfo.create(setting1, makeLabel("//constraint:value"))) + .addEqualityGroup( + // Different label. + ConstraintValueInfo.create(setting1, makeLabel("//constraint:otherValue"))) + .addEqualityGroup( + // Different setting. + ConstraintValueInfo.create(setting2, makeLabel("//constraint:otherValue"))) + .testEquals(); + } + + @Test + public void constraintValueInfoConstructor() throws Exception { + scratch.file( + "test/platform/my_constraint_value.bzl", + "def _impl(ctx):", + " setting = ctx.attr.setting[platform_common.ConstraintSettingInfo]", + " constraint_value = platform_common.ConstraintValueInfo(", + " label = ctx.label, constraint_setting = setting)", + " return [constraint_value]", + "my_constraint_value = rule(", + " implementation = _impl,", + " attrs = {", + " 'setting': attr.label(providers = [platform_common.ConstraintSettingInfo]),", + " }", + ")"); + scratch.file( + "test/platform/BUILD", + "load('//test/platform:my_constraint_value.bzl', 'my_constraint_value')", + "constraint_setting(name = 'basic')", + "my_constraint_value(", + " name = 'custom',", + " setting = ':basic',", + ")"); + + ConfiguredTarget value = getConfiguredTarget("//test/platform:custom"); + assertThat(value).isNotNull(); + assertThat(ConstraintValue.constraintValue(value)).isNotNull(); + assertThat(ConstraintValue.constraintValue(value).constraint().label()) + .isEqualTo(Label.parseAbsolute("//test/platform:basic")); + assertThat(ConstraintValue.constraintValue(value).label()) + .isEqualTo(Label.parseAbsolute("//test/platform:custom")); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformInfoTest.java b/src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformInfoTest.java new file mode 100644 index 0000000000..d8f0029011 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformInfoTest.java @@ -0,0 +1,154 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.analysis.platform; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.testing.EqualsTester; +import com.google.devtools.build.lib.analysis.ConfiguredTarget; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import com.google.devtools.build.lib.rules.platform.Platform; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests of {@link PlatformInfo}. */ +@RunWith(JUnit4.class) +public class PlatformInfoTest extends BuildViewTestCase { + @Rule public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void createPlatform() throws Exception { + scratch.file( + "constraint/BUILD", + "constraint_setting(name = 'basic')", + "constraint_value(name = 'foo',", + " constraint_setting = ':basic',", + " )"); + } + + @Test + public void platformInfo_overlappingConstraintsError() throws Exception { + ConstraintSettingInfo setting = ConstraintSettingInfo.create(makeLabel("//constraint:basic")); + + ConstraintValueInfo value1 = ConstraintValueInfo.create(setting, makeLabel("//constraint:foo")); + ConstraintValueInfo value2 = ConstraintValueInfo.create(setting, makeLabel("//constraint:bar")); + + PlatformInfo.Builder builder = + PlatformInfo.builder().addConstraint(value1).addConstraint(value2); + + expectedException.expect(PlatformInfo.DuplicateConstraintException.class); + expectedException.expectMessage( + "Duplicate constraint_values for constraint_setting //constraint:basic: " + + "//constraint:foo, //constraint:bar"); + builder.build(); + } + + @Test + public void platformInfo_equalsTester() throws Exception { + ConstraintSettingInfo setting1 = ConstraintSettingInfo.create(makeLabel("//constraint:basic")); + ConstraintSettingInfo setting2 = ConstraintSettingInfo.create(makeLabel("//constraint:other")); + + ConstraintValueInfo value1 = + ConstraintValueInfo.create(setting1, makeLabel("//constraint:value1")); + ConstraintValueInfo value2 = + ConstraintValueInfo.create(setting2, makeLabel("//constraint:value2")); + ConstraintValueInfo value3 = + ConstraintValueInfo.create(setting2, makeLabel("//constraint:value3")); + + new EqualsTester() + .addEqualityGroup( + // Base case. + PlatformInfo.builder().addConstraint(value1).addConstraint(value2).build(), + PlatformInfo.builder().addConstraint(value1).addConstraint(value2).build(), + PlatformInfo.builder() + .addConstraint(value1) + .addConstraint(value2) + .addRemoteExecutionProperty("key", "val") // execution properties are ignored. + .build()) + .addEqualityGroup( + // Extra constraint. + PlatformInfo.builder().addConstraint(value1).addConstraint(value3).build()) + .addEqualityGroup( + // Missing constraint. + PlatformInfo.builder().addConstraint(value1).build()) + .testEquals(); + } + + @Test + public void platformInfoConstructor() throws Exception { + scratch.file( + "test/platform/my_platform.bzl", + "def _impl(ctx):", + " constraints = [val[platform_common.ConstraintValueInfo] " + + "for val in ctx.attr.constraints]", + " platform = platform_common.PlatformInfo(constraint_values = constraints)", + " return [platform]", + "my_platform = rule(", + " implementation = _impl,", + " attrs = {", + " 'constraints': attr.label_list(providers = [platform_common.ConstraintValueInfo])", + " }", + ")"); + scratch.file( + "test/platform/BUILD", + "load('//test/platform:my_platform.bzl', 'my_platform')", + "my_platform(name = 'custom',", + " constraints = [", + " '//constraint:foo',", + " ])"); + + ConfiguredTarget platform = getConfiguredTarget("//test/platform:custom"); + assertThat(platform).isNotNull(); + + PlatformInfo provider = Platform.platform(platform); + assertThat(provider).isNotNull(); + assertThat(provider.constraints()).hasSize(1); + ConstraintSettingInfo constraintSetting = + ConstraintSettingInfo.create(makeLabel("//constraint:basic")); + ConstraintValueInfo constraintValue = + ConstraintValueInfo.create(constraintSetting, makeLabel("//constraint:foo")); + assertThat(provider.constraints()).containsExactly(constraintValue); + assertThat(provider.remoteExecutionProperties()).isEmpty(); + } + + @Test + public void platformInfoConstructor_error_duplicateConstraints() throws Exception { + scratch.file( + "test/platform/my_platform.bzl", + "def _impl(ctx):", + " platform = platform_common.PlatformInfo()", + " return [platform]", + "my_platform = rule(", + " implementation = _impl,", + " attrs = {", + " 'constraints': attr.label_list(providers = [platform_common.ConstraintValueInfo])", + " }", + ")"); + checkError( + "test/platform", + "custom", + "Label '//constraint:foo' is duplicated in the 'constraints' attribute of rule 'custom'", + "load('//test/platform:my_platform.bzl', 'my_platform')", + "my_platform(name = 'custom',", + " constraints = [", + " '//constraint:foo',", + " '//constraint:foo',", + " ])"); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformProvidersTest.java b/src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformProvidersTest.java deleted file mode 100644 index 35f60cd662..0000000000 --- a/src/test/java/com/google/devtools/build/lib/analysis/platform/PlatformProvidersTest.java +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright 2017 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.devtools.build.lib.analysis.platform; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.testing.EqualsTester; -import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; -import com.google.devtools.build.lib.events.Location; -import com.google.devtools.build.lib.packages.ClassObjectConstructor; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Tests of platform providers. */ -@RunWith(JUnit4.class) -public class PlatformProvidersTest extends BuildViewTestCase { - @Rule public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void constraintSetting_equalsTester() { - new EqualsTester() - .addEqualityGroup( - ConstraintSettingInfo.create(makeLabel("//constraint:basic")), - ConstraintSettingInfo.create(makeLabel("//constraint:basic"))) - .addEqualityGroup(ConstraintSettingInfo.create(makeLabel("//constraint:other"))) - .testEquals(); - } - - @Test - public void constraintValue_equalsTester() { - ConstraintSettingInfo setting1 = ConstraintSettingInfo.create(makeLabel("//constraint:basic")); - ConstraintSettingInfo setting2 = ConstraintSettingInfo.create(makeLabel("//constraint:other")); - new EqualsTester() - .addEqualityGroup( - // Base case. - ConstraintValueInfo.create(setting1, makeLabel("//constraint:value")), - ConstraintValueInfo.create(setting1, makeLabel("//constraint:value"))) - .addEqualityGroup( - // Different label. - ConstraintValueInfo.create(setting1, makeLabel("//constraint:otherValue"))) - .addEqualityGroup( - // Different setting. - ConstraintValueInfo.create(setting2, makeLabel("//constraint:ovalue"))) - .testEquals(); - } - - @Test - public void platformInfo_overlappingConstraintsError() throws Exception { - ConstraintSettingInfo setting = ConstraintSettingInfo.create(makeLabel("//constraint:basic")); - - ConstraintValueInfo value1 = ConstraintValueInfo.create(setting, makeLabel("//constraint:foo")); - ConstraintValueInfo value2 = ConstraintValueInfo.create(setting, makeLabel("//constraint:bar")); - - PlatformInfo.Builder builder = - PlatformInfo.builder().addConstraint(value1).addConstraint(value2); - - expectedException.expect(PlatformInfo.DuplicateConstraintException.class); - expectedException.expectMessage( - "Duplicate constraint_values for constraint_setting //constraint:basic: " - + "//constraint:foo, //constraint:bar"); - builder.build(); - } - - @Test - public void platformInfo_equalsTester() throws Exception { - ConstraintSettingInfo setting1 = ConstraintSettingInfo.create(makeLabel("//constraint:basic")); - ConstraintSettingInfo setting2 = ConstraintSettingInfo.create(makeLabel("//constraint:other")); - - ConstraintValueInfo value1 = - ConstraintValueInfo.create(setting1, makeLabel("//constraint:value1")); - ConstraintValueInfo value2 = - ConstraintValueInfo.create(setting2, makeLabel("//constraint:value2")); - ConstraintValueInfo value3 = - ConstraintValueInfo.create(setting2, makeLabel("//constraint:value3")); - - new EqualsTester() - .addEqualityGroup( - // Base case. - PlatformInfo.builder().addConstraint(value1).addConstraint(value2).build(), - PlatformInfo.builder().addConstraint(value1).addConstraint(value2).build(), - PlatformInfo.builder() - .addConstraint(value1) - .addConstraint(value2) - .addRemoteExecutionProperty("key", "val") // execution properties are ignored. - .build()) - .addEqualityGroup( - // Extra constraint. - PlatformInfo.builder().addConstraint(value1).addConstraint(value3).build()) - .addEqualityGroup( - // Missing constraint. - PlatformInfo.builder().addConstraint(value1).build()) - .testEquals(); - } - - @Test - public void toolchainInfo_equalsTester() throws Exception { - ClassObjectConstructor.Key key = new ClassObjectConstructor.Key() {}; - ConstraintSettingInfo setting1 = ConstraintSettingInfo.create(makeLabel("//constraint:basic")); - ConstraintSettingInfo setting2 = ConstraintSettingInfo.create(makeLabel("//constraint:other")); - - ConstraintValueInfo value1 = - ConstraintValueInfo.create(setting1, makeLabel("//constraint:value1")); - ConstraintValueInfo value2 = - ConstraintValueInfo.create(setting2, makeLabel("//constraint:value2")); - ConstraintValueInfo value3 = - ConstraintValueInfo.create(setting2, makeLabel("//constraint:value3")); - - new EqualsTester() - .addEqualityGroup( - // Base case. - new ToolchainInfo( - key, - ImmutableList.of(value1, value2), - ImmutableList.of(value1, value3), - ImmutableMap.<String, Object>of("foo", "val1", "bar", "val2"), - Location.BUILTIN), - new ToolchainInfo( - key, - ImmutableList.of(value1, value2), - ImmutableList.of(value1, value3), - ImmutableMap.<String, Object>of("foo", "val1", "bar", "val2"), - Location.BUILTIN)) - .addEqualityGroup( - // Different type. - new ToolchainInfo( - new ClassObjectConstructor.Key() {}, - ImmutableList.of(value1, value2), - ImmutableList.of(value1, value3), - ImmutableMap.<String, Object>of("foo", "val1", "bar", "val2"), - Location.BUILTIN)) - .addEqualityGroup( - // Different target constraints. - new ToolchainInfo( - key, - ImmutableList.of(value1, value2), - ImmutableList.of(value1, value2), - ImmutableMap.<String, Object>of("foo", "val1", "bar", "val2"), - Location.BUILTIN)) - .addEqualityGroup( - // Different data. - new ToolchainInfo( - key, - ImmutableList.of(value1, value2), - ImmutableList.of(value1, value3), - ImmutableMap.<String, Object>of("foo", "val1", "bar", "val3"), - Location.BUILTIN)) - .testEquals(); - } -} diff --git a/src/test/java/com/google/devtools/build/lib/analysis/platform/ToolchainInfoTest.java b/src/test/java/com/google/devtools/build/lib/analysis/platform/ToolchainInfoTest.java new file mode 100644 index 0000000000..289f72a28b --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/analysis/platform/ToolchainInfoTest.java @@ -0,0 +1,85 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.analysis.platform; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.testing.EqualsTester; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import com.google.devtools.build.lib.events.Location; +import com.google.devtools.build.lib.packages.ClassObjectConstructor; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests of {@link ToolchainInfo}. */ +@RunWith(JUnit4.class) +public class ToolchainInfoTest extends BuildViewTestCase { + + @Test + public void toolchainInfo_equalsTester() throws Exception { + ClassObjectConstructor.Key key = new ClassObjectConstructor.Key() {}; + ConstraintSettingInfo setting1 = ConstraintSettingInfo.create(makeLabel("//constraint:basic")); + ConstraintSettingInfo setting2 = ConstraintSettingInfo.create(makeLabel("//constraint:other")); + + ConstraintValueInfo value1 = + ConstraintValueInfo.create(setting1, makeLabel("//constraint:value1")); + ConstraintValueInfo value2 = + ConstraintValueInfo.create(setting2, makeLabel("//constraint:value2")); + ConstraintValueInfo value3 = + ConstraintValueInfo.create(setting2, makeLabel("//constraint:value3")); + + new EqualsTester() + .addEqualityGroup( + // Base case. + new ToolchainInfo( + key, + ImmutableList.of(value1, value2), + ImmutableList.of(value1, value3), + ImmutableMap.<String, Object>of("foo", "val1", "bar", "val2"), + Location.BUILTIN), + new ToolchainInfo( + key, + ImmutableList.of(value1, value2), + ImmutableList.of(value1, value3), + ImmutableMap.<String, Object>of("foo", "val1", "bar", "val2"), + Location.BUILTIN)) + .addEqualityGroup( + // Different type. + new ToolchainInfo( + new ClassObjectConstructor.Key() {}, + ImmutableList.of(value1, value2), + ImmutableList.of(value1, value3), + ImmutableMap.<String, Object>of("foo", "val1", "bar", "val2"), + Location.BUILTIN)) + .addEqualityGroup( + // Different target constraints. + new ToolchainInfo( + key, + ImmutableList.of(value1, value2), + ImmutableList.of(value1, value2), + ImmutableMap.<String, Object>of("foo", "val1", "bar", "val2"), + Location.BUILTIN)) + .addEqualityGroup( + // Different data. + new ToolchainInfo( + key, + ImmutableList.of(value1, value2), + ImmutableList.of(value1, value3), + ImmutableMap.<String, Object>of("foo", "val1", "bar", "val3"), + Location.BUILTIN)) + .testEquals(); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/rules/platform/ConstraintTest.java b/src/test/java/com/google/devtools/build/lib/rules/platform/ConstraintTest.java index 130e1dd1fa..fbf1e196cb 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/platform/ConstraintTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/platform/ConstraintTest.java @@ -75,7 +75,7 @@ public class ConstraintTest extends BuildViewTestCase { " setting = constraint_value.constraint.label,", " value = constraint_value.label)", "my_rule = rule(", - " _impl,", + " implementation = _impl,", " attrs = { 'constraint': attr.label(providers = [platform_common.ConstraintValueInfo])},", ")"); diff --git a/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java b/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java index 1b17491440..ee430b51e5 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformTest.java @@ -153,7 +153,7 @@ public class PlatformTest extends BuildViewTestCase { " first_setting = platform.constraints[0].constraint.label,", " first_value = platform.constraints[0].label)", "my_rule = rule(", - " _impl,", + " implementation = _impl,", " attrs = { 'platform': attr.label(providers = [platform_common.PlatformInfo])},", ")"); |