Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow all targets to specify exec_compatible_with to add constraints on #8133

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
import com.google.devtools.build.lib.packages.RuleClass.ExecutionPlatformConstraintsAllowed;
import com.google.devtools.build.lib.packages.TestSize;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.syntax.Type;
Expand Down Expand Up @@ -208,7 +207,6 @@ public Object getDefault(AttributeMap rule) {
env.getToolsLabel(DEFAULT_COVERAGE_REPORT_GENERATOR_VALUE))))
// The target itself and run_under both run on the same machine.
.add(attr(":run_under", LABEL).value(RUN_UNDER).skipPrereqValidatorCheck())
.executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_TARGET)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import com.google.devtools.build.lib.packages.Provider;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
import com.google.devtools.build.lib.packages.RuleClass.ExecutionPlatformConstraintsAllowed;
import com.google.devtools.build.lib.packages.RuleFactory;
import com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap;
import com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException;
Expand Down Expand Up @@ -220,7 +219,6 @@ public static final RuleClass getTestBaseRule(String toolsRepository) {
toolsRepository
+ BaseRuleClasses.DEFAULT_COVERAGE_REPORT_GENERATOR_VALUE))))
.add(attr(":run_under", LABEL).value(RUN_UNDER))
.executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_TARGET)
.build();
}

Expand Down Expand Up @@ -278,6 +276,7 @@ public BaseFunction rule(
SkylarkList<?> toolchains,
String doc,
SkylarkList<?> providesArg,
// TODO(https://github.com/bazelbuild/bazel/issues/8026): Remove this.
Boolean executionPlatformConstraintsAllowed,
SkylarkList<?> execCompatibleWith,
Object analysisTest,
Expand Down Expand Up @@ -398,9 +397,6 @@ public BaseFunction rule(
execCompatibleWith.getContents(String.class, "exec_compatile_with"),
ast.getLocation()));
}
if (executionPlatformConstraintsAllowed) {
builder.executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_TARGET);
}

return new SkylarkRuleFunction(builder, type, attributes, ast.getLocation());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.devtools.build.lib.packages.PredicateWithMessage;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
import com.google.devtools.build.lib.packages.RuleClass.ExecutionPlatformConstraintsAllowed;
import com.google.devtools.build.lib.util.FileTypeSet;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -72,7 +71,6 @@ All other files required at runtime (whether scripts or data) belong in the
.allowedRuleClasses("sh_library")
.allowedRuleClassesWithWarning(ALLOWED_RULES_IN_DEPS_WITH_WARNING)
.allowedFileTypes())
.executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_TARGET)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,44 +213,6 @@ TConfiguredTarget create(TContext ruleContext)
public static final class RuleErrorException extends Exception {}
}

/**
* Describes in which way a rule implementation allows additional execution platform constraints.
*/
public enum ExecutionPlatformConstraintsAllowed {
/**
* Allows additional execution platform constraints to be added in the rule definition, which
* apply to all targets of that rule.
*/
PER_RULE(1),
/**
* Users are allowed to specify additional execution platform constraints for each target, using
* the 'exec_compatible_with' attribute. This also allows setting constraints in the rule
* definition, like PER_RULE.
*/
PER_TARGET(2);

private final int priority;

ExecutionPlatformConstraintsAllowed(int priority) {
this.priority = priority;
}

public int priority() {
return priority;
}

public static ExecutionPlatformConstraintsAllowed highestPriority(
ExecutionPlatformConstraintsAllowed first, ExecutionPlatformConstraintsAllowed... rest) {
ExecutionPlatformConstraintsAllowed result = first;
for (ExecutionPlatformConstraintsAllowed value : rest) {
if (result == null || result.priority() < value.priority()) {
result = value;
}
}
return result;
}
}

