aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main
diff options
context:
space:
mode:
authorGravatar John Cater <jcater@google.com>2017-05-25 22:31:39 +0200
committerGravatar Irina Iancu <elenairina@google.com>2017-05-26 09:37:53 +0200
commit04c090adfafd5d6715c4cb17446b7d62607e9a0b (patch)
treedacb0b00f0479e9b13614197df1b7cc63d4a0f95 /src/main
parent3a035d0d76cb46f9ac5fe314b42cfda123ddd9b9 (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/main')
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintSettingInfo.java38
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/platform/ConstraintValueInfo.java43
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/platform/PlatformInfo.java57
3 files changed, 126 insertions, 12 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(