diff --git a/Cargo.lock b/Cargo.lock index eef99c77d..c752dc08c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -258,9 +258,9 @@ dependencies = [ [[package]] name = "cargo-xwin" -version = "0.17.4" +version = "0.17.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "177548480a9f6291a24d0183941d59c4552bb3206c888bf89caef30a1c036e03" +checksum = "9cfe03fae5d4f7192f1a573f84d12b11f20877d43941ea3730289cc7af050531" dependencies = [ "anyhow", "cargo-config2", diff --git a/Cargo.toml b/Cargo.toml index fa51b4962..74b465fca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -98,7 +98,7 @@ clap_complete_command = { version = "0.6.1", optional = true } # cross compile cargo-zigbuild = { version = "0.19.5", default-features = false, optional = true } -cargo-xwin = { version = "0.17.4", default-features = false, optional = true } +cargo-xwin = { version = "0.17.5", default-features = false, optional = true } # log tracing = "0.1.36" diff --git a/guide/src/environment-variables.md b/guide/src/environment-variables.md index 1de6e5e64..42b370bd3 100644 --- a/guide/src/environment-variables.md +++ b/guide/src/environment-variables.md @@ -34,5 +34,6 @@ See [environment variables Cargo reads](https://doc.rust-lang.org/cargo/referenc * `SOURCE_DATE_EPOCH`: The time to use for the timestamp in the wheel metadata * `MATURIN_EMSCRIPTEN_VERSION`: The version of emscripten to use for emscripten builds * `MATURIN_NO_MISSING_BUILD_BACKEND_WARNING`: Suppress missing build backend warning +* `MATURIN_USE_XWIN`: Set to `1` to force to use `xwin` for cross compiling even on Windows that supports native compilation * `TARGET_SYSROOT`: The sysroot to use for auditwheel wheel when cross compiling * `ARCHFLAGS`: Flags to control the architecture of the build on macOS, for example you can use `ARCHFLAGS="-arch x86_64 -arch arm64"` to build universal2 wheels diff --git a/src/compile.rs b/src/compile.rs index cc6a84b42..1e724bcf2 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -287,9 +287,12 @@ fn cargo_build_command( .extend(["-C".to_string(), "strip=symbols".to_string()]); } - let mut build_command = if target.is_msvc() && target.cross_compiling() { + let mut build_command = if target.is_msvc() + && (target.cross_compiling() || env::var("MATURIN_USE_XWIN").ok().as_deref() == Some("1")) + { #[cfg(feature = "xwin")] { + println!("🛠️ Using xwin for cross-compiling to {target_triple}"); let xwin_options = { use clap::Parser; @@ -320,6 +323,7 @@ fn cargo_build_command( build.target = vec![target_triple.to_string()]; } } else { + println!("🛠️ Using zig for cross-compiling to {target_triple}"); build.enable_zig_ar = true; let zig_triple = if target.is_linux() && !target.is_musl_libc() { match context.platform_tag.iter().find(|tag| tag.is_manylinux()) { diff --git a/src/cross_compile.rs b/src/cross_compile.rs index 57428015b..3b62a7233 100644 --- a/src/cross_compile.rs +++ b/src/cross_compile.rs @@ -27,6 +27,21 @@ pub fn is_cross_compiling(target: &Target) -> Result { // Not cross-compiling to compile for 32-bit Python from windows 64-bit return Ok(false); } + if (target_triple == "aarch64-pc-windows-msvc" && host == "x86_64-pc-windows-msvc") + || (target_triple == "x86_64-pc-windows-msvc" && host == "aarch64-pc-windows-msvc") + { + // Not cross-compiling if the Windows MSVC compiler can compile to the target + let native_compile = cc::Build::new() + .opt_level(0) + .host(host) + .target(target_triple) + .cargo_metadata(false) + .cargo_warnings(false) + .cargo_output(false) + .try_get_compiler() + .is_ok(); + return Ok(!native_compile); + } if target_triple.ends_with("windows-gnu") && host.ends_with("windows-msvc") { // Not cross-compiling to compile for Windows GNU from Windows MSVC host return Ok(false);