use of com.google.devtools.build.lib.packages.AttributeMap in project bazel by bazelbuild.
the class ConfiguredAttributeMapper method getAndValidate.
/**
* Variation of {@link #get} that throws an informative exception if the attribute
* can't be resolved due to intrinsic contradictions in the configuration.
*/
private <T> T getAndValidate(String attributeName, Type<T> type) throws EvalException {
SelectorList<T> selectorList = getSelectorList(attributeName, type);
if (selectorList == null) {
// This is a normal attribute.
return super.get(attributeName, type);
}
List<T> resolvedList = new ArrayList<>();
for (Selector<T> selector : selectorList.getSelectors()) {
ConfigKeyAndValue<T> resolvedPath = resolveSelector(attributeName, selector);
if (!selector.isValueSet(resolvedPath.configKey)) {
// Use the default. We don't have access to the rule here, so pass null to
// Attribute.getValue(). This has the result of making attributes with condition
// predicates ineligible for "None" values. But no user-facing attributes should
// do that anyway, so that isn't a loss.
Attribute attr = getAttributeDefinition(attributeName);
Verify.verify(attr.getCondition() == Predicates.<AttributeMap>alwaysTrue());
resolvedList.add((T) attr.getDefaultValue(null));
} else {
resolvedList.add(resolvedPath.value);
}
}
return resolvedList.size() == 1 ? resolvedList.get(0) : type.concat(resolvedList);
}
use of com.google.devtools.build.lib.packages.AttributeMap in project bazel by bazelbuild.
the class AspectDefinitionTest method testAspectWithImplicitOrLateboundAttribute_AddsToAttributeMap.
@Test
public void testAspectWithImplicitOrLateboundAttribute_AddsToAttributeMap() throws Exception {
Attribute implicit = attr("$runtime", BuildType.LABEL).value(Label.parseAbsoluteUnchecked("//run:time")).build();
LateBoundLabel<String> latebound = new LateBoundLabel<String>() {
@Override
public Label resolve(Rule rule, AttributeMap attributes, String configuration) {
return Label.parseAbsoluteUnchecked("//run:away");
}
};
AspectDefinition simple = new AspectDefinition.Builder(TEST_ASPECT_CLASS).add(implicit).add(attr(":latebound", BuildType.LABEL).value(latebound)).build();
assertThat(simple.getAttributes()).containsEntry("$runtime", implicit);
assertThat(simple.getAttributes()).containsKey(":latebound");
assertThat(simple.getAttributes().get(":latebound").getLateBoundDefault()).isEqualTo(latebound);
}
use of com.google.devtools.build.lib.packages.AttributeMap in project bazel by bazelbuild.
the class ConstraintSemantics method getConstraintCheckedDependencies.
/**
* Returns all dependencies that should be constraint-checked against the current rule,
* including both "uncoditional" deps (outside selects) and deps that only appear in selects.
*/
private static DepsToCheck getConstraintCheckedDependencies(RuleContext ruleContext) {
Set<TransitiveInfoCollection> depsToCheck = new LinkedHashSet<>();
Set<TransitiveInfoCollection> selectOnlyDeps = new LinkedHashSet<>();
Set<TransitiveInfoCollection> depsOutsideSelects = new LinkedHashSet<>();
AttributeMap attributes = ruleContext.attributes();
for (String attr : attributes.getAttributeNames()) {
Attribute attrDef = attributes.getAttributeDefinition(attr);
if (attrDef.getType().getLabelClass() != LabelClass.DEPENDENCY || attrDef.skipConstraintsOverride()) {
continue;
}
if (!attrDef.checkConstraintsOverride()) {
// determine exactly which rules need to be constraint-annotated for depot migrations.
if (!DependencyFilter.NO_IMPLICIT_DEPS.apply(ruleContext.getRule(), attrDef) || // because --nodistinct_host_configuration subverts that call.
attrDef.getConfigurationTransition() == Attribute.ConfigurationTransition.HOST) {
continue;
}
}
Set<Label> selectOnlyDepsForThisAttribute = getDepsOnlyInSelects(ruleContext, attr, attributes.getAttributeType(attr));
for (TransitiveInfoCollection dep : ruleContext.getPrerequisites(attr, RuleConfiguredTarget.Mode.DONT_CHECK)) {
// Output files inherit the environment spec of their generating rule.
if (dep instanceof OutputFileConfiguredTarget) {
// Note this reassignment means constraint violation errors reference the generating
// rule, not the file. This makes the source of the environmental mismatch more clear.
dep = ((OutputFileConfiguredTarget) dep).getGeneratingRule();
}
// checking, but for now just pass them by.
if (dep.getProvider(SupportedEnvironmentsProvider.class) != null) {
depsToCheck.add(dep);
if (!selectOnlyDepsForThisAttribute.contains(dep.getLabel())) {
depsOutsideSelects.add(dep);
}
}
}
}
for (TransitiveInfoCollection dep : depsToCheck) {
if (!depsOutsideSelects.contains(dep)) {
selectOnlyDeps.add(dep);
}
}
return new DepsToCheck(depsToCheck, selectOnlyDeps);
}
use of com.google.devtools.build.lib.packages.AttributeMap in project bazel by bazelbuild.
the class ConstraintSemantics method validateAttributes.
/**
* Validity-checks this rule's constraint-related attributes. Returns true if all is good,
* returns false and reports appropriate errors if there are any problems.
*/
private static boolean validateAttributes(RuleContext ruleContext) {
AttributeMap attributes = ruleContext.attributes();
// Report an error if "restricted to" is explicitly set to nothing. Even if this made
// conceptual sense, we don't know which groups we should apply that to.
String restrictionAttr = RuleClass.RESTRICTED_ENVIRONMENT_ATTR;
List<? extends TransitiveInfoCollection> restrictionEnvironments = ruleContext.getPrerequisites(restrictionAttr, RuleConfiguredTarget.Mode.DONT_CHECK);
if (restrictionEnvironments.isEmpty() && attributes.isAttributeValueExplicitlySpecified(restrictionAttr)) {
ruleContext.attributeError(restrictionAttr, "attribute cannot be empty");
return false;
}
return true;
}
use of com.google.devtools.build.lib.packages.AttributeMap in project bazel by bazelbuild.
the class LicensesProviderImpl method of.
/**
* Create the appropriate {@link LicensesProvider} for a rule based on its {@code RuleContext}
*/
public static LicensesProvider of(RuleContext ruleContext) {
if (!ruleContext.getConfiguration().checkLicenses()) {
return EMPTY;
}
NestedSetBuilder<TargetLicense> builder = NestedSetBuilder.linkOrder();
BuildConfiguration configuration = ruleContext.getConfiguration();
Rule rule = ruleContext.getRule();
AttributeMap attributes = ruleContext.attributes();
License toolOutputLicense = rule.getToolOutputLicense(attributes);
TargetLicense outputLicenses = toolOutputLicense == null ? null : new TargetLicense(rule.getLabel(), toolOutputLicense);
if (configuration.isHostConfiguration() && toolOutputLicense != null) {
if (toolOutputLicense != License.NO_LICENSE) {
builder.add(outputLicenses);
}
} else {
if (rule.getLicense() != License.NO_LICENSE) {
builder.add(new TargetLicense(rule.getLabel(), rule.getLicense()));
}
ListMultimap<String, ? extends TransitiveInfoCollection> configuredMap = ruleContext.getConfiguredTargetMap();
for (String depAttrName : attributes.getAttributeNames()) {
// Only add the transitive licenses for the attributes that do not have the output_licenses.
Attribute attribute = attributes.getAttributeDefinition(depAttrName);
for (TransitiveInfoCollection dep : configuredMap.get(depAttrName)) {
LicensesProvider provider = dep.getProvider(LicensesProvider.class);
if (provider == null) {
continue;
}
if (useOutputLicenses(attribute, configuration) && provider.hasOutputLicenses()) {
builder.add(provider.getOutputLicenses());
} else {
builder.addTransitive(provider.getTransitiveLicenses());
}
}
}
}
return new LicensesProviderImpl(builder.build(), outputLicenses);
}
Aggregations