Skip to content

Commit fbc1ec3

Browse files
committedNov 14, 2021
Add the default solib dir to the rpath for cc_imports with transitions
PR bazelbuild#14011 took care of cc_libraries, this fixes the same issue for cc_imports. Work towards bazelbuild#13819.
1 parent 286fb80 commit fbc1ec3

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
 

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

+7
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,13 @@ 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+
Preconditions.checkState(inputArtifact.getRootRelativePathString().startsWith("_solib_"));
339+
rpathRootsForExplicitSoDeps.add(
340+
rpathRoot +
341+
inputArtifact.getRootRelativePath().subFragment(1).getParentDirectory().getPathString());
335342
}
336343

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

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

+124
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,127 @@ 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(
425+
"bin/BUILD",
426+
"cc_binary(name='bin', deps=['//a:foo'])");
427+
428+
Artifact dynamicLibrary =
429+
target
430+
.get(CcInfo.PROVIDER)
431+
.getCcLinkingContext()
432+
.getLibraries()
433+
.getSingleton()
434+
.getResolvedSymlinkDynamicLibrary();
435+
Iterable<Artifact> dynamicLibrariesForRuntime =
436+
target
437+
.get(CcInfo.PROVIDER)
438+
.getCcLinkingContext()
439+
.getDynamicLibrariesForRuntime(/* linkingStatically= */ false);
440+
assertThat(artifactsToStrings(ImmutableList.of(dynamicLibrary)))
441+
.containsExactly("src a/libfoo.so");
442+
assertThat(artifactsToStrings(dynamicLibrariesForRuntime))
443+
.containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so");
444+
445+
ConfiguredTarget main = getConfiguredTarget("//bin:bin");
446+
Artifact mainBin = getBinArtifact("bin", main);
447+
CppLinkAction action = (CppLinkAction) getGeneratingAction(mainBin);
448+
List<String> linkArgv = action.getLinkCommandLine().arguments();
449+
assertThat(linkArgv).contains("-Wl,-rpath,$ORIGIN/../_solib_k8/_U_S_Sa_Cfoo___Ua");
450+
}
451+
452+
@Test
453+
public void testCcImportWithSharedLibraryWithTransitionAddsRpathEntry() throws Exception {
454+
AnalysisMock.get()
455+
.ccSupport()
456+
.setupCcToolchainConfig(
457+
mockToolsConfig,
458+
CcToolchainConfig.builder().withFeatures(CppRuleClasses.SUPPORTS_DYNAMIC_LINKER));
459+
useConfiguration("--cpu=k8");
460+
ConfiguredTarget target =
461+
scratchConfiguredTarget(
462+
"a",
463+
"foo",
464+
starlarkImplementationLoadStatement,
465+
"cc_import(name = 'foo', shared_library = 'libfoo.so')");
466+
467+
scratch.file(
468+
"bin/custom_transition.bzl",
469+
"def _custom_transition_impl(settings, attr):",
470+
" _ignore = settings, attr",
471+
"",
472+
" return {'//command_line_option:copt': ['-DFLAG']}",
473+
"",
474+
"custom_transition = transition(",
475+
" implementation = _custom_transition_impl,",
476+
" inputs = [],",
477+
" outputs = ['//command_line_option:copt'],",
478+
")",
479+
"",
480+
"def _apply_custom_transition_impl(ctx):",
481+
" cc_infos = []",
482+
" for dep in ctx.attr.deps:",
483+
" cc_infos.append(dep[CcInfo])",
484+
" merged_cc_info = cc_common.merge_cc_infos(cc_infos = cc_infos)",
485+
" return merged_cc_info",
486+
"",
487+
"apply_custom_transition = rule(",
488+
" implementation = _apply_custom_transition_impl,",
489+
" attrs = {",
490+
" '_whitelist_function_transition': attr.label(",
491+
" default = '//tools/allowlists/function_transition_allowlist',",
492+
" ),",
493+
" 'deps': attr.label_list(cfg = custom_transition),",
494+
" },",
495+
")");
496+
scratch.overwriteFile(
497+
"tools/allowlists/function_transition_allowlist/BUILD",
498+
"package_group(",
499+
" name = 'function_transition_allowlist',",
500+
" packages = ['//...'],",
501+
")");
502+
scratch.file(
503+
"bin/BUILD",
504+
"load(':custom_transition.bzl', 'apply_custom_transition')",
505+
"cc_library(name='lib', deps=['//a:foo'])",
506+
"apply_custom_transition(name='transitioned_lib', deps=[':lib'])",
507+
"cc_binary(name='bin', deps=[':transitioned_lib'])");
508+
509+
Artifact dynamicLibrary =
510+
target
511+
.get(CcInfo.PROVIDER)
512+
.getCcLinkingContext()
513+
.getLibraries()
514+
.getSingleton()
515+
.getResolvedSymlinkDynamicLibrary();
516+
Iterable<Artifact> dynamicLibrariesForRuntime =
517+
target
518+
.get(CcInfo.PROVIDER)
519+
.getCcLinkingContext()
520+
.getDynamicLibrariesForRuntime(/* linkingStatically= */ false);
521+
assertThat(artifactsToStrings(ImmutableList.of(dynamicLibrary)))
522+
.containsExactly("src a/libfoo.so");
523+
assertThat(artifactsToStrings(dynamicLibrariesForRuntime))
524+
.containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so");
525+
526+
ConfiguredTarget main = getConfiguredTarget("//bin:bin");
527+
Artifact mainBin = getBinArtifact("bin", main);
528+
CppLinkAction action = (CppLinkAction) getGeneratingAction(mainBin);
529+
List<String> linkArgv = action.getLinkCommandLine().arguments();
530+
assertThat(linkArgv).contains("-Wl,-rpath,$ORIGIN/../_solib_k8/_U_S_Sa_Cfoo___Ua");
531+
}
408532
}

0 commit comments

Comments
 (0)
Please sign in to comment.