Skip to content

Commit 6c04872

Browse files
committed
std: Prepare for linking to musl
This commit modifies the standard library and its dependencies to link correctly when built against MUSL. This primarily ensures that the right libraries are linked against and when they're linked against they're linked against statically.
1 parent d098517 commit 6c04872

File tree

7 files changed

+21
-6
lines changed

7 files changed

+21
-6
lines changed

src/liballoc/heap.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ mod imp {
211211
}
212212

213213
// -lpthread needs to occur after -ljemalloc, the earlier argument isn't enough
214-
#[cfg(all(not(windows), not(target_os = "android")))]
214+
#[cfg(all(not(windows),
215+
not(target_os = "android"),
216+
not(target_env = "musl")))]
215217
#[link(name = "pthread")]
216218
extern {}
217219

src/liblibc/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,15 @@ pub use funcs::bsd43::*;
140140

141141
// On NaCl, these libraries are static. Thus it would be a Bad Idea to link them
142142
// in when creating a test crate.
143-
#[cfg(not(any(windows, all(target_os = "nacl", test))))]
143+
#[cfg(not(any(windows, target_env = "musl", all(target_os = "nacl", test))))]
144144
#[link(name = "c")]
145145
#[link(name = "m")]
146146
extern {}
147147

148+
#[cfg(all(target_env = "musl", not(test)))]
149+
#[link(name = "c", kind = "static")]
150+
extern {}
151+
148152
// libnacl provides functions that require a trip through the IRT to work.
149153
// ie: _exit, mmap, nanosleep, etc. Anything that would otherwise require a trip
150154
// to the kernel.

src/libstd/dynamic_lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ mod tests {
125125
use path::Path;
126126

127127
#[test]
128-
#[cfg_attr(any(windows, target_os = "android"), ignore)] // FIXME #8818, #10379
128+
#[cfg_attr(any(windows,
129+
target_os = "android", // FIXME #10379
130+
target_env = "musl"), ignore)]
129131
fn test_loading_cosine() {
130132
// The math library does not need to be loaded since it is already
131133
// statically linked in

src/libstd/rt/libunwind.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,15 @@ pub type _Unwind_Exception_Cleanup_Fn =
9797
extern "C" fn(unwind_code: _Unwind_Reason_Code,
9898
exception: *mut _Unwind_Exception);
9999

100-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
100+
#[cfg(any(all(target_os = "linux", not(target_env = "musl")),
101+
target_os = "freebsd"))]
101102
#[link(name = "gcc_s")]
102103
extern {}
103104

105+
#[cfg(all(target_os = "linux", target_env = "musl", not(test)))]
106+
#[link(name = "unwind", kind = "static")]
107+
extern {}
108+
104109
#[cfg(any(target_os = "android", target_os = "openbsd"))]
105110
#[link(name = "gcc")]
106111
extern {}

src/libstd/rtdeps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern {}
2424
//
2525
// On Linux, librt and libdl are indirect dependencies via std,
2626
// and binutils 2.22+ won't add them automatically
27-
#[cfg(target_os = "linux")]
27+
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
2828
#[link(name = "dl")]
2929
#[link(name = "pthread")]
3030
extern {}

src/libstd/sys/unix/time.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ mod inner {
8282
// OpenBSD provide it via libc
8383
#[cfg(not(any(target_os = "android",
8484
target_os = "bitrig",
85-
target_os = "openbsd")))]
85+
target_os = "openbsd",
86+
target_env = "musl")))]
8687
#[link(name = "rt")]
8788
extern {}
8889

src/libstd/thread/local.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ mod imp {
364364
use sys_common::thread_local as os;
365365

366366
extern {
367+
#[linkage = "extern_weak"]
367368
static __dso_handle: *mut u8;
368369
#[linkage = "extern_weak"]
369370
static __cxa_thread_atexit_impl: *const ();

0 commit comments

Comments
 (0)