From f70ce352ace5db2f0077df9a07f0954ecf2aeec8 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 3 Dec 2024 20:37:18 +0800 Subject: [PATCH] Fix aarch64 Windows cross compilation (#2359) --- src/compile.rs | 46 +++++++++++++++++++++---------- src/cross_compile.rs | 15 ---------- test-crates/pyo3-mixed/Cargo.lock | 4 +-- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/compile.rs b/src/compile.rs index 1e724bcf2..ba85e6a85 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -287,24 +287,40 @@ fn cargo_build_command( .extend(["-C".to_string(), "strip=symbols".to_string()]); } - let mut build_command = if target.is_msvc() - && (target.cross_compiling() || env::var("MATURIN_USE_XWIN").ok().as_deref() == Some("1")) - { + let mut build_command = if target.is_msvc() && target.cross_compiling() { #[cfg(feature = "xwin")] { - println!("🛠️ Using xwin for cross-compiling to {target_triple}"); - let xwin_options = { - use clap::Parser; - - // This will populate the default values for the options - // and then override them with cargo-xwin environment variables. - cargo_xwin::XWinOptions::parse_from(Vec::<&str>::new()) - }; + // Don't use xwin if the Windows MSVC compiler can compile to the target + let native_compile = cc::Build::new() + .opt_level(0) + .host(target.host_triple()) + .target(target_triple) + .cargo_metadata(false) + .cargo_warnings(false) + .cargo_output(false) + .try_get_compiler() + .is_ok(); + let force_xwin = env::var("MATURIN_USE_XWIN").ok().as_deref() == Some("1"); + if !native_compile || force_xwin { + println!("🛠️ Using xwin for cross-compiling to {target_triple}"); + let xwin_options = { + use clap::Parser; + + // This will populate the default values for the options + // and then override them with cargo-xwin environment variables. + cargo_xwin::XWinOptions::parse_from(Vec::<&str>::new()) + }; - let mut build = cargo_xwin::Rustc::from(cargo_rustc); - build.target = vec![target_triple.to_string()]; - build.xwin = xwin_options; - build.build_command()? + let mut build = cargo_xwin::Rustc::from(cargo_rustc); + build.target = vec![target_triple.to_string()]; + build.xwin = xwin_options; + build.build_command()? + } else { + if target.user_specified { + cargo_rustc.target = vec![target_triple.to_string()]; + } + cargo_rustc.command() + } } #[cfg(not(feature = "xwin"))] { diff --git a/src/cross_compile.rs b/src/cross_compile.rs index 3b62a7233..57428015b 100644 --- a/src/cross_compile.rs +++ b/src/cross_compile.rs @@ -27,21 +27,6 @@ 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); diff --git a/test-crates/pyo3-mixed/Cargo.lock b/test-crates/pyo3-mixed/Cargo.lock index a182a107d..a2084b10d 100644 --- a/test-crates/pyo3-mixed/Cargo.lock +++ b/test-crates/pyo3-mixed/Cargo.lock @@ -144,9 +144,9 @@ dependencies = [ [[package]] name = "python3-dll-a" -version = "0.2.9" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f07cd4412be8fa09a721d40007c483981bbe072cd6a21f2e83e04ec8f8343f" +checksum = "9b9e268ee1be609e93a13eb06839f68f67e5fe0fb4049834d261c2d5091c1b6d" dependencies = [ "cc", ]