From ac50612f4448d329ff68bfc35f25161cb42c0510 Mon Sep 17 00:00:00 2001
Date: Mon, 17 May 2021 17:14:45 -0700
Subject: [PATCH 24/24] 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, 13 insertions(+)

diff --git a/compiler-rt/lib/builtins/os_version_check.c b/compiler-rt/lib/builtins/os_version_check.c
index d7194b99ae54..57a065b7da48 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 MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
 #include <dispatch/dispatch.h>
+#endif
 #include <dlfcn.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -23,8 +26,10 @@
 
 // These three variables hold the host's OS version.
 static int32_t GlobalMajor, GlobalMinor, GlobalSubminor;
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
 static dispatch_once_t DispatchOnceCounter;
 static dispatch_once_t CompatibilityDispatchOnceCounter;
+#endif
 
 // _availability_version_check darwin API support.
 typedef uint32_t dyld_platform_t;
@@ -247,8 +252,14 @@ static void initializeAvailabilityCheck(void *Unused) {
 // usable when linked with new compiler-rt.
 int32_t __isOSVersionAtLeast(int32_t Major, int32_t Minor, int32_t Subminor) {
   // Populate the global version variables, if they haven't already.
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
   dispatch_once_f(&CompatibilityDispatchOnceCounter, NULL,
                   compatibilityInitializeAvailabilityCheck);
+#else
+  /* expensive procedure, only do once. GlobalMajor will not be 0 once run. */
+  if (GlobalMajor == 0)
+    _initializeAvailabilityCheck(/*LoadPlist=*/true);
+#endif
 
   if (Major < GlobalMajor)
     return 1;
@@ -268,7 +279,9 @@ static inline uint32_t ConstructVersion(uint32_t Major, uint32_t Minor,
 
 int32_t __isPlatformVersionAtLeast(uint32_t Platform, uint32_t Major,
                                    uint32_t Minor, uint32_t Subminor) {
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
   dispatch_once_f(&DispatchOnceCounter, NULL, initializeAvailabilityCheck);
+#endif
 
   if (!AvailabilityVersionCheck) {
     return __isOSVersionAtLeast(Major, Minor, Subminor);
-- 
2.21.1 (Apple Git-122.3)

