rust-cross / cargo-zigbuild

Compile Cargo project with zig as linker
MIT License
1.45k stars 52 forks source link

Using target `x86_64-pc-windows-gnu` still tries to link in `msvcrt` #269

Open doawoo opened 3 weeks ago

doawoo commented 3 weeks ago

I have had a lot of success targeting Linux and macOS machines with this. However I'm running into a puzzling issue, it seems that Rust really wants to link msvcrt, even when the command I'm specifying is:

cargo zigbuild --target x86_64-pc-windows-gnu

It complains about not being able to locate the library.

  = note: error: unable to find dynamic system library 'msvcrt' using strategy 'paths_first'. searched paths:
            /Users/digit/git/Jsonrs/native/jsonrs/target/x86_64-pc-windows-gnu/debug/deps/msvcrt.dll
            /Users/digit/git/Jsonrs/native/jsonrs/target/x86_64-pc-windows-gnu/debug/deps/msvcrt.lib
            /Users/digit/git/Jsonrs/native/jsonrs/target/x86_64-pc-windows-gnu/debug/deps/libmsvcrt.a
            /Users/digit/git/Jsonrs/native/jsonrs/target/debug/deps/msvcrt.dll
            /Users/digit/git/Jsonrs/native/jsonrs/target/debug/deps/msvcrt.lib
            /Users/digit/git/Jsonrs/native/jsonrs/target/debug/deps/libmsvcrt.a
            /Users/digit/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/msvcrt.dll
            /Users/digit/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/msvcrt.lib
            /Users/digit/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libmsvcrt.a
            /Users/digit/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/msvcrt.dll
            /Users/digit/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/msvcrt.lib
            /Users/digit/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/x86_64-pc-windows-gnu/lib/libmsvcrt.a

Does this flag need to be filtered out in the linker argument processing step? Re-running the long command that's spat out, and then removing the -lmsvcrt results in the build proceeding further until it hits the temp files that no longer exist post-build.

messense commented 3 weeks ago

Can you try filter out it in here? https://github.com/rust-cross/cargo-zigbuild/blob/d420792226cef24e8a718dbef87fec14df77f3d2/src/zig.rs#L105-L132

Thanks

messense commented 3 weeks ago

FYI, -lmsvcrt seems to be added by Rust in https://github.com/rust-lang/rust/blob/b5723af3457b9cd3795eeb97e9af2d34964854f2/compiler/rustc_target/src/spec/base/windows_gnu.rs#L33-L44

doawoo commented 2 weeks ago

Thanks for the quick reply. Looking at the CI results there's some other gotchas involved here. I've cloned down your branch and will be playing a bit with it to see if I can resolve any of the linker issues I've seen in CI.