aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools
diff options
context:
space:
mode:
authorGravatar John Cater <jcater@google.com>2017-04-22 00:27:06 +0200
committerGravatar Vladimir Moskva <vladmos@google.com>2017-04-24 16:51:30 +0200
commit28c7a7ffba65f86be6de21adf64b16c5d9c4211a (patch)
tree27ecf4f4b34143a69189f5ab62497328d0bb0617 /src/main/java/com/google/devtools
parente3d46b7a8edb49415c7a1c23550bf735df35b5da (diff)
Add a undocumented way to define a platform that is autodetected from
the host. Also add a single instance of that platform for standard usage. Change-Id: I0e7a8eb3a44099076540c8d955fc0c0c70447583 PiperOrigin-RevId: 153878880
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/platform/Platform.java44
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/platform/PlatformRule.java29
2 files changed, 68 insertions, 5 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/Platform.java b/src/main/java/com/google/devtools/build/lib/rules/platform/Platform.java
index c39660585b..d1c48e9445 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/platform/Platform.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/Platform.java
@@ -24,6 +24,8 @@ import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.rules.platform.PlatformInfo.DuplicateConstraintException;
import com.google.devtools.build.lib.syntax.Type;
+import com.google.devtools.build.lib.util.CPU;
+import com.google.devtools.build.lib.util.OS;
import java.util.Map;
/** Defines a platform for execution contexts. */
@@ -32,12 +34,16 @@ public class Platform implements RuleConfiguredTargetFactory {
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException {
- Iterable<ConstraintValueInfo> constraintValues =
- ConstraintValueInfo.fromTargets(
- ruleContext.getPrerequisites(PlatformRule.CONSTRAINT_VALUES_ATTR, Mode.DONT_CHECK));
-
PlatformInfo.Builder platformBuilder = PlatformInfo.builder();
- platformBuilder.addConstraints(constraintValues);
+
+ if (ruleContext.attributes().get(PlatformRule.HOST_PLATFORM_ATTR, Type.BOOLEAN)) {
+ // Create default constraints based on the current OS and CPU values.
+ autodetectHostConstraints(ruleContext, platformBuilder);
+ } else {
+ platformBuilder.addConstraints(
+ ConstraintValueInfo.fromTargets(
+ ruleContext.getPrerequisites(PlatformRule.CONSTRAINT_VALUES_ATTR, Mode.DONT_CHECK)));
+ }
Map<String, String> remoteExecutionProperties =
ruleContext.attributes().get(PlatformRule.REMOTE_EXECUTION_PROPS_ATTR, Type.STRING_DICT);
@@ -59,4 +65,32 @@ public class Platform implements RuleConfiguredTargetFactory {
.addNativeDeclaredProvider(platformInfo)
.build();
}
+
+ private void autodetectHostConstraints(
+ RuleContext ruleContext, PlatformInfo.Builder platformBuilder) {
+
+ // Add the CPU.
+ CPU cpu = CPU.getCurrent();
+ Iterable<ConstraintValueInfo> cpuConstraintValues =
+ ConstraintValueInfo.fromTargets(
+ ruleContext.getPrerequisites(PlatformRule.HOST_CPU_CONSTRAINTS_ATTR, Mode.DONT_CHECK));
+ for (ConstraintValueInfo constraint : cpuConstraintValues) {
+ if (cpu.getCanonicalName().equals(constraint.label().getName())) {
+ platformBuilder.addConstraint(constraint);
+ break;
+ }
+ }
+
+ // Add the OS.
+ OS os = OS.getCurrent();
+ Iterable<ConstraintValueInfo> osConstraintValues =
+ ConstraintValueInfo.fromTargets(
+ ruleContext.getPrerequisites(PlatformRule.HOST_OS_CONSTRAINTS_ATTR, Mode.DONT_CHECK));
+ for (ConstraintValueInfo constraint : osConstraintValues) {
+ if (os.getCanonicalName().equals(constraint.label().getName())) {
+ platformBuilder.addConstraint(constraint);
+ break;
+ }
+ }
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformRule.java b/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformRule.java
index cd332435ca..126cd9d15d 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformRule.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/platform/PlatformRule.java
@@ -30,6 +30,9 @@ public class PlatformRule implements RuleDefinition {
public static final String RULE_NAME = "platform";
public static final String CONSTRAINT_VALUES_ATTR = "constraint_values";
public static final String REMOTE_EXECUTION_PROPS_ATTR = "remote_execution_properties";
+ static final String HOST_PLATFORM_ATTR = "host_platform";
+ static final String HOST_CPU_CONSTRAINTS_ATTR = "host_cpu_constraints";
+ static final String HOST_OS_CONSTRAINTS_ATTR = "host_os_constraints";
@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
@@ -41,11 +44,37 @@ public class PlatformRule implements RuleDefinition {
// No need to show up in ":all", etc. target patterns.
.value(ImmutableList.of("manual"))
.nonconfigurable("low-level attribute, used in platform configuration"))
+ /* <!-- #BLAZE_RULE(platform).ATTRIBUTE(constraint_values) -->
+ The constraint_values that define this platform.
+ <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(
attr(CONSTRAINT_VALUES_ATTR, BuildType.LABEL_LIST)
.allowedFileTypes(FileTypeSet.NO_FILE)
.mandatoryProviders(ImmutableList.of(ConstraintValueInfo.SKYLARK_IDENTIFIER)))
+
+ /* <!-- #BLAZE_RULE(platform).ATTRIBUTE(remote_execution_properties) -->
+ A key/value dict of values that will be sent to a remote execution platform.
+ <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr(REMOTE_EXECUTION_PROPS_ATTR, Type.STRING_DICT))
+
+ // Undocumented. Indicates that this platform should auto-configure the platform constraints
+ // based on the current OS and CPU settings.
+ .add(
+ attr(HOST_PLATFORM_ATTR, Type.BOOLEAN)
+ .value(false)
+ .undocumented("Should only be used by internal packages."))
+ // Undocumented. Indicates to the rule which constraint_values to use for host CPU mapping.
+ .add(
+ attr(HOST_CPU_CONSTRAINTS_ATTR, BuildType.LABEL_LIST)
+ .allowedFileTypes(FileTypeSet.NO_FILE)
+ .mandatoryProviders(ImmutableList.of(ConstraintValueInfo.SKYLARK_IDENTIFIER))
+ .undocumented("Should only be used by internal packages."))
+ // Undocumented. Indicates to the rule which constraint_values to use for host OS mapping.
+ .add(
+ attr(HOST_OS_CONSTRAINTS_ATTR, BuildType.LABEL_LIST)
+ .allowedFileTypes(FileTypeSet.NO_FILE)
+ .mandatoryProviders(ImmutableList.of(ConstraintValueInfo.SKYLARK_IDENTIFIER))
+ .undocumented("Should only be used by internal packages."))
.removeAttribute("deps")
.removeAttribute("data")
.exemptFromConstraintChecking("this rule is part of constraint definition")