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 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 messense/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 messense/cargo-zigbuild.windows `
cargo zigbuild --target x86_64-apple-darwin
[!NOTE]
Windows docker image can compile debug builds, but does NOT supportcargo build --release
for *-apple-darwin targets. You will geterror: unable to run `strip`: program not found
. If you know a solution to this, please open an issue and/or PR.
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
File details
Details for the file cargo_zigbuild-0.19.5-py3-none-win_arm64.whl
.
File metadata
- Download URL: cargo_zigbuild-0.19.5-py3-none-win_arm64.whl
- Upload date:
- Size: 1.3 MB
- Tags: Python 3, Windows ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | baf5f18df485ba1541ff4e6a779890660deaee3e66c9331fef9facbf3a57a93a |
|
MD5 | 4f76ffd9f18d7e3e31f9f8d79556088b |
|
BLAKE2b-256 | cc1e32172fbff32f18823981c3abafb09b91172b1b75160f0f2f4e9eaab729e7 |
File details
Details for the file cargo_zigbuild-0.19.5-py3-none-win_amd64.whl
.
File metadata
- Download URL: cargo_zigbuild-0.19.5-py3-none-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: Python 3, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f451f2d57cf287a4f95be691cbad8161fbb4bf3b709218cd3fb7ea6c4f5c39d |
|
MD5 | 2dd54e3c1586721a1a4e411571bf3ec2 |
|
BLAKE2b-256 | 4eb5bc5cad9607e314b8b5c8b2f03435fccdab2d1543ff1836882a6da36d0c64 |
File details
Details for the file cargo_zigbuild-0.19.5-py3-none-win32.whl
.
File metadata
- Download URL: cargo_zigbuild-0.19.5-py3-none-win32.whl
- Upload date:
- Size: 1.2 MB
- Tags: Python 3, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c0889fc9d60e3a66a58affd2b4aa729ba9f326618c61c272d24f4d6a397bd39 |
|
MD5 | 19b95feb6c9989848af85186e0affb4b |
|
BLAKE2b-256 | 3e2824d974e1908272f64a29a1820fe6a65e04b79ef45f008357e959f80fed67 |
File details
Details for the file cargo_zigbuild-0.19.5-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
.
File metadata
- Download URL: cargo_zigbuild-0.19.5-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
- Upload date:
- Size: 1.5 MB
- Tags: Python 3, manylinux: glibc 2.17+ ARMv7l
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2fa665c35878f91430596687652a4f5874b5feb134744bef0512bd5297bf7e11 |
|
MD5 | d2112094e28e277b4372153a21007c33 |
|
BLAKE2b-256 | b1cff48d00cd1ab706cf96cc89e3473c865435768e39b7d4d5ca8e2a40c1eead |
File details
Details for the file cargo_zigbuild-0.19.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: cargo_zigbuild-0.19.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.6 MB
- Tags: Python 3, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25ef665c40e2fb12cda66730860befc6842de53d745613d542c1d9bea9510e06 |
|
MD5 | e08db317f4cf650e0b1d0f03a2fe2154 |
|
BLAKE2b-256 | 668bca896f48f14fe2ba56020f4fd90656c2ec60aa6c97e58ed86aedb23860f8 |
File details
Details for the file cargo_zigbuild-0.19.5-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl
.
File metadata
- Download URL: cargo_zigbuild-0.19.5-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl
- Upload date:
- Size: 1.7 MB
- Tags: Python 3, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbc48ab39e0a4b960e99d77ae636eca445dba867313b0e1b8ca4b7977f8ebdba |
|
MD5 | 6e385efb889858b5b558cb3b8d2e2479 |
|
BLAKE2b-256 | 9a845f7a7f0f5735530b657b3e74a91986bfe93005f77df3bf3b88fa8a10eb13 |
File details
Details for the file cargo_zigbuild-0.19.5-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
.
File metadata
- Download URL: cargo_zigbuild-0.19.5-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
- Upload date:
- Size: 1.7 MB
- Tags: Python 3, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e74c0ef9ee429f2c262a150a32ae11f55473d012f28a96d2b39d791eef38a563 |
|
MD5 | fe1704cd0006f4a59fce9de1e9ab571c |
|
BLAKE2b-256 | 5c3bff0f43619751388fcb424e6df02018e9bce02bb8f489f2acf9afd850c4e9 |
File details
Details for the file cargo_zigbuild-0.19.5-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
.
File metadata
- Download URL: cargo_zigbuild-0.19.5-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
- Upload date:
- Size: 2.7 MB
- Tags: Python 3, macOS 10.12+ universal2 (ARM64, x86-64), macOS 10.12+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4120e75b5f1d6986f2bd3920bc4a75a67d0e0fbd6e1c2e26c2bb496d0db069e4 |
|
MD5 | df98eee4d72ce091d3a025a5594576ba |
|
BLAKE2b-256 | fd920dc723b7af2c9075c46b22501665118ea140e44e58bb2d94f875e83d1b42 |