aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/platform
diff options
context:
space:
mode:
authorGravatar John Cater <jcater@google.com>2017-02-28 13:15:28 +0000
committerGravatar Yue Gan <yueg@google.com>2017-02-28 17:02:08 +0000
commit691fbc1a5fd3ab03ab6c21e6c04fa9c528bbf1ba (patch)
tree3235bb9c6a3382f40e0bf1a933a22b2967a23c69 /src/main/java/com/google/devtools/build/lib/rules/platform
parent55e4c51ac76f1da3537f33536c1d2bfea15e5b93 (diff)
Add constraint_setting and constraint_value rules, to enable defining
platform-related constraints and values. Part of ongoing work on #2219. -- Change-Id: Ice370ee26469f4992faf72c0c95a1a3e51a9f9e7 Reviewed-on: https://cr.bazel.build/9091 PiperOrigin-RevId: 148758190 MOS_MIGRATED_REVID=148758190
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/platform')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/platform/BUILD25
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSetting.java45
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingProvider.java31
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingRule.java57
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValue.java48
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueProvider.java33
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueRule.java74
7 files changed, 313 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/BUILD b/src/main/java/com/google/devtools/build/lib/rules/platform/BUILD
new file mode 100644
index 0000000000..d8c6ca9b79
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/BUILD
@@ -0,0 +1,25 @@
+# Description:
+# Rules to define Platforms and related Constraints.
+
+package(
+ default_visibility = ["//src:__subpackages__"],
+)
+
+java_library(
+ name = "platform",
+ srcs = glob([
+ "*.java",
+ ]),
+ deps = [
+ "//src/main/java/com/google/devtools/build/lib:build-base",
+ "//src/main/java/com/google/devtools/build/lib:packages",
+ "//third_party:auto_value",
+ "//third_party:guava",
+ ],
+)
+
+filegroup(
+ name = "srcs",
+ testonly = 0, # All srcs should be not test only, overwrite package default.
+ srcs = glob(["**"]),
+)
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSetting.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSetting.java
new file mode 100644
index 0000000000..03e924f1db
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSetting.java
@@ -0,0 +1,45 @@
+// 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.rules.platform;
+
+import com.google.devtools.build.lib.analysis.ConfiguredTarget;
+import com.google.devtools.build.lib.analysis.FileProvider;
+import com.google.devtools.build.lib.analysis.FilesToRunProvider;
+import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
+import com.google.devtools.build.lib.analysis.RuleContext;
+import com.google.devtools.build.lib.analysis.RunfilesProvider;
+import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
+import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
+
+/**
+ * Defines a category of constraint that can be fulfilled by a constraint_value rule in a platform
+ * definition.
+ */
+public class ConstraintSetting implements RuleConfiguredTargetFactory {
+
+ @Override
+ public ConfiguredTarget create(RuleContext ruleContext)
+ throws InterruptedException, RuleErrorException {
+
+ return new RuleConfiguredTargetBuilder(ruleContext)
+ .addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY)
+ .addProvider(FileProvider.class, FileProvider.EMPTY)
+ .addProvider(FilesToRunProvider.class, FilesToRunProvider.EMPTY)
+ .addProvider(
+ ConstraintSettingProvider.class,
+ ConstraintSettingProvider.create(ruleContext.getLabel()))
+ .build();
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingProvider.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingProvider.java
new file mode 100644
index 0000000000..9459a07e91
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingProvider.java
@@ -0,0 +1,31 @@
+// 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.rules.platform;
+
+import com.google.auto.value.AutoValue;
+import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
+
+/** Provider for a platform constraint setting that is available to be fulfilled. */
+@AutoValue
+@Immutable
+public abstract class ConstraintSettingProvider implements TransitiveInfoProvider {
+ public abstract Label constraintSetting();
+
+ public static ConstraintSettingProvider create(Label constraintSetting) {
+ return new AutoValue_ConstraintSettingProvider(constraintSetting);
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingRule.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingRule.java
new file mode 100644
index 0000000000..6b239834ac
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintSettingRule.java
@@ -0,0 +1,57 @@
+// 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.rules.platform;
+
+import static com.google.devtools.build.lib.packages.Attribute.attr;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.analysis.BaseRuleClasses;
+import com.google.devtools.build.lib.analysis.RuleDefinition;
+import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
+import com.google.devtools.build.lib.packages.RuleClass;
+import com.google.devtools.build.lib.syntax.Type;
+
+/** Rule definition for {@link ConstraintSetting}. */
+public class ConstraintSettingRule implements RuleDefinition {
+ public static final String RULE_NAME = "constraint_setting";
+
+ @Override
+ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
+ return builder
+ .override(
+ attr("tags", Type.STRING_LIST)
+ // No need to show up in ":all", etc. target patterns.
+ .value(ImmutableList.of("manual"))
+ .nonconfigurable("low-level attribute, used in platform configuration"))
+ .removeAttribute("deps")
+ .removeAttribute("data")
+ .exemptFromConstraintChecking("this rule *defines* a constraint")
+ .build();
+ }
+
+ @Override
+ public RuleDefinition.Metadata getMetadata() {
+ return RuleDefinition.Metadata.builder()
+ .name(RULE_NAME)
+ .ancestors(BaseRuleClasses.RuleBase.class)
+ .factoryClass(ConstraintSetting.class)
+ .build();
+ }
+}
+/*<!-- #BLAZE_RULE (NAME = constraint_setting, TYPE = OTHER, FAMILY = Platform)[GENERIC_RULE] -->
+
+<p>This rule defines a type of constraint that can be used to define an execution platform.</p>
+
+<!-- #END_BLAZE_RULE -->*/
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValue.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValue.java
new file mode 100644
index 0000000000..c232c92212
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValue.java
@@ -0,0 +1,48 @@
+// 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.rules.platform;
+
+import com.google.devtools.build.lib.analysis.ConfiguredTarget;
+import com.google.devtools.build.lib.analysis.FileProvider;
+import com.google.devtools.build.lib.analysis.FilesToRunProvider;
+import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
+import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
+import com.google.devtools.build.lib.analysis.RuleContext;
+import com.google.devtools.build.lib.analysis.RunfilesProvider;
+import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
+
+/** Defines a potential value of a constraint. */
+public class ConstraintValue implements RuleConfiguredTargetFactory {
+
+ @Override
+ public ConfiguredTarget create(RuleContext ruleContext)
+ throws InterruptedException, RuleErrorException {
+
+ ConstraintSettingProvider constraint =
+ ruleContext.getPrerequisite(
+ ConstraintValueRule.CONSTRAINT_SETTING_ATTR,
+ Mode.DONT_CHECK,
+ ConstraintSettingProvider.class);
+
+ return new RuleConfiguredTargetBuilder(ruleContext)
+ .addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY)
+ .addProvider(FileProvider.class, FileProvider.EMPTY)
+ .addProvider(FilesToRunProvider.class, FilesToRunProvider.EMPTY)
+ .addProvider(
+ ConstraintValueProvider.class,
+ ConstraintValueProvider.create(constraint, ruleContext.getLabel()))
+ .build();
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueProvider.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueProvider.java
new file mode 100644
index 0000000000..1ade6ddded
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueProvider.java
@@ -0,0 +1,33 @@
+// 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.rules.platform;
+
+import com.google.auto.value.AutoValue;
+import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
+
+/** Provider for a platform constraint value that fulfills a {@link ConstraintSettingProvider}. */
+@AutoValue
+@Immutable
+public abstract class ConstraintValueProvider implements TransitiveInfoProvider {
+ public abstract ConstraintSettingProvider constraint();
+
+ public abstract Label value();
+
+ public static ConstraintValueProvider create(ConstraintSettingProvider constraint, Label value) {
+ return new AutoValue_ConstraintValueProvider(constraint, value);
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueRule.java b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueRule.java
new file mode 100644
index 0000000000..55e85b1048
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/ConstraintValueRule.java
@@ -0,0 +1,74 @@
+// 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.rules.platform;
+
+import static com.google.devtools.build.lib.packages.Attribute.attr;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.analysis.BaseRuleClasses;
+import com.google.devtools.build.lib.analysis.RuleDefinition;
+import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
+import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
+import com.google.devtools.build.lib.packages.BuildType;
+import com.google.devtools.build.lib.packages.RuleClass;
+import com.google.devtools.build.lib.syntax.Type;
+import com.google.devtools.build.lib.util.FileTypeSet;
+
+/** Rule definition for {@link ConstraintValue}. */
+public class ConstraintValueRule implements RuleDefinition {
+ public static final String RULE_NAME = "constraint_value";
+ public static final String CONSTRAINT_SETTING_ATTR = "constraint_setting";
+
+ @Override
+ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
+ return builder
+ .override(
+ attr("tags", Type.STRING_LIST)
+ // No need to show up in ":all", etc. target patterns.
+ .value(ImmutableList.of("manual"))
+ .nonconfigurable("low-level attribute, used in platform configuration"))
+
+ /* <!-- #BLAZE_RULE(constraint_value).ATTRIBUTE(constraint) -->
+ The constraint_setting rule this value is applied to.
+ <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
+ .add(
+ attr(CONSTRAINT_SETTING_ATTR, BuildType.LABEL)
+ .mandatory()
+ .allowedRuleClasses(ConstraintSettingRule.RULE_NAME)
+ .allowedFileTypes(FileTypeSet.NO_FILE)
+ .mandatoryNativeProviders(
+ ImmutableList.<Class<? extends TransitiveInfoProvider>>of(
+ ConstraintSettingProvider.class)))
+ .removeAttribute("deps")
+ .removeAttribute("data")
+ .exemptFromConstraintChecking("this rule *defines* a constraint")
+ .build();
+ }
+
+ @Override
+ public Metadata getMetadata() {
+ return Metadata.builder()
+ .name(RULE_NAME)
+ .ancestors(BaseRuleClasses.RuleBase.class)
+ .factoryClass(ConstraintValue.class)
+ .build();
+ }
+}
+/*<!-- #BLAZE_RULE (NAME = constraint_value, TYPE = OTHER, FAMILY = Platform)[GENERIC_RULE] -->
+
+<p>This rule defines a specific value of a constraint, which can be used to define execution
+platforms.
+
+<!-- #END_BLAZE_RULE -->*/