Skip to content

coolaj86/rust-hello-cross-zig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-hello-cross-zig

A simple Rust "Hello World" with the necessary config for cross-compiling with Zig

Adapted from https://actually.fyi/posts/zig-makes-rust-cross-compilation-just-work/.

This Doesn't Work Yet, except for these specific scenarios:

  • Linux x86_64 Host
    • Apple M1 Target
    • Apple x86_64 Target
git clone https://github.com/coolaj86/rust-hello-cross-zig.git

pushd ./rust-hello-cross-zig/

# Add the bin/zig-cc-* to the current PATH
export PATH="$PWD/bin:$PATH"

Assuming that you have rust and zig installed, try adding a few toolchains and building manually (.cargo/config.yaml is already correctly configured).

rustup target add aarch64-apple-darwin
CC="zig-cc-aarch64-macos-gnu" cargo build --target aarch64-apple-darwin

rustup target add x86_64-pc-windows-gnu
CC="zig-cc-x86_64-windows-gnu" cargo build --target x86_64-pc-windows-gnu

rustup target add x86_64-unknown-linux-musl
CC="zig-cc-x86_64-linux-musl" cargo build --target x86_64-unknown-linux-musl

Cross-compile a few more, if you so desire.

./bin/rustup-target-add-common

./bin/cargo-build-common

Install Rust & Zig

  1. Install webi

    curl https://webinstall.dev/ | bash
    
    export PATH="$HOME/.local/bin:$PATH"
  2. Install rustup, cargo, and rustc

    webi rust
    
    pathman add ~/.cargo/bin
    export PATH="$HOME/.cargo/bin:$PATH"
  3. Install zig

    webi zig
    
    export PATH="$HOME/.local/opt/zig:$PATH"

The Cross Toolchains

Here's a short table of some target host triples for toolchains that Rust & Zig appear to have in common.

rustup target list zig targets | jq -r '.libc[]'
aarch64-apple-darwin aarch64-macos-gnu
aarch64-pc-windows-msvc -
- aarch64-windows-gnu
aarch64-unknown-linux-gnu aarch64-linux-gnu
aarch64-unknown-linux-musl aarch64-linux-musl
wasm32-wasi wasm32-wasi-musl
x86_64-apple-darwin x86_64-macos-gnu
x86_64-pc-windows-gnu x86_64-windows-gnu
x86_64-pc-windows-msvc -
x86_64-unknown-linux-gnu x86_64-linux-gnu
x86_64-unknown-linux-musl x86_64-linux-musl

How to install Rust Toolchains:

#rustup target add <target-host-triple>
rustup target add aarch64-apple-darwin
rustup target add x86_64-unknown-linux-musl

How this was created

cargo new hello

mkdir -p .cargo/

.cargo/config.toml:

[target.aarch64-apple-darwin]
linker = "zig-cc-aarch64-macos-gnu"

[target.aarch64-unknown-linux-gnu]
linker = "zig-cc-aarch64-linux-gnu"

[target.aarch64-unknown-linux-musl]
linker = "zig-cc-aarch64-linux-musl"

[target.x86_64-apple-darwin]
linker = "zig-cc-x86_64-macos-gnu"

[target.x86_64-pc-windows-gnu]
linker = "zig-cc-x86_64-windows-gnu"

[target.x86_64-unknown-linux-gnu]
linker = "zig-cc-x86_64-linux-gnu"

[target.x86_64-unknown-linux-musl]
linker = "zig-cc-x86_64-linux-musl"

About

Rust "Hello World", cross-compiled with Zig

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published