From fd727ec96d861573dcbad3249d727a94eff84789 Mon Sep 17 00:00:00 2001 From: plf Date: Tue, 16 Nov 2021 03:33:33 -0800 Subject: [PATCH] Do location expansion in copts of objc_library 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 #13862 RELNOTES:none PiperOrigin-RevId: 410209862 --- .../starlark/StarlarkRuleContext.java | 2 +- .../lib/rules/objc/ObjcStarlarkInternal.java | 17 ++++++++++++++- .../build/lib/rules/objc/ObjcLibraryTest.java | 21 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleContext.java index 07c52118dd8d4f..d6d9577f970a13 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleContext.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleContext.java @@ -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> makeLabelMap( + public static ImmutableMap> makeLabelMap( Iterable knownLabels) { ImmutableMap.Builder> builder = ImmutableMap.builder(); diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkInternal.java b/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkInternal.java index a6fb50770e2c2b..41f90471e9f2a7 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkInternal.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcStarlarkInternal.java @@ -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; @@ -116,7 +119,19 @@ public Sequence expandToolchainAndRuleContextVariables( ImmutableMap.builder().putAll(starlarkRuleContext.var()).build(); List 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) { diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java index bfdf27ba39f5a0..cc1322a4d2f07e 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcLibraryTest.java @@ -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"); + } }