From 5cc2cc3d8045db5d38b05a1efa981d18d1f4a7df Mon Sep 17 00:00:00 2001 From: Mathias Laurin Date: Sat, 3 Aug 2024 11:30:09 +0200 Subject: [PATCH] Implement static_link_cpp_runtimes feature This patch implements the `static_link_cpp_runtimes` feature in `unix_cc_toolchain_config`. The feature is documented as a well-known feature[1] and was added to the toolchain[2] but not implemented. The patch follows the solution proposed on GitHub.[3] For the feature to work as expected, users have to remove the linker flag `"-lstdc++"` from `link_libs` (or `link_flags`) from the call to `unix_cc_toolchain_config`. [1] https://bazel.build/docs/cc-toolchain-config-reference#wellknown-features [2] https://github.com/bazelbuild/bazel/pull/17391 [3] https://github.com/bazelbuild/bazel/issues/14342 --- tools/cpp/unix_cc_toolchain_config.bzl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/cpp/unix_cc_toolchain_config.bzl b/tools/cpp/unix_cc_toolchain_config.bzl index 6f0baad25cc689..7581cc91bdda70 100644 --- a/tools/cpp/unix_cc_toolchain_config.bzl +++ b/tools/cpp/unix_cc_toolchain_config.bzl @@ -269,6 +269,17 @@ def _impl(ctx): static_link_cpp_runtimes_feature = feature( name = "static_link_cpp_runtimes", enabled = False, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.lto_index_for_executable, + ACTION_NAMES.lto_index_for_dynamic_library, + ], + flag_groups = [flag_group(flags = ["-static-libstdc++"])], + ), + ], ) default_compile_flags_feature = feature( @@ -1059,6 +1070,15 @@ def _impl(ctx): ), ], ), + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [flag_group(flags = ["-lstdc++"])], + with_features = [ + with_feature_set( + not_features = ["static_link_cpp_runtimes"], + ), + ], + ), ], )