|
24 | 24 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
|
25 | 25 | import com.google.devtools.build.lib.collect.nestedset.NestedSet;
|
26 | 26 | import com.google.devtools.build.lib.packages.util.Crosstool.CcToolchainConfig;
|
| 27 | +import java.util.List; |
27 | 28 | import org.junit.Before;
|
28 | 29 | import org.junit.Test;
|
29 | 30 | import org.junit.runner.RunWith;
|
@@ -405,4 +406,125 @@ private void setupTestCcImportLoadedThroughMacro(boolean loadMacro) throws Excep
|
405 | 406 | getAnalysisMock().ccSupport().getMacroLoadStatement(loadMacro, "cc_import"),
|
406 | 407 | "cc_import(name='a', static_library='a.a')");
|
407 | 408 | }
|
| 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 | + } |
408 | 530 | }
|
0 commit comments