sdl2-compat: cmake: install PDB's of SDL2_test and SDL2main

From e838cf8161934142a1cd915907b740dbb7e268a9 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Wed, 22 Nov 2023 19:21:50 +0100
Subject: [PATCH] cmake: install PDB's of SDL2_test and SDL2main

---
 CMakeLists.txt     |  5 ++++-
 cmake/macros.cmake | 13 +++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 cmake/macros.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 290c09a..b4db914 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -44,6 +44,7 @@ include(CMakeParseArguments)
 include(CMakePushCheckState)
 include(GNUInstallDirs)
 include("${CMAKE_CURRENT_LIST_DIR}/cmake/CheckCPUArchitecture.cmake")
+include("${CMAKE_CURRENT_LIST_DIR}/cmake/macros.cmake")
 include("${CMAKE_CURRENT_LIST_DIR}/cmake/sdlchecks.cmake")
 include("${CMAKE_CURRENT_LIST_DIR}/cmake/sdlcompilers.cmake")
 include("${CMAKE_CURRENT_LIST_DIR}/cmake/sdlplatform.cmake")
@@ -641,7 +642,9 @@ if(SDL2COMPAT_INSTALL)
     )
   endforeach()
   if(MSVC)
-    install(FILES $<TARGET_PDB_FILE:SDL2> DESTINATION "${CMAKE_INSTALL_BINDIR}" OPTIONAL)
+    SDL_install_pdb(SDL2 "${CMAKE_INSTALL_BINDIR}")
+    SDL_install_pdb(SDL2main "${CMAKE_INSTALL_LIBDIR}")
+    SDL_install_pdb(SDL2_test "${CMAKE_INSTALL_LIBDIR}")
   endif()
   configure_package_config_file(SDL2Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/SDL2Config.cmake"
     PATH_VARS CMAKE_INSTALL_PREFIX CMAKE_INSTALL_FULL_BINDIR CMAKE_INSTALL_FULL_INCLUDEDIR CMAKE_INSTALL_FULL_LIBDIR
diff --git a/cmake/macros.cmake b/cmake/macros.cmake
new file mode 100644
index 0000000..6e0d84b
--- /dev/null
+++ b/cmake/macros.cmake
@@ -0,0 +1,13 @@
+function(SDL_install_pdb TARGET DIRECTORY)
+  get_property(type TARGET ${TARGET} PROPERTY TYPE)
+  if(type MATCHES "^(SHARED_LIBRARY|EXECUTABLE)$")
+    install(FILES $<TARGET_PDB_FILE:${TARGET}> DESTINATION "${DIRECTORY}" OPTIONAL)
+  elseif(type STREQUAL "STATIC_LIBRARY")
+    # FIXME: Use $<TARGET_COMPILE_PDB_FILE:${TARGET} once it becomes available (https://gitlab.kitware.com/cmake/cmake/-/issues/25244)
+    if(CMAKE_GENERATOR MATCHES "^Visual Studio.*")
+      install(CODE "file(INSTALL DESTINATION \"\${CMAKE_INSTALL_PREFIX}/${DIRECTORY}\" TYPE FILE OPTIONAL FILES \"${CMAKE_CURRENT_BINARY_DIR}/\${CMAKE_INSTALL_CONFIG_NAME}/${TARGET}.pdb\")")
+    else()
+      install(CODE "file(INSTALL DESTINATION \"\${CMAKE_INSTALL_PREFIX}/${DIRECTORY}\" TYPE FILE OPTIONAL FILES \"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${TARGET}.dir/${TARGET}.pdb\")")
+    endif()
+  endif()
+endfunction()