Skip to content

Commit

Permalink
cargo: Use the right LLVM tools and arguments for MSVC cross-compilation
Browse files Browse the repository at this point in the history
`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;
- `-fuse-ld=lld-link` does not strictly need to be set in `C(XX)FLAGS`
  unless a native project uses `CC`/`CXX` to link libraries
  (otherwise only `cargo`/`rustc` does this, but invokes it through the
   `LINKER` environment variable which is configured to `rust-lld`, as
   the default is otherwise `link.exe`).
  • Loading branch information
MarijnS95 committed Jan 4, 2024
1 parent 268939a commit 1b96e8b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions xbuild/src/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,18 @@ impl CargoBuild {

pub fn use_windows_sdk(&mut self, path: &Path) -> Result<()> {
let path = dunce::canonicalize(path)?;
self.cfg_tool(Tool::Cc, "clang");
self.cfg_tool(Tool::Cxx, "clang++");
self.cfg_tool(Tool::Cc, "clang-cl");
self.cfg_tool(Tool::Cxx, "clang-cl");
self.cfg_tool(Tool::Ar, "llvm-lib");
self.cfg_tool(Tool::Linker, "rust-lld");
self.use_ld("lld-link");
self.cfg_tool(Tool::Linker, "rust-lld"); // Rust defaults to link.exe, use its rust-lld binary instead
self.use_ld("lld-link"); // Use lld's link.exe wrapper when a C(++) requests the linker to be used
// (Uncommon, typically all objects are only Ar-chived, and linked at once by cargo as the last build step)
self.add_target_feature("+crt-static");
self.add_cxxflag("-stdlib=libc++");
self.add_include_dir(&path.join("crt").join("include"));
self.add_include_dir(&path.join("sdk").join("include").join("um"));
self.add_include_dir(&path.join("sdk").join("include").join("ucrt"));
self.add_include_dir(&path.join("sdk").join("include").join("shared"));
self.add_msvc_include_dir(&path.join("crt").join("include"));
self.add_msvc_include_dir(&path.join("sdk").join("include").join("um"));
self.add_msvc_include_dir(&path.join("sdk").join("include").join("ucrt"));
self.add_msvc_include_dir(&path.join("sdk").join("include").join("shared"));
self.add_lib_dir(&path.join("crt").join("lib").join("x86_64"));
self.add_lib_dir(&path.join("sdk").join("lib").join("um").join("x86_64"));
self.add_lib_dir(&path.join("sdk").join("lib").join("ucrt").join("x86_64"));
Expand Down Expand Up @@ -439,6 +440,10 @@ impl CargoBuild {
self.c_flags.push_str(&format!("-I{} ", path.display()));
}

pub fn add_msvc_include_dir(&mut self, path: &Path) {
self.c_flags.push_str(&format!("-imsvc{} ", path.display()));
}

pub fn set_sysroot(&mut self, path: &Path) {
let arg = format!("--sysroot={}", path.display());
self.add_cflag(&arg);
Expand Down

0 comments on commit 1b96e8b

Please sign in to comment.