Skip to content

Commit e898060

Browse files
Googlercopybara-github
authored andcommitted
Automated rollback of commit 14a0d83.
*** Reason for rollback *** Causes failures due to targets with commas in their names PiperOrigin-RevId: 470940885 Change-Id: I94631f541412a8650ea7598237738923b1de008f
1 parent aab90ef commit e898060

File tree

2 files changed

+17
-47
lines changed

2 files changed

+17
-47
lines changed

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

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.google.common.collect.ImmutableList;
1818
import com.google.common.collect.ImmutableSet;
1919
import com.google.common.collect.Iterables;
20-
import com.google.common.collect.Lists;
2120
import com.google.devtools.build.lib.actions.Artifact;
2221
import com.google.devtools.build.lib.analysis.RuleErrorConsumer;
2322
import com.google.devtools.build.lib.cmdline.LabelConstants;
@@ -41,6 +40,7 @@ public class LibrariesToLinkCollector {
4140
private final PathFragment toolchainLibrariesSolibDir;
4241
private final CppConfiguration cppConfiguration;
4342
private final CcToolchainProvider ccToolchainProvider;
43+
private final Artifact outputArtifact;
4444
private final boolean isLtoIndexing;
4545
private final PathFragment solibDir;
4646
private final Iterable<? extends LinkerInput> linkerInputs;
@@ -49,8 +49,7 @@ public class LibrariesToLinkCollector {
4949
private final Artifact thinltoParamFile;
5050
private final FeatureConfiguration featureConfiguration;
5151
private final boolean needWholeArchive;
52-
private final ImmutableList<String> potentialExecRoots;
53-
private final ImmutableList<String> rpathRoots;
52+
private final String rpathRoot;
5453
private final boolean needToolchainLibrariesRpath;
5554
private final Map<Artifact, Artifact> ltoMap;
5655
private final RuleErrorConsumer ruleErrorConsumer;
@@ -77,6 +76,7 @@ public LibrariesToLinkCollector(
7776
this.cppConfiguration = cppConfiguration;
7877
this.ccToolchainProvider = toolchain;
7978
this.toolchainLibrariesSolibDir = toolchainLibrariesSolibDir;
79+
this.outputArtifact = output;
8080
this.solibDir = solibDir;
8181
this.isLtoIndexing = isLtoIndexing;
8282
this.allLtoArtifacts = allLtoArtifacts;
@@ -106,13 +106,12 @@ public LibrariesToLinkCollector(
106106
// and the second could use $ORIGIN/../_solib_[arch]. But since this is a shared
107107
// artifact, both are symlinks to the same place, so
108108
// there's no *one* RPATH setting that fits all targets involved in the sharing.
109-
potentialExecRoots = ImmutableList.of();
110-
rpathRoots = ImmutableList.of(ccToolchainProvider.getSolibDirectory() + "/");
109+
rpathRoot = ccToolchainProvider.getSolibDirectory() + "/";
111110
} else {
112111
// When executed from within a runfiles directory, the binary lies under a path such as
113112
// target.runfiles/some_repo/pkg/file, whereas the solib directory is located under
114113
// target.runfiles/main_repo.
115-
PathFragment runfilesPath = output.getRunfilesPath();
114+
PathFragment runfilesPath = outputArtifact.getRunfilesPath();
116115
String runfilesExecRoot;
117116
if (runfilesPath.startsWith(LabelConstants.EXTERNAL_RUNFILES_PATH_PREFIX)) {
118117
// runfilesPath is of the form ../some_repo/pkg/file, walk back some_repo/pkg and then
@@ -122,22 +121,7 @@ public LibrariesToLinkCollector(
122121
// runfilesPath is of the form pkg/file, walk back pkg to reach the main workspace.
123122
runfilesExecRoot = "../".repeat(runfilesPath.segmentCount() - 1);
124123
}
125-
126-
// When the binary gets run through remote execution as the main executable (i.e., not as a
127-
// dependency of another target), it will not be able to find its libraries at
128-
// $ORIGIN/../../../_solib_[arch], as execution is not taking place inside the runfiles
129-
// directory. Solve this by adding ${executable}.runfiles/${workspace}/_solib_[arch] as an
130-
// alternative rpath root.
131-
PathFragment rootRelativePath = output.getRootRelativePath();
132-
potentialExecRoots =
133-
ImmutableList.of(
134-
rootRelativePath.getBaseName() + ".runfiles/" + workspaceName + "/",
135-
runfilesExecRoot);
136-
rpathRoots =
137-
ImmutableList.copyOf(
138-
Lists.transform(
139-
potentialExecRoots,
140-
(execRoot) -> execRoot + ccToolchainProvider.getSolibDirectory() + "/"));
124+
rpathRoot = runfilesExecRoot + ccToolchainProvider.getSolibDirectory() + "/";
141125
}
142126

143127
ltoMap = generateLtoMap();
@@ -212,10 +196,10 @@ public CollectedLibrariesToLink collectLibrariesToLink() {
212196
// directory. In other words, given blaze-bin/my/package/binary, rpathRoot would be
213197
// "../../_solib_[arch]".
214198
if (needToolchainLibrariesRpath) {
215-
for (String potentialExecRoot : potentialExecRoots) {
216-
runtimeLibrarySearchDirectories.add(
217-
potentialExecRoot + toolchainLibrariesSolibName + "/");
218-
}
199+
runtimeLibrarySearchDirectories.add(
200+
"../".repeat(outputArtifact.getRootRelativePath().segmentCount() - 1)
201+
+ toolchainLibrariesSolibName
202+
+ "/");
219203
}
220204
if (isNativeDeps) {
221205
// We also retain the $ORIGIN/ path to solibs that are in _solib_<arch>, as opposed to
@@ -247,9 +231,7 @@ public CollectedLibrariesToLink collectLibrariesToLink() {
247231
NestedSetBuilder<String> allRuntimeLibrarySearchDirectories = NestedSetBuilder.linkOrder();
248232
// rpath ordering matters for performance; first add the one where most libraries are found.
249233
if (includeSolibDir) {
250-
for (String rpathRoot : rpathRoots) {
251-
allRuntimeLibrarySearchDirectories.add(rpathRoot);
252-
}
234+
allRuntimeLibrarySearchDirectories.add(rpathRoot);
253235
}
254236
allRuntimeLibrarySearchDirectories.addAll(rpathRootsForExplicitSoDeps.build());
255237
if (includeToolchainLibrariesSolibDir) {
@@ -364,21 +346,17 @@ private void addDynamicInputLinkOptions(
364346
// When all dynamic deps are built in transitioned configurations, the default solib dir is
365347
// not created. While resolving paths, the dynamic linker stops at the first directory that
366348
// does not exist, even when followed by "../". We thus have to normalize the relative path.
367-
for (String rpathRoot : rpathRoots) {
368-
String relativePathToRoot =
369-
rpathRoot + dotdots + libDir.relativeTo(commonParent).getPathString();
370-
String normalizedPathToRoot = PathFragment.create(relativePathToRoot).getPathString();
371-
rpathRootsForExplicitSoDeps.add(normalizedPathToRoot);
372-
}
349+
String relativePathToRoot =
350+
rpathRoot + dotdots + libDir.relativeTo(commonParent).getPathString();
351+
String normalizedPathToRoot = PathFragment.create(relativePathToRoot).getPathString();
352+
rpathRootsForExplicitSoDeps.add(normalizedPathToRoot);
373353

374354
// Unless running locally, libraries will be available under the root relative path, so we
375355
// should add that to the rpath as well.
376356
if (inputArtifact.getRootRelativePathString().startsWith("_solib_")) {
377357
PathFragment artifactPathUnderSolib = inputArtifact.getRootRelativePath().subFragment(1);
378-
for (String rpathRoot : rpathRoots) {
379-
rpathRootsForExplicitSoDeps.add(
380-
rpathRoot + artifactPathUnderSolib.getParentDirectory().getPathString());
381-
}
358+
rpathRootsForExplicitSoDeps.add(
359+
rpathRoot + artifactPathUnderSolib.getParentDirectory().getPathString());
382360
}
383361
}
384362

src/test/java/com/google/devtools/build/lib/rules/cpp/CcLibraryConfiguredTargetTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,10 +2155,6 @@ public void testRpathAndLinkPathsWithoutTransitions() throws Exception {
21552155
Artifact mainBin = getBinArtifact("main", main);
21562156
CppLinkAction action = (CppLinkAction) getGeneratingAction(mainBin);
21572157
List<String> linkArgv = action.getLinkCommandLine().arguments();
2158-
assertThat(linkArgv)
2159-
.contains(
2160-
String.format(
2161-
"-Wl,-rpath,$ORIGIN/main.runfiles/%s/_solib_k8/", TestConstants.WORKSPACE_NAME));
21622158
assertThat(linkArgv).contains("-Wl,-rpath,$ORIGIN/../_solib_k8/");
21632159
assertThat(linkArgv)
21642160
.contains("-L" + TestConstants.PRODUCT_NAME + "-out/k8-fastbuild/bin/_solib_k8");
@@ -2219,10 +2215,6 @@ public void testRpathRootIsAddedEvenWithTransitionedDepsOnly() throws Exception
22192215
Artifact mainBin = getBinArtifact("main", main);
22202216
CppLinkAction action = (CppLinkAction) getGeneratingAction(mainBin);
22212217
List<String> linkArgv = action.getLinkCommandLine().arguments();
2222-
assertThat(linkArgv)
2223-
.contains(
2224-
String.format(
2225-
"-Wl,-rpath,$ORIGIN/main.runfiles/%s/_solib_k8/", TestConstants.WORKSPACE_NAME));
22262218
assertThat(linkArgv).contains("-Wl,-rpath,$ORIGIN/../_solib_k8/");
22272219
assertThat(Joiner.on(" ").join(linkArgv))
22282220
.contains("-Wl,-rpath,$ORIGIN/../../../k8-fastbuild-ST-");

0 commit comments

Comments
 (0)