rust-cross / cargo-zigbuild

Compile Cargo project with zig as linker
MIT License
1.35k stars 50 forks source link

Fails to build some crates that depend on CC variable - ring etc #217

Closed richarddd closed 5 months ago

richarddd commented 5 months ago

Ring expects host to set CC env. If we don't have the correct cross compiler installed, build will fail.

It's not possible to set this explicitly via env since most build systems wont accept arguments to CC variable.

A possible workaround would be to create a zig cc wrapper file for the target we're building for:

/tmp/zigcc-aarch64-linux-gnu:

#!/bin/bash
set -e

new_array=()
for value in "$@"
do
    [[ $value != *self-contained/*crt* ]] && new_array+=($value)
done

zig cc -target aarch64-linux-gnu "${new_array[@]}"

Make it executable and set it as CC, CXX, AR, RANLIB etc for the target we're building for.

messense commented 5 months ago

I don't see how this is related to cargo-zigbuild.

richarddd commented 5 months ago

Well, the whole point of cargo-zigbuild would be to have a simple way to cross compile. But crates like ring, aws-lc-rs and a lot of other crates won't compile with cargo-zigbuild due to the issue i mentioned above.

messense commented 5 months ago

Then you should open an issue with repro steps using cargo-zigbuild.

messense commented 5 months ago

I'd also like to point out that we have a test for ring (which rustls depends on by default) in CI and it works fine:

https://github.com/rust-cross/cargo-zigbuild/actions/runs/7666563864/job/20894629394#step:19:231

so please open an issue with detailed repro steps if you are having issue with it.

richarddd commented 5 months ago

I'd also like to point out that we have a test for ring (which rustls depends on by default) in CI and it works fine:

https://github.com/rust-cross/cargo-zigbuild/actions/runs/7666563864/job/20894629394#step:19:231

so please open an issue with detailed repro steps if you are having issue with it.

~~That test only works because you are installing gcc-aarch64-linux-gnu that ring uses to build. If you have gcc-aarch64-linux-gnu you won’t need zig to compile :)~~

I will create a simple reproducible to illustrate my point. Thanks

I can see now that you're already doing what i proposed. I must have misconfigured something, sorry about that:

"/root/.cache/cargo-zigbuild/0.18.3/zigcc-aarch64-unknown-linux-gnu.sh"

richarddd commented 5 months ago

@messense i have now found how to reproduce the issue. This only occurs when targeting another platform than your host. Like i mentioned before, this is not catched in your test as you are installing gcc-aarch64-linux-gnu when you are installing qemu: https://github.com/rust-cross/cargo-zigbuild/actions/runs/7666563864/job/20894629394#step:19:50

Here is the repoducer: https://github.com/richarddd/cargo-zigbuild-cc-issue

And the failed step: https://github.com/richarddd/cargo-zigbuild-cc-issue/actions/runs/7712241228/job/21019355357#step:5:218

messense commented 5 months ago

cargo-zigbuild isn't a rustup proxy, so it doesn't work with +nightly, you need to use cargo +nightly zigbuild ... instead.

richarddd commented 5 months ago

Oh that was it! Thank you so much! Works flawlessly now. QQ: What would be the equivalent of cargo +nightly test?

messense commented 5 months ago

What would be the equivalent of cargo +nightly test?

I think something like this should work:

export CARGO_TARGET_<TARGET-TRIPLE>_RUNNER=qemu-xxx
RUSTUP_TOOLCHAIN=nightly cargo-zigbuild test