From b4c19d923fa1fcff5c08d5bdd211997a1e4ba9a7 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill@korins.ky>
Date: Wed, 17 Aug 2022 14:03:50 +0200
Subject: [PATCH] 10.5 and less: compiler-rt work around no libdispatch before
 10.6

---
 compiler-rt/lib/builtins/os_version_check.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/builtins/os_version_check.c b/compiler-rt/lib/builtins/os_version_check.c
index 3794b979434c..553e809a492c 100644
--- a/compiler-rt/lib/builtins/os_version_check.c
+++ b/compiler-rt/lib/builtins/os_version_check.c
@@ -13,8 +13,11 @@
 
 #ifdef __APPLE__
 
+#include <AvailabilityMacros.h>
 #include <TargetConditionals.h>
+#if (__APPLE__ && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060)
 #include <dispatch/dispatch.h>
+#endif
 #include <dlfcn.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -23,7 +26,9 @@
 
 // These three variables hold the host's OS version.
 static int32_t GlobalMajor, GlobalMinor, GlobalSubminor;
-static dispatch_once_t DispatchOnceCounter;
+#if (__APPLE__ && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060)
+ static dispatch_once_t DispatchOnceCounter;
+#endif
 
 // We can't include <CoreFoundation/CoreFoundation.h> directly from here, so
 // just forward declare everything that we need from it.
@@ -203,7 +208,13 @@ Fail:
 
 int32_t __isOSVersionAtLeast(int32_t Major, int32_t Minor, int32_t Subminor) {
   // Populate the global version variables, if they haven't already.
+#if (__APPLE__ && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060)
   dispatch_once_f(&DispatchOnceCounter, NULL, parseSystemVersionPList);
+#else
+  /* expensive procedure, only do once. GlobalMajor will not be 0 once run. */
+  if (GlobalMajor == 0)
+    parseSystemVersionPList(NULL);
+#endif
 
   if (Major < GlobalMajor)
     return 1;
-- 
2.37.2

