Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement static_link_cpp_runtimes feature #23204

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Synss
Copy link

@Synss Synss commented Aug 3, 2024

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] #17391
[3] #14342

Copy link

google-cla bot commented Aug 3, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added team-Rules-CPP Issues for C++ rules awaiting-review PR is awaiting review from an assigned reviewer labels Aug 3, 2024
@fmeum
Copy link
Collaborator

fmeum commented Aug 3, 2024

The test failure is legitimate: With this setup, it is no longer possible to influence the C++ standard library choice via user link flags. In particular, the logic in place to avoid linking it into pure C targets is no longer functional.

Maybe all user link libs should be gated behind this feature not being enabled? Or, perhaps better, we move the "as needed" logic into the toolchain and then filter out -libstdc++ and -libc++.

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
@Synss Synss force-pushed the static_link_cpp_runtimes branch from 5cc2cc3 to 4e752d8 Compare August 3, 2024 13:21
@Synss
Copy link
Author

Synss commented Aug 3, 2024

I thought of something like that when the clang tests failed. Thank you for the analysis!

Or, perhaps better, we move the "as needed" logic into the toolchain and then filter out -libstdc++ and -libc++

I love the idea! I think it's much better. Let's see if the tests pass, now.

I'm not sure what the preferred way would be for filtering out flags but I saw branching based on with_feature_set(features = ...)/with_feature_set(not_feature = ...) in windows_cc_toolchain_config.bzl as well.

@fmeum
Copy link
Collaborator

fmeum commented Aug 3, 2024

This looks good to me!

Could you add a next to the one that failed previously?

@Synss
Copy link
Author

Synss commented Aug 4, 2024

The tests won't pass because the cc_toolchain provided doesn't define static_runtime_lib (s. CCToolchainInfo). Setting CCToolchainInfo.static_runtime_lib to filegroup(name = "empty") would let the tests pass thanks to --linkopt '-l:libstdc++.a' but that's a hack.

I'm not sure how to go about it. Maybe I'm doing it wrong from the beginning. :/

@fmeum
Copy link
Collaborator

fmeum commented Aug 4, 2024

Could we add logic to the repo rule for the toolchain that finds the C++ standard library and rctx.symlinks it into the toolchain repository? Then we could add it to static_runtime_lib and this feature would be fully functional out of the box.

@Synss Synss marked this pull request as draft August 5, 2024 05:26
@comius comius added awaiting-user-response Awaiting a response from the author and removed awaiting-review PR is awaiting review from an assigned reviewer labels Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-user-response Awaiting a response from the author team-Rules-CPP Issues for C++ rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants