Search in sources :

Example 1 with FileTarget

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

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

the class CompileOneDependencyTransformer method transformCompileOneDependency.

private Target transformCompileOneDependency(ExtendedEventHandler eventHandler, Target target) throws TargetParsingException, InterruptedException {
    if (!(target instanceof FileTarget)) {
        throw new TargetParsingException("--compile_one_dependency target '" + target.getLabel() + "' must be a file");
    }
    Rule result = null;
    Iterable<Rule> orderedRuleList = getOrderedRuleList(target.getPackage());
    for (Rule rule : orderedRuleList) {
        Set<Label> labels = getInputLabels(rule);
        if (listContainsFile(eventHandler, labels, target.getLabel(), Sets.<Label>newHashSet())) {
            if (rule.getRuleClassObject().isPreferredDependency(target.getName())) {
                result = rule;
                break;
            }
            if (result == null) {
                result = rule;
            }
        }
    }
    if (result == null) {
        throw new TargetParsingException("Couldn't find dependency on target '" + target.getLabel() + "'");
    }
    // If the rule has source targets, return it.
    if (!RawAttributeMapper.of(result).getMergedValues("srcs", BuildType.LABEL_LIST).isEmpty()) {
        return result;
    }
    // Try to find a rule in the same package that has 'result' as a dependency.
    for (Rule rule : orderedRuleList) {
        RawAttributeMapper attributes = RawAttributeMapper.of(rule);
        // We don't know which path to follow for configurable attributes, so skip them.
        if (attributes.isConfigurable("deps") || attributes.isConfigurable("srcs")) {
            continue;
        }
        RuleClass ruleClass = rule.getRuleClassObject();
        if (ruleClass.hasAttr("deps", BuildType.LABEL_LIST) && ruleClass.hasAttr("srcs", BuildType.LABEL_LIST)) {
            for (Label dep : attributes.get("deps", BuildType.LABEL_LIST)) {
                if (dep.equals(result.getLabel())) {
                    if (!attributes.get("srcs", BuildType.LABEL_LIST).isEmpty()) {
                        return rule;
                    }
                }
            }
        }
    }
    return result;
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) FileTarget(com.google.devtools.build.lib.packages.FileTarget) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule) RuleClass(com.google.devtools.build.lib.packages.RuleClass)

Aggregations

Label (com.google.devtools.build.lib.cmdline.Label)2 FileTarget (com.google.devtools.build.lib.packages.FileTarget)2 RawAttributeMapper (com.google.devtools.build.lib.packages.RawAttributeMapper)2 Rule (com.google.devtools.build.lib.packages.Rule)2 TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)1 NoSuchThingException (com.google.devtools.build.lib.packages.NoSuchThingException)1 OutputFile (com.google.devtools.build.lib.packages.OutputFile)1 RuleClass (com.google.devtools.build.lib.packages.RuleClass)1 Target (com.google.devtools.build.lib.packages.Target)1