From 97b062503185cbafaf80cb5ec1da1c1c953411e1 Mon Sep 17 00:00:00 2001
From: barracuda156 <vital.had@gmail.com>
Date: Tue, 8 Aug 2023 19:03:03 +0800
Subject: [PATCH] Fix libuv

diff --git src/unix/fs.c src/unix/fs.c
index 6b051c12..18e274cf 100644
--- 3rdparty/libuv/src/unix/fs.c
+++ 3rdparty/libuv/src/unix/fs.c
@@ -1079,7 +1079,7 @@
     return -1;
   }
 /* sendfile() on iOS(arm64) will throw SIGSYS signal cause crash. */
-#elif (defined(__APPLE__) && !TARGET_OS_IPHONE)                               \
+#elif (defined(__APPLE__) && (!TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= 1050))) \
     || defined(__DragonFly__)                                                 \
     || defined(__FreeBSD__)
   {
@@ -1458,7 +1458,7 @@
   dst->st_blksize = src->st_blksize;
   dst->st_blocks = src->st_blocks;
 
-#if defined(__APPLE__)
+#if defined(__APPLE__) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1050)
   dst->st_atim.tv_sec = src->st_atimespec.tv_sec;
   dst->st_atim.tv_nsec = src->st_atimespec.tv_nsec;
   dst->st_mtim.tv_sec = src->st_mtimespec.tv_sec;
diff --git src/unix/process.c src/unix/process.c
index dd58c18d..35aa9b1b 100644
--- 3rdparty/libuv/src/unix/process.c
+++ 3rdparty/libuv/src/unix/process.c
@@ -36,7 +36,9 @@
 #include <poll.h>
 
 #if defined(__APPLE__)
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
 # include <spawn.h>
+#endif
 # include <paths.h>
 # include <sys/kauth.h>
 # include <sys/types.h>
@@ -387,7 +389,7 @@
 #endif
 
 
-#if defined(__APPLE__)
+#if defined(__APPLE__) && (MAC_OS_X_VERSION_MIN_REQUIRED >= 1050)
 typedef struct uv__posix_spawn_fncs_tag {
   struct {
     int (*addchdir_np)(const posix_spawn_file_actions_t *, const char *);
@@ -588,9 +590,11 @@
       }
     }
 
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
     if (fd == use_fd)
         err = posix_spawn_file_actions_addinherit_np(actions, fd);
     else
+#endif
         err = posix_spawn_file_actions_adddup2(actions, use_fd, fd);
     assert(err != ENOSYS);
     if (err != 0)
@@ -839,7 +843,7 @@
   int exec_errorno;
   ssize_t r;
 
-#if defined(__APPLE__)
+#if defined(__APPLE__) && (MAC_OS_X_VERSION_MIN_REQUIRED >= 1050)
   uv_once(&posix_spawn_init_once, uv__spawn_init_posix_spawn);
 
   /* Special child process spawn case for macOS Big Sur (11.0) onwards
diff --git src/unix/tty.c src/unix/tty.c
index d099bdb3..899e3a66 100644
--- 3rdparty/libuv/src/unix/tty.c
+++ 3rdparty/libuv/src/unix/tty.c
@@ -85,7 +85,7 @@
   int dummy;
 
   result = ioctl(fd, TIOCGPTN, &dummy) != 0;
-#elif defined(__APPLE__)
+#elif defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
   char dummy[256];
 
   result = ioctl(fd, TIOCPTYGNAME, &dummy) != 0;
diff --git src/unix/udp.c src/unix/udp.c
index c2814512..cba9e821 100644
--- 3rdparty/libuv/src/unix/udp.c
+++ 3rdparty/libuv/src/unix/udp.c
@@ -938,6 +938,7 @@
     !defined(__ANDROID__) &&                                        \
     !defined(__DragonFly__) &&                                      \
     !defined(__QNX__) &&                                            \
+    (!defined(__APPLE__) || (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)) && \
     !defined(__GNU__)
 static int uv__udp_set_source_membership4(uv_udp_t* handle,
                                           const struct sockaddr_in* multicast_addr,
@@ -1131,6 +1132,7 @@
     !defined(__ANDROID__) &&                                        \
     !defined(__DragonFly__) &&                                      \
     !defined(__QNX__) &&                                            \
+    (!defined(__APPLE__) || (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)) && \
     !defined(__GNU__)
   int err;
   union uv__sockaddr mcast_addr;


# Fix breakage from https://github.com/libuv/libuv/commit/c0a61c3bb323724532fa9c1ac190afb36e4ae264
--- 3rdparty/libuv/src/unix/darwin.c	2024-12-15 01:06:02.000000000 +0800
+++ 3rdparty/libuv/src/unix/darwin.c	2025-01-03 14:40:05.000000000 +0800
@@ -25,6 +25,7 @@
 #include <stdint.h>
 #include <errno.h>
 
+#include <dlfcn.h>
 #include <mach/mach.h>
 #include <mach/mach_time.h>
 #include <mach-o/dyld.h> /* _NSGetExecutablePath */
@@ -33,6 +34,7 @@
 #include <unistd.h>  /* sysconf */
 
 static uv_once_t once = UV_ONCE_INIT;
+static uint64_t (*time_func)(void);
 static mach_timebase_info_data_t timebase;
 
 
@@ -54,12 +56,16 @@
 static void uv__hrtime_init_once(void) {
   if (KERN_SUCCESS != mach_timebase_info(&timebase))
     abort();
+
+  time_func = (uint64_t (*)(void)) dlsym(RTLD_DEFAULT, "mach_continuous_time");
+  if (time_func == NULL)
+    time_func = mach_absolute_time;
 }
 
 
 uint64_t uv__hrtime(uv_clocktype_t type) {
   uv_once(&once, uv__hrtime_init_once);
-  return mach_continuous_time() * timebase.numer / timebase.denom;
+  return time_func() * timebase.numer / timebase.denom;
 }
 
 
Fix breakage from https://github.com/libuv/libuv/commit/1c778bd001543371c915a79b7ac3c5864fe59e74
--- 3rdparty/libuv/src/unix/udp.c.orig	2025-02-15 22:42:34.000000000 +0800
+++ 3rdparty/libuv/src/unix/udp.c	2025-02-15 22:56:05.000000000 +0800
@@ -152,7 +152,7 @@
 }
 
 static int uv__udp_recvmmsg(uv_udp_t* handle, uv_buf_t* buf) {
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
+#if defined(__linux__) || defined(__FreeBSD__) || (defined(__APPLE__) && (MAC_OS_X_VERSION_MIN_REQUIRED >= 101000))
   struct sockaddr_in6 peers[20];
   struct iovec iov[ARRAY_SIZE(peers)];
   struct mmsghdr msgs[ARRAY_SIZE(peers)];
@@ -215,9 +215,9 @@
       handle->recv_cb(handle, 0, buf, NULL, UV_UDP_MMSG_FREE);
   }
   return nread;
