From ab9433d7054e3e599976d1831f9d32469b72965e Mon Sep 17 00:00:00 2001
From: Wowfunhappy <Wowfunhappy@gmail.com>
Date: Sun, 30 May 2021 15:28:58 -0400
Subject: [PATCH] Revert changes to qoperatingsystemversion_darwin.mm

Very fuzzy on whether this is actually necessary! QT builds and seems to work without it, _except_ when I try to add this patch to the MacPorts version of QT 5.9, as I attempted to do. Can't hurt?
---
 .../global/qoperatingsystemversion_darwin.mm       | 53 ++++++++++++++++++++--
 1 file changed, 49 insertions(+), 4 deletions(-)

diff --git src/corelib/global/qoperatingsystemversion_darwin.mm src/corelib/global/qoperatingsystemversion_darwin.mm
index d8b927ff5d..3dd007cbb3 100644
--- src/corelib/global/qoperatingsystemversion_darwin.mm
+++ src/corelib/global/qoperatingsystemversion_darwin.mm
@@ -40,16 +40,61 @@
 #include "qoperatingsystemversion_p.h"
 #import <Foundation/Foundation.h>
 
+#ifdef Q_OS_IOS
+#import <UIKit/UIKit.h>
+#endif
+
 QT_BEGIN_NAMESPACE
 
+typedef qint16 (*GestaltFunction)(quint32 selector, qint32 *response);
+
 QOperatingSystemVersion QOperatingSystemVersion::current()
 {
-    NSOperatingSystemVersion osv = NSProcessInfo.processInfo.operatingSystemVersion;
     QOperatingSystemVersion v;
     v.m_os = currentType();
-    v.m_major = osv.majorVersion;
-    v.m_minor = osv.minorVersion;
-    v.m_micro = osv.patchVersion;
+    v.m_major = -1;
+    v.m_minor = -1;
+    v.m_micro = -1;
+#if QT_MACOS_IOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_10, __IPHONE_8_0) || defined(Q_OS_TVOS) || defined(Q_OS_WATCHOS)
+    if ([NSProcessInfo instancesRespondToSelector:@selector(operatingSystemVersion)]) {
+        NSOperatingSystemVersion osv = NSProcessInfo.processInfo.operatingSystemVersion;
+        v.m_major = osv.majorVersion;
+        v.m_minor = osv.minorVersion;
+        v.m_micro = osv.patchVersion;
+        return v;
+    }
+#endif
+    // Use temporary variables so we can return 0.0.0 (unknown version)
+    // in case of an error partway through determining the OS version
+    qint32 major = 0, minor = 0, patch = 0;
+#if QT_MACOS_IOS_DEPLOYMENT_TARGET_BELOW(__MAC_10_10, __IPHONE_8_0)
+#if defined(Q_OS_IOS)
+    @autoreleasepool {
+        NSArray *parts = [UIDevice.currentDevice.systemVersion componentsSeparatedByString:@"."];
+        major = parts.count > 0 ? [[parts objectAtIndex:0] intValue] : 0;
+        minor = parts.count > 1 ? [[parts objectAtIndex:1] intValue] : 0;
+        patch = parts.count > 2 ? [[parts objectAtIndex:2] intValue] : 0;
+    }
+#elif defined(Q_OS_MACOS)
+    static GestaltFunction pGestalt = 0;
+    if (!pGestalt) {
+        CFBundleRef b = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.CoreServices"));
+        pGestalt = reinterpret_cast<GestaltFunction>(CFBundleGetFunctionPointerForName(b,
+                                                     CFSTR("Gestalt")));
+    }
+    if (!pGestalt)
+        return v;
+    if (pGestalt('sys1', &major) != 0)
+        return v;
+    if (pGestalt('sys2', &minor) != 0)
+        return v;
+    if (pGestalt('sys3', &patch) != 0)
+        return v;
+#endif
+#endif
+    v.m_major = major;
+    v.m_minor = minor;
+    v.m_micro = patch;
     return v;
 }
 
-- 
2.14.1

