SDL_image: cmake: fix finding shared libraries on Apple

From 5f4a94acce566b29b51bcc1ef77ac74fe2652b98 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 8 Sep 2023 22:39:56 +0200
Subject: [PATCH] cmake: fix finding shared libraries on Apple

---
 cmake/PrivateSdlFunctions.cmake | 63 ++++++++++++++++++++-------------
 1 file changed, 39 insertions(+), 24 deletions(-)

diff --git a/cmake/PrivateSdlFunctions.cmake b/cmake/PrivateSdlFunctions.cmake
index ae925414..367e1719 100644
--- a/cmake/PrivateSdlFunctions.cmake
+++ b/cmake/PrivateSdlFunctions.cmake
@@ -28,23 +28,24 @@ endmacro()
 
 function(read_absolute_symlink DEST PATH)
     file(READ_SYMLINK "${PATH}" p)
-    if (NOT IS_ABSOLUTE p)
+    if(NOT IS_ABSOLUTE "${p}")
         get_filename_component(pdir "${PATH}" DIRECTORY)
         set(p "${pdir}/${p}")
     endif()
+    get_filename_component(p "${p}" ABSOLUTE)
     set("${DEST}" "${p}" PARENT_SCOPE)
 endfunction()
 
 function(win32_implib_identify_dll DEST IMPLIB)
     cmake_parse_arguments(ARGS "NOTFATAL" "" "" ${ARGN})
-    if (CMAKE_DLLTOOL)
+    if(CMAKE_DLLTOOL)
         execute_process(
             COMMAND "${CMAKE_DLLTOOL}" --identify "${IMPLIB}"
             RESULT_VARIABLE retcode
             OUTPUT_VARIABLE stdout
             ERROR_VARIABLE stderr)
-        if (NOT retcode EQUAL 0)
-            if (NOT ARGS_NOTFATAL)
+        if(NOT retcode EQUAL 0)
+            if(NOT ARGS_NOTFATAL)
                 message(FATAL_ERROR "${CMAKE_DLLTOOL} failed.")
             else()
                 set("${DEST}" "${DEST}-NOTFOUND" PARENT_SCOPE)
@@ -53,17 +54,17 @@ function(win32_implib_identify_dll DEST IMPLIB)
         endif()
         string(STRIP "${stdout}" result)
         set(${DEST} "${result}" PARENT_SCOPE)
-    elseif (MSVC)
+    elseif(MSVC)
         get_filename_component(CMAKE_C_COMPILER_DIRECTORY "${CMAKE_C_COMPILER}" DIRECTORY CACHE)
         find_program(CMAKE_DUMPBIN NAMES dumpbin PATHS "${CMAKE_C_COMPILER_DIRECTORY}")
-        if (CMAKE_DUMPBIN)
+        if(CMAKE_DUMPBIN)
             execute_process(
                 COMMAND "${CMAKE_DUMPBIN}" "-headers" "${IMPLIB}"
                 RESULT_VARIABLE retcode
                 OUTPUT_VARIABLE stdout
                 ERROR_VARIABLE stderr)
-            if (NOT retcode EQUAL 0)
-                if (NOT ARGS_NOTFATAL)
+            if(NOT retcode EQUAL 0)
+                if(NOT ARGS_NOTFATAL)
                     message(FATAL_ERROR "dumpbin failed.")
                 else()
                     set(${DEST} "${DEST}-NOTFOUND" PARENT_SCOPE)
@@ -71,8 +72,8 @@ function(win32_implib_identify_dll DEST IMPLIB)
                 endif()
             endif()
             string(REGEX MATCH "DLL name[ ]+:[ ]+([^\n]+)\n" match "${stdout}")
-            if (NOT match)
-                if (NOT ARGS_NOTFATAL)
+            if(NOT match)
+                if(NOT ARGS_NOTFATAL)
                     message(FATAL_ERROR "dumpbin did not find any associated dll for ${IMPLIB}.")
                 else()
                     set(${DEST} "${DEST}-NOTFOUND" PARENT_SCOPE)
@@ -85,7 +86,7 @@ function(win32_implib_identify_dll DEST IMPLIB)
             message(FATAL_ERROR "Cannot find dumpbin, please set CMAKE_DUMPBIN cmake variable")
         endif()
     else()
-        if (NOT ARGS_NOTFATAL)
+        if(NOT ARGS_NOTFATAL)
             message(FATAL_ERROR "Don't know how to identify dll from import library. Set CMAKE_DLLTOOL (for mingw) or CMAKE_DUMPBIN (for MSVC)")
         else()
             set(${DEST} "${DEST}-NOTFOUND")
@@ -96,25 +97,27 @@ endfunction()
 function(get_actual_target)
     set(dst "${ARGV0}")
     set(target "${${dst}}")
