rust-cross / cargo-zigbuild

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

Windows username with space breaks linking #115

Closed MarcoBrieden closed 1 year ago

MarcoBrieden commented 1 year ago

My Windows username contains a space, resulting in this profile directory: "C:\Users\Marco Brieden\".

Trying to compile my code results in this error:

❯ cargo zigbuild --release --target x86_64-unknown-linux-gnu
   Compiling rust-resolver v0.1.0 (D:\Development\vp360\rust)
error: linking with `C:\Users\Marco Brieden\AppData\Local\cargo-zigbuild\0.16.3\zigcc-x86_64-unknown-linux-gnu.bat` failed: exit code: 1
  |
  = note: "cmd" "/c" "C:\\Users\\Marco Brieden\\AppData\\Local\\cargo-zigbuild\\0.16.3\\zigcc-x86_64-unknown-linux-gnu.bat" "-m64" "D:\\Temp\\rustcEc453k\\symbols.o" "D:\\Development\\vp360\\rust\\target\\x86_64-unknown-linux-gnu\\release\\deps\\rust_resolver-f8bda55ce059fbe0.rust_resolver.ff2b47e7-cgu.0.rcgu.o" "-Wl,--as-needed" "-L" "D:\\Development\\vp360\\rust\\target\\x86_64-unknown-linux-gnu\\release\\deps" "-L" "D:\\Development\\vp360\\rust\\target\\release\\deps" "-L" "C:\\Users\\Marco Brieden\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib" "-Wl,-Bstatic" "C:\\Users\\Marco Brieden\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\libcompiler_builtins-87185c5e58e44fea.rlib" "-Wl,-Bdynamic" "-llibpq" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "C:\\Users\\Marco Brieden\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib" "-o" "D:\\Development\\vp360\\rust\\target\\x86_64-unknown-linux-gnu\\release\\deps\\rust_resolver-f8bda55ce059fbe0" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-Wl,--strip-all" "-nodefaultlibs"
  = note: 'C:\Users\Marco' is not recognized as an internal or external command,
          operable program or batch file.

error: could not compile `rust-resolver` due to previous error
messense commented 1 year ago

I don't personally use Windows, so help appreciated.

MarcoBrieden commented 1 year ago

The zigcc-x86_64-unknown-linux-gnu.bat that is failing contains this:

C:\Users\Marco Brieden\.cargo\bin\cargo-zigbuild.exe zig cc -- -target x86_64-linux-gnu  -g -Wl,--version-script=C:\Users\Marco Brieden\AppData\Local\cargo-zigbuild\0.16.3\fcntl.map -include C:\Users\Marco Brieden\AppData\Local\cargo-zigbuild\0.16.3\fcntl.h %*

Surrounding all paths with spaces might fix it:

"C:\Users\Marco Brieden\.cargo\bin\cargo-zigbuild.exe" zig cc -- -target x86_64-linux-gnu  -g -Wl,--version-script="C:\Users\Marco Brieden\AppData\Local\cargo-zigbuild\0.16.3\fcntl.map" -include "C:\Users\Marco Brieden\AppData\Local\cargo-zigbuild\0.16.3\fcntl.h" %*

I tried editing it, but it gets newly generated every run. Write protecting it causes zigbuild to complain and error out.

The same folder has two more files that have the same issue: "zigcxx-x86_64-unknown-linux-gnu.bat" and "zigranlib.bat".

messense commented 1 year ago

You can change it here https://github.com/rust-cross/cargo-zigbuild/blob/fb64c6578cd5e94be74d654dc272b8af539ea414/src/zig.rs#L868-L874

And run cargo run zigbuild instead of cargo zigbuild to test it.

MarcoBrieden commented 1 year ago

I think it fails to execute the .bat file in the first place, because of the path. Any idea where that happens?

messense commented 1 year ago

I think it's https://github.com/rust-lang/rust/issues/29494, unfortunate it's out of our control because we didn't use cmd /c, something else did.

MarcoBrieden commented 1 year ago

That bug has been fixed a year ago though, right?

Are you saying the batch file isn't executed from within cargo-zigbuild? Where is it executed from then?

messense commented 1 year ago

That bug has been fixed a year ago though, right?

They just added a new API which needs to be adopted.

Are you saying the batch file isn't executed from within cargo-zigbuild? Where is it executed from then?

Probably cargo or cc-rs.

messense commented 1 year ago

Actually, it's rust: https://github.com/rust-lang/rust/blob/6dc3999c2699461aa930b8c1e00f99e73dcc0174/compiler/rustc_codegen_ssa/src/back/linker.rs#L49-L58

MarcoBrieden commented 1 year ago

Thanks for finding this!

messense commented 1 year ago

I think you can workaround it by setting the CARGO_ZIGBUILD_CACHE_DIR env var to a directory that doesn't contain whitespace.

MarcoBrieden commented 1 year ago

That worked, thank you very much messense!