From 74558fcc8953dec64c2ba5920c8f7a7e3ada36ab Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 6 May 2016 21:47:42 +0000 Subject: Expose parameterized aspects to Skylark. There are no syntactic changes within Skylark; the only difference is that aspects may have non-implicit attributes, so long as they have type 'string' and use the 'values' restriction. Such aspects may only be requested by rules which have attributes with types and names matching the non-implicit, non-defaulted attributes of the aspect. This is not yet a complete match for native AspectParameters functionality since implicit attributes cannot yet be affected by attribute values, but that will be added later. Implicit aspects are still required to have default values. Non-implicit aspect attributes are considered "required" unless they have a default value. An error will occur if they are applied to a rule that does not "cover" all required attributes by having attributes of matching name and type. While non-implicit aspect attributes with a default are not required, they will still take on the value of a rule attribute with the same name and type if it is present. Aspects with non-implicit, non-defaulted ("required") attributes cannot be requested on the command line, only by a rule. RELNOTES: Expose parameterized aspects to Skylark. -- MOS_MIGRATED_REVID=121711715 --- .../devtools/build/lib/packages/RuleClass.java | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/main/java/com/google/devtools/build/lib/packages/RuleClass.java') diff --git a/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java b/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java index 7fd42dcca3..b4aac3be6e 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java +++ b/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java @@ -1343,6 +1343,7 @@ public final class RuleClass { throws LabelSyntaxException, InterruptedException { Rule rule = pkgBuilder.createRule(ruleLabel, this, location, attributeContainer); populateRuleAttributeValues(rule, pkgBuilder, attributeValues, eventHandler); + checkAspectAllowedValues(rule, eventHandler); rule.populateOutputFiles(eventHandler, pkgBuilder); if (ast != null) { populateAttributeLocations(rule, ast); @@ -1741,6 +1742,30 @@ public final class RuleClass { } } + private static void checkAspectAllowedValues( + Rule rule, EventHandler eventHandler) { + for (Attribute attrOfRule : rule.getAttributes()) { + for (Aspect aspect : attrOfRule.getAspects(rule)) { + for (Attribute attrOfAspect : aspect.getDefinition().getAttributes().values()) { + // By this point the AspectDefinition has been created and values assigned. + if (attrOfAspect.checkAllowedValues()) { + PredicateWithMessage allowedValues = attrOfAspect.getAllowedValues(); + Object value = attrOfAspect.getDefaultValue(rule); + if (!allowedValues.apply(value)) { + rule.reportError( + String.format( + "%s: invalid value in '%s' attribute: %s", + rule.getLabel(), + attrOfAspect.getName(), + allowedValues.getErrorReason(value)), + eventHandler); + } + } + } + } + } + } + @Override public String toString() { return name; -- cgit v1.2.3