cross-rs / cross

“Zero setup” cross compilation and “cross testing” of Rust crates
Apache License 2.0
6.49k stars 365 forks source link

Wrong binary when compiling with cross to x86_64-unknown-linux-musl target (error: No such file or directory) #1329

Closed bonigarcia closed 1 year ago

bonigarcia commented 1 year ago

Checklist

Describe your issue

The source code of my Rust project is the following: https://github.com/SeleniumHQ/selenium/tree/trunk/rust

We build the binary for Windows, Linux, and macOS using the following workflow in GitHub Actions: https://github.com/SeleniumHQ/selenium/blob/trunk/.github/workflows/build-selenium-manager.yml

In Linux, we use cross and x86_64-unknown-linux-musl target. As you can see in the workflow before, the command to build the release is the following:

cross build --target x86_64-unknown-linux-musl --release

The workflow finishes correctly in GitHub Actions, and we can download the resulting binaries (for Windows, Linux, and macOS) from GitHub. For example: https://github.com/SeleniumHQ/selenium/actions/runs/6099198005

The problem is that the resulting Linux binary (e.g., selenium-manager_linux-x64) does not execute correctly. After downloading it, the error I faced when trying to execute it in Linux (Ubuntu 22.04.3 LTS) is the following:

boni@slimbook:~/Downloads$ ./selenium-manager 
bash: ./selenium-manager: No such file or directory

What target(s) are you cross-compiling for?

x86_64-unknown-linux-musl

Which operating system is the host (e.g computer cross is on) running?

What architecture is the host?

What container engine is cross using?

cross version

cross 0.2.5 (a599f20 2023-08-23)

Example

No response

Additional information / notes

No response

Emilgardis commented 1 year ago

this is expected behaviour with your current setup, you're explicitly telling rustc/the linker to "please don't statically link" with https://github.com/SeleniumHQ/selenium/blob/trunk@%7B2023-09-06T15:41:52Z%7D/.github/workflows/build-selenium-manager.yml#L35

Remove RUSTFLAGS=-Ctarget-feature=-crt-static and it should be fine. The actual error message is one of

# qemu reveals the fault
root@f0ae95a03065:/ ./selenium-manager 
qemu-x86_64: Could not open '/lib/ld-musl-x86_64.so.1': No such file or directory

or

root@f0ae95a03065:/ ./selenium-manager 
Error loading shared library libgcc_s.so.1: No such file or directory (needed by ./selenium-manager)

here's the wrong binary, a dynamically linked musl binary

root@f0ae95a03065:/ file selenium-manager
selenium-manager: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, stripped
bonigarcia commented 1 year ago

Yes, that was the problem. Thanks a lot, @Emilgardis!