SDL: ci: Add iOS and tvOS CMake GitHub Actions checks (#9465)

From 2d640d0b593a2859811d3c6f4fbc288b13a9f767 Mon Sep 17 00:00:00 2001
From: Ravbug <[EMAIL REDACTED]>
Date: Mon, 8 Apr 2024 13:18:28 -0400
Subject: [PATCH] ci: Add iOS and tvOS CMake GitHub Actions checks  (#9465)

- Also add watchos support to CMake (SDL does not support this platform yet)

Co-authored-by: Ravbug <ravbug@users.noreply.github.com>
Co-authored-by: Anonymous Maarten <anonymous.maarten@gmail.com>
Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>
---
 .github/workflows/main.yml |  9 +++++++--
 CMakeLists.txt             | 38 +++++++++++++++++++-------------------
 cmake/sdlplatform.cmake    | 11 ++++++-----
 test/CMakeLists.txt        | 26 +++++++++++++++++---------
 4 files changed, 49 insertions(+), 35 deletions(-)

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 66b68983a9656..dc0cb6632b4c7 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -30,8 +30,10 @@ jobs:
         - { name: Intel Compiler (Ubuntu 20.04),  os: ubuntu-20.04,   shell: bash, artifact: 'SDL-ubuntu20.04-icc', intel: true, cmake: '-DSDL_CLANG_TIDY=OFF',
             source_cmd: 'source /opt/intel/oneapi/setvars.sh; export CC=icc; export CXX=icpc; export CFLAGS=-diag-disable=10441; export CXXFLAGS=-diag-disable=10441; '}
         - { name: Ubuntu 22.04,                   os: ubuntu-22.04,   shell: sh, artifact: 'SDL-ubuntu22.04' }
-        - { name: MacOS (Framework),              os: macos-latest,   shell: sh,    cmake: '-DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" -DSDL_FRAMEWORK=ON -DSDL_CLANG_TIDY=OFF', skip_test_pkgconfig: true, artifact: 'SDL-macos-framework', no-static: true }
-        - { name: MacOS (GNU prefix),             os: macos-latest,   shell: sh,    cmake: '-DCMAKE_OSX_ARCHITECTURES="x86_64" -DCLANG_TIDY_BINARY="$(brew --prefix llvm)/bin/clang-tidy"', artifact: 'SDL-macos-gnu' }
+        - { name: MacOS (Framework),              os: macos-latest,   shell: sh, cmake-platform: '-DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"', cmake: '-DSDL_FRAMEWORK=ON -DSDL_CLANG_TIDY=OFF', skip_test_pkgconfig: true, artifact: 'SDL-macos-framework', no-static: true }
+        - { name: MacOS (GNU prefix),             os: macos-latest,   shell: sh, cmake-platform: '-DCMAKE_OSX_ARCHITECTURES="x86_64"', cmake: '-DCLANG_TIDY_BINARY="$(brew --prefix llvm)/bin/clang-tidy"', artifact: 'SDL-macos-gnu' }
+        - { name: iOS (GNU prefix),               os: macos-latest,   shell: sh, cmake-platform: '-DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"',  cross-build: true, skip_test_pkgconfig: true, artifact: 'SDL-ios-gnu' }
+        - { name: tvOS (GNU prefix),              os: macos-latest,   shell: sh, cmake-platform: '-DCMAKE_SYSTEM_NAME=tvOS -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"', cross-build: true, skip_test_pkgconfig: true, artifact: 'SDL-tvos-gnu' }
 
     steps:
     - name: Set up MSYS2
@@ -108,6 +110,7 @@ jobs:
           -DSDL_CLANG_TIDY=ON \
           -DCMAKE_INSTALL_PREFIX=cmake_prefix \
           -DCMAKE_BUILD_TYPE=Release \
+          ${{ matrix.platform.cmake-platform }} \
           ${{ matrix.platform.cmake }}
     - name: Build (CMake)
       id: build
@@ -115,6 +118,7 @@ jobs:
         ${{ matrix.platform.source_cmd }}
         cmake --build build/ --config Release --verbose --parallel
     - name: Run build-time tests (CMake)
+      if: ${{ !matrix.platform.cross-build }}
       run: |
         ${{ matrix.platform.source_cmd }}
         set -eu
@@ -138,6 +142,7 @@ jobs:
       run: |
         ${{ matrix.platform.source_cmd }}
         cmake -S cmake/test -B cmake_config_build -G Ninja \
+          ${{ matrix.platform.cmake-platform }} \
           -DTEST_SHARED=ON \
           -DTEST_STATIC=${{ !matrix.platform.no-static }} \
           -DCMAKE_BUILD_TYPE=Release \
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0cd4903280b31..98e137d7644b9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -133,7 +133,7 @@ endif()
 # The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers,
 #  so we'll just use libusb when it's available. libusb does not support iOS,
 #  so we default to yes on iOS.
-if(IOS OR TVOS OR VISIONOS OR ANDROID)
+if(IOS OR TVOS OR VISIONOS OR WATCHOS OR ANDROID)
   set(SDL_HIDAPI_LIBUSB_AVAILABLE FALSE)
 else()
   set(SDL_HIDAPI_LIBUSB_AVAILABLE TRUE)
@@ -152,7 +152,7 @@ endif()
 # Default option knobs
 set(SDL_LIBC_DEFAULT ON)
 set(SDL_SYSTEM_ICONV_DEFAULT ON)
-if(WINDOWS OR IOS OR TVOS OR VISIONOS)
+if(WINDOWS OR IOS OR TVOS OR VISIONOS OR WATCHOS)
   set(SDL_SYSTEM_ICONV_DEFAULT OFF)
 endif()
 
@@ -303,8 +303,8 @@ dep_option(SDL_DISKAUDIO           "Support the disk writer audio driver" ON "SD
 dep_option(SDL_DUMMYAUDIO          "Support the dummy audio driver" ON "SDL_AUDIO" OFF)
 dep_option(SDL_DUMMYVIDEO          "Use dummy video driver" ON "SDL_VIDEO" OFF)
 dep_option(SDL_IBUS                "Enable IBus support" ON "${UNIX_SYS}" OFF)
-dep_option(SDL_OPENGL              "Include OpenGL support" ON "SDL_VIDEO;NOT VISIONOS" OFF)
-dep_option(SDL_OPENGLES            "Include OpenGL ES support" ON "SDL_VIDEO;NOT VISIONOS" OFF)
+dep_option(SDL_OPENGL              "Include OpenGL support" ON "SDL_VIDEO;NOT IOS;NOT VISIONOS;NOT TVOS;NOT WATCHOS" OFF)
+dep_option(SDL_OPENGLES            "Include OpenGL ES support" ON "SDL_VIDEO;NOT IOS;NOT VISIONOS;NOT TVOS;NOT WATCHOS" OFF)
 set_option(SDL_PTHREADS            "Use POSIX threads for multi-threading" ${SDL_PTHREADS_DEFAULT})
 dep_option(SDL_PTHREADS_SEM        "Use pthread semaphores" ON "SDL_PTHREADS" OFF)
 dep_option(SDL_OSS                 "Support the OSS audio API" ${SDL_OSS_DEFAULT} "UNIX_SYS OR RISCOS;SDL_AUDIO" OFF)
@@ -2134,7 +2134,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)
     set(SDL_FRAMEWORK_UTTYPES 1)
@@ -2143,7 +2143,7 @@ elseif(APPLE)
   set(SDL_FRAMEWORK_COREVIDEO 1)
 
   # iOS can use a CADisplayLink for main callbacks. macOS just uses the generic one atm.
-  if(IOS OR TVOS OR VISIONOS)
+  if(IOS OR TVOS OR VISIONOS OR WATCHOS)
     sdl_glob_sources("${SDL3_SOURCE_DIR}/src/main/ios/*.m")
     set(HAVE_SDL_MAIN_CALLBACKS TRUE)
   endif()
@@ -2151,14 +2151,14 @@ elseif(APPLE)
   sdl_glob_sources("${SDL3_SOURCE_DIR}/src/file/cocoa/*.m")
 
   if(SDL_CAMERA)
-    if(IOS OR TVOS OR MACOSX OR DARWIN)
+    if(MACOS OR IOS)
       set(SDL_CAMERA_DRIVER_COREMEDIA 1)
       set(HAVE_CAMERA TRUE)
       sdl_glob_sources("${SDL3_SOURCE_DIR}/src/camera/coremedia/*.m")
     endif()
   endif()
 
-  if(IOS OR TVOS OR VISIONOS)
+  if(IOS OR TVOS OR VISIONOS OR WATCHOS)
     sdl_glob_sources("${SDL3_SOURCE_DIR}/src/misc/ios/*.m")
   else()
     sdl_glob_sources("${SDL3_SOURCE_DIR}/src/misc/macos/*.m")
@@ -2180,10 +2180,10 @@ elseif(APPLE)
 
   if(SDL_JOYSTICK)
     sdl_glob_sources("${SDL3_SOURCE_DIR}/src/joystick/apple/*.m")
-    if(IOS OR TVOS OR VISIONOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       sdl_glob_sources("${SDL3_SOURCE_DIR}/src/joystick/steam/*.c")
       set(SDL_JOYSTICK_MFI 1)
-      if(IOS OR VISIONOS)
+      if(IOS OR VISIONOS OR WATCHOS)
         set(SDL_FRAMEWORK_COREMOTION 1)
       endif()
       set(SDL_FRAMEWORK_GAMECONTROLLER 1)
@@ -2225,7 +2225,7 @@ elseif(APPLE)
   endif()
 
   if(SDL_HAPTIC)
-    if (IOS OR TVOS OR VISIONOS)
+    if (IOS OR TVOS OR VISIONOS OR WATCHOS)
       sdl_glob_sources("${SDL3_SOURCE_DIR}/src/haptic/dummy/*.c")
       set(SDL_HAPTIC_DUMMY 1)
     else()
@@ -2238,7 +2238,7 @@ elseif(APPLE)
   endif()
 
   if(SDL_POWER)
-    if (IOS OR TVOS OR VISIONOS)
+    if (IOS OR TVOS OR VISIONOS OR WATCHOS)
       sdl_glob_sources("${SDL3_SOURCE_DIR}/src/power/uikit/*.m")
       set(SDL_POWER_UIKIT 1)
     else()
@@ -2267,7 +2267,7 @@ elseif(APPLE)
   # TODO: SDL_STORAGE_ICLOUD
   set(SDL_STORAGE_GENERIC 1)
   sdl_glob_sources("${SDL3_SOURCE_DIR}/src/storage/generic/*.c")
-  if(DARWIN OR MACOSX)
+  if(MACOS)
     set(SDL_STORAGE_STEAM 1)
     sdl_glob_sources("${SDL3_SOURCE_DIR}/src/storage/steam/*.c")
   endif()
@@ -2278,7 +2278,7 @@ elseif(APPLE)
   set(HAVE_SDL_FSOPS TRUE)
 
   if(SDL_SENSOR)
-    if(IOS OR VISIONOS)
+    if(IOS OR VISIONOS OR WATCHOS)
       set(SDL_SENSOR_COREMOTION 1)
       set(HAVE_SDL_SENSORS TRUE)
       sdl_glob_sources("${SDL3_SOURCE_DIR}/src/sensor/coremotion/*.m")
@@ -2287,7 +2287,7 @@ elseif(APPLE)
 
   # iOS hack needed - http://code.google.com/p/ios-cmake/ ?
   if(SDL_VIDEO)
-    if (IOS OR TVOS OR VISIONOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       set(SDL_VIDEO_DRIVER_UIKIT 1)
       set(SDL_FRAMEWORK_COREGRAPHICS 1)
       set(SDL_FRAMEWORK_QUARTZCORE 1)
@@ -2307,7 +2307,7 @@ elseif(APPLE)
     endif()
 
     if(SDL_OPENGLES)
-      if(IOS OR TVOS OR VISIONOS)
+      if(IOS OR TVOS OR VISIONOS OR WATCHOS)
         set(SDL_FRAMEWORK_OPENGLES 1)
         set(SDL_VIDEO_OPENGL_ES 1)
       else()
@@ -2400,7 +2400,7 @@ elseif(APPLE)
     endif()
   endif()
   if(SDL_FRAMEWORK_METAL)
-    if(IOS OR TVOS OR VISIONOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       sdl_link_dependency(metal LINK_OPTIONS "-Wl,-framework,Metal")
     else()
       sdl_link_dependency(metal LINK_OPTIONS "-Wl,-weak_framework,Metal")
@@ -2410,7 +2410,7 @@ elseif(APPLE)
     sdl_link_dependency(opengles LINK_OPTIONS "-Wl,-framework,OpenGLES")
   endif()
   if(SDL_FRAMEWORK_QUARTZCORE)
-    if(IOS OR TVOS OR VISIONOS)
+    if(IOS OR TVOS OR VISIONOS OR WATCHOS)
       sdl_link_dependency(quartz_core LINK_OPTIONS "-Wl,-framework,QuartzCore")
     else()
       sdl_link_dependency(metal LINK_OPTIONS "-Wl,-weak_framework,QuartzCore")
@@ -2885,7 +2885,7 @@ if (SDL_DIALOG)
   elseif(WINDOWS AND NOT WINDOWS_STORE)
     sdl_sources(${SDL3_SOURCE_DIR}/src/dialog/windows/SDL_windowsdialog.c)
     set(HAVE_SDL_DIALOG TRUE)
-  elseif(APPLE)
+  elseif(MACOS)
     sdl_sources(${SDL3_SOURCE_DIR}/src/dialog/cocoa/SDL_cocoadialog.m)
     sdl_link_dependency(uniformtypeidentifiers LINK_OPTIONS "-Wl,-weak_framework,UniformTypeIdentifiers")
     set(HAVE_SDL_DIALOG TRUE)
diff --git a/cmake/sdlplatform.cmake b/cmake/sdlplatform.cmake
index 025bb1b7b2ef0..d4af66631c4cb 100644
--- a/cmake/sdlplatform.cmake
+++ b/cmake/sdlplatform.cmake
@@ -6,17 +6,18 @@ macro(SDL_DetectCMakePlatform)
   elseif(PSP)
     set(SDL_CMAKE_PLATFORM psp)
   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.*")
+      set(SDL_CMAKE_PLATFORM watchOS)
+    elseif(CMAKE_SYSTEM_NAME MATCHES ".*watchOS.*")
       set(SDL_CMAKE_PLATFORM iOS)
     elseif (CMAKE_SYSTEM_NAME MATCHES "visionOS")
       set(SDL_CMAKE_PLATFORM visionOS)
-      set(VISIONOS ON)       # CMAKE does not set this automatically yet
+    else()
+      message(WARNING "Unknown Apple platform: \"${CMAKE_SYSTEM_NAME}\"")
     endif()
   elseif(CMAKE_SYSTEM_NAME MATCHES "Haiku.*")
     set(SDL_CMAKE_PLATFORM Haiku)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 009f679d1fea8..cba5a5b9deae5 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -279,21 +279,13 @@ set(build_options_dependent_tests )
 
 add_sdl_test_executable(testevdev BUILD_DEPENDENT NONINTERACTIVE SOURCES testevdev.c)
 
-if(APPLE)
+if(MACOS)
     add_sdl_test_executable(testnative BUILD_DEPENDENT NEEDS_RESOURCES TESTUTILS
         SOURCES
             testnative.c
             testnativecocoa.m
             testnativex11.c
     )
-
-    cmake_push_check_state()
-    check_c_compiler_flag(-Wno-error=deprecated-declarations HAVE_WNO_ERROR_DEPRECATED_DECLARATIONS)
-    cmake_pop_check_state()
-    target_link_libraries(testnative PRIVATE "-Wl,-framework,Cocoa")
-    if(HAVE_WNO_ERROR_DEPRECATED_DECLARATIONS)
-        set_property(SOURCE "testnativecocoa.m" APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-error=deprecated-declarations")
-    endif()
 elseif(WINDOWS)
     add_sdl_test_executable(testnative BUILD_DEPENDENT NEEDS_RESOURCES TESTUTILS SOURCES testnative.c testnativew32.c)
 elseif(HAVE_X11 OR HAVE_WAYLAND)
@@ -363,6 +355,9 @@ add_sdl_test_executable(testgl SOURCES testgl.c)
 add_sdl_test_executable(testgles SOURCES testgles.c)
 if(ANDROID)
     target_link_libraries(testgles PRIVATE GLESv1_CM)
+elseif(IOS OR TVOS)
+    find_library(GLES_LIB OpenGLES REQUIRED)
+    target_link_libraries(testgles PRIVATE "${GLES_LIB}")
 endif()
 check_include_file("GLES2/gl2platform.h" HAVE_GLES2_GL2PLATFORM_H)
 if(HAVE_GLES2_GL2PLATFORM_H OR (TARGET SDL3-static OR SDL3-shared))
@@ -482,6 +477,19 @@ if(EMSCRIPTEN)
             COMMAND Python3::Interpreter "${CMAKE_CURRENT_SOURCE_DIR}/emscripten/server.py" -d "${CMAKE_CURRENT_BINARY_DIR}")
     endif()
 endif()
+if(MACOS)
+    target_link_options(testnative PRIVATE "-Wl,-framework,Cocoa")
+endif()
+if(APPLE)
+    cmake_push_check_state()
+    check_c_compiler_flag(-Wno-error=deprecated-declarations HAVE_WNO_ERROR_DEPRECATED_DECLARATIONS)
+    cmake_pop_check_state()
+    if(HAVE_WNO_ERROR_DEPRECATED_DECLARATIONS)
+        set_property(SOURCE "testnativecocoa.m" APPEND PROPERTY COMPILE_OPTIONS "-Wno-error=deprecated-declarations")
+        set_property(TARGET testgles APPEND PROPERTY COMPILE_OPTIONS "-Wno-error=deprecated-declarations")
+    endif()
+endif()
+
 
 if(PSP)
     # Build EBOOT files if building for PSP