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 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.0-py3-none-win_arm64.whl (1.2 MB view details)

Uploaded Python 3 Windows ARM64

cargo_zigbuild-0.19.0-py3-none-win_amd64.whl (1.2 MB view details)

Uploaded Python 3 Windows x86-64

cargo_zigbuild-0.19.0-py3-none-win32.whl (1.1 MB view details)

Uploaded Python 3 Windows x86

cargo_zigbuild-0.19.0-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.0-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.0-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.0-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.0-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.0-py3-none-win_arm64.whl.

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 d6532f03d831826a6ecebeaa9e84456d203d299b7a2e26623a9542de1845a33d
MD5 37320d3f3532d8044f772c9ec6ca6500
BLAKE2b-256 44228575a28d3d5b7a05c720d5b91d01537220cc5b9383e16b13b8a3eeb5c6c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 a5429270b09371d6842fc23f2e249d30a304cddff827d266da3890716f7a1086
MD5 a20b74ba001fcc741bc5d3c26647b284
BLAKE2b-256 ac4ed4c9ef7c3c32a13b4bdfc11999dc4186d33a17c8363955b9dcd11b7191e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.0-py3-none-win32.whl
Algorithm Hash digest
SHA256 8f790ac692ebd332ba3e2f5d420ed83c1b8f6e4d0a6054555e63317863f13083
MD5 855714f97063a398dd9203269fa22e53
BLAKE2b-256 cba70161758574ab273a2dcf4ebd1cbb5fd2efce1caecd52f9a2f885fde069f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 4c991873d9ddcc862414f67cc4f661679168e0637f76276dad1f6bfe5e4b7ed2
MD5 47a9457db3beffc255d47185ab57c029
BLAKE2b-256 f3aa3769c50819cff42428029a6026f30cb6c232b6f42796f27c75bd50c3e145

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 db7bc012d993645dff2719a2aab33f2aa2fc2ee160b2c0d555c1ea729eab29b7
MD5 8248416edabc2550f9ab78d012b12519
BLAKE2b-256 031f9b0c672cf688e16864d15efc196526d10a0cc7511def16863570170795db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.0-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 72d8f09566af938589cccbc0a97043639ff144742a9354796269f4e51507e25a
MD5 8fed974c10b017c89e4dc36defbfa809
BLAKE2b-256 ed7221bf6281d7a0e3bc9cfa3ff6ec66328ed6176d11a59a2ee284aa500866fe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cargo_zigbuild-0.19.0-py3-none-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 4becbb716d6757c952ae4d61a49020f8a6085102a316d5314feceedccdc17748
MD5 ab2da4aebfdee856b902be554811e38f
BLAKE2b-256 3d19d6b88bdce2d9052eb79380c59d7059de325e9cb71248b23944037eff0906

See more details on using hashes here.

File details

Details for the file cargo_zigbuild-0.19.0-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.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 4da0a0e6491e1fb9ac75f0dbaba6186a85e5654536c005ae911ac1751b132e9a
MD5 ee6671166049178530adeed2a981c964
BLAKE2b-256 8138629f243aec02b774432d8240dd82b867a6535fa92c8610804fd53848f661

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