From e624aff6d63dd6264d7ff56ec9650b7a1aeb3a36 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Wed, 2 Mar 2022 13:31:04 +0100 Subject: [PATCH] Normalize rpath entries to guard against missing default solib dir (#14929) When all dynamic deps in a build are built in transitioned configurations, the default solib dir is not created. However, while resolving paths, the dynamic linker stops at the first directory that does not exist, even when followed by `../`. Before this commit, all rpath entries would consist of the relative path to the default solib dir followed by the relative path to the particular library's solib dir. Thus, if the default solib dir was missing, the dynamic linker wouldn't resolve any of these paths. This commit ensures that the relative path entries are normalized and thus contain no references to non-existing directories assuming the normalized path itself exists. Work towards #13819. Closes #14660. PiperOrigin-RevId: 431671888 --- .../build/lib/rules/cpp/LibrariesToLinkCollector.java | 9 +++++++-- .../lib/rules/cpp/CcLibraryConfiguredTargetTest.java | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/LibrariesToLinkCollector.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/LibrariesToLinkCollector.java index 9e37b9737b9c15..a1bb3b99d64b2d 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/LibrariesToLinkCollector.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/LibrariesToLinkCollector.java @@ -331,8 +331,13 @@ private void addDynamicInputLinkOptions( commonParent = commonParent.getParentDirectory(); } - rpathRootsForExplicitSoDeps.add( - rpathRoot + dotdots + libDir.relativeTo(commonParent).getPathString()); + // When all dynamic deps are built in transitioned configurations, the default solib dir is + // not created. While resolving paths, the dynamic linker stops at the first directory that + // does not exist, even when followed by "../". We thus have to normalize the relative path. + String relativePathToRoot = + rpathRoot + dotdots + libDir.relativeTo(commonParent).getPathString(); + String normalizedPathToRoot = PathFragment.create(relativePathToRoot).getPathString(); + rpathRootsForExplicitSoDeps.add(normalizedPathToRoot); // Unless running locally, libraries will be available under the root relative path, so we // should add that to the rpath as well. diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java index 12e7f8846d655e..3de59f77d21281 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java @@ -2178,7 +2178,7 @@ public void testRpathRootIsAddedEvenWithTransitionedDepsOnly() throws Exception List linkArgv = action.getLinkCommandLine().arguments(); assertThat(linkArgv).contains("-Wl,-rpath,$ORIGIN/../_solib_k8/"); assertThat(Joiner.on(" ").join(linkArgv)) - .contains("-Wl,-rpath,$ORIGIN/../_solib_k8/../../../k8-fastbuild-ST-"); + .contains("-Wl,-rpath,$ORIGIN/../../../k8-fastbuild-ST-"); assertThat(Joiner.on(" ").join(linkArgv)) .contains("-L" + TestConstants.PRODUCT_NAME + "-out/k8-fastbuild-ST-"); assertThat(Joiner.on(" ").join(linkArgv)).containsMatch("-lST-[0-9a-f]+_transition_Slibdep2");