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

Can't build executables for musl #59

Closed
vi opened this issue Sep 17, 2020 · 5 comments · Fixed by rust-lang/rust#77086
Closed

Can't build executables for musl #59

vi opened this issue Sep 17, 2020 · 5 comments · Fixed by rust-lang/rust#77086

Comments

@vi
Copy link

vi commented Sep 17, 2020

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
Copy link
Author

vi commented Sep 17, 2020

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
Copy link
Contributor

ehuss commented Sep 18, 2020

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

@alexcrichton
Copy link
Member

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
Copy link
Contributor

ehuss commented Sep 23, 2020

Posted a fix at rust-lang/rust#77086.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Sep 24, 2020
Include libunwind in the rust-src component.

Some targets, such as musl, need the libunwind source to build the unwind crate (referenced [here](https://github.com/rust-lang/rust/blob/0da58007451a154da2480160429e1604a1f5f0ec/library/unwind/build.rs#L142)).

Fixes rust-lang/wg-cargo-std-aware#59
RalfJung added a commit to RalfJung/rust that referenced this issue Sep 25, 2020
Include libunwind in the rust-src component.

Some targets, such as musl, need the libunwind source to build the unwind crate (referenced [here](https://github.com/rust-lang/rust/blob/0da58007451a154da2480160429e1604a1f5f0ec/library/unwind/build.rs#L142)).

Fixes rust-lang/wg-cargo-std-aware#59
RalfJung added a commit to RalfJung/rust that referenced this issue Sep 25, 2020
Include libunwind in the rust-src component.

Some targets, such as musl, need the libunwind source to build the unwind crate (referenced [here](https://github.com/rust-lang/rust/blob/0da58007451a154da2480160429e1604a1f5f0ec/library/unwind/build.rs#L142)).

Fixes rust-lang/wg-cargo-std-aware#59
@ehuss
Copy link
Contributor

ehuss commented Oct 8, 2020

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

@ehuss ehuss closed this as completed Oct 8, 2020
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

Successfully merging a pull request may close this issue.

3 participants