Skip to content

Commit

Permalink
Do not force linking of default link libs in Unix toolchain
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
fmeum authored and copybara-github committed Sep 13, 2023
1 parent 1200ad2 commit 2482322
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ function test_cc_test() {

function test_number_of_linked_libs() {
binary=$(find . -name binary)
expected_num_libs="6"
num_libs=$(readelf -d $binary | grep NEEDED | wc -l)
echo "$num_libs" | (grep -q "$expected_num_libs" \
|| (echo "Expected no more than "$expected_num_libs" linked libraries but was $num_libs" && exit 1))
echo "$num_libs" | (grep -q "$EXPECTED_NUM_LIBS" \
|| (echo "Expected $EXPECTED_NUM_LIBS linked libraries but was $num_libs" && exit 1))
}

test_shared_library_user_link_flags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@

LDD_BINARY="ldd"
RPATH="RUNPATH"
EXPECTED_NUM_LIBS="5"
24 changes: 24 additions & 0 deletions src/test/shell/bazel/cc_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1928,4 +1928,28 @@ function test_find_optional_cpp_toolchain_not_present_with_toolchain_resolution(
assert_contains "Toolchain not found" bazel-bin/pkg/my_rule
}

function test_no_cpp_stdlib_linked_to_c_library() {
mkdir pkg
cat > pkg/BUILD <<'EOF'
cc_binary(
name = 'example',
srcs = ['example.c'],
)
EOF
cat > pkg/example.c <<'EOF'
int main() {}
EOF

bazel build //pkg:example &> "$TEST_log" || fail "Build failed"
if is_darwin; then
otool -L bazel-bin/pkg/example &> "$TEST_log" || fail "otool failed"
expect_log 'libc'
expect_not_log 'libc\+\+'
else
ldd bazel-bin/pkg/example &> "$TEST_log" || fail "ldd failed"
expect_log 'libc'
expect_not_log 'libstdc\+\+'
fi
}

run_suite "cc_integration_test"
8 changes: 4 additions & 4 deletions third_party/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -583,14 +583,14 @@ alias(
actual = "@maven//:org_mockito_mockito_core",
)

alias(
filegroup(
name = "turbine_direct",
actual = "@maven//:com_google_turbine_turbine",
srcs = ["turbine/turbine_direct.jar"],
)

alias(
java_import(
name = "turbine",
actual = "@maven//:com_google_turbine_turbine",
jars = ["turbine/turbine_direct.jar"],
)

java_library(
Expand Down
36 changes: 30 additions & 6 deletions tools/cpp/unix_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,34 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
), ":")

use_libcpp = darwin or bsd
bazel_linklibs = "-lc++:-lm" if use_libcpp else "-lstdc++:-lm"
is_as_needed_supported = _is_linker_option_supported(
repository_ctx,
cc,
"-Wl,-no-as-needed",
"-no-as-needed",
)
is_push_state_supported = _is_linker_option_supported(
repository_ctx,
cc,
"-Wl,--push-state",
"--push-state",
)
if use_libcpp:
bazel_default_libs = ["-lc++", "-lm"]
else:
bazel_default_libs = ["-lstdc++", "-lm"]
if is_as_needed_supported and is_push_state_supported:
# Do not link against C++ standard libraries unless they are actually
# used.
# We assume that --push-state support implies --pop-state support.
bazel_linklibs_elements = [
arg
for lib in bazel_default_libs
for arg in ["-Wl,--push-state,-as-needed", lib, "-Wl,--pop-state"]
]
else:
bazel_linklibs_elements = bazel_default_libs
bazel_linklibs = ":".join(bazel_linklibs_elements)
bazel_linkopts = ""

link_opts = split_escaped(get_env_var(
Expand Down Expand Up @@ -585,11 +612,8 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
"%{conly_flags}": get_starlark_list(conly_opts),
"%{link_flags}": get_starlark_list((
["-fuse-ld=" + gold_or_lld_linker_path] if gold_or_lld_linker_path else []
) + _add_linker_option_if_supported(
repository_ctx,
cc,
"-Wl,-no-as-needed",
"-no-as-needed",
) + (
["-Wl,-no-as-needed"] if is_as_needed_supported else []
) + _add_linker_option_if_supported(
repository_ctx,
cc,
Expand Down

0 comments on commit 2482322

Please sign in to comment.