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 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, macOS and Windows gnu 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.18.4-py3-none-win_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12fa2c181c18be7fbfcac59ea779910beefb47dcfbfe125410a0521a6497d943 |
|
MD5 | ed8f6afed66ef5ef1b55d83bebf069d8 |
|
BLAKE2b-256 | de98a485ea2a1096f36770ff2f90fd8206379918093c6541913629e740132c87 |
Hashes for cargo_zigbuild-0.18.4-py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdab564f6625566f681706c34ed7578afb14affd27243f609cb3da4abb72c7de |
|
MD5 | 80d8ecc0dc5a093b012488da778234bb |
|
BLAKE2b-256 | 55aec930af36a752fecec8e236c430643935dbc288a08418e7f6665b56add936 |
Hashes for cargo_zigbuild-0.18.4-py3-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e5079021df7139a23e7405bd023ee3c9b0030520d80705c4d7c1ec1e9f22118 |
|
MD5 | 5c1c170ccdf92cc5ca7dd873df97b51e |
|
BLAKE2b-256 | 15ad255f18930b2ae699aa9d03c7c9e1659dc7af664ed909d0b3324e44bf421a |
Hashes for cargo_zigbuild-0.18.4-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 766a36b8bdd54e2c7232fddda31c203c74601540d56acf3b9d443cd94a47dd53 |
|
MD5 | 3d289dffa0f1a2e9af9a42a40fca579d |
|
BLAKE2b-256 | b7dc8ed1c0e9c996926e0713bf7dcec6a981fba2079c1c45c1434ebbdda60cfc |
Hashes for cargo_zigbuild-0.18.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc9b0b2919e2c7e972d5dcdac4eaec7bf0c37e1d00caefe5cf75cf1d26fc824e |
|
MD5 | 7d9bdd3b1f52404f131ce9ca7597f356 |
|
BLAKE2b-256 | de9901f32961ec723776ddcdf966ad12d601af0b1eaec5dff4a23d8c9ff88517 |
Hashes for cargo_zigbuild-0.18.4-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d5ba79436bbac60955f2c773ba6790c1f602a3876e292c2d4634e308a0fc3d8 |
|
MD5 | bfa6068045e17d1e8e3a354c207c3a25 |
|
BLAKE2b-256 | 67aa448bb07c4e82414cd9f444a0e3ea1d30d70beb3f451c11519c22b6aa4284 |
Hashes for cargo_zigbuild-0.18.4-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a762f957b2e3fd5537ed3395aaae31361171ab3532e9a59466d4807ae77c2aea |
|
MD5 | aea6bfd0e356b89efb19aab385bd85cc |
|
BLAKE2b-256 | 775c14e41c064dd716a3a8d816fbe262ccaa7bf7522e185b02beb9fe9bc38b31 |
Hashes for cargo_zigbuild-0.18.4-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f976ca4c8b89aa2fe1938ec0c518f19660eccf8071ca61a48639422a4db82e09 |
|
MD5 | 9cd2c6f5c1ae3251e01eb07ac57a4aa3 |
|
BLAKE2b-256 | 101c74d180285919c690683f729e99a0d68e2e8da3e3cfae286a0ccace79fc32 |