aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/utils/universal.py
diff options
context:
space:
mode:
authorGustavoLCR <gugulcr@gmail.com>2022-09-14 01:34:46 -0300
committerJussi Pakkanen <jpakkane@gmail.com>2022-10-25 14:59:06 +0300
commit32bc64e63210cee6df7364a39005d89cdfdc6b71 (patch)
tree281c411c322547f9ca45fe14a62795e03eef3da1 /mesonbuild/utils/universal.py
parent3c0ac626d7bbf667762b72a8b075617763e17795 (diff)
downloadmeson-32bc64e63210cee6df7364a39005d89cdfdc6b71.zip
meson-32bc64e63210cee6df7364a39005d89cdfdc6b71.tar.gz
meson-32bc64e63210cee6df7364a39005d89cdfdc6b71.tar.bz2
Fix native compilation on ARM64 Windows
Move `detect_native_windows_arch()` to `mesonlib/universal.py` and rename it to `windows_detect_native_arch()` Use `IsWow64Process2()` to detect native architecture if available Use native `vcvarsarm64.bat` to initialize vsenv if available
Diffstat (limited to 'mesonbuild/utils/universal.py')
-rw-r--r--mesonbuild/utils/universal.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/mesonbuild/utils/universal.py b/mesonbuild/utils/universal.py
index aaefbc5..0c611bb 100644
--- a/mesonbuild/utils/universal.py
+++ b/mesonbuild/utils/universal.py
@@ -17,6 +17,7 @@
from __future__ import annotations
from pathlib import Path
import argparse
+import ctypes
import enum
import sys
import stat
@@ -141,6 +142,7 @@ __all__ = [
'version_compare_condition_with_min',
'version_compare_many',
'search_version',
+ 'windows_detect_native_arch',
'windows_proof_rm',
'windows_proof_rmtree',
]
@@ -689,6 +691,41 @@ def darwin_get_object_archs(objpath: str) -> 'ImmutableListProtocol[str]':
stdo += ' arm'
return stdo.split()
+def windows_detect_native_arch() -> str:
+ """
+ The architecture of Windows itself: x86, amd64 or arm64
+ """
+ if sys.platform != 'win32':
+ return ''
+ try:
+ process_arch = ctypes.c_ushort()
+ native_arch = ctypes.c_ushort()
+ kernel32 = ctypes.windll.kernel32
+ process = ctypes.c_void_p(kernel32.GetCurrentProcess())
+ # This is the only reliable way to detect an arm system if we are an x86/x64 process being emulated
+ if kernel32.IsWow64Process2(process, ctypes.byref(process_arch), ctypes.byref(native_arch)):
+ # https://docs.microsoft.com/en-us/windows/win32/sysinfo/image-file-machine-constants
+ if native_arch.value == 0x8664:
+ return 'amd64'
+ elif native_arch.value == 0x014C:
+ return 'x86'
+ elif native_arch.value == 0xAA64:
+ return 'arm64'
+ elif native_arch.value == 0x01C4:
+ return 'arm'
+ except (OSError, AttributeError):
+ pass
+ # These env variables are always available. See:
+ # https://msdn.microsoft.com/en-us/library/aa384274(VS.85).aspx
+ # https://blogs.msdn.microsoft.com/david.wang/2006/03/27/howto-detect-process-bitness/
+ arch = os.environ.get('PROCESSOR_ARCHITEW6432', '').lower()
+ if not arch:
+ try:
+ # If this doesn't exist, something is messing with the environment
+ arch = os.environ['PROCESSOR_ARCHITECTURE'].lower()
+ except KeyError:
+ raise EnvironmentException('Unable to detect native OS architecture')
+ return arch
def detect_vcs(source_dir: T.Union[str, Path]) -> T.Optional[T.Dict[str, str]]:
vcs_systems = [