94 releases

0.19.5 Nov 30, 2024
0.19.3 Sep 24, 2024
0.19.1 Jul 9, 2024
0.18.3 Jan 26, 2024
0.7.1 Mar 31, 2022

#14 in Cargo plugins

Download history 17627/week @ 2024-08-25 18987/week @ 2024-09-01 19007/week @ 2024-09-08 15946/week @ 2024-09-15 20821/week @ 2024-09-22 15422/week @ 2024-09-29 20991/week @ 2024-10-06 20925/week @ 2024-10-13 17027/week @ 2024-10-20 17236/week @ 2024-10-27 17634/week @ 2024-11-03 18923/week @ 2024-11-10 19130/week @ 2024-11-17 21685/week @ 2024-11-24 24603/week @ 2024-12-01 19896/week @ 2024-12-08

86,563 downloads per month
Used in 6 crates (3 directly)

MIT license

99KB
2K SLoC

cargo-zigbuild

CI Crates.io docs.rs PyPI Docker Image

🚀 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.

Installation

cargo install --locked cargo-zigbuild

You can also install it using pip which will also install ziglang automatically:

pip install cargo-zigbuild

We 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:

docker run --rm -it -v $(pwd):/io -w /io messense/cargo-zigbuild \
  cargo zigbuild --release --target x86_64-apple-darwin
docker run --rm -it -v ${pwd}:c:\io -w c:\io messense/cargo-zigbuild.windows `
  cargo zigbuild --target x86_64-apple-darwin

[!NOTE]
Windows docker image can compile debug builds, but does NOT support cargo build --release for *-apple-darwin targets. You will get error: unable to run `strip`: program not found. If you know a solution to this, please open an issue and/or PR.

Packaging status

Usage

  1. Install zig following the official documentation, on macOS, Windows and Linux you can also install zig from PyPI via pip3 install ziglang
  2. Install Rust target via rustup, for example, rustup target add aarch64-unknown-linux-gnu
  3. Run cargo zigbuild, for example, cargo zigbuild --target aarch64-unknown-linux-gnu

Specify glibc version

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.17

[!NOTE] 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 regular cargo build.
  • If you specify an invalid glibc version, cargo zigbuild will not relay the warning emitted from zig cc about 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 RUSTFLAGS like -C linker opt-out of using Zig, while -L path/to/files will have Zig ignore -C target-feature=+crt-static.
  • -C target-feature=+crt-static for statically linking to a glibc version is not supported (upstream zig cc lacks support)

macOS universal2 target

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-darwin

Note

Note that Cargo --message-format option doesn't work with universal2 target currently.

Caveats

  1. Currently only Linux and macOS targets are supported, other target platforms can be added if you can make it work, pull requests are welcome.
  2. Only current Rust stable and nightly versions are regularly tested on CI, other versions may not work.

Known upstream zig issues:

  1. zig cc: parse -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 #58
  2. ability to link against darwin frameworks (such as CoreFoundation) when cross compiling: Set the SDKROOT environment variable to a macOS SDK path to workaround it
  3. zig misses some compiler_rt functions that may lead to undefined symbol error for certain targets. See also: zig compiler-rt status.
  4. CPU features are not passed to clang

License

This work is released under the MIT license. A copy of the license is provided in the LICENSE file.

Dependencies

~6–20MB
~243K SLoC