rust-lang / wg-cargo-std-aware

Repo for working on "std aware cargo"
133 stars 8 forks source link

Can't build executables for musl #59

Closed vi closed 3 years ago

vi commented 3 years ago

Usual target works:

$ grep panic Cargo.toml
panic = 'abort'

$ cargo build --release -Zbuild-std=panic_abort,std -Zbuild-std-features=panic_immediate_abort --target=x86_64-unknown-linux-gnu
...

$ strip target/x86_64-unknown-linux-gnu/release/koo -o koo
$ ls -lh koo
-rwxrwxr-x 1 vi vi 35K Sep 18 00:57 koo
$ ./koo
Hello, world!

But musl don't:

$ cargo build --release -Zbuild-std=panic_abort,std -Zbuild-std-features=panic_immediate_abort --target=x86_64-unknown-linux-musl
   Compiling compiler_builtins v0.1.35
   Compiling libc v0.2.77
   Compiling unwind v0.0.0 (/nix/rust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/unwind)
The following warnings were emitted during compilation:

warning: cc: error: ../../src/llvm-project/libunwind/src/Unwind-EHABI.cpp: No such file or directory
warning: cc: fatal error: no input files
warning: compilation terminated.

error: failed to run custom build command for `unwind v0.0.0 (/nix/rust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/unwind)`

Caused by:
  process didn't exit successfully: `/tmp/rustexp/koo/target/release/build/unwind-3ba23c349fd7fbd4/build-script-build` (exit code: 1)
  --- stdout
  cargo:rerun-if-changed=build.rs
  cargo:rustc-link-search=native=/tmp/rustexp/koo/target/x86_64-unknown-linux-musl/release/build/unwind-030b776e200555ef/out
  CC_x86_64-unknown-linux-musl = None
  CC_x86_64_unknown_linux_musl = None
  TARGET_CC = None
  CC = None
  CROSS_COMPILE = None
  CFLAGS_x86_64-unknown-linux-musl = None
  CFLAGS_x86_64_unknown_linux_musl = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  CARGO_CFG_TARGET_FEATURE = Some("fxsr,mmx,sse,sse2")
  running: "musl-gcc" "-Os" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I" "../../src/llvm-project/libunwind/include" "-std=c99" "-std=c++11" "-nostdinc++" "-fno-exceptions" "-fno-rtti" "-fstrict-aliasing" "-funwind-tables" "-fvisibility=hidden" "-D__LITTLE_ENDIAN__=1" "-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS" "-o" "/tmp/rustexp/koo/target/x86_64-unknown-linux-musl/release/build/unwind-030b776e200555ef/out/../../src/llvm-project/libunwind/src/Unwind-EHABI.o" "-c" "../../src/llvm-project/libunwind/src/Unwind-EHABI.cpp"
  cargo:warning=cc: error: ../../src/llvm-project/libunwind/src/Unwind-EHABI.cpp: No such file or directory
  cargo:warning=cc: fatal error: no input files
  cargo:warning=compilation terminated.
  exit code: 1

  --- stderr

  error occurred: Command "musl-gcc" "-Os" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I" "../../src/llvm-project/libunwind/include" "-std=c99" "-std=c++11" "-nostdinc++" "-fno-exceptions" "-fno-rtti" "-fstrict-aliasing" "-funwind-tables" "-fvisibility=hidden" "-D__LITTLE_ENDIAN__=1" "-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS" "-o" "/tmp/rustexp/koo/target/x86_64-unknown-linux-musl/release/build/unwind-030b776e200555ef/out/../../src/llvm-project/libunwind/src/Unwind-EHABI.o" "-c" "../../src/llvm-project/libunwind/src/Unwind-EHABI.cpp" with args "musl-gcc" did not execute successfully (status code exit code: 1).

warning: build failed, waiting for other jobs to finish...
error: could not find native static library `c`, perhaps an -L flag is missing?

error: aborting due to previous error

error: build failed

Why unwind? I specified that panic aborts. Can all the stack trickery be completely opted out?

$ rustc --version
rustc 1.48.0-nightly (285fc7d70 2020-09-16)
$ cargo --version
cargo 1.48.0-nightly (8777a6b1e 2020-09-15)
vi commented 3 years ago

Workaround by building staticlib for x86_64-unknown-linux-gnu with #[no_mangle] pub fn main, then linking with musl-gcc -static seems to work and produce slightly a larger, yet comparable-sized helloworld.

$ grep crate-type Cargo.toml
crate-type = ["staticlib"]

$ cat src/lib.rs
#[no_mangle]
pub fn main() -> i32 {
    println!("Hello, world!");
    0
}

$ cargo build --release -Zbuild-std=panic_abort,std -Zbuild-std-features=panic_immediate_abort --target=x86_64-unknown-linux-gnu
    Finished release [optimized] target(s) in 0.04s

$ musl-gcc -static target/x86_64-unknown-linux-gnu/release/libkoo.a -o koo_static
$ strip koo_static
$ file koo_static
koo_static: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped
$ ls -lh koo_static
-rwxrwxr-x 1 vi vi 38K Sep 18 01:13 koo_static
$ ./koo_static
Hello, world!

So in theory it should be possible to build std for musl with std-aware-cargo.

ehuss commented 3 years ago

@alexcrichton Should the rust-src component include a copy of the libunwind source from llvm?

alexcrichton commented 3 years ago

Ah yeah since some targets use it we should probably include it. I think it should be safe to include as it's pretty small and I believe it's self-contained.

ehuss commented 3 years ago

Posted a fix at https://github.com/rust-lang/rust/pull/77086.

ehuss commented 3 years ago

I forgot to close this. Feel free to reopen or open a new issue if it hasn't been resolved.