Search in sources :

Example 1 with OutputFile

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

the class CompileOneDependencyTransformer method listContainsFile.

/**
   * Returns true if a specific rule compiles a specific source. Looks through genrules and
   * filegroups.
   */
private boolean listContainsFile(ExtendedEventHandler eventHandler, Collection<Label> srcLabels, Label source, Set<Label> visitedRuleLabels) throws TargetParsingException, InterruptedException {
    if (srcLabels.contains(source)) {
        return true;
    }
    for (Label label : srcLabels) {
        if (!visitedRuleLabels.add(label)) {
            continue;
        }
        Target target = null;
        try {
            target = targetProvider.getTarget(eventHandler, label);
        } catch (NoSuchThingException e) {
        // Just ignore failing sources/packages. We could report them here, but as long as we do
        // early return, the presence of this error would then be determined by the order of items
        // in the srcs attribute. A proper error will be created by the subsequent loading.
        }
        if (target == null || target instanceof FileTarget) {
            continue;
        }
        Rule targetRule = target.getAssociatedRule();
        if ("filegroup".equals(targetRule.getRuleClass())) {
            RawAttributeMapper attributeMapper = RawAttributeMapper.of(targetRule);
            Collection<Label> srcs = attributeMapper.getMergedValues("srcs", BuildType.LABEL_LIST);
            if (listContainsFile(eventHandler, srcs, source, visitedRuleLabels)) {
                return true;
            }
        } else if ("genrule".equals(targetRule.getRuleClass())) {
            // TODO(djasper): Likely, it makes much more sense to look at the inputs of a genrule.
            for (OutputFile file : targetRule.getOutputFiles()) {
                if (file.getLabel().equals(source)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) OutputFile(com.google.devtools.build.lib.packages.OutputFile) FileTarget(com.google.devtools.build.lib.packages.FileTarget) Target(com.google.devtools.build.lib.packages.Target) NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) FileTarget(com.google.devtools.build.lib.packages.FileTarget) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule)

Example 2 with OutputFile

use of com.google.devtools.build.lib.packages.OutputFile 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 &lt;source-file>, &lt;generated-file> or &lt;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 3 with OutputFile

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

the class BlazeQueryEnvironment method getTargetsMatchingPatternImpl.

private void getTargetsMatchingPatternImpl(String pattern, Callback<Target> callback) throws QueryException, InterruptedException {
    // We can safely ignore the boolean error flag. The evaluateQuery() method above wraps the
    // entire query computation in an error sensor.
    Set<Target> targets = new LinkedHashSet<>(resolvedTargetPatterns.get(pattern));
    // Sets.filter would be more convenient here, but can't deal with exceptions.
    Iterator<Target> targetIterator = targets.iterator();
    while (targetIterator.hasNext()) {
        Target target = targetIterator.next();
        if (!validateScope(target.getLabel(), strictScope)) {
            targetIterator.remove();
        }
    }
    Set<PathFragment> packages = new HashSet<>();
    for (Target target : targets) {
        packages.add(target.getLabel().getPackageFragment());
    }
    Set<Target> result = new LinkedHashSet<>();
    for (Target target : targets) {
        result.add(getOrCreate(target));
        // targets in this set.
        if (target instanceof OutputFile) {
            OutputFile outputFile = (OutputFile) target;
            if (targets.contains(outputFile.getGeneratingRule())) {
                makeEdge(outputFile, outputFile.getGeneratingRule());
            }
        } else if (target instanceof Rule) {
            Rule rule = (Rule) target;
            for (Label label : rule.getLabels(dependencyFilter)) {
                if (!packages.contains(label.getPackageFragment())) {
                    // don't cause additional package loading
                    continue;
                }
                try {
                    if (!validateScope(label, strictScope)) {
                        // Don't create edges to targets which are out of scope.
                        continue;
                    }
                    Target to = getTargetOrThrow(label);
                    if (targets.contains(to)) {
                        makeEdge(rule, to);
                    }
                } catch (NoSuchThingException e) {
                /* ignore */
                }
            }
        }
    }
    callback.process(result);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OutputFile(com.google.devtools.build.lib.packages.OutputFile) Target(com.google.devtools.build.lib.packages.Target) NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with OutputFile

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

the class LocationExpander method buildLocationMap.

/**
   * Extracts all possible target locations from target specification.
   *
   * @param ruleContext BUILD target object
   * @param labelMap map of labels to build artifacts
   * @return map of all possible target locations
   */
private static Map<Label, Collection<Artifact>> buildLocationMap(RuleContext ruleContext, Map<Label, ? extends Collection<Artifact>> labelMap, boolean allowDataAttributeEntriesInLabel) {
    Map<Label, Collection<Artifact>> locationMap = Maps.newHashMap();
    if (labelMap != null) {
        for (Map.Entry<Label, ? extends Collection<Artifact>> entry : labelMap.entrySet()) {
            mapGet(locationMap, entry.getKey()).addAll(entry.getValue());
        }
    }
    // Add all destination locations.
    for (OutputFile out : ruleContext.getRule().getOutputFiles()) {
        mapGet(locationMap, out.getLabel()).add(ruleContext.createOutputArtifact(out));
    }
    if (ruleContext.getRule().isAttrDefined("srcs", BuildType.LABEL_LIST)) {
        for (TransitiveInfoCollection src : ruleContext.getPrerequisitesIf("srcs", Mode.TARGET, FileProvider.class)) {
            Iterables.addAll(mapGet(locationMap, AliasProvider.getDependencyLabel(src)), src.getProvider(FileProvider.class).getFilesToBuild());
        }
    }
    // Add all locations associated with dependencies and tools
    List<TransitiveInfoCollection> depsDataAndTools = new ArrayList<>();
    if (ruleContext.getRule().isAttrDefined("deps", BuildType.LABEL_LIST)) {
        Iterables.addAll(depsDataAndTools, ruleContext.getPrerequisitesIf("deps", Mode.DONT_CHECK, FilesToRunProvider.class));
    }
    if (allowDataAttributeEntriesInLabel && ruleContext.getRule().isAttrDefined("data", BuildType.LABEL_LIST)) {
        Iterables.addAll(depsDataAndTools, ruleContext.getPrerequisitesIf("data", Mode.DATA, FilesToRunProvider.class));
    }
    if (ruleContext.getRule().isAttrDefined("tools", BuildType.LABEL_LIST)) {
        Iterables.addAll(depsDataAndTools, ruleContext.getPrerequisitesIf("tools", Mode.HOST, FilesToRunProvider.class));
    }
    for (TransitiveInfoCollection dep : depsDataAndTools) {
        Label label = AliasProvider.getDependencyLabel(dep);
        FilesToRunProvider filesToRun = dep.getProvider(FilesToRunProvider.class);
        Artifact executableArtifact = filesToRun.getExecutable();
        // If the label has an executable artifact add that to the multimaps.
        if (executableArtifact != null) {
            mapGet(locationMap, label).add(executableArtifact);
        } else {
            mapGet(locationMap, label).addAll(filesToRun.getFilesToRun());
        }
    }
    return locationMap;
}
Also used : OutputFile(com.google.devtools.build.lib.packages.OutputFile) Label(com.google.devtools.build.lib.cmdline.Label) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ImmutableCollection(com.google.common.collect.ImmutableCollection) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 5 with OutputFile

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

Aggregations

OutputFile (com.google.devtools.build.lib.packages.OutputFile)10 Rule (com.google.devtools.build.lib.packages.Rule)8 Label (com.google.devtools.build.lib.cmdline.Label)6 InputFile (com.google.devtools.build.lib.packages.InputFile)5 PackageGroup (com.google.devtools.build.lib.packages.PackageGroup)5 Target (com.google.devtools.build.lib.packages.Target)5 EnvironmentGroup (com.google.devtools.build.lib.packages.EnvironmentGroup)4 Attribute (com.google.devtools.build.lib.packages.Attribute)3 Artifact (com.google.devtools.build.lib.actions.Artifact)2 NoSuchThingException (com.google.devtools.build.lib.packages.NoSuchThingException)2 FakeSubincludeTarget (com.google.devtools.build.lib.query2.FakeSubincludeTarget)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 FileConfiguredTarget (com.google.devtools.build.lib.analysis.FileConfiguredTarget)1 RuleConfiguredTarget (com.google.devtools.build.lib.analysis.RuleConfiguredTarget)1