Skip to content

Commit 46a8e09

Browse files
joelwilliamsoncopybara-github
authored andcommitted
Use data dependencies in Make variable substitution
This is a fix for bazelbuild#13930 Closes bazelbuild#13938. PiperOrigin-RevId: 430677775
1 parent 912e446 commit 46a8e09

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,19 +580,29 @@ private List<String> getDefinesFromAttribute(String attr) {
580580
List<String> defines = new ArrayList<>();
581581

582582
// collect labels that can be subsituted in defines
583-
ImmutableMap.Builder<Label, ImmutableCollection<Artifact>> builder = ImmutableMap.builder();
583+
Map<Label, ImmutableCollection<Artifact>> map = Maps.newLinkedHashMap();
584584

585585
if (ruleContext.attributes().has("deps", LABEL_LIST)) {
586586
for (TransitiveInfoCollection current : ruleContext.getPrerequisites("deps")) {
587-
builder.put(
587+
map.put(
588588
AliasProvider.getDependencyLabel(current),
589589
current.getProvider(FileProvider.class).getFilesToBuild().toList());
590590
}
591591
}
592592

593+
if (ruleContext.attributes().has("data", LABEL_LIST)) {
594+
for (TransitiveInfoCollection current : ruleContext.getPrerequisites("data")) {
595+
Label dataDependencyLabel = AliasProvider.getDependencyLabel(current);
596+
if (!map.containsKey(dataDependencyLabel)) {
597+
map.put(
598+
dataDependencyLabel,
599+
current.getProvider(FileProvider.class).getFilesToBuild().toList());
600+
}
601+
}
602+
}
593603
// tokenize defines and substitute make variables
594604
for (String define :
595-
ruleContext.getExpander().withExecLocations(builder.buildOrThrow()).list(attr)) {
605+
ruleContext.getExpander().withExecLocations(ImmutableMap.copyOf(map)).list(attr)) {
596606
List<String> tokens = new ArrayList<>();
597607
try {
598608
ShellUtils.tokenize(tokens, define);

src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,21 @@ public void testExpandedDefinesAgainstSrcs() throws Exception {
269269
.containsExactly("FOO=expanded_defines/defines.cc");
270270
}
271271

272+
@Test
273+
public void testExpandedDefinesAgainstData() throws Exception {
274+
scratch.file("data/BUILD", "filegroup(name = 'data', srcs = ['data.txt'])");
275+
ConfiguredTarget expandedDefines =
276+
scratchConfiguredTarget(
277+
"expanded_defines",
278+
"expand_srcs",
279+
"cc_library(name = 'expand_srcs',",
280+
" srcs = ['defines.cc'],",
281+
" data = ['//data'],",
282+
" defines = ['FOO=$(location //data)'])");
283+
assertThat(expandedDefines.get(CcInfo.PROVIDER).getCcCompilationContext().getDefines())
284+
.containsExactly("FOO=data/data.txt");
285+
}
286+
272287
@Test
273288
public void testStartEndLib() throws Exception {
274289
getAnalysisMock()

0 commit comments

Comments
 (0)