🚀 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.
cargo install --locked cargo-zigbuildYou can also install it using pip which will also install ziglang automatically:
pip install cargo-zigbuildWe also provide Docker images which has macOS SDK pre-installed in addition to cargo-zigbuild and Rust, for example to build for x86_64 macOS:
- Linux docker image (ghcr.io, Docker Hub):
docker run --rm -it -v $(pwd):/io -w /io ghcr.io/rust-cross/cargo-zigbuild \
cargo zigbuild --release --target x86_64-apple-darwin- Windows docker image (ghcr.io, Docker Hub):
docker run --rm -it -v ${pwd}:c:\io -w c:\io ghcr.io/rust-cross/cargo-zigbuild.windows `
cargo zigbuild --target x86_64-apple-darwin- Install zig following the official documentation,
on macOS, Windows and Linux you can also install zig from PyPI via
pip3 install ziglang - Install Rust target via rustup, for example,
rustup target add aarch64-unknown-linux-gnu - Run
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:
cargo zigbuild --target aarch64-unknown-linux-gnu.2.17Note
There are various caveats with the glibc version targeting feature:
- If you do not provide a
--target, Zig is not used and the command effectively runs a regularcargo build. - If you specify an invalid glibc version,
cargo zigbuildwill not relay the warning emitted fromzig ccabout the fallback version selected. - This feature does not necessarily match the behaviour of dynamically linking to a specific version of glibc on the build host.
- Version 2.32 can be specified, but runs on a host with only 2.31 available when it should instead abort with an error.
- Meanwhile specifying 2.33 will correctly be detected as incompatible when run on a host with glibc 2.31.
- Certain
RUSTFLAGSlike-C linkeropt-out of using Zig, while-L path/to/fileswill have Zig ignore-C target-feature=+crt-static. -C target-feature=+crt-staticfor statically linking to a glibc version is not supported (upstreamzig cclacks support)
cargo zigbuild supports a special universal2-apple-darwin target for building macOS universal2 binaries/libraries on Rust 1.64.0 and later.
rustup target add x86_64-apple-darwin
rustup target add aarch64-apple-darwin
cargo zigbuild --target universal2-apple-darwinNote
Note that Cargo
--message-formatoption doesn't work with universal2 target currently.
- Currently only Linux and macOS targets are supported, other target platforms can be added if you can make it work, pull requests are welcome.
- Only current Rust stable and nightly versions are regularly tested on CI, other versions may not work.
Known upstream zig issues:
- zig cc: parse
-targetand-mcpu/-march/-mtuneflags according to clang: Some Rust targets aren't recognized byzig cc, for examplearmv7-unknown-linux-gnueabihf, workaround by using-mcpu=genericand explicitly passing target features in #58 - ability to link against darwin frameworks (such as CoreFoundation) when cross compiling:
Set the
SDKROOTenvironment variable to a macOS SDK path to workaround it - zig misses some
compiler_rtfunctions that may lead to undefined symbol error for certain targets. See also: zig compiler-rt status. - CPU features are not passed to clang
This work is released under the MIT license. A copy of the license is provided in the LICENSE file.