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);
}
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());
}
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;
}
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);
}
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();
}
Aggregations