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.4-py3-none-win_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60fbd184a7dafe543039e8ba5cb99f5795220666debca19ab23a2486e8db5f31 |
|
MD5 | 3eb428f5c5ba9b61e79f90d0f806a512 |
|
BLAKE2b-256 | f26febfee011a09f0c6d96510fbfa2e318b04553350f248d665d5969665f35bf |
Hashes for cargo_zigbuild-0.19.4-py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d22d17e78522320796327e7239653b724854c118687997cc372306598d103fcc |
|
MD5 | 5a66ac1491b65b68b812d7e31daa1031 |
|
BLAKE2b-256 | 9142cc8ec20b2b006d48ef2c25dd3480efc4b1939a5d0cb5ff22e0defd0ff9c1 |
Hashes for cargo_zigbuild-0.19.4-py3-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2071badb1e7d045312909c1652152a7d333df0d53947beacbb4b809ca42441d3 |
|
MD5 | e760b2a1d16f9ed011b0eb4390c3b577 |
|
BLAKE2b-256 | fd36d773074d46e34462f31cc9a0a6c6a6603f0dd18b534f379954ec78898739 |
Hashes for cargo_zigbuild-0.19.4-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de09d4cafaabf602dc363a4bac510820a96b6a7f13fa39426abd11b8baeab55e |
|
MD5 | dc680c310217cad2c2a852366d38ba27 |
|
BLAKE2b-256 | eb443d5674513a17f31f8d5133887b0837168297a4021b37589eff4d5c917074 |
Hashes for cargo_zigbuild-0.19.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50b366d3f9fb129610c0b63b6fe3e42a4dc24694b1da853740f593e59c77ffca |
|
MD5 | 45f9419ea68997cd53c9f72dacc08e74 |
|
BLAKE2b-256 | 52339683c8fceba0760fbf4939ba3174734e3699de2b2bd8ecfa1b28d3ecfdcc |
Hashes for cargo_zigbuild-0.19.4-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 431455b955c923a9de53041340f2f8e0e7e959a8ac9b52495f76531b79b40ce1 |
|
MD5 | 021431454645901083f18c6a65cdeb7c |
|
BLAKE2b-256 | 1142ecb23e717f3bfa53feec69005db4c5e87d7a0b4b7077b2d754faa41c768c |
Hashes for cargo_zigbuild-0.19.4-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc5429b828e194b715f1e808f7c2598694ea3dc9800ad380a47cbfc1ad935e66 |
|
MD5 | 55edaa6b373748180a9ba6c9f753530e |
|
BLAKE2b-256 | 26842780f4595260f99db7ffd354a5439458351b3b58f964a2c9f0bec2a6dee4 |
Hashes for cargo_zigbuild-0.19.4-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b81cf3d6908728611cfacf942a439152a4ae4cab810b2bfcc30326787084fee |
|
MD5 | 7448505880c1bd9d246332654a0dc0ca |
|
BLAKE2b-256 | 86a17961c4af3f48157a8c9ac2212c051547702cb1ad21a0c34154e7fc50f9ad |