Skip to content

Enabling +crt-static in a blanket way breaks dynamic libraries including proc macros #71651

Open
@petrochenkov

Description

@petrochenkov

This is pretty much rust-lang/cargo#7563 generalized, which was fixed in #69519, which I don't consider a correct or satisfactory solution.
#71586 may be a good preliminary reading.


If you are building something with Cargo and set -C target-feature=+crt-static through RUSTFLAGS, or crt-static is enabled by default through a target specification, it will be set for all crates during the build, including proc macros and cdylibs (and build scripts?).

In most cases this is not an intention when we are enabling crt-static.
In most cases the intention is to enable it for executables only.

So if enabling crt-static for executables (via env var or a target spec) also enables it for libraries, it usually results only in "collateral damage".
If the target doesn't support +crt-static for libaries, we just get an error like rust-lang/cargo#7563 reported.
If the target supports +crt-static for libraries, we get a very weird library which is unlikely to work when dynamically loaded as a proc macro crate (I didn't verify that though).

As a result, we need a way to enable crt-static for executables without collaterally damaging libaries.
Moreover, this way should be the default.


#71586 (comment) lists some possible alternatives of doing this.

  • Automatically enable -Ctarget-feature=-crt-static for proc macro crates or all libraries in Cargo (cc @ehuss), modifying RUSTFLAGS. Question: how to opt-out?
  • Introduce a new options -Ctarget-feature=+crt-static-dylib controlling static linking of libraries instead of -Ctarget-feature=-crt-static. It would almost never be used on Unix-like targets (not sure about windows-msvc and wasm).
  • Keep the existing meaning of crt-static, but introduce a new option -C disable-crt-static-for-dylibs or something. Cargo would then use it for proc macro crates or all libraries. Question: how to opt-out?
  • Ignore +crt-static for libraries if the target doesn't support it. This is fragile, musl actually supports it despite the current value of the flag of the musl target spec. If the flag is enabled, proc macros will break.
  • Ignore +crt-static for libraries always. It is almost never used on Unix-like targets (not sure about windows-msvc and wasm). +crt-static-dylib looks strictly better.
  • Have two crt-static defaults in target specs - one for executables and one for libraries. Solves one half of the problem, but explicit +crt-static in RUSTFLAGS will still cause collateral damage.

Metadata

Metadata

Assignees

Labels

A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions