SDL_image: Add CMake Find modules for all big SDL2 components

From 541ecb3107003cc10ad2d87fb5d21a3dbbebea7a Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Wed, 25 May 2022 18:05:45 +0200
Subject: [PATCH] Add CMake Find modules for all big SDL2 components

Ideally, these should be provided by SDL2 as a config module.
Or can be provided by one module, using COMPONENTS.
But for simplicity, and until SDL2 has sorted out its CMake support, use 3
CMake modules.
---
 cmake/CommonFindSDL2.cmake  | 22 ++++++++++++++++
 cmake/FindPrivateSDL2.cmake | 50 +++++++++++++++++++++++++++++++++++++
 cmake/FindSDL2main.cmake    | 24 ++++++++++++++++++
 cmake/FindSDL2test.cmake    | 31 +++++++++++++++++++++++
 4 files changed, 127 insertions(+)
 create mode 100644 cmake/CommonFindSDL2.cmake
 create mode 100644 cmake/FindPrivateSDL2.cmake
 create mode 100644 cmake/FindSDL2main.cmake
 create mode 100644 cmake/FindSDL2test.cmake

diff --git a/cmake/CommonFindSDL2.cmake b/cmake/CommonFindSDL2.cmake
new file mode 100644
index 00000000..0a41bc6c
--- /dev/null
+++ b/cmake/CommonFindSDL2.cmake
@@ -0,0 +1,22 @@
+# Common variables for FindSDL2*.cmake modules
+
+set(_inc_suffixes include)
+set(_lib_suffixes)
+if(MSVC)
+    if(CMAKE_SIZEOF_VOID_P EQUAL 4)
+        list(APPEND _lib_suffixes "lib/x86")
+    endif()
+    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+        list(APPEND _lib_suffixes "lib/x64")
+    endif()
+endif()
+if(MINGW)
+    if(CMAKE_SIZEOF_VOID_P EQUAL 4)
+        list(APPEND _lib_suffixes "i686-w64-mingw32/lib")
+        list(APPEND _inc_suffixes "i686-w64-mingw32/include")
+    endif()
+    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+        list(APPEND _lib_suffixes "x86_46-w64-mingw32/lib")
+        list(APPEND _inc_suffixes "x86_46-w64-mingw32/include")
+    endif()
+endif()
\ No newline at end of file
diff --git a/cmake/FindPrivateSDL2.cmake b/cmake/FindPrivateSDL2.cmake
new file mode 100644
index 00000000..1f2e2ec7
--- /dev/null
+++ b/cmake/FindPrivateSDL2.cmake
@@ -0,0 +1,50 @@
+# FIXME: this should be provided by SDL2
+
+include(FindPackageHandleStandardArgs)
+include("${CMAKE_CURRENT_LIST_DIR}/CommonFindSDL2.cmake")
+
+message("_lib_suffixes: ${_lib_suffixes}")
+message("_inc_suffixes: ${_inc_suffixes}")
+
+find_library(SDL2_LIBRARY
+    NAMES SDL2
+    HINTS ${SDL2_DIR} ENV SDL2_DIR
+    PATH_SUFFIXES ${_lib_suffixes}
+)
+
+find_path(SDL2_INCLUDE_DIR
+    NAMES SDL_haptic.h
+    PATH_SUFFIXES SDL2
+    HINTS ${SDL2_DIR} ENV SDL2_DIR
+    PATH_SUFFIXES ${_inc_suffixes}
+)
+
+set(SDL2_VERSION)
+if(SDL2_INCLUDE_DIR)
+    file(READ "${SDL2_INCLUDE_DIR}/SDL_version.h" _sdl_version_h)
+    string(REGEX MATCH "#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)" _sdl2_major_re "${_sdl_version_h}")
+    set(_sdl2_major "${CMAKE_MATCH_1}")
+    string(REGEX MATCH "#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)" _sdl2_minor_re "${_sdl_version_h}")
+    set(_sdl2_minor "${CMAKE_MATCH_1}")
+    string(REGEX MATCH "#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)" _sdl2_patch_re "${_sdl_version_h}")
+    set(_sdl2_patch "${CMAKE_MATCH_1}")
+    if(_sdl2_major_re AND _sdl2_minor_re AND _sdl2_patch_re)
+        set(SDL2_VERSION "${_sdl2_major}.${_sdl2_minor}.${_sdl2_patch}")
+    endif()
+endif()
+
+find_package_handle_standard_args(PrivateSDL2
+    REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR
+    VERSION_VAR SDL2_VERSION
+)
+
+if(PrivateSDL2_FOUND)
+    if(NOT TARGET PrivateSDL2::PrivateSDL2)
+        add_library(PrivateSDL2::PrivateSDL2 UNKNOWN IMPORTED)
+        set_target_properties(PrivateSDL2::PrivateSDL2 PROPERTIES
+            INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
+            IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+            IMPORTED_LOCATION "${SDL2_LIBRARY}"
+        )
+    endif()
+endif()
diff --git a/cmake/FindSDL2main.cmake b/cmake/FindSDL2main.cmake
new file mode 100644
index 00000000..cbdfee86
--- /dev/null
+++ b/cmake/FindSDL2main.cmake
@@ -0,0 +1,24 @@
+# FIXME: this should be provided by SDL2
+
+include(FindPackageHandleStandardArgs)
+include("${CMAKE_CURRENT_LIST_DIR}/CommonFindSDL2.cmake")
+
+find_library(SDL2_MAIN_LIBRARY
+    NAMES SDL2main
+    HINTS ${SDL2_DIR} ENV SDL2_DIR
+    PATH_SUFFIXES ${_lib_suffixes}
+)
+
+find_package_handle_standard_args(SDL2main
+    REQUIRED_VARS SDL2_MAIN_LIBRARY
+)
+
+if(SDL2main_FOUND)
+    if(NOT TARGET SDL2::SDL2main)
+        add_library(SDL2::SDL2main UNKNOWN IMPORTED)
+        set_target_properties(SDL2::SDL2main PROPERTIES
+            IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+            IMPORTED_LOCATION "${SDL2_MAIN_LIBRARY}"
+        )
+    endif()
+endif()
diff --git a/cmake/FindSDL2test.cmake b/cmake/FindSDL2test.cmake
new file mode 100644
index 00000000..1318fba6
--- /dev/null
+++ b/cmake/FindSDL2test.cmake
@@ -0,0 +1,31 @@
+# FIXME: this should be provided by SDL2
+
+include(FindPackageHandleStandardArgs)
+include("${CMAKE_CURRENT_LIST_DIR}/CommonFindSDL2.cmake")
+
+find_library(SDL2_TEST_LIBRARY
+    NAMES SDL2test SDL2_test
+    HINTS ${SDL2_DIR} ENV SDL2_DIR
+    PATH_SUFFIXES ${_lib_suffixes}
+)
+
+find_package_handle_standard_args(SDL2test
+    REQUIRED_VARS SDL2_TEST_LIBRARY
+)
+
+if(SDL2test_FOUND)
+    if(NOT TARGET SDL2::SDL2test)
+        add_library(SDL2::SDL2test UNKNOWN IMPORTED)
+        set_target_properties(SDL2::SDL2test PROPERTIES
+            IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+            IMPORTED_LOCATION "${SDL2_TEST_LIBRARY}"
+        )
+        if(MSVC AND ((SDL2_VERSION AND SDL2_VERSION VERSION_LESS "2.0.20") OR NOT SDL2_VERSION))
+            # FIXME: remove once minimum required SDL library is >=2.0.20
+            # Until 2.0.18, SDL2test.lib used `printf` in SDL_test_common.c. instead of `SDL_log`. (fixed in 2.0.20)
+            set_target_properties(SDL2::SDL2test PROPERTIES
+                INTERFACE_LINK_LIBRARIES "legacy_stdio_definitions.lib"
+            )
+        endif()
+    endif()
+endif()