Compile Cargo project with zig as linker
Project description
cargo-zigbuild
🚀 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 a Docker image 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
Usage
- 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
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 regularcargo build
.- If you specify an invalid glibc version,
cargo zigbuild
will not relay the warning emitted fromzig 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 (upstreamzig 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
- 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
-target
and-mcpu
/-march
/-mtune
flags according to clang: Some Rust targets aren't recognized byzig cc
, for examplearmv7-unknown-linux-gnueabihf
, workaround by using-mcpu=generic
and explicitly passing target features in #58 - 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 - zig misses some
compiler_rt
functions that may lead to undefined symbol error for certain targets. See also: zig compiler-rt status. - 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for cargo_zigbuild-0.19.2-py3-none-win_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34745912e6c76f12e3f4331f0eb4572b71c6e9dd6ee8e0f5e6af1b9623ac7a05 |
|
MD5 | 7fe9f2b0ee7146527c682f5f25959d00 |
|
BLAKE2b-256 | 474028146caf444675e2073c2f18d8cef4764379301b0e3823d9dd4867f3fbda |
Hashes for cargo_zigbuild-0.19.2-py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db4cef683a7cd16cc1ee7a8a5bff3c4cf6c9613360c460a0dd719cb27c289747 |
|
MD5 | f50ae96b2e9c35d1f680113da1a465ea |
|
BLAKE2b-256 | 48d7823ecf8b7bccd686fc846dec4de090a58e17b66db889714eb61edbd29488 |
Hashes for cargo_zigbuild-0.19.2-py3-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9bbdfe7b794a0ef429f98bc7b6a558eeca1a0591e8db2a9c672d9063c2c1a41c |
|
MD5 | 5f042e84247b611607dada3e6e673e59 |
|
BLAKE2b-256 | 2d57478f944e27489814ab2c86b3e4833b2ad0a3808fb1fe7ca4b61ea0132ae6 |
Hashes for cargo_zigbuild-0.19.2-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba7d3545e98a34ebd6f8ffe8a9cc6c9a54be1534e78a66e2e003e68201cc4878 |
|
MD5 | 195b4dd310243a75a1599b5ad0dfdbb2 |
|
BLAKE2b-256 | ac53972c01af20b7a01fd8811f95b8c0100c257982ea59a7437a6f072f6e46b9 |
Hashes for cargo_zigbuild-0.19.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5554bfdfa1c52fa3930afc3d05654e6f7ac44b1597c4dee517551fe39bdd0789 |
|
MD5 | 9753bae255b0d41a6876f701d6107044 |
|
BLAKE2b-256 | 928aa712ab3ac0b57c0bed82c571a75ad49e48a0a726cfa3a42ac3dce93a8fb8 |
Hashes for cargo_zigbuild-0.19.2-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b09df256120b022eda120f3d99c96f08c4832a11c9446ab48da1f27266246492 |
|
MD5 | cdfc18eb9370e9d1b6b344e862ac7c21 |
|
BLAKE2b-256 | 35168dbf877e9c1e82beb1a25271a9ff77894c5f966bf4b4019e7c4c18cff266 |
Hashes for cargo_zigbuild-0.19.2-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc3fde5d9c5d080c8f67ab8251ca0e1f9ce7ca89e62370b0ee93a1cbae70cca2 |
|
MD5 | b60eb382df789f9eb5e8341bd4bc5b18 |
|
BLAKE2b-256 | fc7b9754ca32e8e2b412bb7df4abe984a06f819b4837385426e83ac663ff2fb6 |
Hashes for cargo_zigbuild-0.19.2-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ef4a55f57247843aa2c645eb50ebea7aa26dc0b210979289579a3ce76ee4a53 |
|
MD5 | 29e786cb2bdc11f4f5537790815c1855 |
|
BLAKE2b-256 | e2ea513ca8bb50a2e89132a4def9a6f57149b68a38d0b331d3d92408defa43cb |