🚀 Help me to become a full-time open-source developer by sponsoring me on GitHub
Compile Cargo project with zig as linker for easier cross compiling.
bash
cargo install cargo-zigbuild
You can also install it using pip which will also install ziglang
automatically:
bash
pip install cargo-zigbuild
We also provide a Docker image which has macOS SDK pre-installed in addition to cargo-zigbuild and Rust, for example to build for x86_64 macOS:
bash
docker run --rm -it -v $(pwd):/io -w /io messense/cargo-zigbuild \
cargo zigbuild --release --target x86_64-apple-darwin
pip3 install ziglang
rustup target add aarch64-unknown-linux-gnu
cargo zigbuild
, for example, cargo zigbuild --target aarch64-unknown-linux-gnu
cargo zigbuild
supports passing glibc version in --target
option, for example,
to compile for glibc 2.17 with the aarch64-unknown-linux-gnu
target:
bash
cargo zigbuild --target aarch64-unknown-linux-gnu.2.17
cargo zigbuild
supports a special universal2-apple-darwin
target for building macOS universal2 binaries/libraries on Rust 1.64.0 and later.
bash
rustup target add x86_64-apple-darwin
rustup target add aarch64-apple-darwin
cargo zigbuild --target universal2-apple-darwin
Note
Note that Cargo
--message-format
option doesn't work with universal2 target currently.
Known upstream zig issues:
-target
and -mcpu
/-march
/-mtune
flags according to clang:
Some Rust targets aren't recognized by zig cc
, for example armv7-unknown-linux-gnueabihf
, workaround by using -mcpu=generic
and
explicitly passing target features in #58SDKROOT
environment variable to a macOS SDK path to workaround itThis work is released under the MIT license. A copy of the license is provided in the LICENSE file.