FiloSottile / homebrew-musl-cross

Homebrew Formula for static-friendly musl-based GCC macOS-to-Linux cross-compilers
https://blog.filippo.io/easy-windows-and-linux-cross-compilers-for-macos/
ISC License
547 stars 45 forks source link

feat: add riscv64/s390x/i386/i686 targets #49

Closed YOU54F closed 1 month ago

YOU54F commented 3 months ago

Add support for additional targets

All built fine on my machine

ProductName: macOS ProductVersion: 14.3.1 BuildVersion: 23D60

bar mips64el which requires xcode 15.1 (I'm currently running xcode 15.0)

(was trying to build cross musl targets for multiple rust platforms I am targeting)

Tested with cargo, for compiling rust apps from macos.

Requires rustup target adding, before compiling rust app

rustup target add x86_64-unknown-linux-musl

Here is what my .cargo/config looks like

[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-musl-gcc"
[target.arm-unknown-linux-musleabihf]
linker = "arm-linux-musleabihf-gcc"
[target.i686-unknown-linux-musl]
linker = "i686-linux-musl-gcc"
[target.i586-unknown-linux-musl]
linker = "i586-linux-musl-gcc"
[target.powerpc64le-unknown-linux-musl]
linker = "powerpc64le-linux-musl-gcc"
[target.s390x-unknown-linux-musl]
linker = "s390x-linux-musl-gcc"
[target.riscv64-unknown-linux-musl]
linker = "riscv64-linux-musl-gcc"

Built rust apps appear to work on glibc based distros with no special flags required, at least with an initial smoke test of the binary

YOU54F commented 3 months ago

Hey,

so it seems like for rust, the following are tier3 targets

so there is no std library for them available to download with rustup target add <target>

Building for those targets with -Z build-std on nightly channel

cargo +nightly build --release --target riscv64gc-unknown-linux-musl -Zbuild-std

results in some errors

  = note: riscv64-linux-musl-gcc: error: crt1.o: No such file or directory
          riscv64-linux-musl-gcc: error: crti.o: No such file or directory
          riscv64-linux-musl-gcc: error: crtbegin.o: No such file or directory
          riscv64-linux-musl-gcc: error: crtend.o: No such file or directory
          riscv64-linux-musl-gcc: error: crtn.o: No such file or directory

They actually reside in, so can hopefully just add in a linker path to include.

/opt/homebrew/Cellar/musl-cross/0.9.9_2/libexec/riscv64-linux-musl/lib/ /opt/homebrew/Cellar/musl-cross/0.9.9_2/libexec/lib/gcc/riscv64-linux-musl/9.2.0

Using a simple hello world app setting "target-feature=+crt-static" returns a cannot find -lunwind error, even if we set panic=abort and follow guidance from min-sized-rust project

powerpc64le

[target.powerpc64le-unknown-linux-musl]
linker = "powerpc64le-linux-musl-gcc"
rustflags = [
"-L/opt/homebrew/Cellar/musl-cross/0.9.9_2/libexec/powerpc64le-linux-musl/lib",
"-L/opt/homebrew/Cellar/musl-cross/0.9.9_2/libexec/lib/gcc/powerpc64le-linux-musl/9.2.0",
"-C", "target-feature=-crt-static",
"-C","panic=abort"
]
cargo +nightly build --bin hello_world --release --target powerpc64le-unknown-linux-musl -Z build-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort

cargo +nightly build --bin hello_world --release --target powerpc64le-unknown-linux-musl -Z build-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort
    Finished `release` profile [optimized] target(s) in 10.46s

s390x

[target.s390x-unknown-linux-musl]
rustflags = [
"-L/opt/homebrew/Cellar/musl-cross/0.9.9_2/libexec/s390x-linux-musl/lib",
"-L/opt/homebrew/Cellar/musl-cross/0.9.9_2/libexec/lib/gcc/s390x-linux-musl/9.2.0",
"-C", "target-feature=-crt-static",
"-C","panic=abort"
]
linker = "s390x-linux-musl-gcc"
cargo +nightly build --bin hello_world --release --target s390x-unknown-linux-musl -Z build-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort

cargo +nightly build --bin hello_world --release --target s390x-unknown-linux-musl -Z build-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort
Finished `release` profile [optimized] target(s) in 10.81s

riscv64gc

[target.riscv64gc-unknown-linux-musl]
rustflags = [
"-L/opt/homebrew/Cellar/musl-cross/0.9.9_2/libexec/riscv64-linux-musl/lib",
"-L/opt/homebrew/Cellar/musl-cross/0.9.9_2/libexec/lib/gcc/riscv64-linux-musl/9.2.0",
"-C", "target-feature=-crt-static",
"-C","panic=abort"
]
linker = "riscv64-linux-musl-gcc"
cargo +nightly build --bin hello_world --release --target riscv64-unknown-linux-musl -Z build-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort

/opt/homebrew/Cellar/musl-cross/0.9.9_2/libexec/bin/../lib/gcc/riscv64-linux-musl/9.2.0/../../../../riscv64-linux-musl/bin/ld: -march=rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0: unsupported ISA subset `z'

This looks to be an error I've seen over with the cross project

https://github.com/cross-rs/cross/issues/1423#issuecomment-1922130249

YOU54F commented 3 months ago

So this is working nicely for me now

I elected to provide separate formulas for each target, so they can be bottled separately - link

This is the way its done on another cross compilation repo

https://github.com/messense/homebrew-macos-cross-toolchains

for me it makes sense, I'm likely to want to use these cross-compilers in CI systems, and I don't want to build from source for exotic targets.

Thanks for your work on the formula chap!

example cargo/config.toml build run cross compiling all the targets

YOU54F commented 1 month ago

closing, will maintain via my own fork