From 4e752d8c766c269a2c78e38465c8b231dfee55c2 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 adapts a solution proposed on GitHub.[3] [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 | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tools/cpp/unix_cc_toolchain_config.bzl b/tools/cpp/unix_cc_toolchain_config.bzl index 6f0baad25cc689..de4ab203d4fb56 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( @@ -1062,6 +1073,7 @@ def _impl(ctx): ], ) + cpp_runtimes = ["-lstdc++", "-lc++"] default_link_libs_feature = feature( name = "default_link_libs", enabled = True, @@ -1069,6 +1081,22 @@ def _impl(ctx): flag_set( actions = all_link_actions + lto_index_actions, flag_groups = [flag_group(flags = ctx.attr.link_libs)] if ctx.attr.link_libs else [], + with_features = [ + with_feature_set(not_features = ["static_link_cpp_runtimes"]), + ], + ), + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [flag_group( + flags = [ + link_lib + for link_lib in ctx.attr.link_libs + if link_lib not in cpp_runtimes + ], + )] if ctx.attr.link_libs else [], + with_features = [ + with_feature_set(features = ["static_link_cpp_runtimes"]), + ], ), ], )