SDL: cmake: optionally install pdb's

From 1ae33f675118f32b6faab7645f140bab26d432d5 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Thu, 12 Oct 2023 00:49:43 +0200
Subject: [PATCH] cmake: optionally install pdb's

---
 .github/workflows/msvc.yml | 19 +++++++++++++------
 CMakeLists.txt             |  3 +++
 test/CMakeLists.txt        |  5 +++++
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/.github/workflows/msvc.yml b/.github/workflows/msvc.yml
index 7d5d0f5dd118..9195a94e6abb 100644
--- a/.github/workflows/msvc.yml
+++ b/.github/workflows/msvc.yml
@@ -36,13 +36,20 @@ jobs:
         srcdir = r"${{ github.workspace }}".replace("\\", "/")
         builddir = f"{ srcdir }/build"
         os.makedirs(builddir)
+        cmakelists_txt = textwrap.dedent(f"""\
+          # 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)
+          enable_testing()
+          add_subdirectory("{ srcdir }" SDL)
+        """)
+        print(cmakelists_txt)
         with open(f"{ builddir }/CMakeLists.txt", "w") as f:
-          f.write(textwrap.dedent(f"""\
-            cmake_minimum_required(VERSION 3.0...3.25)
-            project(sdl_user)
-            enable_testing()
-            add_subdirectory("{ srcdir }" SDL)
-          """))
+          f.write(cmakelists_txt)
     - name: Configure (CMake)
       run: cmake -S build -B build `
         -Wdeprecated -Wdev -Werror `
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6f03982f3638..a5188e5380a8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3264,6 +3264,9 @@ if(NOT SDL_DISABLE_INSTALL)
       FRAMEWORK DESTINATION "."
       RESOURCE DESTINATION "${SDL_SDL_INSTALL_RESOURCEDIR}"
     )
+    if(MSVC)
+      install(FILES $<TARGET_PDB_FILE:SDL3-shared> DESTINATION "${CMAKE_INSTALL_BINDIR}" OPTIONAL)
+    endif()
   endif()
 
   if(SDL_STATIC)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index fa4175a8df9e..942400ff77ec 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -557,6 +557,11 @@ if(SDL_INSTALL_TESTS)
             DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3
         )
     endif()
+    if(MSVC)
+        foreach(test IN LISTS SDL_TEST_EXECUTABLES)
+            install(FILES $<TARGET_PDB_FILE:${test}> DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3" OPTIONAL)
+        endforeach()
+    endif()
     install(
         FILES ${RESOURCE_FILES}
         DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3