SDL_image: cmake: build examples as shared library on Android

From 7b993be7057b80514914de275b0625a0e4a8bbd1 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sat, 12 Apr 2025 18:20:29 +0200
Subject: [PATCH] cmake: build examples as shared library on Android

These examples won't be useful without accompanying Java classes
or Android manifest, but they will at least successfully link.
---
 CMakeLists.txt | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f4bbafed..f338feb4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1190,13 +1190,16 @@ if(SDLIMAGE_INSTALL)
 endif()
 
 if(SDLIMAGE_SAMPLES)
-    add_executable(showanim examples/showanim.c)
-    add_executable(showimage examples/showimage.c)
-
-    foreach(prog showanim showimage)
-        sdl_add_warning_options(${prog} WARNING_AS_ERROR ${SDLIMAGE_WERROR})
-        target_link_libraries(${prog} PRIVATE SDL3_image::${sdl3_image_target_name})
-        target_link_libraries(${prog} PRIVATE ${sdl3_target_name})
+    function(add_sdl_image_example_executable TARGET)
+        if(ANDROID)
+            add_library(${TARGET} SHARED ${ARGN})
+        else()
+            add_executable(${TARGET} ${ARGN})
+        endif()
+        sdl_add_warning_options(${TARGET} WARNING_AS_ERROR ${SDLIMAGE_WERROR})
+        sdl_target_link_options_no_undefined(${TARGET})
+        target_link_libraries(${TARGET} PRIVATE SDL3_image::${sdl3_image_target_name})
+        target_link_libraries(${TARGET} PRIVATE ${sdl3_target_name})
 
         if(SDLIMAGE_SAMPLES_INSTALL)
             install(TARGETS ${prog}
@@ -1206,7 +1209,9 @@ if(SDLIMAGE_SAMPLES)
                 SDL_install_pdb("${prog}" "${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3_image")
             endif()
         endif()
-    endforeach()
+    endfunction()
+    add_sdl_image_example_executable(showanim examples/showanim.c)
+    add_sdl_image_example_executable(showimage examples/showimage.c)
 endif()
 
 if(SDLIMAGE_TESTS)