sdl2-compat: Add cmake uninstall target

From a009821ab25a688545d3e778a93dc8d0c50c43d7 Mon Sep 17 00:00:00 2001
From: ChillerDragon <[EMAIL REDACTED]>
Date: Fri, 31 Jan 2025 11:31:11 +0100
Subject: [PATCH] Add cmake uninstall target

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

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e817451..cd04475 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -544,6 +544,11 @@ if(SDL2COMPAT_INSTALL)
 
     configure_file("${CMAKE_SOURCE_DIR}/sdl2-config.in" "${CMAKE_BINARY_DIR}/sdl2-config" @ONLY)
     install(PROGRAMS "${CMAKE_BINARY_DIR}/sdl2-config" DESTINATION "${CMAKE_INSTALL_BINDIR}")
+
+    # uninstall
+    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()
 
   set(SOEXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
diff --git a/cmake/cmake_uninstall.cmake.in b/cmake/cmake_uninstall.cmake.in
new file mode 100644
index 0000000..9a59b4f
--- /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()