SDL: cmake: install pdb's when present

From 90255914a848a6ad4dc62dfb95b9a73a49b03ee6 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sun, 22 Oct 2023 16:11:40 +0200
Subject: [PATCH] cmake: install pdb's when present

---
 .github/workflows/msvc.yml | 7 ++++++-
 CMakeLists.txt             | 3 +++
 test/CMakeLists.txt        | 5 +++++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/msvc.yml b/.github/workflows/msvc.yml
index 2f962a0c4ff0..7cfacdc92db4 100644
--- a/.github/workflows/msvc.yml
+++ b/.github/workflows/msvc.yml
@@ -34,7 +34,12 @@ jobs:
         os.makedirs(builddir)
         with open(f"{ builddir }/CMakeLists.txt", "w") as f:
           f.write(textwrap.dedent(f"""\
-            cmake_minimum_required(VERSION 3.0...3.5)
+            # Always build .PDB symbol file
+            set(CMAKE_POLICY_DEFAULT_CMP0141 "NEW" CACHE STRING "MSVC debug information format flags are selected by an abstraction")
+            set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "ProgramDatabase" CACHE STRING "MSVC debug information format")
+            set(CMAKE_EXE_LINKER_FLAGS "-DEBUG" CACHE STRING "Linker flags for executables")
+            set(CMAKE_SHARED_LINKER_FLAGS "-DEBUG" CACHE STRING "Linker flag for shared libraries")
+            cmake_minimum_required(VERSION 3.0...3.25)
             project(sdl_user)
             add_subdirectory("{ srcdir }" SDL)
           """))
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 511f97130d44..1980514707ab 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3507,6 +3507,9 @@ if(NOT SDL2_DISABLE_INSTALL)
       LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
       ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
       RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
+    if(MSVC AND NOT CMAKE_VERSION VERSION_LESS "3.1")
+      install(FILES $<TARGET_PDB_FILE:SDL2> DESTINATION "${CMAKE_INSTALL_BINDIR}" OPTIONAL)
+    endif()
   endif()
 
   if(NOT WINDOWS_STORE AND NOT SDL2_DISABLE_SDL2MAIN)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 7a070392cefd..17bf76402d0b 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -479,6 +479,11 @@ if(SDL_INSTALL_TESTS)
             DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL2
         )
     endif()
+    if(MSVC)
+        foreach(test ${SDL_TEST_EXECUTABLES})
+            install(FILES $<TARGET_PDB_FILE:${test}> DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL2" OPTIONAL)
+        endforeach()
+    endif()
     install(
         FILES ${RESOURCE_FILES}
         DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL2