use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.
the class PreciseAspectResolver method computeBuildFileDependencies.
@Override
public Set<Label> computeBuildFileDependencies(Package pkg, BuildFileDependencyMode mode) throws InterruptedException {
Set<Label> result = new LinkedHashSet<>();
result.addAll(mode.getDependencies(pkg));
Set<PackageIdentifier> dependentPackages = new LinkedHashSet<>();
// Iterate over all rules...
for (Target target : pkg.getTargets()) {
if (!(target instanceof Rule)) {
continue;
}
// ...figure out which direct dependencies can possibly have aspects attached to them...
Multimap<Attribute, Label> depsWithPossibleAspects = ((Rule) target).getTransitions(new BinaryPredicate<Rule, Attribute>() {
@Override
public boolean apply(@Nullable Rule rule, Attribute attribute) {
for (Aspect aspectWithParameters : attribute.getAspects(rule)) {
if (!aspectWithParameters.getDefinition().getAttributes().isEmpty()) {
return true;
}
}
return false;
}
});
// ...and add the package of the aspect.
for (Label depLabel : depsWithPossibleAspects.values()) {
dependentPackages.add(depLabel.getPackageIdentifier());
}
}
// Then add all the subinclude labels of the packages thus found to the result.
for (PackageIdentifier packageIdentifier : dependentPackages) {
try {
result.add(Label.create(packageIdentifier, "BUILD"));
Package dependentPackage = packageProvider.getPackage(eventHandler, packageIdentifier);
result.addAll(mode.getDependencies(dependentPackage));
} catch (NoSuchPackageException e) {
// If the package is not found, just add its BUILD file, which is already done above.
// Hopefully this error is not raised when there is a syntax error in a subincluded file
// or something.
} catch (LabelSyntaxException e) {
throw new IllegalStateException(e);
}
}
return result;
}
use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.
the class ProtoOutputFormatter method toTargetProtoBuffer.
/** Converts a logical {@link Target} object into a {@link Build.Target} protobuffer. */
@VisibleForTesting
public Build.Target toTargetProtoBuffer(Target target) throws InterruptedException {
Build.Target.Builder targetPb = Build.Target.newBuilder();
String location = getLocation(target, relativeLocations);
if (target instanceof Rule) {
Rule rule = (Rule) target;
Build.Rule.Builder rulePb = Build.Rule.newBuilder().setName(rule.getLabel().toString()).setRuleClass(rule.getRuleClass());
if (includeLocation()) {
rulePb.setLocation(location);
}
Map<Attribute, Build.Attribute> serializedAttributes = Maps.newHashMap();
for (Attribute attr : rule.getAttributes()) {
if ((!includeDefaultValues && !rule.isAttributeValueExplicitlySpecified(attr)) || !includeAttribute(rule, attr)) {
continue;
}
Object flattenedAttributeValue = flattenAttributeValues(attr.getType(), getPossibleAttributeValues(rule, attr));
Build.Attribute serializedAttribute = AttributeFormatter.getAttributeProto(attr, flattenedAttributeValue, rule.isAttributeValueExplicitlySpecified(attr), /*encodeBooleanAndTriStateAsIntegerAndString=*/
true);
rulePb.addAttribute(serializedAttribute);
serializedAttributes.put(attr, serializedAttribute);
}
postProcess(rule, rulePb, serializedAttributes);
Environment env = rule.getRuleClassObject().getRuleDefinitionEnvironment();
if (env != null && includeRuleDefinitionEnvironment()) {
// The RuleDefinitionEnvironment is always defined for Skylark rules and
// always null for non Skylark rules.
rulePb.addAttribute(Build.Attribute.newBuilder().setName(RULE_IMPLEMENTATION_HASH_ATTR_NAME).setType(ProtoUtils.getDiscriminatorFromType(Type.STRING)).setStringValue(env.getTransitiveContentHashCode()));
}
ImmutableMultimap<Attribute, Label> aspectsDependencies = aspectResolver.computeAspectDependencies(target, dependencyFilter);
// Add information about additional attributes from aspects.
for (Entry<Attribute, Collection<Label>> entry : aspectsDependencies.asMap().entrySet()) {
Attribute attribute = entry.getKey();
Collection<Label> labels = entry.getValue();
if (!includeAspectAttribute(attribute, labels)) {
continue;
}
Object attributeValue = getAspectAttributeValue(attribute, labels);
Build.Attribute serializedAttribute = AttributeFormatter.getAttributeProto(attribute, attributeValue, /*explicitlySpecified=*/
false, /*encodeBooleanAndTriStateAsIntegerAndString=*/
true);
rulePb.addAttribute(serializedAttribute);
}
if (includeRuleInputsAndOutputs()) {
// Add all deps from aspects as rule inputs of current target.
for (Label label : aspectsDependencies.values()) {
rulePb.addRuleInput(label.toString());
}
// host-configuration outputs, and default values.
for (Label label : rule.getLabels(dependencyFilter)) {
rulePb.addRuleInput(label.toString());
}
for (OutputFile outputFile : rule.getOutputFiles()) {
Label fileLabel = outputFile.getLabel();
rulePb.addRuleOutput(fileLabel.toString());
}
}
for (String feature : rule.getFeatures()) {
rulePb.addDefaultSetting(feature);
}
targetPb.setType(RULE);
targetPb.setRule(rulePb);
} else if (target instanceof OutputFile) {
OutputFile outputFile = (OutputFile) target;
Label label = outputFile.getLabel();
Rule generatingRule = outputFile.getGeneratingRule();
GeneratedFile.Builder output = GeneratedFile.newBuilder().setGeneratingRule(generatingRule.getLabel().toString()).setName(label.toString());
if (includeLocation()) {
output.setLocation(location);
}
targetPb.setType(GENERATED_FILE);
targetPb.setGeneratedFile(output.build());
} else if (target instanceof InputFile) {
InputFile inputFile = (InputFile) target;
Label label = inputFile.getLabel();
Build.SourceFile.Builder input = Build.SourceFile.newBuilder().setName(label.toString());
if (includeLocation()) {
input.setLocation(location);
}
if (inputFile.getName().equals("BUILD")) {
Set<Label> subincludeLabels = new LinkedHashSet<>();
subincludeLabels.addAll(aspectResolver == null ? inputFile.getPackage().getSubincludeLabels() : aspectResolver.computeBuildFileDependencies(inputFile.getPackage(), BuildFileDependencyMode.SUBINCLUDE));
subincludeLabels.addAll(aspectResolver == null ? inputFile.getPackage().getSkylarkFileDependencies() : aspectResolver.computeBuildFileDependencies(inputFile.getPackage(), BuildFileDependencyMode.SKYLARK));
for (Label skylarkFileDep : subincludeLabels) {
input.addSubinclude(skylarkFileDep.toString());
}
for (String feature : inputFile.getPackage().getFeatures()) {
input.addFeature(feature);
}
input.setPackageContainsErrors(inputFile.getPackage().containsErrors());
}
for (Label visibilityDependency : target.getVisibility().getDependencyLabels()) {
input.addPackageGroup(visibilityDependency.toString());
}
for (Label visibilityDeclaration : target.getVisibility().getDeclaredLabels()) {
input.addVisibilityLabel(visibilityDeclaration.toString());
}
targetPb.setType(SOURCE_FILE);
targetPb.setSourceFile(input);
} else if (target instanceof FakeSubincludeTarget) {
Label label = target.getLabel();
SourceFile.Builder input = SourceFile.newBuilder().setName(label.toString());
if (includeLocation()) {
input.setLocation(location);
}
targetPb.setType(SOURCE_FILE);
targetPb.setSourceFile(input.build());
} else if (target instanceof PackageGroup) {
PackageGroup packageGroup = (PackageGroup) target;
Build.PackageGroup.Builder packageGroupPb = Build.PackageGroup.newBuilder().setName(packageGroup.getLabel().toString());
for (String containedPackage : packageGroup.getContainedPackages()) {
packageGroupPb.addContainedPackage(containedPackage);
}
for (Label include : packageGroup.getIncludes()) {
packageGroupPb.addIncludedPackageGroup(include.toString());
}
targetPb.setType(PACKAGE_GROUP);
targetPb.setPackageGroup(packageGroupPb);
} else if (target instanceof EnvironmentGroup) {
EnvironmentGroup envGroup = (EnvironmentGroup) target;
Build.EnvironmentGroup.Builder envGroupPb = Build.EnvironmentGroup.newBuilder().setName(envGroup.getLabel().toString());
for (Label env : envGroup.getEnvironments()) {
envGroupPb.addEnvironment(env.toString());
}
for (Label defaultEnv : envGroup.getDefaults()) {
envGroupPb.addDefault(defaultEnv.toString());
}
targetPb.setType(ENVIRONMENT_GROUP);
targetPb.setEnvironmentGroup(envGroupPb);
} else {
throw new IllegalArgumentException(target.toString());
}
return targetPb.build();
}
use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.
the class BlazeTargetAccessor method getAttrAsString.
@Override
public Iterable<String> getAttrAsString(Target target, String attrName) {
Preconditions.checkArgument(target instanceof Rule);
// May hold null values.
List<String> values = new ArrayList<>();
Attribute attribute = ((Rule) target).getAttributeDefinition(attrName);
if (attribute != null) {
Type<?> attributeType = attribute.getType();
for (Object attrValue : AggregatingAttributeMapper.of((Rule) target).visitAttribute(attribute.getName(), attributeType)) {
// opposite of the code in BooleanType and TriStateType respectively.
if (attributeType == BOOLEAN) {
values.add(Type.BOOLEAN.cast(attrValue) ? "1" : "0");
} else if (attributeType == TRISTATE) {
switch(BuildType.TRISTATE.cast(attrValue)) {
case AUTO:
values.add("-1");
break;
case NO:
values.add("0");
break;
case YES:
values.add("1");
break;
default:
throw new AssertionError("This can't happen!");
}
} else {
values.add(attrValue == null ? null : attrValue.toString());
}
}
}
return values;
}
use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.
the class ConservativeAspectResolver method computeAspectDependencies.
@Override
public ImmutableMultimap<Attribute, Label> computeAspectDependencies(Target target, DependencyFilter dependencyFilter) throws InterruptedException {
if (!(target instanceof Rule)) {
return ImmutableMultimap.of();
}
Rule rule = (Rule) target;
Multimap<Attribute, Label> result = LinkedHashMultimap.create();
for (Attribute attribute : rule.getAttributes()) {
for (Aspect aspect : attribute.getAspects(rule)) {
AspectDefinition.addAllAttributesOfAspect(rule, result, aspect, dependencyFilter);
}
}
return ImmutableMultimap.copyOf(result);
}
use of com.google.devtools.build.lib.packages.Attribute in project bazel by bazelbuild.
the class InfoItem method getBuildLanguageDefinition.
/**
* Returns a byte array containing a proto-buffer describing the build language.
*/
private static byte[] getBuildLanguageDefinition(RuleClassProvider provider) {
BuildLanguage.Builder resultPb = BuildLanguage.newBuilder();
Collection<RuleClass> ruleClasses = provider.getRuleClassMap().values();
for (RuleClass ruleClass : ruleClasses) {
if (!ruleClass.isDocumented()) {
continue;
}
RuleDefinition.Builder rulePb = RuleDefinition.newBuilder();
rulePb.setName(ruleClass.getName());
for (Attribute attr : ruleClass.getAttributes()) {
if (!attr.isDocumented()) {
continue;
}
AttributeDefinition.Builder attrPb = AttributeDefinition.newBuilder();
attrPb.setName(attr.getName());
// The protocol compiler, in its infinite wisdom, generates the field as one of the
// integer type and the getTypeEnum() method is missing. WTF?
attrPb.setType(ProtoUtils.getDiscriminatorFromType(attr.getType()));
attrPb.setMandatory(attr.isMandatory());
if (BuildType.isLabelType(attr.getType())) {
attrPb.setAllowedRuleClasses(getAllowedRuleClasses(ruleClasses, attr));
}
rulePb.addAttribute(attrPb);
}
resultPb.addRule(rulePb);
}
return resultPb.build().toByteArray();
}
Aggregations