SDL_image: Add simple CMake SDL_image user application

From 9ed9070531ff74bcaf5326eaebd1df3ce5f65015 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sat, 4 Jun 2022 02:51:10 +0200
Subject: [PATCH] Add simple CMake SDL_image user application

---
 cmake/test/CMakeLists.txt | 49 +++++++++++++++++++++++++++++++++++++++
 cmake/test/main.c         | 19 +++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 cmake/test/CMakeLists.txt
 create mode 100644 cmake/test/main.c

diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt
new file mode 100644
index 00000000..064d997d
--- /dev/null
+++ b/cmake/test/CMakeLists.txt
@@ -0,0 +1,49 @@
+# This cmake build script is meant for verifying the various CMake configuration script.
+
+cmake_minimum_required(VERSION 3.12)
+project(sdl_test LANGUAGES C)
+
+cmake_policy(SET CMP0074 NEW)
+
+# Override CMAKE_FIND_ROOT_PATH_MODE to allow search for SDL2 outside of sysroot
+set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER)
+
+include(FeatureSummary)
+
+option(TEST_SHARED "Test linking to shared SDL2_image library" ON)
+add_feature_info("TEST_SHARED" TEST_SHARED "Test linking with shared library")
+
+option(TEST_STATIC "Test linking to static SDL2_image libary" ON)
+add_feature_info("TEST_STATIC" TEST_STATIC "Test linking with static library")
+
+if(TEST_SHARED)
+    # FIXME: in the distant future, must become REQUIRED
+    find_package(SDL2 CONFIG COMPONENTS SDL2)
+    # FIXME: and the following should be removed
+    if(NOT TARGET SDL2::SDL2)
+        list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/..")
+        include(PrivateSdlFunctions)
+        sdl_find_sdl2(SDL2::SDL2 2.0)
+    endif()
+    find_package(SDL2_image REQUIRED CONFIG)
+    add_executable(main_shared main.c)
+    target_link_libraries(main_shared PRIVATE SDL2::SDL2 SDL2_image::SDL2_image)
+endif()
+
+if(TEST_STATIC)
+    # FIXME: in the distant future, must become REQUIRED
+    find_package(SDL2 CONFIG COMPONENTS SDL2-static)
+    # FIXME: and the following should be removed
+    if(NOT TARGET SDL2::SDL2-static)
+        list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/..")
+        include(PrivateSdlFunctions)
+        sdl_find_sdl2(SDL2::SDL2-static 2.0)
+    endif()
+    # some static vendored libraries use c++ (enable CXX after `find_package` might show a warning)
+    enable_language(CXX)
+    find_package(SDL2_image REQUIRED CONFIG)
+    add_executable(main_static main.c)
+    target_link_libraries(main_static PRIVATE SDL2::SDL2-static SDL2_image::SDL2_image-static)
+endif()
+
+feature_summary(WHAT ALL)
diff --git a/cmake/test/main.c b/cmake/test/main.c
new file mode 100644
index 00000000..fb655ef2
--- /dev/null
+++ b/cmake/test/main.c
@@ -0,0 +1,19 @@
+#define SDL_MAIN_HANDLED
+#include "SDL.h"
+#include "SDL_image.h"
+#include <stdio.h>
+
+int main(int argc, char *argv[]) {
+    SDL_SetMainReady();
+    if (SDL_Init(0) < 0) {
+        fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
+        return 1;
+    }
+    SDL_SetMainReady();
+    if (IMG_Init(0) == 0) {
+        fprintf(stderr, "no image formats supported\n");
+    }
+    IMG_Quit();
+    SDL_Quit();
+    return 0;
+}