/**
* For Bazel's constraint system: the attribute that declares the set of environments a rule
* supports, overriding the defaults for their respective groups.
Expand Down Expand Up @@ -717,8 +679,6 @@ public enum ThirdPartyLicenseExistencePolicy {
private final Map<String, Attribute> attributes = new LinkedHashMap<>();
private final Set<Label> requiredToolchains = new HashSet<>();
private boolean supportsPlatforms = true;
private ExecutionPlatformConstraintsAllowed executionPlatformConstraintsAllowed =
ExecutionPlatformConstraintsAllowed.PER_RULE;
private Set<Label> executionPlatformConstraints = new HashSet<>();
private OutputFile.Kind outputFileKind = OutputFile.Kind.FILE;

Expand Down Expand Up @@ -754,10 +714,6 @@ public Builder(String name, RuleClassType type, boolean skylark, RuleClass... pa
addRequiredToolchains(parent.getRequiredToolchains());
supportsPlatforms = parent.supportsPlatforms;

// Make sure we use the highest priority value from all parents.
executionPlatformConstraintsAllowed(
ExecutionPlatformConstraintsAllowed.highestPriority(
executionPlatformConstraintsAllowed, parent.executionPlatformConstraintsAllowed()));
addExecutionPlatformConstraints(parent.getExecutionPlatformConstraints());

for (Attribute attribute : parent.getAttributes()) {
Expand Down Expand Up @@ -819,8 +775,7 @@ public RuleClass build(String name, String key) {
if (type == RuleClassType.PLACEHOLDER) {
Preconditions.checkNotNull(ruleDefinitionEnvironmentHashCode, this.name);
}
if (executionPlatformConstraintsAllowed == ExecutionPlatformConstraintsAllowed.PER_TARGET
&& !this.contains(EXEC_COMPATIBLE_WITH_ATTR)) {
if (!this.contains(EXEC_COMPATIBLE_WITH_ATTR)) {
this.add(
attr(EXEC_COMPATIBLE_WITH_ATTR, BuildType.LABEL_LIST)
.allowedFileTypes()
Expand Down Expand Up @@ -871,7 +826,6 @@ public RuleClass build(String name, String key) {
thirdPartyLicenseExistencePolicy,
requiredToolchains,
supportsPlatforms,
executionPlatformConstraintsAllowed,
executionPlatformConstraints,
outputFileKind,
attributes.values(),
Expand Down Expand Up @@ -1380,20 +1334,6 @@ public Builder supportsPlatforms(boolean flag) {
return this;
}

/**
* Specifies whether targets of this rule can add additional constraints on the execution
* platform selected. If this is {@link ExecutionPlatformConstraintsAllowed#PER_TARGET}, there
* will be an attribute named {@code exec_compatible_with} that can be used to add these
* constraints.
*
* <p>Please note that this value is not inherited by child rules, and must be re-set on them if
* the same behavior is required.
*/
public Builder executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed value) {
this.executionPlatformConstraintsAllowed = value;
return this;
}

/**
* Adds additional execution platform constraints that apply for all targets from this rule.
*
Expand Down Expand Up @@ -1542,7 +1482,6 @@ public Attribute.Builder<?> copy(String name) {

private final ImmutableSet<Label> requiredToolchains;
private final boolean supportsPlatforms;
private final ExecutionPlatformConstraintsAllowed executionPlatformConstraintsAllowed;
private final ImmutableSet<Label> executionPlatformConstraints;

/**
Expand Down Expand Up @@ -1597,7 +1536,6 @@ public Attribute.Builder<?> copy(String name) {
ThirdPartyLicenseExistencePolicy thirdPartyLicenseExistencePolicy,
Set<Label> requiredToolchains,
boolean supportsPlatforms,
ExecutionPlatformConstraintsAllowed executionPlatformConstraintsAllowed,
Set<Label> executionPlatformConstraints,
OutputFile.Kind outputFileKind,
Collection<Attribute> attributes,
Expand Down Expand Up @@ -1636,7 +1574,6 @@ public Attribute.Builder<?> copy(String name) {
this.thirdPartyLicenseExistencePolicy = thirdPartyLicenseExistencePolicy;
this.requiredToolchains = ImmutableSet.copyOf(requiredToolchains);
this.supportsPlatforms = supportsPlatforms;
this.executionPlatformConstraintsAllowed = executionPlatformConstraintsAllowed;
this.executionPlatformConstraints = ImmutableSet.copyOf(executionPlatformConstraints);
this.buildSetting = buildSetting;

Expand Down Expand Up @@ -2540,10 +2477,6 @@ public boolean supportsPlatforms() {
return supportsPlatforms;
}

public ExecutionPlatformConstraintsAllowed executionPlatformConstraintsAllowed() {
return executionPlatformConstraintsAllowed;
}

public ImmutableSet<Label> getExecutionPlatformConstraints() {
return executionPlatformConstraints;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
import com.google.devtools.build.lib.packages.RuleClass.ExecutionPlatformConstraintsAllowed;
import com.google.devtools.build.lib.util.FileTypeSet;

/**
Expand Down Expand Up @@ -226,7 +225,6 @@ public Object getDefault(AttributeMap rule) {
.add(attr("heuristic_label_expansion", BOOLEAN).value(false))
.removeAttribute("data")
.removeAttribute("deps")
.executionPlatformConstraintsAllowed(ExecutionPlatformConstraintsAllowed.PER_TARGET)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import com.google.devtools.build.lib.packages.RawAttributeMapper;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.ExecutionPlatformConstraintsAllowed;
import com.google.devtools.build.lib.packages.RuleClassProvider;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.packages.TargetUtils;
Expand Down Expand Up @@ -457,8 +456,7 @@ private static ImmutableSet<Label> getExecutionPlatformConstraints(Rule rule) {

execConstraintLabels.addAll(rule.getRuleClassObject().getExecutionPlatformConstraints());

if (rule.getRuleClassObject().executionPlatformConstraintsAllowed()
== ExecutionPlatformConstraintsAllowed.PER_TARGET) {
if (mapper.has(RuleClass.EXEC_COMPATIBLE_WITH_ATTR, BuildType.LABEL_LIST)) {
execConstraintLabels.addAll(
mapper.get(RuleClass.EXEC_COMPATIBLE_WITH_ATTR, BuildType.LABEL_LIST));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ public interface SkylarkRuleFunctionsApi<FileApiT extends FileApi> {
positional = false,
defaultValue = "[]",
doc = PROVIDES_DOC),
// TODO(https://github.com/bazelbuild/bazel/issues/8026): Remove this.
@Param(
name = "execution_platform_constraints_allowed",
type = Boolean.class,
Expand Down
Loading