Search in sources :

Example 1 with AttributeMap

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);
}
Also used : AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Attribute(com.google.devtools.build.lib.packages.Attribute) ArrayList(java.util.ArrayList)

Example 2 with AttributeMap

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);
}
Also used : AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Attribute(com.google.devtools.build.lib.packages.Attribute) LateBoundLabel(com.google.devtools.build.lib.packages.Attribute.LateBoundLabel) AspectDefinition(com.google.devtools.build.lib.packages.AspectDefinition) Rule(com.google.devtools.build.lib.packages.Rule) Test(org.junit.Test)

Example 3 with AttributeMap

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) OutputFileConfiguredTarget(com.google.devtools.build.lib.analysis.OutputFileConfiguredTarget)

Example 4 with AttributeMap

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;
}
Also used : AttributeMap(com.google.devtools.build.lib.packages.AttributeMap)

Example 5 with AttributeMap

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);
}
Also used : BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Attribute(com.google.devtools.build.lib.packages.Attribute) License(com.google.devtools.build.lib.packages.License) Rule(com.google.devtools.build.lib.packages.Rule)

Aggregations

AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)11 Attribute (com.google.devtools.build.lib.packages.Attribute)5 Rule (com.google.devtools.build.lib.packages.Rule)5 Test (org.junit.Test)3 Label (com.google.devtools.build.lib.cmdline.Label)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 OutputFileConfiguredTarget (com.google.devtools.build.lib.analysis.OutputFileConfiguredTarget)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)1 IterablesChain (com.google.devtools.build.lib.collect.IterablesChain)1 Location (com.google.devtools.build.lib.events.Location)1 AspectDefinition (com.google.devtools.build.lib.packages.AspectDefinition)1 LateBoundLabel (com.google.devtools.build.lib.packages.Attribute.LateBoundLabel)1 License (com.google.devtools.build.lib.packages.License)1 Target (com.google.devtools.build.lib.packages.Target)1 DefaultModelResolver (com.google.devtools.build.workspace.maven.DefaultModelResolver)1 InvalidArtifactCoordinateException (com.google.devtools.build.workspace.maven.Resolver.InvalidArtifactCoordinateException)1 Rule (com.google.devtools.build.workspace.maven.Rule)1