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;
}
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;
}
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);
}
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();
}
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();
}
Aggregations