Skip to content

Commit 5194dd7

Browse files
author
Andrew Pearce
committed
fix(RustInterop): add fallback architecture detection for older systems
1 parent 4c2c2b4 commit 5194dd7

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

src/Convert/Private/RustInterop.ps1

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,40 @@
55
$ErrorActionPreference = 'Stop'
66

77
# Detect architecture (x64, ARM64, or x86)
8+
# Try RuntimeInformation first, fall back to environment/pointer size for older systems
9+
$architecture = $null
10+
811
$runtimeArch = [System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture
9-
$architecture = switch ($runtimeArch) {
10-
([System.Runtime.InteropServices.Architecture]::X64) { 'x64' }
11-
([System.Runtime.InteropServices.Architecture]::Arm64) { 'arm64' }
12-
([System.Runtime.InteropServices.Architecture]::X86) { 'x86' }
13-
([System.Runtime.InteropServices.Architecture]::Arm) { 'arm' }
14-
default { throw "Unsupported architecture: $runtimeArch" }
12+
if ($null -ne $runtimeArch) {
13+
$architecture = switch ($runtimeArch) {
14+
([System.Runtime.InteropServices.Architecture]::X64) { 'x64' }
15+
([System.Runtime.InteropServices.Architecture]::Arm64) { 'arm64' }
16+
([System.Runtime.InteropServices.Architecture]::X86) { 'x86' }
17+
([System.Runtime.InteropServices.Architecture]::Arm) { 'arm' }
18+
}
19+
}
20+
21+
# Fallback detection when RuntimeInformation is unavailable or returns null
22+
if ($null -eq $architecture) {
23+
$processorArch = $env:PROCESSOR_ARCHITECTURE
24+
if ($processorArch -eq 'AMD64') {
25+
$architecture = 'x64'
26+
} elseif ($processorArch -eq 'ARM64') {
27+
$architecture = 'arm64'
28+
} elseif ($processorArch -eq 'x86') {
29+
$architecture = 'x86'
30+
} elseif ($processorArch -eq 'ARM') {
31+
$architecture = 'arm'
32+
} else {
33+
# Last resort: use pointer size to distinguish 32-bit vs 64-bit
34+
if ([IntPtr]::Size -eq 8) {
35+
$architecture = 'x64'
36+
} elseif ([IntPtr]::Size -eq 4) {
37+
$architecture = 'x86'
38+
} else {
39+
throw "Unable to detect architecture. PROCESSOR_ARCHITECTURE='$processorArch', IntPtr.Size=$([IntPtr]::Size)"
40+
}
41+
}
1542
}
1643

1744
# Determine library filename based on platform

0 commit comments

Comments
 (0)