Skip to content

Commit 8734ccf

Browse files
quvalcopybara-github
authored andcommitted
Add the default solib dir to the rpath for cc_imports with transitions
PR #14011 took care of cc_libraries. This fixes the same issue for cc_imports. Work towards #13819. Closes #14272. PiperOrigin-RevId: 427137675
1 parent 138a1cb commit 8734ccf

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ private void addDynamicInputLinkOptions(
332332

333333
rpathRootsForExplicitSoDeps.add(
334334
rpathRoot + dotdots + libDir.relativeTo(commonParent).getPathString());
335+
336+
// Unless running locally, libraries will be available under the root relative path, so we
337+
// should add that to the rpath as well.
338+
if (inputArtifact.getRootRelativePathString().startsWith("_solib_")) {
339+
PathFragment artifactPathUnderSolib = inputArtifact.getRootRelativePath().subFragment(1);
340+
rpathRootsForExplicitSoDeps.add(
341+
rpathRoot + artifactPathUnderSolib.getParentDirectory().getPathString());
342+
}
335343
}
336344

337345
librarySearchDirectories.add(inputArtifact.getExecPath().getParentDirectory().getPathString());

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

+122
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
2525
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
2626
import com.google.devtools.build.lib.packages.util.Crosstool.CcToolchainConfig;
27+
import java.util.List;
2728
import org.junit.Before;
2829
import org.junit.Test;
2930
import org.junit.runner.RunWith;
@@ -405,4 +406,125 @@ private void setupTestCcImportLoadedThroughMacro(boolean loadMacro) throws Excep
405406
getAnalysisMock().ccSupport().getMacroLoadStatement(loadMacro, "cc_import"),
406407
"cc_import(name='a', static_library='a.a')");
407408
}
409+
410+
@Test
411+
public void testCcImportWithSharedLibraryAddsRpathEntry() throws Exception {
412+
AnalysisMock.get()
413+
.ccSupport()
414+
.setupCcToolchainConfig(
415+
mockToolsConfig,
416+
CcToolchainConfig.builder().withFeatures(CppRuleClasses.SUPPORTS_DYNAMIC_LINKER));
417+
useConfiguration("--cpu=k8");
418+
ConfiguredTarget target =
419+
scratchConfiguredTarget(
420+
"a",
421+
"foo",
422+
starlarkImplementationLoadStatement,
423+
"cc_import(name = 'foo', shared_library = 'libfoo.so')");
424+
scratch.file("bin/BUILD", "cc_binary(name='bin', deps=['//a:foo'])");
425+
426+
Artifact dynamicLibrary =
427+
target
428+
.get(CcInfo.PROVIDER)
429+
.getCcLinkingContext()
430+
.getLibraries()
431+
.getSingleton()
432+
.getResolvedSymlinkDynamicLibrary();
433+
Iterable<Artifact> dynamicLibrariesForRuntime =
434+
target
435+
.get(CcInfo.PROVIDER)
436+
.getCcLinkingContext()
437+
.getDynamicLibrariesForRuntime(/* linkingStatically= */ false);
438+
assertThat(artifactsToStrings(ImmutableList.of(dynamicLibrary)))
439+
.containsExactly("src a/libfoo.so");
440+
assertThat(artifactsToStrings(dynamicLibrariesForRuntime))
441+
.containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so");
442+
443+
ConfiguredTarget main = getConfiguredTarget("//bin:bin");
444+
Artifact mainBin = getBinArtifact("bin", main);
445+
CppLinkAction action = (CppLinkAction) getGeneratingAction(mainBin);
446+
List<String> linkArgv = action.getLinkCommandLine().arguments();
447+
assertThat(linkArgv).contains("-Wl,-rpath,$ORIGIN/../_solib_k8/_U_S_Sa_Cfoo___Ua");
448+
}
449+
450+
@Test
451+
public void testCcImportWithSharedLibraryWithTransitionAddsRpathEntry() throws Exception {
452+
AnalysisMock.get()
453+
.ccSupport()
454+
.setupCcToolchainConfig(
455+
mockToolsConfig,
456+
CcToolchainConfig.builder().withFeatures(CppRuleClasses.SUPPORTS_DYNAMIC_LINKER));
457+
useConfiguration("--cpu=k8");
458+
ConfiguredTarget target =
459+
scratchConfiguredTarget(
460+
"a",
461+
"foo",
462+
starlarkImplementationLoadStatement,
463+
"cc_import(name = 'foo', shared_library = 'libfoo.so')");
464+
465+
scratch.file(
466+
"bin/custom_transition.bzl",
467+
"def _custom_transition_impl(settings, attr):",
468+
" _ignore = settings, attr",
469+
"",
470+
" return {'//command_line_option:copt': ['-DFLAG']}",
471+
"",
472+
"custom_transition = transition(",
473+
" implementation = _custom_transition_impl,",
474+
" inputs = [],",
475+
" outputs = ['//command_line_option:copt'],",
476+
")",
477+
"",
478+
"def _apply_custom_transition_impl(ctx):",
479+
" cc_infos = []",
480+
" for dep in ctx.attr.deps:",
481+
" cc_infos.append(dep[CcInfo])",
482+
" merged_cc_info = cc_common.merge_cc_infos(cc_infos = cc_infos)",
483+
" return merged_cc_info",
484+
"",
485+
"apply_custom_transition = rule(",
486+
" implementation = _apply_custom_transition_impl,",
487+
" attrs = {",
488+
" '_whitelist_function_transition': attr.label(",
489+
" default = '//tools/allowlists/function_transition_allowlist',",
490+
" ),",
491+
" 'deps': attr.label_list(cfg = custom_transition),",
492+
" },",
493+
")");
494+
scratch.overwriteFile(
495+
"tools/allowlists/function_transition_allowlist/BUILD",
496+
"package_group(",
497+
" name = 'function_transition_allowlist',",
498+
" packages = ['//...'],",
499+
")");
500+
scratch.file(
501+
"bin/BUILD",
502+
"load(':custom_transition.bzl', 'apply_custom_transition')",
503+
"cc_library(name='lib', deps=['//a:foo'])",
504+
"apply_custom_transition(name='transitioned_lib', deps=[':lib'])",
505+
"cc_binary(name='bin', deps=[':transitioned_lib'])");
506+
507+
Artifact dynamicLibrary =
508+
target
509+
.get(CcInfo.PROVIDER)
510+
.getCcLinkingContext()
511+
.getLibraries()
512+
.getSingleton()
513+
.getResolvedSymlinkDynamicLibrary();
514+
Iterable<Artifact> dynamicLibrariesForRuntime =
515+
target
516+
.get(CcInfo.PROVIDER)
517+
.getCcLinkingContext()
518+
.getDynamicLibrariesForRuntime(/* linkingStatically= */ false);
519+
assertThat(artifactsToStrings(ImmutableList.of(dynamicLibrary)))
520+
.containsExactly("src a/libfoo.so");
521+
assertThat(artifactsToStrings(dynamicLibrariesForRuntime))
522+
.containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so");
523+
524+
ConfiguredTarget main = getConfiguredTarget("//bin:bin");
525+
Artifact mainBin = getBinArtifact("bin", main);
526+
CppLinkAction action = (CppLinkAction) getGeneratingAction(mainBin);
527+
List<String> linkArgv = action.getLinkCommandLine().arguments();
528+
assertThat(linkArgv).contains("-Wl,-rpath,$ORIGIN/../_solib_k8/_U_S_Sa_Cfoo___Ua");
529+
}
408530
}

0 commit comments

Comments
 (0)