Skip to content

Commit

Permalink
Expand make variables in defines of cc_* rules
Browse files Browse the repository at this point in the history
To match the documented behavior:

"List of defines to add to the compile line. Subject to "Make" variable
substitution and Bourne shell tokenization."
https://docs.bazel.build/versions/master/be/c-cpp.html#cc_binary.defines

Fixes bazelbuild#12482

Closes bazelbuild#12483.

PiperOrigin-RevId: 355126491
  • Loading branch information
erenon authored and copybara-github committed Feb 2, 2021
1 parent 20ef2ea commit 28fc8a1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
package com.google.devtools.build.lib.rules.cpp;

import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.devtools.build.lib.actions.ActionAnalysisMetadata;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.AliasProvider;
import com.google.devtools.build.lib.analysis.AnalysisEnvironment;
import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.MakeVariableSupplier;
Expand Down Expand Up @@ -525,7 +528,20 @@ public List<String> getNonTransitiveDefines() {

private List<String> getDefinesFromAttribute(String attr) {
List<String> defines = new ArrayList<>();
for (String define : ruleContext.getExpander().list(attr)) {

// collect labels that can be subsituted in defines
ImmutableMap.Builder<Label, ImmutableCollection<Artifact>> builder = ImmutableMap.builder();

if (ruleContext.attributes().has("deps", LABEL_LIST)) {
for (TransitiveInfoCollection current : ruleContext.getPrerequisites("deps")) {
builder.put(
AliasProvider.getDependencyLabel(current),
current.getProvider(FileProvider.class).getFilesToBuild().toList());
}
}

// tokenize defines and substitute make variables
for (String define : ruleContext.getExpander().withExecLocations(builder.build()).list(attr)) {
List<String> tokens = new ArrayList<>();
try {
ShellUtils.tokenize(tokens, define);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,34 @@ public void testIsolatedDefines() throws Exception {
.inOrder();
}

@Test
public void testExpandedDefinesAgainstDeps() throws Exception {
ConfiguredTarget expandedDefines =
scratchConfiguredTarget(
"expanded_defines",
"expand_deps",
"cc_library(name = 'expand_deps',",
" srcs = ['defines.cc'],",
" deps = ['//foo'],",
" defines = ['FOO=$(location //foo)'])");
assertThat(expandedDefines.get(CcInfo.PROVIDER).getCcCompilationContext().getDefines())
.containsExactly(
String.format("FOO=%s/foo/libfoo.a", getRuleContext(expandedDefines).getBinFragment()));
}

@Test
public void testExpandedDefinesAgainstSrcs() throws Exception {
ConfiguredTarget expandedDefines =
scratchConfiguredTarget(
"expanded_defines",
"expand_srcs",
"cc_library(name = 'expand_srcs',",
" srcs = ['defines.cc'],",
" defines = ['FOO=$(location defines.cc)'])");
assertThat(expandedDefines.get(CcInfo.PROVIDER).getCcCompilationContext().getDefines())
.containsExactly("FOO=expanded_defines/defines.cc");
}

@Test
public void testStartEndLib() throws Exception {
getAnalysisMock()
Expand Down

0 comments on commit 28fc8a1

Please sign in to comment.