diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/cc_shared_library_integration_test.sh b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/cc_shared_library_integration_test.sh index cf2769994b5c4a..535c0312ef4619 100755 --- a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/cc_shared_library_integration_test.sh +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/cc_shared_library_integration_test.sh @@ -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 diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/testenv.sh b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/testenv.sh index 5e946c88ca2052..9834a502efc51e 100644 --- a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/testenv.sh +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/testenv.sh @@ -15,3 +15,4 @@ LDD_BINARY="ldd" RPATH="RUNPATH" +EXPECTED_NUM_LIBS="5" \ No newline at end of file diff --git a/src/test/shell/bazel/cc_integration_test.sh b/src/test/shell/bazel/cc_integration_test.sh index e4fb0e2598b4ea..53c71d786ba756 100755 --- a/src/test/shell/bazel/cc_integration_test.sh +++ b/src/test/shell/bazel/cc_integration_test.sh @@ -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" diff --git a/third_party/BUILD b/third_party/BUILD index b7a75a1bcf34b6..4be737053dc2e0 100644 --- a/third_party/BUILD +++ b/third_party/BUILD @@ -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( diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl index df2034786b35de..6d6e7171edc98b 100644 --- a/tools/cpp/unix_cc_configure.bzl +++ b/tools/cpp/unix_cc_configure.bzl @@ -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( @@ -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,