SDL_image: cmake: install pdb's, when available

From a6a9cc9cad81aafd1c23f205a441bfe6c708e5d7 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Thu, 14 Dec 2023 19:47:05 +0100
Subject: [PATCH] cmake: install pdb's, when available

---
 .github/workflows/main.yml      |  5 +++--
 CMakeLists.txt                  | 16 ++++++++++++++++
 cmake/PrivateSdlFunctions.cmake | 14 ++++++++++++++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 33c6a04b..f5de31ce 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -16,7 +16,7 @@ jobs:
       matrix:
         platform:
         - { name: Windows (MSVC),    os: windows-latest, shell: sh,  vendored: true, msvc: 1, shared: 1, static: 0,
-            cmake: '-DSDL3IMAGE_BACKEND_STB=OFF -DPerl_ROOT=C:/Strawberry/perl/bin/ -GNinja' }
+            cmake: '-DSDL3IMAGE_BACKEND_STB=OFF -DPerl_ROOT=C:/Strawberry/perl/bin/ -GNinja -DCMAKE_POLICY_DEFAULT_CMP0141=NEW -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=ProgramDatabase -DCMAKE_EXE_LINKER_FLAGS=-DEBUG -DCMAKE_SHARED_LINKER_FLAGS=-DEBUG' }
         - { name: Windows (mingw64), os: windows-latest, shell: 'msys2 {0}', vendored: false, msystem: mingw64, msys-env: mingw-w64-x86_64, shared: 1, static: 0,
             cmake: '-DSDL3IMAGE_BACKEND_STB=OFF -DSDL3IMAGE_BACKEND_WIC=OFF  -DSDL3IMAGE_AVIF=ON -G "Ninja Multi-Config"' }
         - { name: Linux,             os: ubuntu-latest,  shell: sh,  vendored: false, cmake: '-GNinja', shared: 1, static: 0, nojxl: true }
@@ -121,6 +121,7 @@ jobs:
           -DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE=$PWD/build \
           ${{ matrix.platform.cmake }}
     - name: Build (CMake)
+      id: build
       run: cmake --build build/ --config Release --parallel --verbose
     - name: Run build-time tests
       run: |
@@ -158,7 +159,7 @@ jobs:
         cd build
         ctest --no-tests=error -VV -C Release
     - name: Install (CMake)
-      if: ${{ matrix.platform.shell == 'sh' || contains(matrix.platform.shell, 'msys2') }}
+      if: ${{ always() && steps.build.outcome == 'success' }}
       run: |
         set -eu
         rm -fr DESTDIR-cmake
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e689a83c..5d9d7e3c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -787,6 +787,14 @@ if(SDL3IMAGE_INSTALL)
         FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/SDL3_image/SDL_image.h"
         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/SDL3_image" COMPONENT DEVEL
     )
+    if(BUILD_SHARED_LIBS)
+        set(pdbdir "${CMAKE_INSTALL_BINDIR}")
+    else()
+        set(pdbdir "${CMAKE_INSTALL_LIBDIR}")
+    endif()
+    if(MSVC)
+        SDL_install_pdb("${sdl3_image_target_Name}" "${pdbdir}")
+    endif()
 
     if(INSTALL_EXTRA_TARGETS)
         set_property(TARGET ${INSTALL_EXTRA_TARGETS} PROPERTY PUBLIC_HEADER "")
@@ -801,6 +809,11 @@ if(SDL3IMAGE_INSTALL)
             LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT library
             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT library
         )
+        if(MSVC)
+            foreach(tgt IN LISTS INSTALL_EXTRA_TARGETS)
+                SDL_install_pdb("${tgt}" "${pdbdir}")
+            endforeach()
+        endif()
     endif()
 
     if(WIN32 AND NOT MINGW)
@@ -881,6 +894,9 @@ if(SDL3IMAGE_SAMPLES)
             install(TARGETS ${prog}
                 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
             )
+            if(MSVC)
+                SDL_install_pdb("${prog}" "${pdbdir}")
+            endif()
         endif()
     endforeach()
 endif()
diff --git a/cmake/PrivateSdlFunctions.cmake b/cmake/PrivateSdlFunctions.cmake
index 47d820d0..b414607c 100644
--- a/cmake/PrivateSdlFunctions.cmake
+++ b/cmake/PrivateSdlFunctions.cmake
@@ -338,3 +338,17 @@ function(sdl_get_git_revision_hash VARNAME)
     endif()
     set("${VARNAME}" "${revision}" PARENT_SCOPE)
 endfunction()
+
+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()