Skip to content

Commit 34c7146

Browse files
oquenchilcopybara-github
authored andcommitted
Do location expansion in copts of objc_library
Fixes bazelbuild#13862 RELNOTES:none PiperOrigin-RevId: 409139722
1 parent 1cb1779 commit 34c7146

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ private static void checkDeprecated(String newApi, String oldApi, StarlarkSemant
11031103
* @param knownLabels List of known labels
11041104
* @return Immutable map with immutable collections as values
11051105
*/
1106-
private static ImmutableMap<Label, ImmutableCollection<Artifact>> makeLabelMap(
1106+
public static ImmutableMap<Label, ImmutableCollection<Artifact>> makeLabelMap(
11071107
Iterable<TransitiveInfoCollection> knownLabels) {
11081108
ImmutableMap.Builder<Label, ImmutableCollection<Artifact>> builder = ImmutableMap.builder();
11091109

src/main/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkInternal.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.ImmutableMap;
21+
import com.google.common.collect.Iterables;
2122
import com.google.devtools.build.docgen.annot.DocCategory;
2223
import com.google.devtools.build.lib.actions.Artifact;
24+
import com.google.devtools.build.lib.analysis.LocationExpander;
2325
import com.google.devtools.build.lib.analysis.TemplateVariableInfo;
2426
import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext;
2527
import com.google.devtools.build.lib.analysis.test.InstrumentedFilesInfo;
@@ -116,7 +118,18 @@ public Sequence<String> expandToolchainAndRuleContextVariables(
116118
ImmutableMap.<String, String>builder().putAll(starlarkRuleContext.var()).build();
117119
List<String> expandedFlags = new ArrayList<>();
118120
for (String flag : Sequence.cast(flags, String.class, "flags")) {
119-
String expandedFlag = expandFlag(flag, toolchainMap, starlarkRuleContextMap);
121+
@SuppressWarnings("unchecked")
122+
String expandedFlag =
123+
LocationExpander.withExecPaths(
124+
starlarkRuleContext.getRuleContext(),
125+
StarlarkRuleContext.makeLabelMap(
126+
Iterables.concat(
127+
starlarkRuleContext.getRuleContext().getPrerequisites("srcs"),
128+
starlarkRuleContext.getRuleContext().getPrerequisites("non_arc_srcs"),
129+
starlarkRuleContext.getRuleContext().getPrerequisites("hdrs"),
130+
starlarkRuleContext.getRuleContext().getPrerequisites("data"))))
131+
.expand(flag);
132+
expandedFlag = expandFlag(expandedFlag, toolchainMap, starlarkRuleContextMap);
120133
try {
121134
ShellUtils.tokenize(expandedFlags, expandedFlag);
122135
} catch (TokenizationException e) {

src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -2383,4 +2383,25 @@ public void testCompilationPrerequisitesHasHeaders() throws Exception {
23832383
.getOutputGroup(OutputGroupInfo.COMPILATION_PREREQUISITES)))
23842384
.contains("src bin/cc.h");
23852385
}
2386+
2387+
@Test
2388+
public void testCoptsLocationIsExpanded() throws Exception {
2389+
scratch.file(
2390+
"bin/BUILD",
2391+
"objc_library(",
2392+
" name = 'lib',",
2393+
" copts = ['$(rootpath lib1.m) $(location lib2.m) $(location data.data) $(execpath"
2394+
+ " header.h)'],",
2395+
" srcs = ['lib1.m'],",
2396+
" non_arc_srcs = ['lib2.m'],",
2397+
" data = ['data.data'],",
2398+
" hdrs = ['header.h'],",
2399+
")");
2400+
2401+
useConfiguration("--apple_platform_type=ios", "--cpu=ios_x86_64");
2402+
2403+
CppCompileAction compileA = (CppCompileAction) compileAction("//bin:lib", "lib1.o");
2404+
assertThat(compileA.compileCommandLine.getCopts())
2405+
.containsAtLeast("bin/lib1.m", "bin/lib2.m", "bin/data.data", "bin/header.h");
2406+
}
23862407
}

0 commit comments

Comments
 (0)