type) {
return new Builder<>(name, type);
}
/**
* A fluent builder for the {@code Attribute} instances.
*
* All methods could be called only once per builder. The attribute
* already undocumented based on its name cannot be marked as undocumented.
*/
public static class Builder {
private String name;
private final Type type;
private Transition configTransition = ConfigurationTransition.NONE;
private Predicate allowedRuleClassesForLabels = Predicates.alwaysTrue();
private Predicate allowedRuleClassesForLabelsWarning = Predicates.alwaysFalse();
private Configurator, ?> configurator;
private FileTypeSet allowedFileTypesForLabels;
private ValidityPredicate validityPredicate = ANY_EDGE;
private Object value;
private boolean valueSet;
private Predicate condition;
private Set propertyFlags = EnumSet.noneOf(PropertyFlag.class);
private PredicateWithMessage allowedValues = null;
private ImmutableSet mandatoryProviders = ImmutableSet.of();
private Set aspects = new LinkedHashSet<>();
/**
* Creates an attribute builder with given name and type. This attribute is optional, uses
* target configuration and has a default value the same as its type default value. This
* attribute will be marked as undocumented if its name starts with the dollar sign ({@code $})
* or colon ({@code :}).
*
* @param name attribute name
* @param type attribute type
*/
public Builder(String name, Type type) {
this.name = Preconditions.checkNotNull(name);
this.type = Preconditions.checkNotNull(type);
if (isImplicit(name) || isLateBound(name)) {
setPropertyFlag(PropertyFlag.UNDOCUMENTED, "undocumented");
}
}
private Builder setPropertyFlag(PropertyFlag flag, String propertyName) {
Preconditions.checkState(
!propertyFlags.contains(flag), "%s flag is already set", propertyName);
propertyFlags.add(flag);
return this;
}
/**
* Sets the property flag of the corresponding name if exists, otherwise throws an Exception.
* Only meant to use from Skylark, do not use from Java.
*/
public Builder setPropertyFlag(String propertyName) {
PropertyFlag flag = null;
try {
flag = PropertyFlag.valueOf(propertyName);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("unknown attribute flag " + propertyName);
}
setPropertyFlag(flag, propertyName);
return this;
}
/**
* Makes the built attribute mandatory.
*/
public Builder mandatory() {
return setPropertyFlag(PropertyFlag.MANDATORY, "mandatory");
}
/**
* Makes the built attribute non empty, meaning the attribute cannot have an empty list value.
* Only applicable for list type attributes.
*/
public Builder nonEmpty() {
Preconditions.checkNotNull(type.getListElementType(), "attribute '%s' must be a list", name);
return setPropertyFlag(PropertyFlag.NON_EMPTY, "non_empty");
}
/**
* Makes the built attribute producing a single artifact.
*/
public Builder singleArtifact() {
Preconditions.checkState((type == BuildType.LABEL) || (type == BuildType.LABEL_LIST),
"attribute '%s' must be a label-valued type", name);
return setPropertyFlag(PropertyFlag.SINGLE_ARTIFACT, "single_artifact");
}
/**
* Forces silent ruleclass filtering on the label type attribute.
* This flag is introduced to handle plugins, do not use it in other cases.
*/
public Builder silentRuleClassFilter() {
Preconditions.checkState((type == BuildType.LABEL) || (type == BuildType.LABEL_LIST),
"must be a label-valued type");
return setPropertyFlag(PropertyFlag.SILENT_RULECLASS_FILTER, "silent_ruleclass_filter");
}
/**
* Skip analysis time filetype check. Don't use it if avoidable.
*/
public Builder skipAnalysisTimeFileTypeCheck() {
Preconditions.checkState((type == BuildType.LABEL) || (type == BuildType.LABEL_LIST),
"must be a label-valued type");
return setPropertyFlag(PropertyFlag.SKIP_ANALYSIS_TIME_FILETYPE_CHECK,
"skip_analysis_time_filetype_check");
}
/**
* Mark the built attribute as order-independent.
*/
public Builder orderIndependent() {
Preconditions.checkNotNull(type.getListElementType(), "attribute '%s' must be a list", name);
return setPropertyFlag(PropertyFlag.ORDER_INDEPENDENT, "order-independent");
}
/**
* Defines the configuration transition for this attribute. Defaults to
* {@code NONE}.
*/
public Builder cfg(Transition configTransition) {
Preconditions.checkState(this.configTransition == ConfigurationTransition.NONE,
"the configuration transition is already set");
this.configTransition = configTransition;
return this;
}
public Builder cfg(Configurator, ?> configurator) {
this.configurator = configurator;
return this;
}
/**
* Requires the attribute target to be executable; only for label or label
* list attributes. Defaults to {@code false}.
*/
public Builder exec() {
return setPropertyFlag(PropertyFlag.EXECUTABLE, "executable");
}
/**
* Indicates that the attribute (like srcs or hdrs) should be used as an input when calculating
* compile_one_dependency.
*/
public Builder direct_compile_time_input() {
return setPropertyFlag(PropertyFlag.DIRECT_COMPILE_TIME_INPUT,
"direct_compile_time_input");
}
/**
* Makes the built attribute undocumented.
*
* @param reason explanation why the attribute is undocumented. This is not
* used but required for documentation
*/
public Builder undocumented(String reason) {
return setPropertyFlag(PropertyFlag.UNDOCUMENTED, "undocumented");
}
/**
* Sets the attribute default value. The type of the default value must
* match the type parameter. (e.g. list=[], integer=0, string="",
* label=null). The {@code defaultValue} must be immutable.
*
* If defaultValue is of type Label and is a target, that target will
* become an implicit dependency of the Rule; we will load the target
* (and its dependencies) if it encounters the Rule and build the target
* if needs to apply the Rule.
*/
public Builder value(TYPE defaultValue) {
Preconditions.checkState(!valueSet, "the default value is already set");
value = defaultValue;
valueSet = true;
return this;
}
/**
* See value(TYPE) above. This method is only meant for Skylark usage.
*/
public Builder defaultValue(Object defaultValue) throws ConversionException {
Preconditions.checkState(!valueSet, "the default value is already set");
value = type.convert(defaultValue, "attribute " + name);
valueSet = true;
return this;
}
public boolean isValueSet() {
return valueSet;
}
/**
* Sets the attribute default value to a computed default value - use
* this when the default value is a function of other attributes of the
* Rule. The type of the computed default value for a mandatory attribute
* must match the type parameter: (e.g. list=[], integer=0, string="",
* label=null). The {@code defaultValue} implementation must be immutable.
*
* If computedDefault returns a Label that is a target, that target will
* become an implicit dependency of this Rule; we will load the target
* (and its dependencies) if it encounters the Rule and build the target if
* needs to apply the Rule.
*/
public Builder value(ComputedDefault defaultValue) {
Preconditions.checkState(!valueSet, "the default value is already set");
value = defaultValue;
valueSet = true;
return this;
}
/**
* Sets the attribute default value to be late-bound, i.e., it is derived from the build
* configuration.
*/
public Builder value(LateBoundDefault> defaultValue) {
Preconditions.checkState(!valueSet, "the default value is already set");
Preconditions.checkState(name.isEmpty() || isLateBound(name));
value = defaultValue;
valueSet = true;
return this;
}
/**
* Returns true if a late-bound value has been set. Useful only for Skylark.
*/
public boolean hasLateBoundValue() {
return value instanceof LateBoundDefault;
}
/**
* Sets a condition predicate. The default value of the attribute only applies if the condition
* evaluates to true. If the value is explicitly provided, then this condition is ignored.
*
* The condition is only evaluated if the attribute is not explicitly set, and after all
* explicit attributes have been set. It can generally not access default values of other
* attributes.
*/
public Builder condition(Predicate condition) {
Preconditions.checkState(this.condition == null, "the condition is already set");
this.condition = condition;
return this;
}
/**
* Switches on the capability of an attribute to be published to the rule's
* tag set.
*/
public Builder taggable() {
return setPropertyFlag(PropertyFlag.TAGGABLE, "taggable");
}
/**
* Disables dependency checks done by
* {@link com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider.PrerequisiteValidator}.
*/
public Builder skipPrereqValidatorCheck() {
return setPropertyFlag(PropertyFlag.SKIP_PREREQ_VALIDATOR_CHECKS,
"skip_prereq_validator_checks");
}
/**
* Enforces constraint checking on dependencies under this attribute. Not calling this method
* does not mean the attribute won't be enforced. This method simply overrides default
* enforcement policy, so it's useful for special-case attributes that would otherwise be
* skipped.
*
* See {@link com.google.devtools.build.lib.analysis.constraints.ConstraintSemantics#getConstraintCheckedDependencies}
* for default enforcement policy.
*/
public Builder checkConstraints() {
return setPropertyFlag(PropertyFlag.CHECK_CONSTRAINTS, "check_constraints");
}
/**
* If this is a label or label-list attribute, then this sets the allowed
* rule types for the labels occurring in the attribute. If the attribute
* contains Labels of any other rule type, then an error is produced during
* the analysis phase. Defaults to allow any types.
*
* This only works on a per-target basis, not on a per-file basis; with
* other words, it works for 'deps' attributes, but not 'srcs' attributes.
*/
public Builder allowedRuleClasses(Iterable allowedRuleClasses) {
return allowedRuleClasses(
new RuleClass.Builder.RuleClassNamePredicate(allowedRuleClasses));
}
/**
* If this is a label or label-list attribute, then this sets the allowed
* rule types for the labels occurring in the attribute. If the attribute
* contains Labels of any other rule type, then an error is produced during
* the analysis phase. Defaults to allow any types.
*
* This only works on a per-target basis, not on a per-file basis; with
* other words, it works for 'deps' attributes, but not 'srcs' attributes.
*/
public Builder allowedRuleClasses(Predicate allowedRuleClasses) {
Preconditions.checkState((type == BuildType.LABEL) || (type == BuildType.LABEL_LIST),
"must be a label-valued type");
propertyFlags.add(PropertyFlag.STRICT_LABEL_CHECKING);
allowedRuleClassesForLabels = allowedRuleClasses;
return this;
}
/**
* If this is a label or label-list attribute, then this sets the allowed
* rule types for the labels occurring in the attribute. If the attribute
* contains Labels of any other rule type, then an error is produced during
* the analysis phase. Defaults to allow any types.
*
* This only works on a per-target basis, not on a per-file basis; with
* other words, it works for 'deps' attributes, but not 'srcs' attributes.
*/
public Builder allowedRuleClasses(String... allowedRuleClasses) {
return allowedRuleClasses(ImmutableSet.copyOf(allowedRuleClasses));
}
/**
* If this is a label or label-list attribute, then this sets the allowed
* file types for file labels occurring in the attribute. If the attribute
* contains labels that correspond to files of any other type, then an error
* is produced during the analysis phase.
*
* This only works on a per-target basis, not on a per-file basis; with
* other words, it works for 'deps' attributes, but not 'srcs' attributes.
*/
public Builder allowedFileTypes(FileTypeSet allowedFileTypes) {
Preconditions.checkState((type == BuildType.LABEL) || (type == BuildType.LABEL_LIST),
"must be a label-valued type");
propertyFlags.add(PropertyFlag.STRICT_LABEL_CHECKING);
allowedFileTypesForLabels = Preconditions.checkNotNull(allowedFileTypes);
return this;
}
/**
* Allow all files for legacy compatibility. All uses of this method should be audited and then
* removed. In some cases, it's correct to allow any file, but mostly the set of files should be
* restricted to a reasonable set.
*/
public Builder legacyAllowAnyFileType() {
return allowedFileTypes(FileTypeSet.ANY_FILE);
}
/**
* If this is a label or label-list attribute, then this sets the allowed
* file types for file labels occurring in the attribute. If the attribute
* contains labels that correspond to files of any other type, then an error
* is produced during the analysis phase.
*
* This only works on a per-target basis, not on a per-file basis; with
* other words, it works for 'deps' attributes, but not 'srcs' attributes.
*/
public Builder allowedFileTypes(FileType... allowedFileTypes) {
return allowedFileTypes(FileTypeSet.of(allowedFileTypes));
}
/**
* If this is a label or label-list attribute, then this sets the allowed
* rule types with warning for the labels occurring in the attribute. If the attribute
* contains Labels of any other rule type (other than this or those set in
* allowedRuleClasses()), then a warning is produced during
* the analysis phase. Defaults to deny any types.
*
* This only works on a per-target basis, not on a per-file basis; with
* other words, it works for 'deps' attributes, but not 'srcs' attributes.
*/
public Builder allowedRuleClassesWithWarning(Collection allowedRuleClasses) {
return allowedRuleClassesWithWarning(
new RuleClass.Builder.RuleClassNamePredicate(allowedRuleClasses));
}
/**
* If this is a label or label-list attribute, then this sets the allowed
* rule types for the labels occurring in the attribute. If the attribute
* contains Labels of any other rule type (other than this or those set in
* allowedRuleClasses()), then a warning is produced during
* the analysis phase. Defaults to deny any types.
*
* This only works on a per-target basis, not on a per-file basis; with
* other words, it works for 'deps' attributes, but not 'srcs' attributes.
*/
public Builder allowedRuleClassesWithWarning(Predicate allowedRuleClasses) {
Preconditions.checkState((type == BuildType.LABEL) || (type == BuildType.LABEL_LIST),
"must be a label-valued type");
propertyFlags.add(PropertyFlag.STRICT_LABEL_CHECKING);
allowedRuleClassesForLabelsWarning = allowedRuleClasses;
return this;
}
/**
* If this is a label or label-list attribute, then this sets the allowed
* rule types for the labels occurring in the attribute. If the attribute
* contains Labels of any other rule type (other than this or those set in
* allowedRuleClasses()), then a warning is produced during
* the analysis phase. Defaults to deny any types.
*
* This only works on a per-target basis, not on a per-file basis; with
* other words, it works for 'deps' attributes, but not 'srcs' attributes.
*/
public Builder allowedRuleClassesWithWarning(String... allowedRuleClasses) {
return allowedRuleClassesWithWarning(ImmutableSet.copyOf(allowedRuleClasses));
}
/**
* Sets a set of mandatory Skylark providers. Every configured target occurring in
* this label type attribute has to provide all of these providers, otherwise an
* error is produces during the analysis phase for every missing provider.
*/
public Builder mandatoryProviders(Iterable providers) {
Preconditions.checkState((type == BuildType.LABEL) || (type == BuildType.LABEL_LIST),
"must be a label-valued type");
this.mandatoryProviders = ImmutableSet.copyOf(providers);
return this;
}
/**
* Asserts that a particular aspect probably needs to be computed for all direct dependencies
* through this attribute.
*/
public Builder aspect(Class aspect) {
Function noParameters = new Function() {
@Override
public AspectParameters apply(Rule input) {
return AspectParameters.EMPTY;
}
};
return this.aspect(aspect, noParameters);
}
/**
* Asserts that a particular parameterized aspect probably needs to be computed for all direct
* dependencies through this attribute.
*
* @param evaluator function that extracts aspect parameters from rule.
*/
public Builder aspect(
Class aspect, Function evaluator) {
return this.aspect(new NativeAspectClass(aspect), evaluator);
}
/**
* Asserts that a particular parameterized aspect probably needs to be computed for all direct
* dependencies through this attribute.
*
* @param evaluator function that extracts aspect parameters from rule.
*/
public Builder aspect(AspectClass aspect, Function evaluator) {
this.aspects.add(new RuleAspect(aspect, evaluator));
return this;
}
/**
* Asserts that a particular parameterized aspect probably needs to be computed for all direct
* dependencies through this attribute.
*/
public Builder aspect(AspectClass aspect) {
Function noParameters =
new Function() {
@Override
public AspectParameters apply(Rule input) {
return AspectParameters.EMPTY;
}
};
return this.aspect(aspect, noParameters);
}
/**
* Sets the predicate-like edge validity checker.
*/
public Builder validityPredicate(ValidityPredicate validityPredicate) {
propertyFlags.add(PropertyFlag.STRICT_LABEL_CHECKING);
this.validityPredicate = validityPredicate;
return this;
}
/**
* The value of the attribute must be one of allowedValues.
*/
public Builder allowedValues(PredicateWithMessage allowedValues) {
this.allowedValues = allowedValues;
propertyFlags.add(PropertyFlag.CHECK_ALLOWED_VALUES);
return this;
}
/**
* Makes the built attribute "non-configurable", i.e. its value cannot be influenced by
* the build configuration. Attributes are "configurable" unless explicitly opted out here.
*
* Non-configurability indicates an exceptional state: there exists Blaze logic that needs
* the attribute's value, has no access to configurations, and can't apply a workaround
* through an appropriate {@link AbstractAttributeMapper} implementation. Scenarios like
* this should be as uncommon as possible, so it's important we maintain clear documentation
* on what causes them and why users consequently can't configure certain attributes.
*
* @param reason why this attribute can't be configurable. This isn't used by Blaze - it's
* solely a documentation mechanism.
*/
public Builder nonconfigurable(String reason) {
Preconditions.checkState(!reason.isEmpty());
return setPropertyFlag(PropertyFlag.NONCONFIGURABLE, "nonconfigurable");
}
/**
* Creates the attribute. Uses name, type, optionality, configuration type
* and the default value configured by the builder.
*/
public Attribute build() {
return build(this.name);
}
/**
* Creates the attribute. Uses type, optionality, configuration type
* and the default value configured by the builder. Use the name
* passed as an argument. This function is used by Skylark where the
* name is provided only when we build. We don't want to modify the
* builder, as it is shared in a multithreaded environment.
*/
public Attribute build(String name) {
Preconditions.checkState(!name.isEmpty(), "name has not been set");
// TODO(bazel-team): Set the default to be no file type, then remove this check, and also
// remove all allowedFileTypes() calls without parameters.
if ((type == BuildType.LABEL) || (type == BuildType.LABEL_LIST)) {
if ((name.startsWith("$") || name.startsWith(":")) && allowedFileTypesForLabels == null) {
allowedFileTypesForLabels = FileTypeSet.ANY_FILE;
}
if (allowedFileTypesForLabels == null) {
throw new IllegalStateException(name);
}
} else if ((type == BuildType.OUTPUT) || (type == BuildType.OUTPUT_LIST)) {
// TODO(bazel-team): Set the default to no file type and make explicit calls instead.
if (allowedFileTypesForLabels == null) {
allowedFileTypesForLabels = FileTypeSet.ANY_FILE;
}
}
return new Attribute(name, type, Sets.immutableEnumSet(propertyFlags),
valueSet ? value : type.getDefaultValue(), configTransition, configurator,
allowedRuleClassesForLabels, allowedRuleClassesForLabelsWarning,
allowedFileTypesForLabels, validityPredicate, condition,
allowedValues, mandatoryProviders, ImmutableSet.copyOf(aspects));
}
}
/**
* A computed default is a default value for a Rule attribute that is a
* function of other attributes of the rule.
*
* Attributes whose defaults are computed are first initialized to the default
* for their type, and then the computed defaults are evaluated after all
* non-computed defaults have been initialized. There is no defined order
* among computed defaults, so they must not depend on each other.
*
*
If a computed default reads the value of another attribute, at least one of
* the following must be true:
*
*
* The other attribute must be declared in the computed default's constructor
* The other attribute must be non-configurable ({@link Builder#nonconfigurable}
*
*
* The reason for enforced declarations is that, since attribute values might be
* configurable, a computed default that depends on them may itself take multiple
* values. Since we have no access to a target's configuration at the time these values
* are computed, we need the ability to probe the default's *complete* dependency space.
* Declared dependencies allow us to do so sanely. Non-configurable attributes don't have
* this problem because their value is fixed and known even without configuration information.
*
*
Implementations of this interface must be immutable.
*/
public abstract static class ComputedDefault {
private final List dependencies;
List dependencies() { return dependencies; }
/**
* Create a computed default that can read all non-configurable attribute values and no
* configurable attribute values.
*/
public ComputedDefault() {
dependencies = ImmutableList.of();
}
/**
* Create a computed default that can read all non-configurable attributes values and one
* explicitly specified configurable attribute value
*/
public ComputedDefault(String depAttribute) {
dependencies = ImmutableList.of(depAttribute);
}
/**
* Create a computed default that can read all non-configurable attributes values and two
* explicitly specified configurable attribute values.
*/
public ComputedDefault(String depAttribute1, String depAttribute2) {
dependencies = ImmutableList.of(depAttribute1, depAttribute2);
}
public abstract Object getDefault(AttributeMap rule);
}
/**
* Marker interface for late-bound values. Unfortunately, we can't refer to BuildConfiguration
* right now, since that is in a separate compilation unit.
*
* Implementations of this interface must be immutable.
*
*
Use sparingly - having different values for attributes during loading and analysis can
* confuse users.
*/
public interface LateBoundDefault {
/**
* Whether to look up the label in the host configuration. This is only here for the host JDK -
* we usually need to look up labels in the target configuration.
*/
boolean useHostConfiguration();
/**
* Returns the set of required configuration fragments, i.e., fragments that will be accessed by
* the code.
*/
Set> getRequiredConfigurationFragments();
/**
* The default value for the attribute that is set during the loading phase.
*/
Object getDefault();
/**
* The actual value for the attribute for the analysis phase, which depends on the build
* configuration. Note that configurations transitions are applied after the late-bound
* attribute was evaluated.
*/
Object getDefault(Rule rule, T o) throws EvalException, InterruptedException;
}
/**
* Abstract super class for label-typed {@link LateBoundDefault} implementations that simplifies
* the client code a little and makes it a bit more type-safe.
*/
public abstract static class LateBoundLabel implements LateBoundDefault {
private final Label label;
private final ImmutableSet> requiredConfigurationFragments;
public LateBoundLabel() {
this((Label) null);
}
public LateBoundLabel(Class>... requiredConfigurationFragments) {
this((Label) null, requiredConfigurationFragments);
}
public LateBoundLabel(Label label) {
this.label = label;
this.requiredConfigurationFragments = ImmutableSet.of();
}
public LateBoundLabel(Label label, Class>... requiredConfigurationFragments) {
this.label = label;
this.requiredConfigurationFragments = ImmutableSet.copyOf(requiredConfigurationFragments);
}
public LateBoundLabel(String label) {
this(Label.parseAbsoluteUnchecked(label));
}
public LateBoundLabel(String label, Class>... requiredConfigurationFragments) {
this(Label.parseAbsoluteUnchecked(label), requiredConfigurationFragments);
}
@Override
public boolean useHostConfiguration() {
return false;
}
@Override
public ImmutableSet> getRequiredConfigurationFragments() {
return requiredConfigurationFragments;
}
@Override
public final Label getDefault() {
return label;
}
@Override
public abstract Label getDefault(Rule rule, T configuration);
}
/**
* Abstract super class for label-list-typed {@link LateBoundDefault} implementations that
* simplifies the client code a little and makes it a bit more type-safe.
*/
public abstract static class LateBoundLabelList implements LateBoundDefault {
private final ImmutableList labels;
public LateBoundLabelList() {
this.labels = ImmutableList.of();
}
public LateBoundLabelList(List labels) {
this.labels = ImmutableList.copyOf(labels);
}
@Override
public boolean useHostConfiguration() {
return false;
}
@Override
public ImmutableSet> getRequiredConfigurationFragments() {
return ImmutableSet.of();
}
@Override
public final List getDefault() {
return labels;
}
@Override
public abstract List getDefault(Rule rule, T configuration);
}
/**
* A class for late bound attributes defined in Skylark.
*/
public static final class SkylarkLateBound implements LateBoundDefault {
private final SkylarkCallbackFunction callback;
public SkylarkLateBound(SkylarkCallbackFunction callback) {
this.callback = callback;
}
@Override
public boolean useHostConfiguration() {
return false;
}
@Override
public ImmutableSet> getRequiredConfigurationFragments() {
return ImmutableSet.of();
}
@Override
public Object getDefault() {
return null;
}
@Override
public Object getDefault(Rule rule, Object o) throws EvalException, InterruptedException {
Map attrValues = new HashMap<>();
// TODO(bazel-team): support configurable attributes here. RawAttributeMapper will throw
// an exception on any instance of configurable attributes.
AttributeMap attributes = RawAttributeMapper.of(rule);
for (Attribute attr : rule.getAttributes()) {
if (!attr.isLateBound()) {
Object value = attributes.get(attr.getName(), attr.getType());
if (value != null) {
attrValues.put(attr.getName(), value);
}
}
}
ClassObject attrs = new SkylarkClassObject(attrValues,
"No such regular (non late-bound) attribute '%s'.");
return callback.call(attrs, o);
}
}
private final String name;
private final Type> type;
private final Set propertyFlags;
// Exactly one of these conditions is true:
// 1. defaultValue == null.
// 2. defaultValue instanceof ComputedDefault &&
// type.isValid(defaultValue.getDefault())
// 3. type.isValid(defaultValue).
// 4. defaultValue instanceof LateBoundDefault &&
// type.isValid(defaultValue.getDefault(configuration))
// (We assume a hypothetical Type.isValid(Object) predicate.)
private final Object defaultValue;
private final Transition configTransition;
private final Configurator, ?> configurator;
/**
* For label or label-list attributes, this predicate returns which rule
* classes are allowed for the targets in the attribute.
*/
private final Predicate allowedRuleClassesForLabels;
/**
* For label or label-list attributes, this predicate returns which rule
* classes are allowed for the targets in the attribute with warning.
*/
private final Predicate allowedRuleClassesForLabelsWarning;
/**
* For label or label-list attributes, this predicate returns which file
* types are allowed for targets in the attribute that happen to be file
* targets (rather than rules).
*/
private final FileTypeSet allowedFileTypesForLabels;
/**
* This predicate-like object checks
* if the edge between two rules using this attribute is valid
* in the dependency graph. Returns null if valid, otherwise an error message.
*/
private final ValidityPredicate validityPredicate;
private final Predicate condition;
private final PredicateWithMessage allowedValues;
private final ImmutableSet mandatoryProviders;
private final ImmutableSet aspects;
/**
* Constructs a rule attribute with the specified name, type and default
* value.
*
* @param name the name of the attribute
* @param type the type of the attribute
* @param defaultValue the default value to use for this attribute if none is
* specified in rule declaration in the BUILD file. Must be null, or of
* type "type". May be an instance of ComputedDefault, in which case
* its getDefault() method must return an instance of "type", or null.
* Must be immutable.
* @param configTransition the configuration transition for this attribute
* (which must be of type LABEL, LABEL_LIST, NODEP_LABEL or
* NODEP_LABEL_LIST).
*/
private Attribute(String name, Type> type, Set propertyFlags,
Object defaultValue, Transition configTransition,
Configurator, ?> configurator,
Predicate allowedRuleClassesForLabels,
Predicate allowedRuleClassesForLabelsWarning,
FileTypeSet allowedFileTypesForLabels,
ValidityPredicate validityPredicate,
Predicate condition,
PredicateWithMessage allowedValues,
ImmutableSet mandatoryProviders,
ImmutableSet aspects) {
Preconditions.checkNotNull(configTransition);
Preconditions.checkArgument(
(configTransition == ConfigurationTransition.NONE && configurator == null)
|| type == BuildType.LABEL || type == BuildType.LABEL_LIST
|| type == BuildType.NODEP_LABEL || type == BuildType.NODEP_LABEL_LIST,
"Configuration transitions can only be specified for label or label list attributes");
Preconditions.checkArgument(
isLateBound(name) == (defaultValue instanceof LateBoundDefault),
"late bound attributes require a default value that is late bound (and vice versa): %s",
name);
if (isLateBound(name)) {
LateBoundDefault> lateBoundDefault = (LateBoundDefault>) defaultValue;
Preconditions.checkArgument((configurator == null),
"a late bound attribute cannot specify a configurator");
Preconditions.checkArgument(!lateBoundDefault.useHostConfiguration()
|| (configTransition == ConfigurationTransition.HOST),
"a late bound default value using the host configuration must use the host transition");
}
this.name = name;
this.type = type;
this.propertyFlags = propertyFlags;
this.defaultValue = defaultValue;
this.configTransition = configTransition;
this.configurator = configurator;
this.allowedRuleClassesForLabels = allowedRuleClassesForLabels;
this.allowedRuleClassesForLabelsWarning = allowedRuleClassesForLabelsWarning;
this.allowedFileTypesForLabels = allowedFileTypesForLabels;
this.validityPredicate = validityPredicate;
this.condition = condition;
this.allowedValues = allowedValues;
this.mandatoryProviders = mandatoryProviders;
this.aspects = aspects;
}
/**
* Returns the name of this attribute.
*/
public String getName() {
return name;
}
/**
* Returns the public name of this attribute. This is the name we use in Skylark code
* and we can use it to display to the end-user.
* Implicit and late-bound attributes start with '_' (instead of '$' or ':').
*/
public String getPublicName() {
String name = getName();
// latebound and implicit attributes have a one-character prefix we want to drop
if (isLateBound() || isImplicit()) {
return "_" + name.substring(1);
}
return name;
}
/**
* Returns the logical type of this attribute. (May differ from the actual
* representation as a value in the build interpreter; for example, an
* attribute may logically be a list of labels, but be represented as a list
* of strings.)
*/
public Type> getType() {
return type;
}
private boolean getPropertyFlag(PropertyFlag flag) {
return propertyFlags.contains(flag);
}
/**
* Returns true if this parameter is mandatory.
*/
public boolean isMandatory() {
return getPropertyFlag(PropertyFlag.MANDATORY);
}
/**
* Returns true if this list parameter cannot have an empty list as a value.
*/
public boolean isNonEmpty() {
return getPropertyFlag(PropertyFlag.NON_EMPTY);
}
/**
* Returns true if this label parameter must produce a single artifact.
*/
public boolean isSingleArtifact() {
return getPropertyFlag(PropertyFlag.SINGLE_ARTIFACT);
}
/**
* Returns true if this label type parameter is checked by silent ruleclass filtering.
*/
public boolean isSilentRuleClassFilter() {
return getPropertyFlag(PropertyFlag.SILENT_RULECLASS_FILTER);
}
/**
* Returns true if this label type parameter skips the analysis time filetype check.
*/
public boolean isSkipAnalysisTimeFileTypeCheck() {
return getPropertyFlag(PropertyFlag.SKIP_ANALYSIS_TIME_FILETYPE_CHECK);
}
/**
* Returns true if this parameter is order-independent.
*/
public boolean isOrderIndependent() {
return getPropertyFlag(PropertyFlag.ORDER_INDEPENDENT);
}
/**
* Returns the configuration transition for this attribute for label or label
* list attributes. For other attributes it will always return {@code NONE}.
*/
public Transition getConfigurationTransition() {
return configTransition;
}
/**
* Returns the configurator instance for this attribute for label or label list attributes.
* For other attributes it will always return {@code null}.
*/
public Configurator, ?> getConfigurator() {
return configurator;
}
/**
* Returns whether the target is required to be executable for label or label
* list attributes. For other attributes it always returns {@code false}.
*/
public boolean isExecutable() {
return getPropertyFlag(PropertyFlag.EXECUTABLE);
}
/**
* Returns {@code true} iff the rule is a direct input for an action.
*/
public boolean isDirectCompileTimeInput() {
return getPropertyFlag(PropertyFlag.DIRECT_COMPILE_TIME_INPUT);
}
/**
* Returns {@code true} iff this attribute requires documentation.
*/
public boolean isDocumented() {
return !getPropertyFlag(PropertyFlag.UNDOCUMENTED);
}
/**
* Returns {@code true} iff this attribute should be published to the rule's
* tag set. Note that not all Type classes support tag conversion.
*/
public boolean isTaggable() {
return getPropertyFlag(PropertyFlag.TAGGABLE);
}
public boolean isStrictLabelCheckingEnabled() {
return getPropertyFlag(PropertyFlag.STRICT_LABEL_CHECKING);
}
/**
* Returns true if the value of this attribute should be a part of a given set.
*/
public boolean checkAllowedValues() {
return getPropertyFlag(PropertyFlag.CHECK_ALLOWED_VALUES);
}
public boolean performPrereqValidatorCheck() {
return !getPropertyFlag(PropertyFlag.SKIP_PREREQ_VALIDATOR_CHECKS);
}
public boolean checkConstraintsOverride() {
return getPropertyFlag(PropertyFlag.CHECK_CONSTRAINTS);
}
/**
* Returns true if this attribute's value can be influenced by the build configuration.
*/
public boolean isConfigurable() {
return !(type == BuildType.OUTPUT // Excluded because of Rule#populateExplicitOutputFiles.
|| type == BuildType.OUTPUT_LIST
|| getPropertyFlag(PropertyFlag.NONCONFIGURABLE));
}
/**
* Returns a predicate that evaluates to true for rule classes that are
* allowed labels in this attribute. If this is not a label or label-list
* attribute, the returned predicate always evaluates to true.
*/
public Predicate getAllowedRuleClassesPredicate() {
return allowedRuleClassesForLabels;
}
/**
* Returns a predicate that evaluates to true for rule classes that are
* allowed labels in this attribute with warning. If this is not a label or label-list
* attribute, the returned predicate always evaluates to true.
*/
public Predicate getAllowedRuleClassesWarningPredicate() {
return allowedRuleClassesForLabelsWarning;
}
/**
* Returns the set of mandatory Skylark providers.
*/
public ImmutableSet getMandatoryProviders() {
return mandatoryProviders;
}
public FileTypeSet getAllowedFileTypesPredicate() {
return allowedFileTypesForLabels;
}
public ValidityPredicate getValidityPredicate() {
return validityPredicate;
}
public Predicate getCondition() {
return condition == null ? Predicates.alwaysTrue() : condition;
}
public PredicateWithMessage getAllowedValues() {
return allowedValues;
}
/**
* Returns the list of aspects required for dependencies through this attribute.
*/
public ImmutableList getAspects(Rule rule) {
ImmutableList.Builder builder = ImmutableList.builder();
for (RuleAspect aspect : aspects) {
builder.add(
new Aspect(aspect.getAspectFactory(), aspect.getParametersExtractor().apply(rule)));
}
return builder.build();
}
/**
* Returns the default value of this attribute in the context of the
* specified Rule. For attributes with a computed default, i.e. {@code
* hasComputedDefault()}, {@code rule} must be non-null since the result may
* depend on the values of its other attributes.
*
* The result may be null (although this is not a value in the build
* language).
*
*
During population of the rule's attribute dictionary, all non-computed
* defaults must be set before all computed ones.
*
* @param rule the rule to which this attribute belongs; non-null if
* {@code hasComputedDefault()}; ignored otherwise.
*/
public Object getDefaultValue(Rule rule) {
if (!getCondition().apply(rule == null ? null : NonconfigurableAttributeMapper.of(rule))) {
return null;
} else if (defaultValue instanceof LateBoundDefault>) {
return ((LateBoundDefault>) defaultValue).getDefault();
} else {
return defaultValue;
}
}
/**
* Returns the default value of this attribute, even if it has a condition, is a computed default,
* or a late-bound default.
*/
@VisibleForTesting
public Object getDefaultValueForTesting() {
return defaultValue;
}
public LateBoundDefault> getLateBoundDefault() {
Preconditions.checkState(isLateBound());
return (LateBoundDefault>) defaultValue;
}
/**
* Returns true iff this attribute has a computed default or a condition.
*
* @see #getDefaultValue(Rule)
*/
boolean hasComputedDefault() {
return (defaultValue instanceof ComputedDefault) || (condition != null);
}
/**
* Returns if this attribute is an implicit dependency according to the naming policy that
* designates implicit attributes.
*/
public boolean isImplicit() {
return isImplicit(getName());
}
/**
* Returns if an attribute with the given name is an implicit dependency according to the
* naming policy that designates implicit attributes.
*/
public static boolean isImplicit(String name) {
return name.startsWith("$");
}
/**
* Returns if this attribute is late-bound according to the naming policy that designates
* late-bound attributes.
*/
public boolean isLateBound() {
return isLateBound(getName());
}
/**
* Returns if an attribute with the given name is late-bound according to the naming policy
* that designates late-bound attributes.
*/
public static boolean isLateBound(String name) {
return name.startsWith(":");
}
@Override
public String toString() {
return "Attribute(" + name + ", " + type + ")";
}
@Override
public int compareTo(Attribute other) {
return name.compareTo(other.name);
}
/**
* Returns a replica builder of this Attribute.
*/
public Attribute.Builder> cloneBuilder() {
Builder> builder = new Builder<>(name, this.type);
builder.allowedFileTypesForLabels = allowedFileTypesForLabels;
builder.allowedRuleClassesForLabels = allowedRuleClassesForLabels;
builder.allowedRuleClassesForLabelsWarning = allowedRuleClassesForLabelsWarning;
builder.validityPredicate = validityPredicate;
builder.condition = condition;
builder.configTransition = configTransition;
builder.propertyFlags = propertyFlags.isEmpty() ?
EnumSet.noneOf(PropertyFlag.class) : EnumSet.copyOf(propertyFlags);
builder.value = defaultValue;
builder.valueSet = false;
builder.allowedValues = allowedValues;
return builder;
}
}