Skip to content

Commit d9b8015

Browse files
committed
Auto merge of #26302 - aidanhs:aphs-fix-musl-libc, r=alexcrichton
musl may not be available on the target user's machine, and even if it is, we may not be able to find it because of how static libraries are searched for. Instead, use the liblibc archive created at rust compile time which already contains libc.a. --- To be honest, my brain is bending a bit at this point and I wonder if I'm doing something a bit stupid. Problem: building the libc crate with target musl. It says "could not find native static library `c`, perhaps an -L flag is missing?". Some pondering: the key problem is the way static archives are searched for (note that a musl build attempts to statically link to libc) - they aren't. There are three locations which are checked (including `$PREFIX/lib/rustlib/x86_64-unknown-linux-musl/lib`), but this does not include `$PREFIX/lib`...and it probably shouldn't - rustc is mimicking the way native lib generation works by forcing you to provide the path yourself. You can make it work `cargo rustc` with `-L native=/path/to/musl/lib`, but even if this went in a build script for the libc crate, it wouldn't work if musl isn't installed by the end user. I've sprinkled `not(test)` around but I've no idea if I've done it right. This patch allows `cargo build --target x86_64-unknown-linux-musl` to work on a crate with a dependency on libc, where the musl-enabled rust was compiled before this patch. I've not yet kicked off the long process to build a musl-enabled rust with this patch, so it might be broken there. Sorry for the rambling. r? @alexcrichton
2 parents 0d82fb5 + 52862e4 commit d9b8015

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/liblibc/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ pub use funcs::bsd43::*;
146146
#[link(name = "m")]
147147
extern {}
148148

149-
#[cfg(all(target_env = "musl", not(test)))]
149+
// When compiling rust with musl, statically include libc.a in liblibc.rlib.
150+
// A cargo build of the libc crate will therefore automatically pick up the
151+
// libc.a symbols because liblibc is transitively linked to by the stdlib.
152+
#[cfg(all(target_env = "musl", not(feature = "cargo-build"), not(test)))]
150153
#[link(name = "c", kind = "static")]
151154
extern {}
152155

0 commit comments

Comments
 (0)