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 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

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, macOS and Windows gnu 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.1-py3-none-win_arm64.whl (1.2 MB view details)

Uploaded Python 3 Windows ARM64

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

Uploaded Python 3 Windows x86-64

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

Uploaded Python 3 Windows x86

cargo_zigbuild-0.19.1-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.1-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.1-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.6 MB view details)

Uploaded Python 3 manylinux: glibc 2.5+ x86-64

cargo_zigbuild-0.19.1-py3-none-manylinux_2_5_i686.manylinux1_i686.whl (1.6 MB view details)

Uploaded Python 3 manylinux: glibc 2.5+ i686

cargo_zigbuild-0.19.1-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (2.6 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.1-py3-none-win_arm64.whl.

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.1-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 dd806843bf0b63ec9817d02a21c204ef4fe41cccfa0b43617bdd58da2bdb8da9
MD5 bdda6ab5752f1e68a034cba0562b1cee
BLAKE2b-256 eb8e6a778f7764b26244d4ac688eb3630aef24e45e2f5fb74893ba6d9b3c330a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 eaf598df2c9288c5b1dbc57935d5ed0e891b9073de81a68ac6d954a7a41728b7
MD5 5032568b2be5b1744116cfd53c309cf6
BLAKE2b-256 c19cbe9f6eb61adbe7f43bc152a3aace07022256a765075aa6bdd390f3a08f04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.1-py3-none-win32.whl
Algorithm Hash digest
SHA256 ebbb20f7ed960c64742e9fb209515232b18ce1822f037c7b6e6f27e67873a772
MD5 a85927552c48cee92d0b473c1297b375
BLAKE2b-256 087d5a394cd16ea0e8038261c445bc80ec8bca2512526bffda3570fccdb3574e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.1-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 8d4f0891936dc7343278f88416cef4177703835f7ea81290fafa3ed5a9962d08
MD5 8a075fa21a9b1a189076a7cec23a5633
BLAKE2b-256 511320dd93f0f340446044524be10493ef14b0580f0545d1363b2a7b5c543519

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9f1fe562bd510b48d31cb7d0bd526a7dc508329672528f859f78ec5ac657b71a
MD5 1469ce3dda8b2b257af0da444f7f2d83
BLAKE2b-256 8208e56792552ccc1a29d1a5eb58677ec4ccacaaa6ebe15a218bb6b37ea7138f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.1-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 21871d1876468eabe48cdfb28bcd3eb18f30dc5ad96d92d39cbcbe7687f400f6
MD5 3c1f6e2767e63a5f75cd9b1f1cec8ee6
BLAKE2b-256 692ba4486cf43182d0234cd9b7c71b08ecf74ce7e0c0c6ed9a0b408e455099cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.1-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 007d6486784c7cf234bb6b4dc3961a81a435b43b922664ce5c932b6a4d22b0fd
MD5 9046dcd99a666481cb81fa8b42c81e8e
BLAKE2b-256 65520b800f2a7eb4c1540fd9179451f109e293f807e41429d71e74c83c3b5daf

See more details on using hashes here.

File details

Details for the file cargo_zigbuild-0.19.1-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.1-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 db1abf030f4ee48d73c604c53d6b25e9da362de648da9f1ee5597b486c33a50e
MD5 c1e04e5f501b40b572ce8f55fef8de63
BLAKE2b-256 5bee2f173cd43122e45c4e5dae1666e9fb42623f33466143055e6e78a5e087d2

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