SDL_image: Add sharedness-check to target_get_dynamic_library

From 71229fe0ea87c9510e06a99ab3954027a61bb530 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Mon, 19 Sep 2022 16:04:22 +0200
Subject: [PATCH] Add sharedness-check to target_get_dynamic_library

---
 cmake/PrivateSdlFunctions.cmake | 42 ++++++++++++++++++++++++++++-----
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/cmake/PrivateSdlFunctions.cmake b/cmake/PrivateSdlFunctions.cmake
index c5c16078..ff3e1670 100644
--- a/cmake/PrivateSdlFunctions.cmake
+++ b/cmake/PrivateSdlFunctions.cmake
@@ -135,13 +135,20 @@ function(win32_implib_identify_dll DEST IMPLIB)
     endif()
 endfunction()
 
+function(get_actual_target)
+    set(dst "${ARGV0}")
+    set(target "${${dst}}")
+    get_target_property(alias "${target}" ALIASED_TARGET)
+    while(alias)
+        set(target "${alias}")
+        get_target_property(alias "${target}" ALIASED_TARGET)
+    endwhile()
+    set("${dst}" "${target}" PARENT_SCOPE)
+endfunction()
+
 function(target_get_dynamic_library DEST TARGET)
     set(result)
-    get_target_property(alias "${TARGET}" ALIASED_TARGET)
-    while (alias)
-        set(TARGET "${alias}")
-        get_target_property(alias "${TARGET}" ALIASED_TARGET)
-    endwhile()
+    get_actual_target(TARGET)
     if (WIN32)
         # Use the target dll of the import library
         set(props_to_check IMPORTED_IMPLIB)
@@ -205,7 +212,30 @@ function(target_get_dynamic_library DEST TARGET)
             endif()
         endforeach()
     endif()
-    if (NOT result)
+    if(result)
+        string(TOLOWER "${result}" result_lower)
+        if(WIN32 OR OS2)
+            if(NOT result_lower MATCHES ".*dll")
+                message(FATAL_ERROR "\"${result}\" is not a .dll library")
+            endif()
+        elseif(APPLE)
+            if(NOT result_lower MATCHES ".*dylib.*")
+                message(FATAL_ERROR "\"${result}\" is not a .dylib shared library")
+            endif()
+        else()
+            if(NOT result_lower MATCHES ".*so.*")
+                message(FATAL_ERROR "\"${result}\" is not a .so shared library")
+            endif()
+        endif()
+    else()
+        get_target_property(target_type ${TARGET} TYPE)
+        if(target_type MATCHES "SHARED_LIBRARY|MODULE_LIBRARY")
+            # OK
+        elseif(target_type MATCHES "STATIC_LIBRARY|OBJECT_LIBRARY|INTERFACE_LIBRARY|EXECUTABLE")
+            message(SEND_ERROR "${TARGET} is not a shared library, but has type=${target_type}")
+        else()
+            message(WARNING "Unable to extract dynamic library from target=${TARGET}, type=${target_type}.")
+        endif()
         set (result "$<TARGET_FILE_NAME:${TARGET}>")
     endif()
     set(${DEST} ${result} PARENT_SCOPE)