-
Notifications
You must be signed in to change notification settings - Fork 154
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
[rust] Image needs to ship the x86_64-unknown-linux-musl target #272
Comments
Building the musl target requires building musl, and having it available at runtime. For this reason, I don't think it is a good idea. Additionally, the musl developers (including myself) pushed back on the Rust community for the whole "musl == static linking" thing they did, so that particular default is likely to be deprecated in the future. It is possible to build a glibc static binary with |
Ack! Though I'm not sure about "and having it available at runtime". Otherwise, the built binary wouldn't work in scratch which it does. |
is there an action we need to take here? @kaniini |
Signed-off-by: Batuhan Apaydin <[email protected]>
Probably just add a mention / example on the Rust image overview page?
You'll usually need to provide an explicit
This is a $ cargo init /example && cd /example
$ echo -e '[profile.release]\npanic = "abort"' >> Cargo.toml
# After swapping `src/main.rs` for the snippet below, build it:
RUSTFLAGS="-C target-feature=+crt-static" cargo run --release --target x86_64-unknown-linux-gnu #![no_std]
#![no_main]
#[no_mangle]
pub extern "C" fn main() -> isize {
const HELLO: &'static str = "Hello, world!\n";
unsafe { write(1, HELLO.as_ptr() as *const i8, HELLO.len()) };
0
}
#[link(name = "c")]
extern "C" {
fn write(fd: i32, buf: *const i8, count: usize) -> isize;
}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} } You'll get a lovely error like this: Error output
This can be fixed by adding near the top of the #![feature(rustc_private)]
extern crate libc; Still you will notice something is up due the static build size difference between glibc and musl targets: # rust:latest
# 699K if processed with `strip` command:
$ du -bh target/x86_64-unknown-linux-gnu/release/example
775K target/x86_64-unknown-linux-gnu/release/example
$ ldd target/x86_64-unknown-linux-gnu/release/example
statically linked
# rust:alpine
# Much smaller:
$ du -bh target/x86_64-unknown-linux-musl/release/example
25.7K target/x86_64-unknown-linux-musl/release/example
# musl ldd is a little different, this is equivalent to "statically linked":
$ ldd target/x86_64-unknown-linux-musl/release/example
/lib/ld-musl-x86_64.so.1: target/x86_64-unknown-linux-musl/release/example: Not a valid dynamic program |
As far as I can tell, the only way to build a fully statically linked rust binary for use with multi stage build and static is to use the build target of
x86_64-unknown-linux-musl
. I think this is a binary. I had to install it locally withrustup target install x86_64-unknown-linux-musl
.If I try to build this target right now I get errors.
The text was updated successfully, but these errors were encountered: