Skip to content

Commit ffe3e34

Browse files
authored
cargo: Use the right LLVM tools and arguments for MSVC cross-compilation (#160)
`xbuild` uses the headers from `xwin` to support cross-compiling, but doesn't fully replicate the other setup steps that it proposes to provide a valid and working cross-compilation setup to MSVC. We need to make the following changes: - `clang-cl` must be used (a `clang` driver with `cl.exe` interface), as all native projects will call `CC`/`CXX` with flags that are only compatible with `cl.exe`; - `-I` does not seem to set up the system headers correctly, for this `-imsvc` should be used; - Remove `-Clink-arg=-fuse-ld=lld-link` because we have already selected a linker for Rust with the `LINKER` env var (see warning below). We might instead want to set it in `C(XX)FLAGS` so that when a native crate uses `CC`/`CXX` to link libraries, it can use `lld-link` instead of (probably, like `cargo`/`rustc` when `LINKER` is unsed) using `link.exe` by default; - Unset forced static CRT and `stdlib=libc++` selection, as these don't appear to ever be needed, and cause some builds to fail (when crates are changing these config options themselves?): = note: rust-lld: warning: ignoring unknown argument '-fuse-ld=lld-link' rust-lld: error: /failifmismatch: mismatch detected for 'RuntimeLibrary': >>> libbasis_universal_sys-4f5abd4a27e6aa13.rlib(basisu_resample_filters.o) has value MT_StaticRelease >>> libintel_tex_2-6d9bd3438efd06d9.rlib(ispc_texcomp_astc.o) has value MD_DynamicRelease
1 parent 08527e7 commit ffe3e34

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

xbuild/src/cargo/mod.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,14 @@ impl CargoBuild {
306306

307307
pub fn use_windows_sdk(&mut self, path: &Path) -> Result<()> {
308308
let path = dunce::canonicalize(path)?;
309-
self.cfg_tool(Tool::Cc, "clang");
310-
self.cfg_tool(Tool::Cxx, "clang++");
309+
self.cfg_tool(Tool::Cc, "clang-cl");
310+
self.cfg_tool(Tool::Cxx, "clang-cl");
311311
self.cfg_tool(Tool::Ar, "llvm-lib");
312-
self.cfg_tool(Tool::Linker, "rust-lld");
313-
self.use_ld("lld-link");
314-
self.add_target_feature("+crt-static");
315-
self.add_cxxflag("-stdlib=libc++");
316-
self.add_include_dir(&path.join("crt").join("include"));
317-
self.add_include_dir(&path.join("sdk").join("include").join("um"));
318-
self.add_include_dir(&path.join("sdk").join("include").join("ucrt"));
319-
self.add_include_dir(&path.join("sdk").join("include").join("shared"));
312+
self.cfg_tool(Tool::Linker, "rust-lld"); // Rust defaults to link.exe, use its rust-lld binary instead
313+
self.add_msvc_include_dir(&path.join("crt").join("include"));
314+
self.add_msvc_include_dir(&path.join("sdk").join("include").join("um"));
315+
self.add_msvc_include_dir(&path.join("sdk").join("include").join("ucrt"));
316+
self.add_msvc_include_dir(&path.join("sdk").join("include").join("shared"));
320317
self.add_lib_dir(&path.join("crt").join("lib").join("x86_64"));
321318
self.add_lib_dir(&path.join("sdk").join("lib").join("um").join("x86_64"));
322319
self.add_lib_dir(&path.join("sdk").join("lib").join("ucrt").join("x86_64"));
@@ -439,6 +436,10 @@ impl CargoBuild {
439436
self.c_flags.push_str(&format!("-I{} ", path.display()));
440437
}
441438

439+
pub fn add_msvc_include_dir(&mut self, path: &Path) {
440+
self.c_flags.push_str(&format!("-imsvc{} ", path.display()));
441+
}
442+
442443
pub fn set_sysroot(&mut self, path: &Path) {
443444
let arg = format!("--sysroot={}", path.display());
444445
self.add_cflag(&arg);

0 commit comments

Comments
 (0)