Search in sources :

Example 1 with Rule

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

the class AspectDefinitionTest method testAspectWithImplicitOrLateboundAttribute_AddsToAttributeMap.

@Test
public void testAspectWithImplicitOrLateboundAttribute_AddsToAttributeMap() throws Exception {
    Attribute implicit = attr("$runtime", BuildType.LABEL).value(Label.parseAbsoluteUnchecked("//run:time")).build();
    LateBoundLabel<String> latebound = new LateBoundLabel<String>() {

        @Override
        public Label resolve(Rule rule, AttributeMap attributes, String configuration) {
            return Label.parseAbsoluteUnchecked("//run:away");
        }
    };
    AspectDefinition simple = new AspectDefinition.Builder(TEST_ASPECT_CLASS).add(implicit).add(attr(":latebound", BuildType.LABEL).value(latebound)).build();
    assertThat(simple.getAttributes()).containsEntry("$runtime", implicit);
    assertThat(simple.getAttributes()).containsKey(":latebound");
    assertThat(simple.getAttributes().get(":latebound").getLateBoundDefault()).isEqualTo(latebound);
}
Also used : AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Attribute(com.google.devtools.build.lib.packages.Attribute) LateBoundLabel(com.google.devtools.build.lib.packages.Attribute.LateBoundLabel) AspectDefinition(com.google.devtools.build.lib.packages.AspectDefinition) Rule(com.google.devtools.build.lib.packages.Rule) Test(org.junit.Test)

Example 2 with Rule

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

the class CompileOneDependencyTransformer method getOrderedRuleList.

/**
   * Returns a list of rules in the given package sorted by BUILD file order. When
   * multiple rules depend on a target, we choose the first match in this list (after
   * filtering for preferred dependencies - see below).
   */
private Iterable<Rule> getOrderedRuleList(Package pkg) {
    List<Rule> orderedList = Lists.newArrayList();
    for (Rule rule : pkg.getTargets(Rule.class)) {
        orderedList.add(rule);
    }
    Collections.sort(orderedList, new Comparator<Rule>() {

        @Override
        public int compare(Rule o1, Rule o2) {
            return Integer.compare(o1.getLocation().getStartOffset(), o2.getLocation().getStartOffset());
        }
    });
    return orderedList;
}
Also used : Rule(com.google.devtools.build.lib.packages.Rule)

Example 3 with Rule

use of com.google.devtools.build.lib.packages.Rule 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 4 with Rule

use of com.google.devtools.build.lib.packages.Rule 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 5 with Rule

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

Aggregations

Rule (com.google.devtools.build.lib.packages.Rule)79 Test (org.junit.Test)27 Label (com.google.devtools.build.lib.cmdline.Label)26 Attribute (com.google.devtools.build.lib.packages.Attribute)20 Target (com.google.devtools.build.lib.packages.Target)19 Nullable (javax.annotation.Nullable)10 RawAttributeMapper (com.google.devtools.build.lib.packages.RawAttributeMapper)9 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)9 OutputFile (com.google.devtools.build.lib.packages.OutputFile)8 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)7 NoSuchThingException (com.google.devtools.build.lib.packages.NoSuchThingException)7 SkyKey (com.google.devtools.build.skyframe.SkyKey)7 ImmutableList (com.google.common.collect.ImmutableList)6 InputFile (com.google.devtools.build.lib.packages.InputFile)6 IOException (java.io.IOException)6 LinkedHashSet (java.util.LinkedHashSet)6 AggregatingAttributeMapper (com.google.devtools.build.lib.packages.AggregatingAttributeMapper)5 Package (com.google.devtools.build.lib.packages.Package)5 Artifact (com.google.devtools.build.lib.actions.Artifact)4 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)4