SDL: cmake: port back Apple changes from SDL3

From 354a35c1d737b8a219d308738006d7d0552ed15b Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Mon, 12 Aug 2024 19:17:38 +0200
Subject: [PATCH] cmake: port back Apple changes from SDL3

---
 .github/workflows/main.yml |  6 +++++-
 CMakeLists.txt             | 28 ++++++++++++++--------------
 cmake/sdlchecks.cmake      |  2 +-
 cmake/sdlplatform.cmake    | 10 ++++++----
 4 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 21ea7de3f9466..7198dfb8bddc8 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -29,6 +29,7 @@ jobs:
 
         - { name: Ubuntu 22.04 (CMake),           os: ubuntu-22.04,   shell: sh }
         - { name: Ubuntu 22.04 (autotools),       os: ubuntu-22.04,   shell: sh,    autotools: true }
+        - { name: iOS (CMake),                    os: macos-latest,   shell: sh,    cmake: '-DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_ARCHITECTURES="arm64"', cross: true, test-pkg-config: false }
         - { name: MacOS (CMake),                  os: macos-latest,   shell: sh,    cmake: '-DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"' }
         - { name: MacOS (autotools),              os: macos-latest,   shell: sh,    autotools: true }
 
@@ -107,7 +108,7 @@ jobs:
         ${{ matrix.platform.source_cmd }}
         cmake --build build/ --config Release --verbose --parallel
     - name: Run build-time tests (CMake)
-      if: "! matrix.platform.autotools"
+      if: ${{ ! matrix.platform.autotools && !matrix.platform.cross }}
       run: |
         ${{ matrix.platform.source_cmd }}
         set -eu
@@ -197,14 +198,17 @@ jobs:
         ${{ matrix.platform.source_cmd }}
         cmake -S cmake/test -B cmake_config_build -G Ninja \
           -DCMAKE_BUILD_TYPE=Release \
+          ${{ matrix.platform.cmake }} \
           -DCMAKE_PREFIX_PATH=${{ env.SDL2_DIR }}
         cmake --build cmake_config_build --verbose
     - name: Verify sdl2-config
+      if: ${{ !!matrix.platform.test-pkg-config }}
       run: |
         ${{ matrix.platform.source_cmd }}
         export PATH=${{ env.SDL2_DIR }}/bin:$PATH
         cmake/test/test_sdlconfig.sh
     - name: Verify sdl2.pc
+      if: ${{ !!matrix.platform.test-pkg-config }}
       run: |
         ${{ matrix.platform.source_cmd }}
         export PKG_CONFIG_PATH=${{ env.SDL2_DIR }}/lib/pkgconfig
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a30a019a0c7af..514bd1192ffce 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -198,7 +198,7 @@ endif()
 #  so we'll just use libusb when it's available. libusb does not support iOS,
 #  so we default to yes on iOS.
 #  TODO: Windows can support libusb, the hid.c file just depends on Unix APIs
-if((WINDOWS AND NOT WINDOWS_STORE) OR IOS OR TVOS OR ANDROID)
+if((WINDOWS AND NOT WINDOWS_STORE) OR IOS OR TVOS OR VISIONOS OR WATCHOS OR ANDROID)
   set(HIDAPI_SKIP_LIBUSB TRUE)
 else()
   set(HIDAPI_SKIP_LIBUSB FALSE)
@@ -244,7 +244,7 @@ endif()
 if(UNIX OR MINGW OR MSYS OR (USE_CLANG AND NOT WINDOWS) OR VITA OR PSP OR PS2 OR N3DS OR SDL_CPU_ARM64EC)
   set(OPT_DEF_LIBC ON)
 endif()
-if(WINDOWS OR DARWIN OR MACOSX OR IOS OR TVOS)
+if(WINDOWS OR MACOS OR IOS OR TVOS OR VISIONOS OR WATCHOS)
   set(SDL_SYSTEM_ICONV_DEFAULT OFF)
 else()
   set(SDL_SYSTEM_ICONV_DEFAULT ON)
@@ -2076,7 +2076,7 @@ elseif(APPLE)
   # !!! FIXME: we need Carbon for some very old API calls in
   # !!! FIXME:  src/video/cocoa/SDL_cocoakeyboard.c, but we should figure out
   # !!! FIXME:  how to dump those.
-  if(DARWIN OR MACOSX)
+  if(MACOS)
     set(SDL_FRAMEWORK_COCOA 1)
     set(SDL_FRAMEWORK_CARBON 1)
   endif()
@@ -2090,12 +2090,12 @@ elseif(APPLE)
     set(HAVE_SDL_FILE TRUE)
   endif()
 
