SDL_image: cmake: add uninstall target

From dbda6d897fd3c1938ad7fb040a68a09120a33032 Mon Sep 17 00:00:00 2001
From: ChillerDragon <[EMAIL REDACTED]>
Date: Mon, 18 Nov 2024 10:52:51 +0800
Subject: [PATCH] cmake: add uninstall target

---
 CMakeLists.txt                 |  7 +++++++
 cmake/cmake_uninstall.cmake.in | 17 +++++++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 cmake/cmake_uninstall.cmake.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index df02f354..c933aff4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1078,6 +1078,13 @@ if(SDLIMAGE_INSTALL)
             REVISION "${SDLIMAGE_REVISION}"
         )
     endif()
+
+    if(SDLIMAGE_ROOTPROJECT)
+        configure_file(cmake/cmake_uninstall.cmake.in cmake_uninstall.cmake IMMEDIATE @ONLY)
+
+        add_custom_target(uninstall
+            COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
+    endif()
 endif()
 
 if(SDLIMAGE_SAMPLES)
diff --git a/cmake/cmake_uninstall.cmake.in b/cmake/cmake_uninstall.cmake.in
new file mode 100644
index 00000000..9a59b4f4
--- /dev/null
+++ b/cmake/cmake_uninstall.cmake.in
@@ -0,0 +1,17 @@
+if (NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt")
+    message(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_BINARY_DIR@/install_manifest.txt\"")
+endif()
+
+file(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files)
+string(REGEX REPLACE "\n" ";" files "${files}")
+foreach(file ${files})
+    message(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"")
+    execute_process(
+        COMMAND @CMAKE_COMMAND@ -E remove "$ENV{DESTDIR}${file}"
+        OUTPUT_VARIABLE rm_out
+        RESULT_VARIABLE rm_retval
+    )
+    if(NOT ${rm_retval} EQUAL 0)
+        message(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"")
+    endif (NOT ${rm_retval} EQUAL 0)
+endforeach()