Skip to content

Commit

Permalink
Implement static_link_cpp_runtimes feature
Browse files Browse the repository at this point in the history
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] bazelbuild#17391
[3] bazelbuild#14342
  • Loading branch information
Synss committed Aug 3, 2024
1 parent 914db36 commit 4e752d8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tools/cpp/unix_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -1062,13 +1073,30 @@ def _impl(ctx):
],
)

cpp_runtimes = ["-lstdc++", "-lc++"]
default_link_libs_feature = feature(
name = "default_link_libs",
enabled = True,
flag_sets = [
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"]),
],
),
],
)
Expand Down

0 comments on commit 4e752d8

Please sign in to comment.