Skip to content

Commit 2482322

Browse files
fmeumcopybara-github
authored andcommitted
Do not force linking of default link libs in Unix toolchain
`libstdc++` (`libc++` on macOS) and `libm` are no longer linked in if none of their symbols are referenced, e.g., in a pure C target. Fixes #6221 Closes #19440. PiperOrigin-RevId: 565020923 Change-Id: I5e8e5d4b4e67d6eccddcfc6aab0c59005cc4553e
1 parent 1200ad2 commit 2482322

5 files changed

Lines changed: 61 additions & 13 deletions

File tree

src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/cc_shared_library_integration_test.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ function test_cc_test() {
7676

7777
function test_number_of_linked_libs() {
7878
binary=$(find . -name binary)
79-
expected_num_libs="6"
8079
num_libs=$(readelf -d $binary | grep NEEDED | wc -l)
81-
echo "$num_libs" | (grep -q "$expected_num_libs" \
82-
|| (echo "Expected no more than "$expected_num_libs" linked libraries but was $num_libs" && exit 1))
80+
echo "$num_libs" | (grep -q "$EXPECTED_NUM_LIBS" \
81+
|| (echo "Expected $EXPECTED_NUM_LIBS linked libraries but was $num_libs" && exit 1))
8382
}
8483

8584
test_shared_library_user_link_flags

src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/testenv.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515

1616
LDD_BINARY="ldd"
1717
RPATH="RUNPATH"
18+
EXPECTED_NUM_LIBS="5"

src/test/shell/bazel/cc_integration_test.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,4 +1928,28 @@ function test_find_optional_cpp_toolchain_not_present_with_toolchain_resolution(
19281928
assert_contains "Toolchain not found" bazel-bin/pkg/my_rule
19291929
}
19301930

1931+
function test_no_cpp_stdlib_linked_to_c_library() {
1932+
mkdir pkg
1933+
cat > pkg/BUILD <<'EOF'
1934+
cc_binary(
1935+
name = 'example',
1936+
srcs = ['example.c'],
1937+
)
1938+
EOF
1939+
cat > pkg/example.c <<'EOF'
1940+
int main() {}
1941+
EOF
1942+
1943+
bazel build //pkg:example &> "$TEST_log" || fail "Build failed"
1944+
if is_darwin; then
1945+
otool -L bazel-bin/pkg/example &> "$TEST_log" || fail "otool failed"
1946+
expect_log 'libc'
1947+
expect_not_log 'libc\+\+'
1948+
else
1949+
ldd bazel-bin/pkg/example &> "$TEST_log" || fail "ldd failed"
1950+
expect_log 'libc'
1951+
expect_not_log 'libstdc\+\+'
1952+
fi
1953+
}
1954+
19311955
run_suite "cc_integration_test"

third_party/BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,14 +583,14 @@ alias(
583583
actual = "@maven//:org_mockito_mockito_core",
584584
)
585585

586-
alias(
586+
filegroup(
587587
name = "turbine_direct",
588-
actual = "@maven//:com_google_turbine_turbine",
588+
srcs = ["turbine/turbine_direct.jar"],
589589
)
590590

591-
alias(
591+
java_import(
592592
name = "turbine",
593-
actual = "@maven//:com_google_turbine_turbine",
593+
jars = ["turbine/turbine_direct.jar"],
594594
)
595595

596596
java_library(

tools/cpp/unix_cc_configure.bzl

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,34 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
421421
), ":")
422422

423423
use_libcpp = darwin or bsd
424-
bazel_linklibs = "-lc++:-lm" if use_libcpp else "-lstdc++:-lm"
424+
is_as_needed_supported = _is_linker_option_supported(
425+
repository_ctx,
426+
cc,
427+
"-Wl,-no-as-needed",
428+
"-no-as-needed",
429+
)
430+
is_push_state_supported = _is_linker_option_supported(
431+
repository_ctx,
432+
cc,
433+
"-Wl,--push-state",
434+
"--push-state",
435+
)
436+
if use_libcpp:
437+
bazel_default_libs = ["-lc++", "-lm"]
438+
else:
439+
bazel_default_libs = ["-lstdc++", "-lm"]
440+
if is_as_needed_supported and is_push_state_supported:
441+
# Do not link against C++ standard libraries unless they are actually
442+
# used.
443+
# We assume that --push-state support implies --pop-state support.
444+
bazel_linklibs_elements = [
445+
arg
446+
for lib in bazel_default_libs
447+
for arg in ["-Wl,--push-state,-as-needed", lib, "-Wl,--pop-state"]
448+
]
449+
else:
450+
bazel_linklibs_elements = bazel_default_libs
451+
bazel_linklibs = ":".join(bazel_linklibs_elements)
425452
bazel_linkopts = ""
426453

427454
link_opts = split_escaped(get_env_var(
@@ -585,11 +612,8 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
585612
"%{conly_flags}": get_starlark_list(conly_opts),
586613
"%{link_flags}": get_starlark_list((
587614
["-fuse-ld=" + gold_or_lld_linker_path] if gold_or_lld_linker_path else []
588-
) + _add_linker_option_if_supported(
589-
repository_ctx,
590-
cc,
591-
"-Wl,-no-as-needed",
592-
"-no-as-needed",
615+
) + (
616+
["-Wl,-no-as-needed"] if is_as_needed_supported else []
593617
) + _add_linker_option_if_supported(
594618
repository_ctx,
595619
cc,

0 commit comments

Comments
 (0)