Skip to content

Instantly share code, notes, and snippets.

@reubenmiller
Last active May 20, 2024 09:56
Show Gist options
  • Save reubenmiller/376c6faf925250fe6895e5f8d16908ec to your computer and use it in GitHub Desktop.
Save reubenmiller/376c6faf925250fe6895e5f8d16908ec to your computer and use it in GitHub Desktop.
How to cross compile tedge using zig

How to cross compile a rust project using zig? wtf.

I was able to compile and cross compile the gnu and musl variants to all of our target triples :)

  1. Install the cargo-zigbuild and dependencies (requires python3)

    pip3 install ziglang
    cargo install cargo-zigbuild
  2. Cross compiling for all musl variants

    musl

    rustup target add x86_64-unknown-linux-musl
    cargo zigbuild --target x86_64-unknown-linux-musl
    
    rustup target add aarch64-unknown-linux-musl
    cargo zigbuild --target aarch64-unknown-linux-musl
    
    rustup target add armv7-unknown-linux-musleabihf
    cargo zigbuild --target armv7-unknown-linux-musleabihf
    
    rustup target add arm-unknown-linux-musleabihf
    cargo zigbuild --target arm-unknown-linux-musleabihf

    gnu

    rustup target add x86_64-unknown-linux-gnu
    cargo zigbuild --target x86_64-unknown-linux-gnu
    
    rustup target add aarch64-unknown-linux-gnu
    cargo zigbuild --target aarch64-unknown-linux-gnu
    
    rustup target add armv7-unknown-linux-gnueabihf
    cargo zigbuild --target armv7-unknown-linux-gnueabihf
    
    rustup target add arm-unknown-linux-gnueabihf
    cargo zigbuild --target arm-unknown-linux-gnueabihf

Working hosts

The following hosts were able to succesfully build/compile the above targets.

  • MacOS M1 (aarch64) natively
  • MacOS M1 (aarch64) in arm64 debian container
  • Linux (x86_64) GitHub Runner (ubuntu-20.04)

What didn't work

It seems that arm 32 bit targets don't work so well due to some compile issues with the ring dependency. The following targets failed to compile:

  • armv5te-unknown-linux-musleabi
  • armv5te-unknown-linux-gnueabi
  • arm-unknown-linux-musleabi
  • arm-unknown-linux-gnueabi

Troubleshooting tips

Compile errors when building on MacOS M1 native

I ran into an initial problem building natively on MacOS M1. It turned out that there was a conflict between homebrew installed rust package and the rust version installed via rustup. Solution was to uninstall the homebrew rust package.

Fix was provided by this post.

References

@didier-wenzek
Copy link

Thanks for this! It works like a charm!

@reubenmiller
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment