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 Skylark rules to specify whether targets can add execution plat… #5341

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -59,6 +59,7 @@
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 @@ -316,6 +317,8 @@ public BaseFunction rule(
SkylarkList<String> toolchains,
String doc,
SkylarkList<?> providesArg,
Boolean executionPlatformConstraintsAllowed,
SkylarkList<?> execCompatibleWith,
FuncallExpression ast,
Environment funcallEnv)
throws EvalException, ConversionException {
Expand Down Expand Up @@ -397,6 +400,13 @@ public BaseFunction rule(
builder.advertiseSkylarkProvider(skylarkProvider);
}

if (!execCompatibleWith.isEmpty()) {
builder.addExecutionPlatformConstraints(collectConstraintLabels(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 Expand Up @@ -443,6 +453,24 @@ private static ImmutableList<Label> collectToolchainLabels(
return requiredToolchains.build();
}

private static ImmutableList<Label> collectConstraintLabels(
Iterable<String> rawLabels, Location loc) throws EvalException {
ImmutableList.Builder<Label> constraintLabels = new ImmutableList.Builder<>();
for (String rawLabel : rawLabels) {
try {
Label constraintLabel = Label.parseAbsolute(rawLabel);
constraintLabels.add(constraintLabel);
} catch (LabelSyntaxException e) {
throw new EvalException(
loc,
String.format("Unable to parse constraint %s: %s", rawLabel, e.getMessage()),
e);
}
}

return constraintLabels.build();
}

@Override
public SkylarkAspect aspect(
BaseFunction implementation,
Expand Down Expand Up @@ -655,7 +683,11 @@ public void export(Label skylarkLabel, String ruleClassName) throws EvalExceptio
addAttribute(definitionLocation, builder,
descriptor.build(attribute.getFirst()));
}
this.ruleClass = builder.build(ruleClassName, skylarkLabel + "%" + ruleClassName);
try {
this.ruleClass = builder.build(ruleClassName, skylarkLabel + "%" + ruleClassName);
} catch (IllegalArgumentException | IllegalStateException ex) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with the rule class builder, but I'm a little wary when I see broad unchecked exception types reraised as a checked exception like this. Can you add a comment mentioning that it's in case there's a conflict in the attribute builder?

throw new EvalException(location, ex);
}

this.builder = null;
this.attributes = null;
Expand Down
Loading