SDL_image: Fix SDL2IMAGE_TIF_SHARED option when using CMake >= 3.28 (02c81)

From 02c81284c92e2097a6559a46e188f27f2039e2fd Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Tue, 26 Mar 2024 02:27:42 +0100
Subject: [PATCH] Fix SDL2IMAGE_TIF_SHARED option when using CMake >= 3.28

Backport of 2bca7c31b303d471500ed915d02406e691ac4e76 and b9694685952ac6cc287c9e30c03305860496baf6
---
 CMakeLists.txt | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 65a8811d..362b1fe6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -646,6 +646,7 @@ if(SDL2IMAGE_TIF)
         set(BUILD_SHARED_LIBS ${SDL2IMAGE_TIF_SHARED})
         add_subdirectory(external/libtiff EXCLUDE_FROM_ALL)
         add_library(TIFF::TIFF ALIAS tiff)
+        set(SDL2IMAGE_TIF_TARGET "TIFF::tiff")
         list(APPEND INSTALL_EXTRA_TARGETS tiff)
         set_target_properties(tiff PROPERTIES EXPORT_NAME external_libtiff)
         add_library(SDL2_image::external_libtiff ALIAS tiff)
@@ -655,22 +656,29 @@ if(SDL2IMAGE_TIF)
     else()
         message(STATUS "${PROJECT_NAME}: Using system libtiff")
         find_package(TIFF REQUIRED)
+        if(TARGET TIFF::tiff)
+            # Introduced in CMake 3.28
+            # TIFF::TIFF still exists, but it is an INTERFACE library linking to TIFF::tiff (no ALIAS library)
+            set(SDL2IMAGE_TIF_TARGET "TIFF::tiff")
+        else()
+            set(SDL2IMAGE_TIF_TARGET "TIFF::TIFF")
+        endif()
         list(APPEND PC_REQUIRES libtiff-4)
     endif()
     if(SDL2IMAGE_TIF_SHARED)
         target_include_directories(SDL2_image PRIVATE
-            $<TARGET_PROPERTY:TIFF::TIFF,INCLUDE_DIRECTORIES>
-            $<TARGET_PROPERTY:TIFF::TIFF,INTERFACE_INCLUDE_DIRECTORIES>
-            $<TARGET_PROPERTY:TIFF::TIFF,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
+            $<TARGET_PROPERTY:${SDL2IMAGE_TIF_TARGET},INCLUDE_DIRECTORIES>
+            $<TARGET_PROPERTY:${SDL2IMAGE_TIF_TARGET},INTERFACE_INCLUDE_DIRECTORIES>
+            $<TARGET_PROPERTY:${SDL2IMAGE_TIF_TARGET},INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
         )
-        target_get_dynamic_library(dynamic_tif TIFF::TIFF)
+        target_get_dynamic_library(dynamic_tif ${SDL2IMAGE_TIF_TARGET})
         message(STATUS "Dynamic libtiff: ${dynamic_tif}")
         target_compile_definitions(SDL2_image PRIVATE "LOAD_TIF_DYNAMIC=\"${dynamic_tif}\"")
         if(SDL2IMAGE_TIF_VENDORED)
-            add_dependencies(SDL2_image TIFF::TIFF)
+            add_dependencies(SDL2_image ${SDL2IMAGE_TIF_TARGET})
         endif()
     else()
-        target_link_libraries(SDL2_image PRIVATE TIFF::TIFF)
+        target_link_libraries(SDL2_image PRIVATE ${SDL2IMAGE_TIF_TARGET})
     endif()
 endif()