axodotdev / cargo-dist

📦 shippable application packaging
https://axodotdev.github.io/cargo-dist/
Apache License 2.0
1.51k stars 72 forks source link

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

Open markcatley opened 1 year ago

markcatley commented 1 year ago

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.

Gankra commented 1 year ago

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

markcatley commented 1 year ago

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.

Gankra commented 1 year ago

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 commented 1 year ago

Initial Notes From Testing On WSL (Ubuntu 20.04):

Gankra commented 1 year ago

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

ckoehler commented 10 months ago

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.