Costs of Using Zig Linker

I have lost, well, an enormous amount of time trying to produce static Rust binaries for Linux, typically x86_64 and now aarch64 (arm64) architectures, always using musl. I have not compiled for other operating systems such as Windows and macOS, because I imagine that this will eat more and more of my time and sanity.

Musl for x86_64 Linux has come a long way, but I find that I still need to do nonsense like this in my Cargo dependencies:

openssl = { version="*", features=["vendored"]} 
curl = { version="*", features=["static-curl", "static-ssl"] }

This essentially makes sure that if OpenSSL or cURL are ever included, that we force static feature flags on them. In all of my projects which ship static binaries, I often include an executable which simply initializes OpenSSL and cURL, and makes a simple HEAD request to Google to ensure that static compilation has actually worked. In earlier, darker days, more was required and debugging specifically AWS Lambda applications in Rust was a veritable nightmare.

The above does work for compiling x86_64-unknown-linux-musl from a native host.

The same does not work for compiling aarch64-unknown-linux-musl, again from a native aarch64 VM or via QEMU, which is aggravating to say the least.

I know a tremendous amount of work has been put in by the community to improve things, and I am eternally grateful :pray:

I remember reading about using the zig linker and now we even have a Cargo plugin for using Zig, which seems extremely promising! Once again, to all involved, thank you!

My question is this: what tradeoffs are made when using Zig for compilation vs compiling directly with musl or even glibc? The solution seems magical, making even cross compilation work very well, and as a result of all of the pain I have experienced over the years with doing static builds, I feel forced to ask "what's the catch?" What, if anything, am I sacrificing to use Zig for all static binary compilation?

3 Likes

Apologies all, I had accidentally hit submit before the post was ready :slight_smile:

I don't think there's any hidden gotcha. Zig just put a lot of work into bundling all variations of libc and cross-linkers in its distribution, while Rust/Cargo didn't, and treat this as not their problem.

5 Likes

Since you brought up AWS lambda, I wonder whether you're aware of cargo-lambda? It also uses cargo-zigbuild.

I have been using the lambda runtime crate and this has been fine, but getting a static binary for ARM64 has been a challenge. I'll check it out.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.