Search in sources :

Example 1 with PackageGroup

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

the class XmlOutputFormatter method createTargetElement.

/**
   * Creates and returns a new DOM tree for the specified build target.
   *
   * XML structure:
   * - element tag is <source-file>, <generated-file> or <rule
   *   class="cc_library">, following the terminology of
   *   {@link Target#getTargetKind()}.
   * - 'name' attribute is target's label.
   * - 'location' attribute is consistent with output of --output location.
   * - rule attributes are represented in the DOM structure.
   * @throws InterruptedException
   */
private Element createTargetElement(Document doc, Target target) throws InterruptedException {
    Element elem;
    if (target instanceof Rule) {
        Rule rule = (Rule) target;
        elem = doc.createElement("rule");
        elem.setAttribute("class", rule.getRuleClass());
        for (Attribute attr : rule.getAttributes()) {
            PossibleAttributeValues values = getPossibleAttributeValues(rule, attr);
            if (values.source == AttributeValueSource.RULE || options.xmlShowDefaultValues) {
                Element attrElem = createValueElement(doc, attr.getType(), values);
                attrElem.setAttribute("name", attr.getName());
                elem.appendChild(attrElem);
            }
        }
        // host-configuration outputs, and default values.
        for (Label label : rule.getLabels(dependencyFilter)) {
            Element inputElem = doc.createElement("rule-input");
            inputElem.setAttribute("name", label.toString());
            elem.appendChild(inputElem);
        }
        for (Label label : aspectResolver.computeAspectDependencies(target, dependencyFilter).values()) {
            Element inputElem = doc.createElement("rule-input");
            inputElem.setAttribute("name", label.toString());
            elem.appendChild(inputElem);
        }
        for (OutputFile outputFile : rule.getOutputFiles()) {
            Element outputElem = doc.createElement("rule-output");
            outputElem.setAttribute("name", outputFile.getLabel().toString());
            elem.appendChild(outputElem);
        }
        for (String feature : rule.getFeatures()) {
            Element outputElem = doc.createElement("rule-default-setting");
            outputElem.setAttribute("name", feature);
            elem.appendChild(outputElem);
        }
    } else if (target instanceof PackageGroup) {
        PackageGroup packageGroup = (PackageGroup) target;
        elem = doc.createElement("package-group");
        elem.setAttribute("name", packageGroup.getName());
        Element includes = createValueElement(doc, BuildType.LABEL_LIST, packageGroup.getIncludes());
        includes.setAttribute("name", "includes");
        elem.appendChild(includes);
        Element packages = createValueElement(doc, Type.STRING_LIST, packageGroup.getContainedPackages());
        packages.setAttribute("name", "packages");
        elem.appendChild(packages);
    } else if (target instanceof OutputFile) {
        OutputFile outputFile = (OutputFile) target;
        elem = doc.createElement("generated-file");
        elem.setAttribute("generating-rule", outputFile.getGeneratingRule().getLabel().toString());
    } else if (target instanceof InputFile) {
        elem = doc.createElement("source-file");
        InputFile inputFile = (InputFile) target;
        if (inputFile.getName().equals("BUILD")) {
            addSubincludedFilesToElement(doc, elem, inputFile);
            addSkylarkFilesToElement(doc, elem, inputFile);
            addFeaturesToElement(doc, elem, inputFile);
            elem.setAttribute("package_contains_errors", String.valueOf(inputFile.getPackage().containsErrors()));
        }
        addPackageGroupsToElement(doc, elem, inputFile);
    } else if (target instanceof EnvironmentGroup) {
        EnvironmentGroup envGroup = (EnvironmentGroup) target;
        elem = doc.createElement("environment-group");
        elem.setAttribute("name", envGroup.getName());
        Element environments = createValueElement(doc, BuildType.LABEL_LIST, envGroup.getEnvironments());
        environments.setAttribute("name", "environments");
        elem.appendChild(environments);
        Element defaults = createValueElement(doc, BuildType.LABEL_LIST, envGroup.getDefaults());
        defaults.setAttribute("name", "defaults");
        elem.appendChild(defaults);
    } else if (target instanceof FakeSubincludeTarget) {
        elem = doc.createElement("source-file");
    } else {
        throw new IllegalArgumentException(target.toString());
    }
    elem.setAttribute("name", target.getLabel().toString());
    String location = getLocation(target, options.relativeLocations);
    if (!options.xmlLineNumbers) {
        int firstColon = location.indexOf(':');
        if (firstColon != -1) {
            location = location.substring(0, firstColon);
        }
    }
    elem.setAttribute("location", location);
    return elem;
}
Also used : OutputFile(com.google.devtools.build.lib.packages.OutputFile) Attribute(com.google.devtools.build.lib.packages.Attribute) Element(org.w3c.dom.Element) Label(com.google.devtools.build.lib.cmdline.Label) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup) InputFile(com.google.devtools.build.lib.packages.InputFile) EnvironmentGroup(com.google.devtools.build.lib.packages.EnvironmentGroup) FakeSubincludeTarget(com.google.devtools.build.lib.query2.FakeSubincludeTarget) Rule(com.google.devtools.build.lib.packages.Rule)

