Skip to content

Commit

Permalink
Do location expansion in copts of objc_library
Browse files Browse the repository at this point in the history
Roll forward with placing every TransitiveInfoCollection in a set so that duplicate entries are removed. Test has been modified to exercise the duplicate case.

Fixes bazelbuild#13862

RELNOTES:none
PiperOrigin-RevId: 410209862
  • Loading branch information
oquenchil authored and copybara-github committed Nov 16, 2021
1 parent e5b3536 commit fd727ec
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ private static void checkDeprecated(String newApi, String oldApi, StarlarkSemant
* @param knownLabels List of known labels
* @return Immutable map with immutable collections as values
*/
private static ImmutableMap<Label, ImmutableCollection<Artifact>> makeLabelMap(
public static ImmutableMap<Label, ImmutableCollection<Artifact>> makeLabelMap(
Iterable<TransitiveInfoCollection> knownLabels) {
ImmutableMap.Builder<Label, ImmutableCollection<Artifact>> builder = ImmutableMap.builder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.docgen.annot.DocCategory;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.LocationExpander;
import com.google.devtools.build.lib.analysis.TemplateVariableInfo;
import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext;
import com.google.devtools.build.lib.analysis.test.InstrumentedFilesInfo;
Expand Down Expand Up @@ -116,7 +119,19 @@ public Sequence<String> expandToolchainAndRuleContextVariables(
ImmutableMap.<String, String>builder().putAll(starlarkRuleContext.var()).build();
List<String> expandedFlags = new ArrayList<>();
for (String flag : Sequence.cast(flags, String.class, "flags")) {
String expandedFlag = expandFlag(flag, toolchainMap, starlarkRuleContextMap);

String expandedFlag =
LocationExpander.withExecPaths(
starlarkRuleContext.getRuleContext(),
StarlarkRuleContext.makeLabelMap(
ImmutableSet.copyOf(
Iterables.concat(
starlarkRuleContext.getRuleContext().getPrerequisites("srcs"),
starlarkRuleContext.getRuleContext().getPrerequisites("non_arc_srcs"),
starlarkRuleContext.getRuleContext().getPrerequisites("hdrs"),
starlarkRuleContext.getRuleContext().getPrerequisites("data")))))
.expand(flag);
expandedFlag = expandFlag(expandedFlag, toolchainMap, starlarkRuleContextMap);
try {
ShellUtils.tokenize(expandedFlags, expandedFlag);
} catch (TokenizationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2380,4 +2380,25 @@ public void testCompilationPrerequisitesHasHeaders() throws Exception {
.getOutputGroup(OutputGroupInfo.COMPILATION_PREREQUISITES)))
.contains("src bin/cc.h");
}

@Test
public void testCoptsLocationIsExpanded() throws Exception {
scratch.file(
"bin/BUILD",
"objc_library(",
" name = 'lib',",
" copts = ['$(rootpath lib1.m) $(location lib2.m) $(location data.data) $(execpath"
+ " header.h)'],",
" srcs = ['lib1.m'],",
" non_arc_srcs = ['lib2.m'],",
" data = ['data.data', 'lib2.m'],",
" hdrs = ['header.h'],",
")");

useConfiguration("--apple_platform_type=ios", "--cpu=ios_x86_64");

CppCompileAction compileA = (CppCompileAction) compileAction("//bin:lib", "lib1.o");
assertThat(compileA.compileCommandLine.getCopts())
.containsAtLeast("bin/lib1.m", "bin/lib2.m", "bin/data.data", "bin/header.h");
}
}

0 comments on commit fd727ec

Please sign in to comment.