-  if(IOS OR TVOS)
+  if(IOS OR TVOS OR VISIONOS OR WATCHOS)
     file(GLOB SDLMAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/uikit/*.c)
   endif()
 
   if(SDL_MISC)
-    if(IOS OR TVOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       file(GLOB MISC_SOURCES ${SDL2_SOURCE_DIR}/src/misc/ios/*.m)
     else()
       file(GLOB MISC_SOURCES ${SDL2_SOURCE_DIR}/src/misc/macosx/*.m)
@@ -2120,10 +2120,10 @@ elseif(APPLE)
 
   if(SDL_JOYSTICK)
     file(GLOB MFI_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/iphoneos/*.m)
-    if(IOS OR TVOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/steam/*.c)
       set(SDL_JOYSTICK_MFI 1)
-      if(IOS)
+      if(IOS OR VISIONOS OR WATCHOS)
         set(SDL_FRAMEWORK_COREMOTION 1)
       endif()
       set(SDL_FRAMEWORK_GAMECONTROLLER 1)
@@ -2164,7 +2164,7 @@ elseif(APPLE)
   endif()
 
   if(SDL_HAPTIC)
-    if (IOS OR TVOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/dummy/*.c)
       set(SDL_HAPTIC_DUMMY 1)
     else()
@@ -2178,7 +2178,7 @@ elseif(APPLE)
   endif()
 
   if(SDL_POWER)
-    if (IOS OR TVOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/uikit/*.m)
       set(SDL_POWER_UIKIT 1)
     else()
@@ -2211,7 +2211,7 @@ elseif(APPLE)
   endif()
 
   if(SDL_SENSOR)
-    if(IOS)
+    if(IOS OR VISIONOS OR WATCHOS)
       set(SDL_SENSOR_COREMOTION 1)
       set(HAVE_SDL_SENSORS TRUE)
       file(GLOB SENSOR_SOURCES ${SDL2_SOURCE_DIR}/src/sensor/coremotion/*.m)
@@ -2221,7 +2221,7 @@ elseif(APPLE)
 
   # iOS hack needed - http://code.google.com/p/ios-cmake/ ?
   if(SDL_VIDEO)
-    if (IOS OR TVOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       set(SDL_VIDEO_DRIVER_UIKIT 1)
       set(SDL_FRAMEWORK_COREGRAPHICS 1)
       set(SDL_FRAMEWORK_QUARTZCORE 1)
@@ -2242,7 +2242,7 @@ elseif(APPLE)
     endif()
 
     if(SDL_OPENGLES)
-      if(IOS OR TVOS)
+      if(IOS OR TVOS OR VISIONOS OR WATCHOS)
         set(SDL_FRAMEWORK_OPENGLES 1)
         set(SDL_VIDEO_OPENGL_ES 1)
         set(SDL_VIDEO_RENDER_OGL_ES 1)
@@ -2345,7 +2345,7 @@ elseif(APPLE)
     endif()
   endif()
   if(SDL_FRAMEWORK_METAL)
-    if(IOS OR TVOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       list(APPEND PKGCONFIG_LDFLAGS "-Wl,-framework,Metal")
       list(APPEND CMAKE_LIBS "$<LINK_LIBRARY:FRAMEWORK,Metal>")
     else()
@@ -2358,7 +2358,7 @@ elseif(APPLE)
     list(APPEND CMAKE_LIBS "$<LINK_LIBRARY:FRAMEWORK,OpenGLES>")
   endif()
   if(SDL_FRAMEWORK_QUARTZCORE)
-    if(IOS OR TVOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       list(APPEND PKGCONFIG_LDFLAGS "-Wl,-framework,QuartzCore")
       list(APPEND CMAKE_LIBS "$<LINK_LIBRARY:FRAMEWORK,QuartzCore>")
     else()
diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake
index 81430d79adc97..47c94af5df7fa 100644
--- a/cmake/sdlchecks.cmake
+++ b/cmake/sdlchecks.cmake
@@ -947,7 +947,7 @@ macro(CheckPTHREAD)
     elseif(BSDI)
       set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
       set(PTHREAD_LDFLAGS "")
-    elseif(DARWIN)
+    elseif(MACOS)
       set(PTHREAD_CFLAGS "-D_THREAD_SAFE")
       # causes Carbon.p complaints?
       # set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
diff --git a/cmake/sdlplatform.cmake b/cmake/sdlplatform.cmake
index 6d8ece6ac521f..d0d57671ed609 100644
--- a/cmake/sdlplatform.cmake
+++ b/cmake/sdlplatform.cmake
@@ -32,10 +32,8 @@ macro(SDL_DetectCMakePlatform)
       set(SDL_CMAKE_PLATFORM QNX)
     endif()
   elseif(APPLE)
-    if(CMAKE_SYSTEM_NAME MATCHES ".*Darwin.*")
-      set(SDL_CMAKE_PLATFORM DARWIN)
-    elseif(CMAKE_SYSTEM_NAME MATCHES ".*MacOS.*")
-      set(SDL_CMAKE_PLATFORM MACOSX)
+    if(CMAKE_SYSTEM_NAME MATCHES ".*(Darwin|MacOS).*")
+      set(SDL_CMAKE_PLATFORM MACOS)
     elseif(CMAKE_SYSTEM_NAME MATCHES ".*tvOS.*")
       set(SDL_CMAKE_PLATFORM TVOS)
     elseif(CMAKE_SYSTEM_NAME MATCHES ".*iOS.*")
@@ -43,6 +41,10 @@ macro(SDL_DetectCMakePlatform)
       if(CMAKE_VERSION VERSION_LESS 3.14)
         set(SDL_CMAKE_PLATFORM IOS)
       endif()
+    elseif(CMAKE_SYSTEM_NAME MATCHES ".*watchOS.*")
+      set(SDL_CMAKE_PLATFORM WATCHOS)
+    elseif (CMAKE_SYSTEM_NAME MATCHES "visionOS")
+      set(SDL_CMAKE_PLATFORM VISIONOS)
     endif()
   elseif(CMAKE_SYSTEM_NAME MATCHES "BeOS.*")
     message_error("BeOS support has been removed as of SDL 2.0.2.")