Description
Right now the libc crate links native libc with #[link(kind = "static")]
for two targets - musl and wasi.
kind = "static"
means that object files from native libc.a are bundled into liblibc.rlib and distributed with rustc toolchain in this form.
Then if user builds something with +crt-static
then the bundled object files are used, and if user builds with -crt-static
then they are "unbundled" and dynamic linking with libc.so available at user site happens.
Experience with windows-gnu targets (#67429) and porting rust to musl-native targets (#40113) showed that it's strongly preferable to use the native toolchain available at user site to using anything bundled with rustc toolchain, and that includes libc.a as well.
At the same time rustc historically provides more "self-contained" experience for selected targets (windows-gnu, musl, wasm), so we must continue shipping libc.a in some form in case the user doesn't have a native toolchain available.
That we means we need to fall back from using the native toolchain if it's available to using bundled libc.a (and other libraries) if it's not.
Such scheme is already used relatively successfully for windows-gnu, but not for musl and wasi.
So, the plan of the work should be roughly like this:
- Link libc as
kind = "static-nobundle"
instead ofkind = "static"
in the libc crate (Don't bundle static libc. libc#1327). - Modify rustbuild to distribute libc.a for musl (and whatever is its equivalent for wasi) similarly to how we distribute crt1.o objects for the same targets.
- Organize library search directories during linking in such way that they find the native toolchain first and then fall back to the rust toolchain's sysroot where the bundled libc.a resides (should be similar to windows-gnu: prefer system crt libraries if they are available #67429).
- Eliminate the "unbundling" logic from the compiler.
Once this is done #65760 should become fixed as well.
UPD: libunwind (https://github.com/rust-lang/rust/tree/master/src/libunwind) on musl also bundles libunwind.a
, it should probably have the same treatment.