diff options
author | GustavoLCR <gugulcr@gmail.com> | 2022-09-14 01:34:46 -0300 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2022-10-25 14:59:06 +0300 |
commit | 32bc64e63210cee6df7364a39005d89cdfdc6b71 (patch) | |
tree | 281c411c322547f9ca45fe14a62795e03eef3da1 /mesonbuild/utils | |
parent | 3c0ac626d7bbf667762b72a8b075617763e17795 (diff) | |
download | meson-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')
-rw-r--r-- | mesonbuild/utils/universal.py | 37 | ||||
-rw-r--r-- | mesonbuild/utils/vsenv.py | 9 |
2 files changed, 42 insertions, 4 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 = [ diff --git a/mesonbuild/utils/vsenv.py b/mesonbuild/utils/vsenv.py index 5f32990..47055a0 100644 --- a/mesonbuild/utils/vsenv.py +++ b/mesonbuild/utils/vsenv.py @@ -2,12 +2,11 @@ import os import subprocess import json import pathlib -import platform import shutil import tempfile from .. import mlog -from .universal import MesonException, is_windows +from .universal import MesonException, is_windows, windows_detect_native_arch __all__ = [ @@ -72,8 +71,10 @@ def _setup_vsenv(force: bool) -> bool: # VS installer instelled but not VS itself maybe? raise MesonException('Could not parse vswhere.exe output') bat_root = pathlib.Path(bat_info[0]['installationPath']) - if platform.machine() == 'ARM64': - bat_path = bat_root / 'VC/Auxiliary/Build/vcvarsx86_arm64.bat' + if windows_detect_native_arch() == 'arm64': + bat_path = bat_root / 'VC/Auxiliary/Build/vcvarsarm64.bat' + if not bat_path.exists(): + bat_path = bat_root / 'VC/Auxiliary/Build/vcvarsx86_arm64.bat' else: bat_path = bat_root / 'VC/Auxiliary/Build/vcvars64.bat' # if VS is not found try VS Express |