Skip to content

feat: add CXXSTDLIB_STATIC to link the C++ stdlib statically from outside - #1957

Merged
NobodyXu merged 1 commit into
rust-lang:mainfrom
waltuov:feat/cxxstdlib-static-env
Sep 27, 2026
Merged

NobodyXu merged 1 commit into
rust-lang:mainfrom
waltuov:feat/cxxstdlib-static-env

Conversation

@waltuov

@waltuov waltuov commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #1955 for the third point of #1856.

Problem

cpp_link_stdlib_static can only be turned on by the crate itself. When a crate sets cpp_link_stdlib, CXXSTDLIB is ignored as well. So a user can't make such a crate link the C++ standard library statically without patching it.

Change

A new environment variable, CXXSTDLIB_STATIC, as discussed in #1856.

  • When set, every Build links the C++ stdlib as if cpp_link_stdlib_static(true) were set, including crates that set it to false.
  • The stdlib name is still chosen as before (cpp_link_stdlib, then CXXSTDLIB, then the default). cpp_link_stdlib(None) still links nothing.
  • It is read with the usual target prefixes, like CXXSTDLIB, for example CXXSTDLIB_STATIC_x86_64_unknown_linux_gnu or HOST_CXXSTDLIB_STATIC/TARGET_CXXSTDLIB_STATIC. A more specific variable wins, so CXXSTDLIB_STATIC_<target>=0 turns it off for one target.
  • Like CRATE_CC_NO_DEFAULTS, it can only turn the option on. Values follow the same rule as CC_FORCE_DISABLE: on unless set to "", "0", "no" or "false". The check is shared with get_env_boolean.
  • The output is the same as with cpp_link_stdlib_static(true): static:-bundle=<stdlib> once per Build, and static= on wasm32, pauthtest and Apple.
  • Nothing changes unless the variable is set.

Validation

  • New test cxxstdlib_static_env_metadata (same subprocess pattern as cpp_link_stdlib_static_metadata, explicit target and host). It covers unset and false values, the plain and prefixed forms, a cross-compile with TARGET_CXXSTDLIB_STATIC, overriding cpp_link_stdlib_static(false), a target value taking precedence, cpp_link_stdlib(None), Apple keeping static=, and the rerun-if-env-changed lines.
  • The test fails without the src/lib.rs change.
  • Checked on Windows (MSVC) with cargo test, cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings (also with --features cc-test/test_cuda) and cargo test -p cc-test. Linux, MinGW and macOS linking weren't run end to end. CI doesn't set the variable, so cc-test's Linux and CUDA blocks are unaffected.

Limitations

  • The variable applies to every crate in the build. Like cpp_link_stdlib_static(true), a build script that links the C++ stdlib from two separate Builds (for example a C++ build and a CUDA build) then fails in rustc. This is documented.
  • On Android the default stdlib is c++_shared, so this variable should go together with CXXSTDLIB=c++_static there.
  • A false value doesn't turn static linking off for crates that ask for it. If you'd rather have the variable decide both ways, that's a small change.

@NobodyXu NobodyXu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

The variable applies to every crate in the build.

Well, for that, we can potentially add support for crate prefix to every environment variable

@NobodyXu
NobodyXu merged commit c7b90ac into rust-lang:main Sep 27, 2026
82 checks passed
@waltuov
waltuov deleted the feat/cxxstdlib-static-env branch September 27, 2026 17:27
@lockfile-maintenance lockfile-maintenance Bot mentioned this pull request Oct 9, 2026
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants