Skip to content

Commit e1d02c9

Browse files
committed
Purge the other non-working windows-arm64 strategy
The x64 emulation layer is too thorough. Let's try something else.
1 parent adc9fda commit e1d02c9

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/windowsMain/kotlin/platform.kt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ actual val TARGET_ARCH = detectNativeCpuArch()
77

88
@OptIn(ExperimentalForeignApi::class)
99
private fun detectNativeCpuArch(): String {
10-
try {
11-
memScoped {
12-
val systemInfo = alloc<SYSTEM_INFO>()
13-
GetNativeSystemInfo(systemInfo.ptr)
14-
15-
return when (systemInfo.wProcessorArchitecture.toInt()) {
16-
PROCESSOR_ARCHITECTURE_AMD64 -> "X64"
17-
PROCESSOR_ARCHITECTURE_ARM64 -> "ARM64"
18-
PROCESSOR_ARCHITECTURE_INTEL -> "X86"
19-
else -> /*CPU_ARCH*/"UNKNOWN-${systemInfo.wProcessorArchitecture.toInt()}"
10+
// When running in x64 emulation mode:
11+
// * CPU_ARCH is X64.
12+
// * The PROCESSOR_ARCHITECTURE environment variable is AMD64.
13+
// * The GetNativeSystemInfo function's wProcessorArchitecture field is PROCESSOR_ARCHITECTURE_AMD64.
14+
// So it ends up being quite tricky to realize we are actually on an ARM64 machine.
15+
// Good old Windows registry to the rescue!
16+
val regResult = execute("reg query \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /v PROCESSOR_ARCHITECTURE")
17+
if (regResult == null) return CPU_ARCH
18+
for (line in regResult) {
19+
val parts = line.trim().split(Regex("\\s+"))
20+
if (parts.size >= 3 && parts[0] == "PROCESSOR_ARCHITECTURE" && parts[1] == "REG_SZ") {
21+
return when (parts[2].uppercase()) {
22+
"ARM64" -> "ARM64"
23+
"AMD64", "X64" -> "X64"
24+
"X86" -> "X86"
25+
else -> CPU_ARCH
2026
}
2127
}
22-
} catch (e: Exception) {
23-
debug(e)
24-
return CPU_ARCH
2528
}
29+
return CPU_ARCH
2630
}
2731

2832
@OptIn(ExperimentalForeignApi::class)

0 commit comments

Comments
 (0)