Skip to content

Commit 7a750cc

Browse files
committed
Fix local execution of external dynamically linked cc_* targets
The fix for remote execution of external dynamically linked cc_* targets in 95a5bfd regressed local execution of such targets. This is fixed by emitting two rpaths in the case of an external target.
1 parent 34638d6 commit 7a750cc

File tree

3 files changed

+211
-36
lines changed

3 files changed

+211
-36
lines changed

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

Lines changed: 94 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.rules.cpp;
1515

16+
import static com.google.common.collect.ImmutableList.toImmutableList;
17+
1618
import com.google.common.base.Preconditions;
1719
import com.google.common.collect.ImmutableList;
1820
import com.google.common.collect.ImmutableSet;
@@ -40,7 +42,6 @@ public class LibrariesToLinkCollector {
4042
private final PathFragment toolchainLibrariesSolibDir;
4143
private final CppConfiguration cppConfiguration;
4244
private final CcToolchainProvider ccToolchainProvider;
43-
private final Artifact outputArtifact;
4445
private final boolean isLtoIndexing;
4546
private final PathFragment solibDir;
4647
private final Iterable<? extends LinkerInput> linkerInputs;
@@ -49,7 +50,8 @@ public class LibrariesToLinkCollector {
4950
private final Artifact thinltoParamFile;
5051
private final FeatureConfiguration featureConfiguration;
5152
private final boolean needWholeArchive;
52-
private final String rpathRoot;
53+
private final ImmutableList<String> potentialExecRoots;
54+
private final ImmutableList<String> rpathRoots;
5355
private final boolean needToolchainLibrariesRpath;
5456
private final Map<Artifact, Artifact> ltoMap;
5557
private final RuleErrorConsumer ruleErrorConsumer;
@@ -76,7 +78,6 @@ public LibrariesToLinkCollector(
7678
this.cppConfiguration = cppConfiguration;
7779
this.ccToolchainProvider = toolchain;
7880
this.toolchainLibrariesSolibDir = toolchainLibrariesSolibDir;
79-
this.outputArtifact = output;
8081
this.solibDir = solibDir;
8182
this.isLtoIndexing = isLtoIndexing;
8283
this.allLtoArtifacts = allLtoArtifacts;
@@ -106,22 +107,81 @@ public LibrariesToLinkCollector(
106107
// and the second could use $ORIGIN/../_solib_[arch]. But since this is a shared
107108
// artifact, both are symlinks to the same place, so
108109
// there's no *one* RPATH setting that fits all targets involved in the sharing.
109-
rpathRoot = ccToolchainProvider.getSolibDirectory() + "/";
110+
potentialExecRoots = ImmutableList.of();
111+
rpathRoots = ImmutableList.of(ccToolchainProvider.getSolibDirectory() + "/");
110112
} else {
111-
// When executed from within a runfiles directory, the binary lies under a path such as
112-
// target.runfiles/some_repo/pkg/file, whereas the solib directory is located under
113-
// target.runfiles/main_repo.
114-
PathFragment runfilesPath = outputArtifact.getRunfilesPath();
115-
String runfilesExecRoot;
116-
if (runfilesPath.startsWith(LabelConstants.EXTERNAL_RUNFILES_PATH_PREFIX)) {
117-
// runfilesPath is of the form ../some_repo/pkg/file, walk back some_repo/pkg and then
118-
// descend into the main workspace.
119-
runfilesExecRoot = "../".repeat(runfilesPath.segmentCount() - 2) + workspaceName + "/";
120-
} else {
121-
// runfilesPath is of the form pkg/file, walk back pkg to reach the main workspace.
122-
runfilesExecRoot = "../".repeat(runfilesPath.segmentCount() - 1);
113+
// The runtime location of the solib directory relative to the binary depends on four factors:
114+
//
115+
// * whether the binary is contained in the main repository or an external repository;
116+
// * whether the binary is executed directly or from a runfiles tree;
117+
// * whether the binary is staged as a symlink (sandboxed execution; local execution if the
118+
// binary is in the runfiles of another target) or a regular file (remote execution) - the
119+
// dynamic linker follows sandbox and runfiles symlinks into its location under the
120+
// unsandboxed execroot, which thus becomes the effective $ORIGIN;
121+
// * whether --experimental_sibling_repository_layout is enabled or not.
122+
//
123+
// The rpaths emitted into the binary thus have to cover the following cases (assuming that
124+
// the binary target is located in the pkg `pkg` and has name `file`) for the directory used
125+
// as $ORIGIN by the dynamic linker and the directory containing the solib directories:
126+
//
127+
// 1. main, direct, symlink:
128+
// $ORIGIN: $EXECROOT/pkg
129+
// solib root: $EXECROOT
130+
// 2. main, direct, regular file:
131+
// $ORIGIN: $EXECROOT/pkg
132+
// solib root: $EXECROOT/pkg/file.runfiles/main_repo
133+
// 3. main, runfiles, symlink:
134+
// $ORIGIN: $EXECROOT/pkg
135+
// solib root: $EXECROOT
136+
// 4. main, runfiles, regular file:
137+
// $ORIGIN: other_target.runfiles/main_repo/pkg
138+
// solib root: other_target.runfiles/main_repo
139+
// 5a. external, direct, symlink:
140+
// $ORIGIN: $EXECROOT/external/other_repo/pkg
141+
// solib root: $EXECROOT
142+
// 5b. external, direct, symlink, with --experimental_sibling_repository_layout:
143+
// $ORIGIN: $EXECROOT/../other_repo/pkg
144+
// solib root: $EXECROOT/../other_repo
145+
// 6a. external, direct, regular file:
146+
// $ORIGIN: $EXECROOT/external/other_repo/pkg
147+
// solib root: $EXECROOT/external/other_repo/pkg/file.runfiles/main_repo
148+
// 6b. external, direct, regular file, with --experimental_sibling_repository_layout:
149+
// $ORIGIN: $EXECROOT/../other_repo/pkg
150+
// solib root: $EXECROOT/../other_repo/pkg/file.runfiles/other_repo
151+
// 7a. external, runfiles, symlink:
152+
// $ORIGIN: $EXECROOT/external/other_repo/pkg
153+
// solib root: $EXECROOT
154+
// 7b. external, runfiles, symlink, with --experimental_sibling_repository_layout:
155+
// $ORIGIN: $EXECROOT/../other_repo/pkg
156+
// solib root: $EXECROOT/../other_repo
157+
// 8a. external, runfiles, regular file:
158+
// $ORIGIN: other_target.runfiles/some_repo/pkg
159+
// solib root: other_target.runfiles/main_repo
160+
// 8b. external, runfiles, regular file, with --experimental_sibling_repository_layout:
161+
// $ORIGIN: other_target.runfiles/some_repo/pkg
162+
// solib root: other_target.runfiles/some_repo
163+
//
164+
// Cases 1, 3, 4, 5, 7, and 8b are covered by an rpath that walks up the root relative path.
165+
// Case 8a is covered by walking up some_repo/pkg and then into main_repo.
166+
// Cases 2 and 6 are currently not covered as they would require an rpath containing the
167+
// binary filename, which may contain commas that would clash with the `-Wl` argument used to
168+
// pass the rpath to the linker.
169+
// TODO(#14600): Fix this by using `-Xlinker` instead of `-Wl`.
170+
ImmutableList.Builder<String> execRoots = ImmutableList.builder();
171+
// Handles cases 1, 3, 4, 5, and 7.
172+
execRoots.add("../".repeat(output.getRootRelativePath().segmentCount() - 1));
173+
if (output.getRunfilesPath().startsWith(LabelConstants.EXTERNAL_RUNFILES_PATH_PREFIX)
174+
&& output.getRoot().isLegacy()) {
175+
// Handles case 8a. The runfiles path is of the form ../some_repo/pkg/file and we need to
176+
// walk up some_repo/pkg and then down into main_repo.
177+
execRoots.add(
178+
"../".repeat(output.getRunfilesPath().segmentCount() - 2) + workspaceName + "/");
123179
}
124-
rpathRoot = runfilesExecRoot + ccToolchainProvider.getSolibDirectory() + "/";
180+
181+
potentialExecRoots = execRoots.build();
182+
rpathRoots = potentialExecRoots.stream()
183+
.map((execRoot) -> execRoot + ccToolchainProvider.getSolibDirectory() + "/")
184+
.collect(toImmutableList());
125185
}
126186

127187
ltoMap = generateLtoMap();
@@ -196,10 +256,10 @@ public CollectedLibrariesToLink collectLibrariesToLink() {
196256
// directory. In other words, given blaze-bin/my/package/binary, rpathRoot would be
197257
// "../../_solib_[arch]".
198258
if (needToolchainLibrariesRpath) {
199-
runtimeLibrarySearchDirectories.add(
200-
"../".repeat(outputArtifact.getRootRelativePath().segmentCount() - 1)
201-
+ toolchainLibrariesSolibName
202-
+ "/");
259+
for (String potentialExecRoot : potentialExecRoots) {
260+
runtimeLibrarySearchDirectories.add(
261+
potentialExecRoot + toolchainLibrariesSolibName + "/");
262+
}
203263
}
204264
if (isNativeDeps) {
205265
// We also retain the $ORIGIN/ path to solibs that are in _solib_<arch>, as opposed to
@@ -231,7 +291,9 @@ public CollectedLibrariesToLink collectLibrariesToLink() {
231291
NestedSetBuilder<String> allRuntimeLibrarySearchDirectories = NestedSetBuilder.linkOrder();
232292
// rpath ordering matters for performance; first add the one where most libraries are found.
233293
if (includeSolibDir) {
234-
allRuntimeLibrarySearchDirectories.add(rpathRoot);
294+
for (String rpathRoot : rpathRoots) {
295+
allRuntimeLibrarySearchDirectories.add(rpathRoot);
296+
}
235297
}
236298
allRuntimeLibrarySearchDirectories.addAll(rpathRootsForExplicitSoDeps.build());
237299
if (includeToolchainLibrariesSolibDir) {
@@ -346,17 +408,21 @@ private void addDynamicInputLinkOptions(
346408
// When all dynamic deps are built in transitioned configurations, the default solib dir is
347409
// not created. While resolving paths, the dynamic linker stops at the first directory that
348410
// does not exist, even when followed by "../". We thus have to normalize the relative path.
349-
String relativePathToRoot =
350-
rpathRoot + dotdots + libDir.relativeTo(commonParent).getPathString();
351-
String normalizedPathToRoot = PathFragment.create(relativePathToRoot).getPathString();
352-
rpathRootsForExplicitSoDeps.add(normalizedPathToRoot);
411+
for (String rpathRoot : rpathRoots) {
412+
String relativePathToRoot =
413+
rpathRoot + dotdots + libDir.relativeTo(commonParent).getPathString();
414+
String normalizedPathToRoot = PathFragment.create(relativePathToRoot).getPathString();
415+
rpathRootsForExplicitSoDeps.add(normalizedPathToRoot);
416+
}
353417

354418
// Unless running locally, libraries will be available under the root relative path, so we
355419
// should add that to the rpath as well.
356420
if (inputArtifact.getRootRelativePathString().startsWith("_solib_")) {
357421
PathFragment artifactPathUnderSolib = inputArtifact.getRootRelativePath().subFragment(1);
358-
rpathRootsForExplicitSoDeps.add(
359-
rpathRoot + artifactPathUnderSolib.getParentDirectory().getPathString());
422+
for (String rpathRoot : rpathRoots) {
423+
rpathRootsForExplicitSoDeps.add(
424+
rpathRoot + artifactPathUnderSolib.getParentDirectory().getPathString());
425+
}
360426
}
361427
}
362428

src/test/shell/bazel/cc_integration_test.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,4 +1506,92 @@ EOF
15061506
expect_log "\"PWD\": \"/proc/self/cwd\""
15071507
}
15081508

1509+
function external_cc_test_setup() {
1510+
cat >> WORKSPACE <<'EOF'
1511+
local_repository(
1512+
name = "other_repo",
1513+
path = "other_repo",
1514+
)
1515+
EOF
1516+
1517+
mkdir -p other_repo
1518+
touch other_repo/WORKSPACE
1519+
1520+
mkdir -p other_repo/lib
1521+
cat > other_repo/lib/BUILD <<'EOF'
1522+
cc_library(
1523+
name = "lib",
1524+
srcs = ["lib.cpp"],
1525+
hdrs = ["lib.h"],
1526+
visibility = ["//visibility:public"],
1527+
)
1528+
EOF
1529+
cat > other_repo/lib/lib.h <<'EOF'
1530+
void print_greeting();
1531+
EOF
1532+
cat > other_repo/lib/lib.cpp <<'EOF'
1533+
#include <cstdio>
1534+
void print_greeting() {
1535+
printf("Hello, world!\n");
1536+
}
1537+
EOF
1538+
1539+
mkdir -p other_repo/test
1540+
cat > other_repo/test/BUILD <<'EOF'
1541+
cc_test(
1542+
name = "test",
1543+
srcs = ["test.cpp"],
1544+
deps = ["//lib"],
1545+
)
1546+
EOF
1547+
cat > other_repo/test/test.cpp <<'EOF'
1548+
#include "lib/lib.h"
1549+
int main() {
1550+
print_greeting();
1551+
}
1552+
EOF
1553+
}
1554+
1555+
function test_external_cc_test_sandboxed() {
1556+
[ "$PLATFORM" != "windows" ] || return 0
1557+
1558+
external_cc_test_setup
1559+
1560+
bazel test \
1561+
--test_output=errors \
1562+
--strategy=sandboxed \
1563+
@other_repo//test >& $TEST_log || fail "Test should pass"
1564+
}
1565+
1566+
function test_external_cc_test_sandboxed_sibling_repository_layout() {
1567+
[ "$PLATFORM" != "windows" ] || return 0
1568+
1569+
external_cc_test_setup
1570+
1571+
bazel test \
1572+
--test_output=errors \
1573+
--strategy=sandboxed \
1574+
--experimental_sibling_repository_layout \
1575+
@other_repo//test >& $TEST_log || fail "Test should pass"
1576+
}
1577+
1578+
function test_external_cc_test_local() {
1579+
external_cc_test_setup
1580+
1581+
bazel test \
1582+
--test_output=errors \
1583+
--strategy=local \
1584+
@other_repo//test >& $TEST_log || fail "Test should pass"
1585+
}
1586+
1587+
function test_external_cc_test_local_sibling_repository_layout() {
1588+
external_cc_test_setup
1589+
1590+
bazel test \
1591+
--test_output=errors \
1592+
--strategy=local \
1593+
--experimental_sibling_repository_layout \
1594+
@other_repo//test >& $TEST_log || fail "Test should pass"
1595+
}
1596+
15091597
run_suite "cc_integration_test"

src/test/shell/bazel/remote/remote_execution_test.sh

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,14 +2907,7 @@ EOF
29072907
|| fail "Remote execution generated different result"
29082908
}
29092909

2910-
function test_external_cc_test() {
2911-
if [[ "$PLATFORM" == "darwin" ]]; then
2912-
# TODO(b/37355380): This test is disabled due to RemoteWorker not supporting
2913-
# setting SDKROOT and DEVELOPER_DIR appropriately, as is required of
2914-
# action executors in order to select the appropriate Xcode toolchain.
2915-
return 0
2916-
fi
2917-
2910+
function setup_external_cc_test() {
29182911
cat >> WORKSPACE <<'EOF'
29192912
local_repository(
29202913
name = "other_repo",
@@ -2958,10 +2951,38 @@ int main() {
29582951
print_greeting();
29592952
}
29602953
EOF
2954+
}
2955+
2956+
function test_external_cc_test() {
2957+
if [[ "$PLATFORM" == "darwin" ]]; then
2958+
# TODO(b/37355380): This test is disabled due to RemoteWorker not supporting
2959+
# setting SDKROOT and DEVELOPER_DIR appropriately, as is required of
2960+
# action executors in order to select the appropriate Xcode toolchain.
2961+
return 0
2962+
fi
2963+
2964+
setup_external_cc_test
2965+
2966+
bazel test \
2967+
--test_output=errors \
2968+
--remote_executor=grpc://localhost:${worker_port} \
2969+
@other_repo//test >& $TEST_log || fail "Test should pass"
2970+
}
2971+
2972+
function test_external_cc_test_sibling_repository_layout() {
2973+
if [[ "$PLATFORM" == "darwin" ]]; then
2974+
# TODO(b/37355380): This test is disabled due to RemoteWorker not supporting
2975+
# setting SDKROOT and DEVELOPER_DIR appropriately, as is required of
2976+
# action executors in order to select the appropriate Xcode toolchain.
2977+
return 0
2978+
fi
2979+
2980+
setup_external_cc_test
29612981

29622982
bazel test \
29632983
--test_output=errors \
29642984
--remote_executor=grpc://localhost:${worker_port} \
2985+
--experimental_sibling_repository_layout \
29652986
@other_repo//test >& $TEST_log || fail "Test should pass"
29662987
}
29672988

0 commit comments

Comments
 (0)