Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+crt-static is not respected on most of the targets #1711

Closed
petrochenkov opened this issue Mar 28, 2020 · 1 comment
Closed

+crt-static is not respected on most of the targets #1711

petrochenkov opened this issue Mar 28, 2020 · 1 comment

Comments

@petrochenkov
Copy link
Contributor

petrochenkov commented Mar 28, 2020

If I want to link statically to e.g. Android libc, the libc crate will ignore the +crt-static request and will link libc dynamically anyway.

The libc crate should respect user requests like this and link to libc statically in #[cfg(target_feature = "crt-static")] mode if the target provides a static version of libc at all.

The relevant code is here:

libc/src/unix/mod.rs

Lines 295 to 357 in bcbfeb5

cfg_if! {
if #[cfg(target_os = "l4re")] {
// required libraries for L4Re are linked externally, ATM
} else if #[cfg(feature = "std")] {
// cargo build, don't pull in anything extra as the libstd dep
// already pulls in all libs.
} else if #[cfg(target_env = "musl")] {
#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", kind = "static",
cfg(target_feature = "crt-static")))]
#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", cfg(not(target_feature = "crt-static"))))]
extern {}
} else if #[cfg(target_os = "emscripten")] {
#[link(name = "c")]
extern {}
} else if #[cfg(all(target_os = "netbsd",
feature = "rustc-dep-of-std",
target_vendor = "rumprun"))] {
// Since we don't use -nodefaultlibs on Rumprun, libc is always pulled
// in automatically by the linker. We avoid passing it explicitly, as it
// causes some versions of binutils to crash with an assertion failure.
#[link(name = "m")]
extern {}
} else if #[cfg(any(target_os = "macos",
target_os = "ios",
target_os = "android",
target_os = "openbsd"))] {
#[link(name = "c")]
#[link(name = "m")]
extern {}
} else if #[cfg(target_os = "haiku")] {
#[link(name = "root")]
#[link(name = "network")]
extern {}
} else if #[cfg(target_env = "newlib")] {
#[link(name = "c")]
#[link(name = "m")]
extern {}
} else if #[cfg(target_os = "hermit")] {
// no_default_libraries is set to false for HermitCore, so only a link
// to "pthread" needs to be added.
#[link(name = "pthread")]
extern {}
} else if #[cfg(target_env = "illumos")] {
#[link(name = "c")]
#[link(name = "m")]
extern {}
} else if #[cfg(target_os = "redox")] {
#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", kind = "static-nobundle",
cfg(target_feature = "crt-static")))]
#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", cfg(not(target_feature = "crt-static"))))]
extern {}
} else {
#[link(name = "c")]
#[link(name = "m")]
#[link(name = "rt")]
#[link(name = "pthread")]
extern {}
}
}

Static libc may require additionally linking some startup objects etc, and for some targets this is supported implicitly by rustc, but such support is not critical because the necessary linking can be done explicitly by the user with -C link-args and similar options.

@petrochenkov
Copy link
Contributor Author

I don't have resources to pursue the rustc part of this, unfortunately (rust-lang/rust#70501).

The workaround is to produce a staticlib and then link with whatever necessary manually.

Targets respecting +crt-static will have to be whitelisted manually.

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

No branches or pull requests

1 participant