Search in sources :

Example 1 with RawAttributeMapper

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

the class CompileOneDependencyTransformer method getInputLabels.

/** Returns all labels that are contained in direct compile time inputs of {@code rule}. */
private static Set<Label> getInputLabels(Rule rule) {
    RawAttributeMapper attributeMapper = RawAttributeMapper.of(rule);
    Set<Label> labels = new TreeSet<>();
    for (String attrName : attributeMapper.getAttributeNames()) {
        if (!attributeMapper.getAttributeDefinition(attrName).isDirectCompileTimeInput()) {
            continue;
        }
        // attribute xcode_config, which leads to test errors in Bazel tests.
        if (rule.isAttrDefined(attrName, BuildType.LABEL_LIST)) {
            labels.addAll(attributeMapper.getMergedValues(attrName, BuildType.LABEL_LIST));
        }
    }
    return labels;
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) TreeSet(java.util.TreeSet) Label(com.google.devtools.build.lib.cmdline.Label)

Example 2 with RawAttributeMapper

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

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

the class JvmConfigurationLoader method createFromRuntimeSuite.

// TODO(b/34175492): eventually the Jvm fragement will containg only the label of a java_runtime
// rule, and all of the configuration will be accessed using JavaRuntimeProvider.
private static Jvm createFromRuntimeSuite(ConfigurationEnvironment lookup, Rule javaRuntimeSuite, String cpu) throws InvalidConfigurationException, InterruptedException, NoSuchTargetException, NoSuchPackageException {
    Label javaRuntimeLabel = selectRuntime(javaRuntimeSuite, cpu);
    Target javaRuntimeTarget = lookup.getTarget(javaRuntimeLabel);
    if (javaRuntimeTarget == null) {
        return null;
    }
    if (!(javaRuntimeTarget instanceof Rule)) {
        throw new InvalidConfigurationException(String.format("Invalid java_runtime '%s'", javaRuntimeLabel));
    }
    Rule javaRuntimeRule = (Rule) javaRuntimeTarget;
    if (!javaRuntimeRule.getRuleClass().equals("java_runtime")) {
        throw new InvalidConfigurationException(String.format("Expected a java_runtime rule, was '%s'", javaRuntimeRule.getRuleClass()));
    }
    RawAttributeMapper attributes = RawAttributeMapper.of(javaRuntimeRule);
    PathFragment javaHomePath = defaultJavaHome(javaRuntimeLabel);
    if (attributes.isAttributeValueExplicitlySpecified("java_home")) {
        javaHomePath = javaHomePath.getRelative(attributes.get("java_home", Type.STRING));
    }
    return new Jvm(javaHomePath, javaRuntimeLabel);
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) Target(com.google.devtools.build.lib.packages.Target) Label(com.google.devtools.build.lib.cmdline.Label) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Rule(com.google.devtools.build.lib.packages.Rule) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)

Example 4 with RawAttributeMapper

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

the class RawAttributeMapperTest method testGetMergedValues.

@Test
public void testGetMergedValues() throws Exception {
    Rule rule = scratchRule("x", "myrule", "sh_binary(", "    name = 'myrule',", "    srcs = select({", "        '//conditions:a': ['a.sh', 'b.sh'],", "        '//conditions:b': ['b.sh', 'c.sh'],", "    }))");
    RawAttributeMapper rawMapper = RawAttributeMapper.of(rule);
    assertThat(rawMapper.getMergedValues("srcs", BuildType.LABEL_LIST)).containsExactly(Label.parseAbsolute("//x:a.sh"), Label.parseAbsolute("//x:b.sh"), Label.parseAbsolute("//x:c.sh")).inOrder();
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) Rule(com.google.devtools.build.lib.packages.Rule) Test(org.junit.Test)

Example 5 with RawAttributeMapper

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

the class RawAttributeMapperTest method testMergedValuesWithConcatenatedSelects.

@Test
public void testMergedValuesWithConcatenatedSelects() throws Exception {
    Rule rule = scratchRule("x", "myrule", "sh_binary(", "    name = 'myrule',", "    srcs = select({", "            '//conditions:a1': ['a1.sh'],", "            '//conditions:b1': ['b1.sh', 'another_b1.sh']})", "        + select({", "            '//conditions:a2': ['a2.sh'],", "            '//conditions:b2': ['b2.sh']})", "    )");
    RawAttributeMapper rawMapper = RawAttributeMapper.of(rule);
    assertThat(rawMapper.getMergedValues("srcs", BuildType.LABEL_LIST)).containsExactly(Label.parseAbsolute("//x:a1.sh"), Label.parseAbsolute("//x:b1.sh"), Label.parseAbsolute("//x:another_b1.sh"), Label.parseAbsolute("//x:a2.sh"), Label.parseAbsolute("//x:b2.sh")).inOrder();
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) Rule(com.google.devtools.build.lib.packages.Rule) Test(org.junit.Test)

Aggregations

RawAttributeMapper (com.google.devtools.build.lib.packages.RawAttributeMapper)16 Label (com.google.devtools.build.lib.cmdline.Label)10 Rule (com.google.devtools.build.lib.packages.Rule)9 Test (org.junit.Test)7 Attribute (com.google.devtools.build.lib.packages.Attribute)4 ConfigMatchingProvider (com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider)3 LinkedHashMap (java.util.LinkedHashMap)3 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)2 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)2 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)2 FileTarget (com.google.devtools.build.lib.packages.FileTarget)2 Target (com.google.devtools.build.lib.packages.Target)2 SkyKey (com.google.devtools.build.skyframe.SkyKey)2 Nullable (javax.annotation.Nullable)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArtifactOwner (com.google.devtools.build.lib.actions.ArtifactOwner)1 Root (com.google.devtools.build.lib.actions.Root)1 Dependency (com.google.devtools.build.lib.analysis.Dependency)1 InconsistentAspectOrderException (com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException)1