-#else  /* __linux__ || ____FreeBSD__ || __APPLE__ */
+#else  /* __linux__ || __FreeBSD__ || (defined(__APPLE__) && (MAC_OS_X_VERSION_MIN_REQUIRED >= 101000)) */
   return UV_ENOSYS;
-#endif  /* __linux__ || ____FreeBSD__ || __APPLE__ */
+#endif  /* __linux__ || __FreeBSD__ || (defined(__APPLE__) && (MAC_OS_X_VERSION_MIN_REQUIRED >= 101000)) */
 }
 
 static void uv__udp_recvmsg(uv_udp_t* handle) {
@@ -878,7 +878,7 @@
 
 
 int uv_udp_using_recvmmsg(const uv_udp_t* handle) {
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
+#if defined(__linux__) || defined(__FreeBSD__) || (defined(__APPLE__) && (MAC_OS_X_VERSION_MIN_REQUIRED >= 1070))
   if (handle->flags & UV_HANDLE_UDP_RECVMMSG)
     return 1;
 #endif
@@ -1300,7 +1300,7 @@
   r = 0;
   nsent = 0;
 
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
+#if defined(__linux__) || defined(__FreeBSD__) || (defined(__APPLE__) && (MAC_OS_X_VERSION_MIN_REQUIRED >= 101000))
   if (count > 1) {
     for (i = 0; i < count; /*empty*/) {
       struct mmsghdr m[20];
@@ -1327,7 +1327,7 @@
 
     goto exit;
   }
-#endif  /* defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) */
+#endif  /* defined(__linux__) || defined(__FreeBSD__) || (defined(__APPLE__) && (MAC_OS_X_VERSION_MIN_REQUIRED >= 101000)) */
 
   for (i = 0; i < count; i++, nsent++)
     if ((r = uv__udp_sendmsg1(fd, bufs[i], nbufs[i], addrs[i])))