Example 2 with PackageGroup

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

the class DependencyResolver method visitPackageGroup.

private void visitPackageGroup(TargetAndConfiguration node, PackageGroup packageGroup, NestedSetBuilder<Label> rootCauses, Collection<Dependency> outgoingEdges) throws InterruptedException {
    for (Label label : packageGroup.getIncludes()) {
        Target target = getTarget(packageGroup, label, rootCauses);
        if (target == null) {
            continue;
        }
        if (!(target instanceof PackageGroup)) {
            // Note that this error could also be caught in PackageGroupConfiguredTarget, but since
            // these have the null configuration, visiting the corresponding target would trigger an
            // analysis of a rule with a null configuration, which doesn't work.
            invalidPackageGroupReferenceHook(node, label);
            continue;
        }
        outgoingEdges.add(Dependency.withNullConfiguration(label));
    }
}
Also used : Target(com.google.devtools.build.lib.packages.Target) Label(com.google.devtools.build.lib.cmdline.Label) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup)

Example 3 with PackageGroup

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

the class DependencyResolver method visitTargetVisibility.

private void visitTargetVisibility(TargetAndConfiguration node, NestedSetBuilder<Label> rootCauses, Collection<Dependency> outgoingEdges) throws InterruptedException {
    Target target = node.getTarget();
    for (Label label : target.getVisibility().getDependencyLabels()) {
        Target visibilityTarget = getTarget(target, label, rootCauses);
        if (visibilityTarget == null) {
            continue;
        }
        if (!(visibilityTarget instanceof PackageGroup)) {
            // Note that this error could also be caught in
            // AbstractConfiguredTarget.convertVisibility(), but we have an
            // opportunity here to avoid dependency cycles that result from
            // the visibility attribute of a rule referring to a rule that
            // depends on it (instead of its package)
            invalidVisibilityReferenceHook(node, label);
            continue;
        }
        // Visibility always has null configuration
        outgoingEdges.add(Dependency.withNullConfiguration(label));
    }
}
Also used : Target(com.google.devtools.build.lib.packages.Target) Label(com.google.devtools.build.lib.cmdline.Label) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup)

Example 4 with PackageGroup

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

the class TransitiveBaseTraversalFunction method getLabelDeps.

// TODO(bazel-team): Unify this logic with that in LabelVisitor, and possibly DependencyResolver.
private static Iterable<Label> getLabelDeps(Target target) throws InterruptedException {
    final Set<Label> labels = new HashSet<>();
    if (target instanceof OutputFile) {
        Rule rule = ((OutputFile) target).getGeneratingRule();
        labels.add(rule.getLabel());
        visitTargetVisibility(target, labels);
    } else if (target instanceof InputFile) {
        visitTargetVisibility(target, labels);
    } else if (target instanceof Rule) {
        visitTargetVisibility(target, labels);
        visitRule(target, labels);
    } else if (target instanceof PackageGroup) {
        visitPackageGroup((PackageGroup) target, labels);
    }
    return labels;
}
Also used : OutputFile(com.google.devtools.build.lib.packages.OutputFile) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup) HashSet(java.util.HashSet) InputFile(com.google.devtools.build.lib.packages.InputFile)

Example 5 with PackageGroup

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

Aggregations

PackageGroup (com.google.devtools.build.lib.packages.PackageGroup)7 Label (com.google.devtools.build.lib.cmdline.Label)5 InputFile (com.google.devtools.build.lib.packages.InputFile)5 OutputFile (com.google.devtools.build.lib.packages.OutputFile)5 Rule (com.google.devtools.build.lib.packages.Rule)5 EnvironmentGroup (com.google.devtools.build.lib.packages.EnvironmentGroup)4 Target (com.google.devtools.build.lib.packages.Target)4 Attribute (com.google.devtools.build.lib.packages.Attribute)3 FakeSubincludeTarget (com.google.devtools.build.lib.query2.FakeSubincludeTarget)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 PackageSpecification (com.google.devtools.build.lib.packages.PackageSpecification)1 QueryEnvironment (com.google.devtools.build.lib.query2.engine.QueryEnvironment)1 Build (com.google.devtools.build.lib.query2.proto.proto2api.Build)1 Builder (com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult.Builder)1 SourceFile (com.google.devtools.build.lib.query2.proto.proto2api.Build.SourceFile)1 ConfiguredTargetKey (com.google.devtools.build.lib.skyframe.ConfiguredTargetKey)1 Environment (com.google.devtools.build.lib.syntax.Environment)1 Collection (java.util.Collection)1