Search in sources :

Example 31 with Attribute

use of com.google.devtools.build.lib.packages.Attribute 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 32 with Attribute

use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.

the class BuildRuleWithDefaultsBuilder method populateAttributes.

public BuildRuleWithDefaultsBuilder populateAttributes(String rulePkg, boolean heuristics) {
    for (Attribute attribute : ruleClass.getAttributes()) {
        if (attribute.isMandatory()) {
            if (BuildType.isLabelType(attribute.getType())) {
                // TODO(bazel-team): actually an empty list would be fine in the case where
                // attribute instanceof ListType && !attribute.isNonEmpty(), but BuildRuleBuilder
                // doesn't support that, and it makes little sense anyway
                populateLabelAttribute(rulePkg, attribute);
            } else {
                // Non label type attributes
                if (attribute.getAllowedValues() instanceof AllowedValueSet) {
                    Collection<Object> allowedValues = ((AllowedValueSet) attribute.getAllowedValues()).getAllowedValues();
                    setSingleValueAttribute(attribute.getName(), allowedValues.iterator().next());
                } else if (attribute.getType() == Type.STRING) {
                    populateStringAttribute(attribute);
                } else if (attribute.getType() == Type.BOOLEAN) {
                    populateBooleanAttribute(attribute);
                } else if (attribute.getType() == Type.INTEGER) {
                    populateIntegerAttribute(attribute);
                } else if (attribute.getType() == Type.STRING_LIST) {
                    populateStringListAttribute(attribute);
                }
            }
        // TODO(bazel-team): populate for other data types
        } else if (heuristics) {
            populateAttributesHeuristics(rulePkg, attribute);
        }
    }
    return this;
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) AllowedValueSet(com.google.devtools.build.lib.packages.Attribute.AllowedValueSet)

Example 33 with Attribute

use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.

the class PostConfiguredTargetFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException, InterruptedException {
    ImmutableMap<ActionAnalysisMetadata, ConflictException> badActions = PrecomputedValue.BAD_ACTIONS.get(env);
    ConfiguredTargetValue ctValue = (ConfiguredTargetValue) env.getValue(ConfiguredTargetValue.key((ConfiguredTargetKey) skyKey.argument()));
    if (env.valuesMissing()) {
        return null;
    }
    for (ActionAnalysisMetadata action : ctValue.getActions()) {
        if (badActions.containsKey(action)) {
            throw new ActionConflictFunctionException(badActions.get(action));
        }
    }
    ConfiguredTarget ct = ctValue.getConfiguredTarget();
    TargetAndConfiguration ctgValue = new TargetAndConfiguration(ct.getTarget(), ct.getConfiguration());
    ImmutableMap<Label, ConfigMatchingProvider> configConditions = getConfigurableAttributeConditions(ctgValue, env);
    if (configConditions == null) {
        return null;
    }
    OrderedSetMultimap<Attribute, Dependency> deps;
    try {
        BuildConfiguration hostConfiguration = buildViewProvider.getSkyframeBuildView().getHostConfiguration(ct.getConfiguration());
        SkyframeDependencyResolver resolver = buildViewProvider.getSkyframeBuildView().createDependencyResolver(env);
        // We don't track root causes here - this function is only invoked for successfully analyzed
        // targets - as long as we redo the exact same steps here as in ConfiguredTargetFunction, this
        // can never fail.
        deps = resolver.dependentNodeMap(ctgValue, hostConfiguration, /*aspect=*/
        null, configConditions);
        if (ct.getConfiguration() != null && ct.getConfiguration().useDynamicConfigurations()) {
            deps = ConfiguredTargetFunction.getDynamicConfigurations(env, ctgValue, deps, hostConfiguration, ruleClassProvider);
        }
    } catch (EvalException | ConfiguredTargetFunction.DependencyEvaluationException | InvalidConfigurationException | InconsistentAspectOrderException e) {
        throw new PostConfiguredTargetFunctionException(e);
    }
    env.getValues(Iterables.transform(deps.values(), TO_KEYS));
    if (env.valuesMissing()) {
        return null;
    }
    return new PostConfiguredTargetValue(ct);
}
Also used : ConflictException(com.google.devtools.build.lib.skyframe.SkyframeActionExecutor.ConflictException) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) ActionAnalysisMetadata(com.google.devtools.build.lib.actions.ActionAnalysisMetadata) Dependency(com.google.devtools.build.lib.analysis.Dependency) EvalException(com.google.devtools.build.lib.syntax.EvalException) InconsistentAspectOrderException(com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) TargetAndConfiguration(com.google.devtools.build.lib.analysis.TargetAndConfiguration) ConfigMatchingProvider(com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider) Nullable(javax.annotation.Nullable)

Example 34 with Attribute

use of com.google.devtools.build.lib.packages.Attribute 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 35 with Attribute

use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.

the class RuleDocumentationAttributeTest method testSynopsisForIntegerAttribute.

@Test
public void testSynopsisForIntegerAttribute() {
    final int defaultValue = 384;
    Attribute attribute = Attribute.attr("bar_limit", Type.INTEGER).value(defaultValue).build();
    RuleDocumentationAttribute attributeDoc = RuleDocumentationAttribute.create(TestRule.class, "testrule", "", 0, "", NO_FLAGS);
    attributeDoc.setAttribute(attribute);
    String doc = attributeDoc.getSynopsis();
    assertEquals("Integer; optional; default is " + defaultValue, doc);
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) Test(org.junit.Test)

Aggregations

Attribute (com.google.devtools.build.lib.packages.Attribute)76 Test (org.junit.Test)37 Label (com.google.devtools.build.lib.cmdline.Label)20 Rule (com.google.devtools.build.lib.packages.Rule)20 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)7 AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)7 Target (com.google.devtools.build.lib.packages.Target)7 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)6 ConfigMatchingProvider (com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider)6 RuleClass (com.google.devtools.build.lib.packages.RuleClass)6 SkyKey (com.google.devtools.build.skyframe.SkyKey)6 LinkedHashSet (java.util.LinkedHashSet)6 Nullable (javax.annotation.Nullable)6 AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)5 Map (java.util.Map)5 Dependency (com.google.devtools.build.lib.analysis.Dependency)4