rustc_codegen_cranelift icon indicating copy to clipboard operation
rustc_codegen_cranelift copied to clipboard

`cargo clif build` does not work on Windows

Open luxalpa opened this issue 1 year ago • 12 comments

Setting up the tool like in the Readme, I am getting this error when running it as cargo clif build:

error: failed to run `rustc` to learn about target-specific information

Caused by:
  process didn't exit successfully: `rustc - --crate-name ___ --print=file-names -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend=C:\dev\cranelift\bin\rustc_codegen_cranelift.dll --sysroot C:\dev\cranelift --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg` (exit code: 1)
  --- stderr
  error: couldn't load codegen backend "C:\\dev\\cranelift\\bin\\rustc_codegen_cranelift.dll": LoadLibraryExW failed

The problem doesn't appear when using cargo-clif directly, and it appears on both the msvc and the gnu versions of the latest release.

luxalpa avatar Jan 09 '24 12:01 luxalpa

C:\dev\cranelift\bin\rustc_codegen_cranelift.dll does exist, right? Unfortunately libloading (the crate rustc uses for dynamic linking) doesn't print the error returned by LoadLibraryExW and instead only prints a generic error message, making it hard for me to figure out what the issue is: https://github.com/nagisa/rust_libloading/issues/133

bjorn3 avatar Jan 09 '24 13:01 bjorn3

Yes, the file does exist and it seems to be working when using cargo-clif.exe directly (including when using it from path, i.e. from some other directory). I'm only getting the error when using it via cargo clif command.

luxalpa avatar Jan 09 '24 21:01 luxalpa

Could you try again with the latest master version? This now includes https://github.com/rust-lang/rust/pull/119815 which should give a more useful error message.

bjorn3 avatar Jan 21 '24 11:01 bjorn3

Thanks, I've figured out the issue. Cargo uses a different environment based on the toolchain of the project in which it is being run. If it doesn't match the one that's been used to build rustc_codegen_cranelift, it won't find the right versions of the .dlls. Running

rustup run nightly-2024-01-21-x86_64-pc-windows-msvc cargo clif build

in the project folder works.

Ironically, due to this, the recent change to the error formating didn't actually yield any difference in output. It was still using the older nightly version and so it was still only displaying "LoadLibraryExW failed" without any useful error message, which had confused me at first.

luxalpa avatar Jan 22 '24 12:01 luxalpa

That explains it. Does cargo clif -V show the nightly used by cg_clif or the default toolchain for your project?

bjorn3 avatar Jan 22 '24 13:01 bjorn3

It shows the default toolchain for the project (same as rustup shows active toolchain).

luxalpa avatar Jan 22 '24 14:01 luxalpa

I can't reproduce that. No matter what I try cargo clif -V shows the toolchain with which cg_clif was built. I tried both in a directory with a different toolchain in rust-toolchain.toml and one with an override set using rustup override set. cargo resolves to the rustup wrapper, right? Not the cargo of a specific toolchain.

bjorn3 avatar Jan 26 '24 15:01 bjorn3

I think this is caused by some Windows specific behavior described in https://internals.rust-lang.org/t/help-test-windows-behavior-between-rustup-and-cargo/20237

Could you try setting the RUSTUP_WINDOWS_PATH_ADD_BIN env var to 0?

bjorn3 avatar Feb 02 '24 07:02 bjorn3

Yes, setting that environment variable makes cargo clif build work.

luxalpa avatar Feb 02 '24 08:02 luxalpa

Great! As I understand from the linked thread, that will be the default in the future.

bjorn3 avatar Feb 02 '24 11:02 bjorn3

This is now the default? https://github.com/rust-lang/rustup/blob/0c501d55c54031a992395078d535b6d2574dc693/src/toolchain/toolchain.rs#L168-L180

ChrisDenton avatar Mar 24 '24 17:03 ChrisDenton

Rustup 1.27 was released just a couple of days before the default was flipped. So this will remain an issue until rustup 1.28 is released.

bjorn3 avatar Mar 24 '24 17:03 bjorn3