SDL: cmake: add swift test

From 8a5d1fc1967beb66d0be4bf5d3e04a845920effc Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 26 Jul 2024 21:00:33 +0200
Subject: [PATCH] cmake: add swift test

---
 cmake/test/CMakeLists.txt         | 25 +++++++++++++++++++++++++
 cmake/test/main.swift             | 13 +++++++++++++
 cmake/test/swift/module.modulemap |  4 ++++
 cmake/test/swift/shim.h           |  3 +++
 4 files changed, 45 insertions(+)
 create mode 100644 cmake/test/main.swift
 create mode 100644 cmake/test/swift/module.modulemap
 create mode 100644 cmake/test/swift/shim.h

diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt
index 066a1ee4a3e4e..11754a828ef4e 100644
--- a/cmake/test/CMakeLists.txt
+++ b/cmake/test/CMakeLists.txt
@@ -3,6 +3,19 @@
 cmake_minimum_required(VERSION 3.12)
 project(sdl_test LANGUAGES C)
 
+include(CheckLanguage)
+
+if(APPLE)
+    # multiple values for CMAKE_OSX_ARCHITECTURES not supported with Swift
+    list(LENGTH CMAKE_OSX_ARCHITECTURES count_osx_archs)
+    if(count_osx_archs LESS_EQUAL 1)
+        check_language(Swift)
+        if(CMAKE_Swift_COMPILER)
+            enable_language(Swift)
+        endif()
+    endif()
+endif()
+
 message(STATUS "CMAKE_SYSTEM_NAME=      ${CMAKE_SYSTEM_NAME}")
 message(STATUS "CMAKE_SYSTEM_PROCESSOR= ${CMAKE_SYSTEM_PROCESSOR}")
 
@@ -74,6 +87,12 @@ if(TEST_SHARED)
         add_executable(sdltest-shared sdltest.c)
         target_link_libraries(sdltest-shared PRIVATE SDL3::SDL3_test SDL3::SDL3-shared)
     endif()
+
+    if(CMAKE_Swift_COMPILER)
+        add_executable(swift-shared main.swift)
+        target_include_directories(swift-shared PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/swift")
+        target_link_libraries(swift-shared PRIVATE SDL3::SDL3-shared)
+    endif()
 endif()
 
 if(TEST_STATIC)
@@ -92,6 +111,12 @@ if(TEST_STATIC)
         add_executable(sdltest-static sdltest.c)
         target_link_libraries(sdltest-static PRIVATE SDL3::SDL3_test SDL3::SDL3-static)
     endif()
+
+    if(CMAKE_Swift_COMPILER)
+        add_executable(swift-static main.swift)
+        target_include_directories(swift-static PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/swift")
+        target_link_libraries(swift-static PRIVATE SDL3::SDL3-static)
+    endif()
 endif()
 
 find_package(SDL3 REQUIRED CONFIG COMPONENTS SDL3)
diff --git a/cmake/test/main.swift b/cmake/test/main.swift
new file mode 100644
index 0000000000000..6c5d8b8318cbd
--- /dev/null
+++ b/cmake/test/main.swift
@@ -0,0 +1,13 @@
+/* Contributed by Piotr Usewicz (https://github.com/pusewicz) */
+
+import SDL3
+
+guard SDL_Init(Uint32(SDL_INIT_VIDEO)) == 0 else {
+  fatalError("SDL_Init error: \(String(cString: SDL_GetError()))")
+}
+
+var sdlVersion = SDL_GetVersion()
+
+print("SDL version: \(sdlVersion)")
+
+SDL_Quit()
diff --git a/cmake/test/swift/module.modulemap b/cmake/test/swift/module.modulemap
new file mode 100644
index 0000000000000..bbc26a995dcfd
--- /dev/null
+++ b/cmake/test/swift/module.modulemap
@@ -0,0 +1,4 @@
+module SDL3 [extern_c] {
+    header "shim.h"
+    export *
+}
diff --git a/cmake/test/swift/shim.h b/cmake/test/swift/shim.h
new file mode 100644
index 0000000000000..dba8c6fd4c251
--- /dev/null
+++ b/cmake/test/swift/shim.h
@@ -0,0 +1,3 @@
+/* Contributed by Piotr Usewicz (https://github.com/pusewicz) */
+
+#include <SDL3/SDL.h>