Search in sources :

Example 36 with Attribute

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

Example 37 with Attribute

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

the class RuleContext method getSplitPrerequisites.

/**
   * Returns the a prerequisites keyed by the CPU of their configurations.
   * If the split transition is not active (e.g. split() returned an empty
   * list), the key is an empty Optional.
   */
public Map<Optional<String>, ? extends List<? extends TransitiveInfoCollection>> getSplitPrerequisites(String attributeName) {
    checkAttribute(attributeName, Mode.SPLIT);
    Attribute attributeDefinition = attributes().getAttributeDefinition(attributeName);
    // Attribute.java doesn't have the BuildOptions symbol.
    @SuppressWarnings("unchecked") SplitTransition<BuildOptions> transition = (SplitTransition<BuildOptions>) attributeDefinition.getSplitTransition(rule);
    List<ConfiguredTarget> deps = targetMap.get(attributeName);
    List<BuildOptions> splitOptions = transition.split(getConfiguration().getOptions());
    if (splitOptions.isEmpty()) {
        // The split transition is not active. Defer the decision on which CPU to use.
        return ImmutableMap.of(Optional.<String>absent(), deps);
    }
    Set<String> cpus = new HashSet<>();
    for (BuildOptions options : splitOptions) {
        // This method should only be called when the split config is enabled on the command line, in
        // which case this cpu can't be null.
        cpus.add(options.get(BuildConfiguration.Options.class).cpu);
    }
    // Use an ImmutableListMultimap.Builder here to preserve ordering.
    ImmutableListMultimap.Builder<Optional<String>, TransitiveInfoCollection> result = ImmutableListMultimap.builder();
    for (TransitiveInfoCollection t : deps) {
        if (t.getConfiguration() != null) {
            result.put(Optional.of(t.getConfiguration().getCpu()), t);
        } else {
            // Source files don't have a configuration, so we add them to all architecture entries.
            for (String cpu : cpus) {
                result.put(Optional.of(cpu), t);
            }
        }
    }
    return Multimaps.asMap(result.build());
}
Also used : Optional(com.google.common.base.Optional) Attribute(com.google.devtools.build.lib.packages.Attribute) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) SplitTransition(com.google.devtools.build.lib.packages.Attribute.SplitTransition) BuildOptions(com.google.devtools.build.lib.analysis.config.BuildOptions) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 38 with Attribute

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

the class RuleContext method getExecutablePrerequisite.

/**
   * Returns the prerequisite referred to by the specified attribute. Also checks whether
   * the attribute is marked as executable and that the target referred to can actually be
   * executed.
   *
   * <p>The {@code mode} argument must match the configuration transition specified in the
   * definition of the attribute.
   *
   * @param attributeName the name of the attribute
   * @param mode the configuration transition of the attribute
   *
   * @return the {@link FilesToRunProvider} interface of the prerequisite.
   */
@Nullable
public FilesToRunProvider getExecutablePrerequisite(String attributeName, Mode mode) {
    Attribute ruleDefinition = attributes().getAttributeDefinition(attributeName);
    if (ruleDefinition == null) {
        throw new IllegalStateException(getRuleClassNameForLogging() + " attribute " + attributeName + " is not defined");
    }
    if (!ruleDefinition.isExecutable()) {
        throw new IllegalStateException(getRuleClassNameForLogging() + " attribute " + attributeName + " is not configured to be executable");
    }
    TransitiveInfoCollection prerequisite = getPrerequisite(attributeName, mode);
    if (prerequisite == null) {
        return null;
    }
    FilesToRunProvider result = prerequisite.getProvider(FilesToRunProvider.class);
    if (result == null || result.getExecutable() == null) {
        attributeError(attributeName, prerequisite.getLabel() + " does not refer to a valid executable target");
    }
    return result;
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) Nullable(javax.annotation.Nullable)

Example 39 with Attribute

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

the class RuleContext method getPrerequisites.

/**
   * Returns the list of transitive info collections that feed into this target through the
   * specified attribute. Note that you need to specify the correct mode for the attribute,
   * otherwise an assertion will be raised.
   */
public List<? extends TransitiveInfoCollection> getPrerequisites(String attributeName, Mode mode) {
    Attribute attributeDefinition = attributes().getAttributeDefinition(attributeName);
    if ((mode == Mode.TARGET) && (attributeDefinition.hasSplitConfigurationTransition())) {
        // TODO(bazel-team): If you request a split-configured attribute in the target configuration,
        // we return only the list of configured targets for the first architecture; this is for
        // backwards compatibility with existing code in cases where the call to getPrerequisites is
        // deeply nested and we can't easily inject the behavior we want. However, we should fix all
        // such call sites.
        checkAttribute(attributeName, Mode.SPLIT);
        Map<Optional<String>, ? extends List<? extends TransitiveInfoCollection>> map = getSplitPrerequisites(attributeName);
        return map.isEmpty() ? ImmutableList.<TransitiveInfoCollection>of() : map.entrySet().iterator().next().getValue();
    }
    checkAttribute(attributeName, mode);
    return targetMap.get(attributeName);
}
Also used : Optional(com.google.common.base.Optional) Attribute(com.google.devtools.build.lib.packages.Attribute)

Example 40 with Attribute

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

the class BuildView method addExtraActionsFromTargets.

private NestedSet<Artifact> addExtraActionsFromTargets(BuildView.Options viewOptions, Collection<ConfiguredTarget> configuredTargets) {
    NestedSetBuilder<Artifact> builder = NestedSetBuilder.stableOrder();
    for (ConfiguredTarget target : configuredTargets) {
        ExtraActionArtifactsProvider provider = target.getProvider(ExtraActionArtifactsProvider.class);
        if (provider != null) {
            if (viewOptions.extraActionTopLevelOnly) {
                if (!viewOptions.extraActionTopLevelOnlyWithAspects) {
                    builder.addTransitive(provider.getExtraActionArtifacts());
                } else {
                    // Collect all aspect-classes that topLevel might inject.
                    Set<AspectClass> aspectClasses = new HashSet<>();
                    for (Attribute attr : target.getTarget().getAssociatedRule().getAttributes()) {
                        aspectClasses.addAll(attr.getAspectClasses());
                    }
                    builder.addTransitive(provider.getExtraActionArtifacts());
                    if (!aspectClasses.isEmpty()) {
                        builder.addAll(filterTransitiveExtraActions(provider, aspectClasses));
                    }
                }
            } else {
                builder.addTransitive(provider.getTransitiveExtraActionArtifacts());
            }
        }
    }
    return builder.build();
}
Also used : NativeAspectClass(com.google.devtools.build.lib.packages.NativeAspectClass) AspectClass(com.google.devtools.build.lib.packages.AspectClass) Attribute(com.google.devtools.build.lib.packages.Attribute) Artifact(com.google.devtools.build.lib.actions.Artifact) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

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