cargo-dist icon indicating copy to clipboard operation
cargo-dist copied to clipboard

Add support for zigbuild for backward-compatible glibc and macos universal binary support

Open markcatley opened this issue 1 year ago • 6 comments

Building portable binaries on Linux is a bit of a nightmare. There's no simple way to select a minimum version of Linux you'd like to support. The reason for this is that the standard linkers will always dynamically link against the latest version of glibc and any other library on the system. glibc is the most common dynamically linked library in rust binaries and is commonly the only dynamically linked library. Supporting old versions of that would go a long way to support portable binaries on linux.

A common approach, taken by cargo-binstall and cargo-lambda is to use the zig linker via cargo-zigbuild to enable linking against older glibc version. I propose we add support for it.

Having a look at how it works support could be added by looking for a specified glibc version in the target tuple (-unknown-linux-gnu., eg. aarch64-unknown-linux-gnu.2.17 or universal2-apple-darwin) and calling cargo zigbuild instead of cargo build and add some information into the documentation. At a later date, once you're happy the support is stable, you could add the steps to install ziglang and cargo-zigbuild to the github actions templates and add support for glibc 2.17 by default (this seems to be the version supported by other rust binaries that have older linux support - it's from Dec 2012, or you could pick 2.26 which supports the AmazonLinux 2, the runtime for AWS lambda and other AWS services).

I've added macos universal binary support as a selling point for this option too. I am not sure that the need for it is as compelling as older glibc versions but some projects may find it useful.

If you're happy with this approach I'm happy to help with adding support.

markcatley avatar Jun 13 '23 21:06 markcatley

Hey sorry for the delay, but I was planning on adding cargo-zigbuild support ~next week, and this plan seems pretty reasonable!

Gankra avatar Aug 02 '23 13:08 Gankra

No worries. I looked at implementing myself but it was well beyond my skills without a lot of help. More than happy to help with testing though.

markcatley avatar Aug 05 '23 08:08 markcatley

Initial Notes From Testing On Windows:

I expect most people who care about this feature care about linux => other, so I'm going to shift to testing on WSL for now.

Gankra avatar Aug 11 '23 15:08 Gankra

Initial Notes From Testing On WSL (Ubuntu 20.04):

  • recommended zig installation option of snap install zig --classic --beta fails, unclear if it's busted or i did it wrong
  • opt for the combo pip install cargo-zigbuild, works great
  • building cargo-dist seems to work perfectly

Gankra avatar Aug 11 '23 15:08 Gankra

Looks like zigbuild musl support recently broke because zig and rustc can desync on the version of musl they're thinking of https://github.com/rust-cross/cargo-zigbuild/issues/158

Gankra avatar Aug 11 '23 16:08 Gankra

I’d like to be able to build my project with zigbuild as part of cargo dist. It’s fairly simple (for me): use the zigbuild docker image, run ‘cargo zigbuild — target foo’. I’m happy to contribute, but don’t know the codebase.

ckoehler avatar Dec 21 '23 18:12 ckoehler