Skip to content

Commit 1c6b5c5

Browse files
committed
Fix detection of aarch64 host on Windows
1 parent 3277323 commit 1c6b5c5

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ features = [
9595
"shlobj",
9696
"shtypes",
9797
"synchapi",
98-
"sysinfoapi",
9998
"tlhelp32",
10099
"userenv",
101100
"winbase",
102101
"winerror",
103102
"winioctl",
104103
"winnt",
105104
"winuser",
105+
"wow64apiset",
106106
]
107107
version = "0.3"
108108

src/dist/dist.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -228,24 +228,30 @@ impl TargetTriple {
228228
pub(crate) fn from_host() -> Option<Self> {
229229
#[cfg(windows)]
230230
fn inner() -> Option<TargetTriple> {
231-
use std::mem;
232-
use winapi::um::sysinfoapi::GetNativeSystemInfo;
231+
use winapi::um::processthreadsapi::GetCurrentProcess;
232+
use winapi::um::wow64apiset::IsWow64Process2;
233233

234234
// First detect architecture
235-
const PROCESSOR_ARCHITECTURE_AMD64: u16 = 9;
236-
const PROCESSOR_ARCHITECTURE_INTEL: u16 = 0;
237-
const PROCESSOR_ARCHITECTURE_ARM64: u16 = 12;
238-
239-
let mut sys_info;
240-
unsafe {
241-
sys_info = mem::zeroed();
242-
GetNativeSystemInfo(&mut sys_info);
243-
}
235+
const IMAGE_FILE_MACHINE_ARM64: u16 = 0xAA64;
236+
const IMAGE_FILE_MACHINE_AMD64: u16 = 0x8664;
237+
const IMAGE_FILE_MACHINE_I386: u16 = 0x014c;
238+
239+
let native_machine = unsafe {
240+
// cannot fail; handle does not need to be closed.
241+
let process = GetCurrentProcess();
242+
let mut _machine = 0;
243+
let mut native_machine = 0;
244+
// native_machine is the native architecture of host system.
245+
if IsWow64Process2(process, &mut _machine, &mut native_machine) == 0 {
246+
return None;
247+
}
248+
native_machine
249+
};
244250

245-
let arch = match unsafe { sys_info.u.s() }.wProcessorArchitecture {
246-
PROCESSOR_ARCHITECTURE_AMD64 => "x86_64",
247-
PROCESSOR_ARCHITECTURE_INTEL => "i686",
248-
PROCESSOR_ARCHITECTURE_ARM64 => "aarch64",
251+
let arch = match native_machine {
252+
IMAGE_FILE_MACHINE_AMD64 => "x86_64",
253+
IMAGE_FILE_MACHINE_I386 => "i686",
254+
IMAGE_FILE_MACHINE_ARM64 => "aarch64",
249255
_ => return None,
250256
};
251257

@@ -328,12 +334,12 @@ impl TargetTriple {
328334
let ret = if partial_self.os != partial_other.os {
329335
false
330336
} else if partial_self.os.as_deref() == Some("pc-windows") {
331-
// Windows is a special case here, we know we can run 32bit on 64bit
332-
// and we know we can run gnu and msvc on the same system
333-
// We don't immediately assume we can cross between x86 and aarch64 though
337+
// Windows is a special case here: we can run gnu and msvc on the same system,
338+
// i686 can run on x86_64, and both x86_64 and i686 can run on aarch64 through emulation
334339
(partial_self.arch == partial_other.arch)
335340
|| (partial_self.arch.as_deref() == Some("x86_64")
336341
&& partial_other.arch.as_deref() == Some("i686"))
342+
|| partial_self.arch.as_deref() == Some("aarch64")
337343
} else {
338344
// For other OSes, for now, we assume other toolchains won't run
339345
false

0 commit comments

Comments
 (0)