+    set(input "${target}")
     get_target_property(alias "${target}" ALIASED_TARGET)
     while(alias)
         set(target "${alias}")
         get_target_property(alias "${target}" ALIASED_TARGET)
     endwhile()
+    message(DEBUG "get_actual_target(\"${input}\") -> \"${target}\"")
     set("${dst}" "${target}" PARENT_SCOPE)
 endfunction()
 
 function(target_get_dynamic_library DEST TARGET)
     set(result)
     get_actual_target(TARGET)
-    if (WIN32)
+    if(WIN32)
         # Use the target dll of the import library
         set(props_to_check IMPORTED_IMPLIB)
-        if (CMAKE_BUILD_TYPE)
+        if(CMAKE_BUILD_TYPE)
             list(APPEND props_to_check IMPORTED_IMPLIB_${CMAKE_BUILD_TYPE})
         endif()
         list(APPEND props_to_check IMPORTED_LOCATION)
-        if (CMAKE_BUILD_TYPE)
+        if(CMAKE_BUILD_TYPE)
             list(APPEND props_to_check IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
         endif()
         foreach (config_type ${CMAKE_CONFIGURATION_TYPES} RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
@@ -123,9 +126,9 @@ function(target_get_dynamic_library DEST TARGET)
         endforeach()
 
         foreach(prop_to_check ${props_to_check})
-            if (NOT result)
+            if(NOT result)
                 get_target_property(propvalue "${TARGET}" ${prop_to_check})
-                if (propvalue AND EXISTS "${propvalue}")
+                if(propvalue AND EXISTS "${propvalue}")
                     win32_implib_identify_dll(result "${propvalue}" NOTFATAL)
                 endif()
             endif()
@@ -133,37 +136,49 @@ function(target_get_dynamic_library DEST TARGET)
     else()
         # 1. find the target library a file might be symbolic linking to
         # 2. find all other files in the same folder that symolic link to it
-        # 3. sort all these files, and select the 2nd item
+        # 3. sort all these files, and select the 1st item on Linux, and last on Macos
         set(location_properties IMPORTED_LOCATION)
-        if (CMAKE_BUILD_TYPE)
+        if(CMAKE_BUILD_TYPE)
             list(APPEND location_properties IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
         endif()
         foreach (config_type ${CMAKE_CONFIGURATION_TYPES} RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
             list(APPEND location_properties IMPORTED_LOCATION_${config_type})
         endforeach()
+        if(APPLE)
+            set(valid_shared_library_regex "\\.[0-9]+\\.dylib$")
+        else()
+            set(valid_shared_library_regex "\\.so\\.([0-9.]+)?[0-9]")
+        endif()
         foreach(location_property ${location_properties})
-            if (NOT result)
+            if(NOT result)
                 get_target_property(library_path "${TARGET}" ${location_property})
-                if (EXISTS "${library_path}")
+                message(DEBUG "get_target_property(${TARGET} ${location_propert}) -> ${library_path}")
+                if(EXISTS "${library_path}")
+                    get_filename_component(library_path "${library_path}" ABSOLUTE)
                     while (IS_SYMLINK "${library_path}")
                         read_absolute_symlink(library_path "${library_path}")
                     endwhile()
+                    message(DEBUG "${TARGET} -> ${library_path}")
                     get_filename_component(libdir "${library_path}" DIRECTORY)
                     file(GLOB subfiles "${libdir}/*")
                     set(similar_files "${library_path}")
                     foreach(subfile ${subfiles})
-                        if (IS_SYMLINK "${subfile}")
+                        if(IS_SYMLINK "${subfile}")
                             read_absolute_symlink(subfile_target "${subfile}")
-                            while (IS_SYMLINK "${subfile_target}")
+                            while(IS_SYMLINK "${subfile_target}")
                                 read_absolute_symlink(subfile_target "${subfile_target}")
                             endwhile()
-                            if (subfile_target STREQUAL library_path AND NOT "${subfile}" MATCHES ".*(dylib|so)$")
+                            get_filename_component(subfile_target "${subfile_target}" ABSOLUTE)
+                            if(subfile_target STREQUAL library_path AND subfile MATCHES "${valid_shared_library_regex}")
                                 list(APPEND similar_files "${subfile}")
                             endif()
                         endif()
                     endforeach()
                     list(SORT similar_files)
-                    list(LENGTH similar_files eq_length)
+                    message(DEBUG "files that are similar to \"${library_path}\"=${similar_files}")
+                    if(APPLE)
+                        list(REVERSE similar_files)
+                    endif()
                     list(GET similar_files 0 item)
                     get_filename_component(result "${item}" NAME)
                 endif()