Skip to main content

Compile Cargo project with zig as linker

Project description

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.

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

cargo_zigbuild-0.19.5-py3-none-win_arm64.whl (1.3 MB view details)

Uploaded Python 3 Windows ARM64

cargo_zigbuild-0.19.5-py3-none-win_amd64.whl (1.3 MB view details)

Uploaded Python 3 Windows x86-64

cargo_zigbuild-0.19.5-py3-none-win32.whl (1.2 MB view details)

Uploaded Python 3 Windows x86

cargo_zigbuild-0.19.5-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.5 MB view details)

Uploaded Python 3 manylinux: glibc 2.17+ ARMv7l

cargo_zigbuild-0.19.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.6 MB view details)

Uploaded Python 3 manylinux: glibc 2.17+ ARM64

cargo_zigbuild-0.19.5-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.7 MB view details)

Uploaded Python 3 manylinux: glibc 2.5+ x86-64

cargo_zigbuild-0.19.5-py3-none-manylinux_2_5_i686.manylinux1_i686.whl (1.7 MB view details)

Uploaded Python 3 manylinux: glibc 2.5+ i686

cargo_zigbuild-0.19.5-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (2.7 MB view details)

Uploaded Python 3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

File details

Details for the file cargo_zigbuild-0.19.5-py3-none-win_arm64.whl.

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.5-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 baf5f18df485ba1541ff4e6a779890660deaee3e66c9331fef9facbf3a57a93a
MD5 4f76ffd9f18d7e3e31f9f8d79556088b
BLAKE2b-256 cc1e32172fbff32f18823981c3abafb09b91172b1b75160f0f2f4e9eaab729e7

See more details on using hashes here.

File details

Details for the file cargo_zigbuild-0.19.5-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.5-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 7f451f2d57cf287a4f95be691cbad8161fbb4bf3b709218cd3fb7ea6c4f5c39d
MD5 2dd54e3c1586721a1a4e411571bf3ec2
BLAKE2b-256 4eb5bc5cad9607e314b8b5c8b2f03435fccdab2d1543ff1836882a6da36d0c64

See more details on using hashes here.

File details

Details for the file cargo_zigbuild-0.19.5-py3-none-win32.whl.

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.5-py3-none-win32.whl
Algorithm Hash digest
SHA256 4c0889fc9d60e3a66a58affd2b4aa729ba9f326618c61c272d24f4d6a397bd39
MD5 19b95feb6c9989848af85186e0affb4b
BLAKE2b-256 3e2824d974e1908272f64a29a1820fe6a65e04b79ef45f008357e959f80fed67

See more details on using hashes here.

File details

Details for the file cargo_zigbuild-0.19.5-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.5-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 2fa665c35878f91430596687652a4f5874b5feb134744bef0512bd5297bf7e11
MD5 d2112094e28e277b4372153a21007c33
BLAKE2b-256 b1cff48d00cd1ab706cf96cc89e3473c865435768e39b7d4d5ca8e2a40c1eead

See more details on using hashes here.

File details

Details for the file cargo_zigbuild-0.19.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 25ef665c40e2fb12cda66730860befc6842de53d745613d542c1d9bea9510e06
MD5 e08db317f4cf650e0b1d0f03a2fe2154
BLAKE2b-256 668bca896f48f14fe2ba56020f4fd90656c2ec60aa6c97e58ed86aedb23860f8

See more details on using hashes here.

File details

Details for the file cargo_zigbuild-0.19.5-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.5-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fbc48ab39e0a4b960e99d77ae636eca445dba867313b0e1b8ca4b7977f8ebdba
MD5 6e385efb889858b5b558cb3b8d2e2479
BLAKE2b-256 9a845f7a7f0f5735530b657b3e74a91986bfe93005f77df3bf3b88fa8a10eb13

See more details on using hashes here.

File details

Details for the file cargo_zigbuild-0.19.5-py3-none-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.5-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 e74c0ef9ee429f2c262a150a32ae11f55473d012f28a96d2b39d791eef38a563
MD5 fe1704cd0006f4a59fce9de1e9ab571c
BLAKE2b-256 5c3bff0f43619751388fcb424e6df02018e9bce02bb8f489f2acf9afd850c4e9

See more details on using hashes here.

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

File hashes

Hashes for cargo_zigbuild-0.19.5-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 4120e75b5f1d6986f2bd3920bc4a75a67d0e0fbd6e1c2e26c2bb496d0db069e4
MD5 df98eee4d72ce091d3a025a5594576ba
BLAKE2b-256 fd920dc723b7af2c9075c46b22501665118ea140e44e58bb2d94f875e83d1b42

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page