--- mesonbuild/envconfig.py.orig	2026-01-06 07:17:02
+++ mesonbuild/envconfig.py	2026-01-06 07:19:57
@@ -8,6 +8,7 @@
 from enum import Enum
 import os
 import platform
+import subprocess
 import sys
 
 from . import mesonlib
@@ -557,6 +558,21 @@
         if compiler.id == 'gcc' and compiler.has_builtin_define('__i386__'):
             return 'x86'
     return os_arch
+
+def detect_osx_arch() -> str:
+    """
+    per #6187, handle early Mac 64-bit Intel CPU with 64-bit OSX using a **32-bit kernel**
+    testing this requires old MacOS and hardware, not easily available for cloud CI,
+    so users needing this functionality may kindly need to help with debugging info.
+    """
+    try:
+        ret = subprocess.run(['sysctl', '-n', 'hw.cpu64bit_capable'],
+                             universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout
+        trial = 'x86_64' if ret.strip() == '1' else 'x86'
+    except subprocess.SubprocessError:
+        # very old MacOS version with implicit 32-bit CPU due to calling if-elif stack
+        trial = 'x86'
+    return trial
 
 def any_compiler_has_define(compilers: T.Dict[str, Compiler], define: str) -> bool:
     for c in compilers.values():
@@ -582,7 +598,11 @@
     else:
         trial = platform.machine().lower()
     if trial.startswith('i') and trial.endswith('86'):
-        trial = 'x86'
+        if mesonlib.is_osx():
+            # handle corner case with early Mac 64-bit CPU and older OSX
+            trial = detect_osx_arch()
+        else:
+            trial = 'x86'
     elif trial == 'bepc':
         trial = 'x86'
     elif trial == 'arm64':
