Search in sources :

Example 56 with Attribute

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) Aspect(com.google.devtools.build.lib.packages.Aspect) Target(com.google.devtools.build.lib.packages.Target) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Rule(com.google.devtools.build.lib.packages.Rule) Package(com.google.devtools.build.lib.packages.Package)

Example 57 with Attribute

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Attribute(com.google.devtools.build.lib.packages.Attribute) Builder(com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult.Builder) Label(com.google.devtools.build.lib.cmdline.Label) EnvironmentGroup(com.google.devtools.build.lib.packages.EnvironmentGroup) FakeSubincludeTarget(com.google.devtools.build.lib.query2.FakeSubincludeTarget) Target(com.google.devtools.build.lib.packages.Target) FakeSubincludeTarget(com.google.devtools.build.lib.query2.FakeSubincludeTarget) Build(com.google.devtools.build.lib.query2.proto.proto2api.Build) SourceFile(com.google.devtools.build.lib.query2.proto.proto2api.Build.SourceFile) OutputFile(com.google.devtools.build.lib.packages.OutputFile) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup) InputFile(com.google.devtools.build.lib.packages.InputFile) Environment(com.google.devtools.build.lib.syntax.Environment) QueryEnvironment(com.google.devtools.build.lib.query2.engine.QueryEnvironment) Collection(java.util.Collection) Rule(com.google.devtools.build.lib.packages.Rule) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 58 with Attribute

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

Example 59 with Attribute

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);
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule) Aspect(com.google.devtools.build.lib.packages.Aspect)

Example 60 with Attribute

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();
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) AttributeDefinition(com.google.devtools.build.lib.query2.proto.proto2api.Build.AttributeDefinition) RuleDefinition(com.google.devtools.build.lib.query2.proto.proto2api.Build.RuleDefinition) RuleClass(com.google.devtools.build.lib.packages.RuleClass) BuildLanguage(com.google.devtools.build.lib.query2.proto.proto2api.Build.BuildLanguage)

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