SDL_net: cmake: sync cmake/PrivateSdlFunctions.cmake with SDL2_image

From c7606c898cc109131ad41cf16bdfb9e56a6e5002 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sat, 9 Sep 2023 21:09:02 +0200
Subject: [PATCH] cmake: sync cmake/PrivateSdlFunctions.cmake with SDL2_image

---
 cmake/PrivateSdlFunctions.cmake | 91 ++++++++++++++++++---------------
 1 file changed, 51 insertions(+), 40 deletions(-)

diff --git a/cmake/PrivateSdlFunctions.cmake b/cmake/PrivateSdlFunctions.cmake
index 693c688..b30f380 100644
--- a/cmake/PrivateSdlFunctions.cmake
+++ b/cmake/PrivateSdlFunctions.cmake
@@ -1,7 +1,7 @@
-# This file is shared amongst SDL_image/SDL_mixer/SDL_net/SDL_ttf
+# This file is shared amongst SDL_image/SDL_mixer/SDL_ttf
 
 macro(sdl_calculate_derived_version_variables)
-    if (NOT DEFINED MAJOR_VERSION OR NOT DEFINED MINOR_VERSION OR NOT DEFINED MICRO_VERSION)
+    if(NOT DEFINED MAJOR_VERSION OR NOT DEFINED MINOR_VERSION OR NOT DEFINED MICRO_VERSION)
         message(FATAL_ERROR "MAJOR_VERSION, MINOR_VERSION and MICRO_VERSION need to be defined")
     endif()
 
@@ -10,7 +10,7 @@ macro(sdl_calculate_derived_version_variables)
     # Calculate a libtool-like version number
     math(EXPR BINARY_AGE "${MINOR_VERSION} * 100 + ${MICRO_VERSION}")
     math(EXPR IS_DEVELOPMENT "${MINOR_VERSION} % 2")
-    if (IS_DEVELOPMENT)
+    if(IS_DEVELOPMENT)
         # Development branch, 2.5.1 -> libSDL2_XXXXX-2.0.so.0.501.0
         set(INTERFACE_AGE 0)
     else()
@@ -58,7 +58,7 @@ macro(sdl_find_sdl2 TARGET VERSION)
 
     # Use Private FindSDL2.cmake module to find SDL2 for installations where no SDL2Config.cmake is available,
     # or for those installations where no target is generated.
-    if (NOT TARGET ${TARGET})
+    if(NOT TARGET ${TARGET})
         message(STATUS "Using private SDL2 find module")
         find_package(PrivateSDL2 ${VERSION} REQUIRED)
         add_library(${TARGET} INTERFACE IMPORTED)
@@ -70,23 +70,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)
@@ -95,17 +96,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)
@@ -113,8 +114,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)
@@ -127,7 +128,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")
@@ -149,14 +150,14 @@ 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)
@@ -165,9 +166,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()
@@ -175,39 +176,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
-        set(props_to_check IMPORTED_LOCATION)
-        if (CMAKE_BUILD_TYPE)
-            list(APPEND props_to_check IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
+        # 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)
+            list(APPEND location_properties IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
         endif()
         foreach (config_type ${CMAKE_CONFIGURATION_TYPES} RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
-            list(APPEND props_to_check IMPORTED_LOCATION_${config_type})
+            list(APPEND location_properties IMPORTED_LOCATION_${config_type})
         endforeach()
-        foreach(prop_to_check ${props_to_check})
-            if (NOT result)
-                get_target_property(propvalue "${TARGET}" ${prop_to_check})
-                if (EXISTS "${propvalue}")
-                    while (IS_SYMLINK "${propvalue}")
-                        read_absolute_symlink(propvalue "${propvalue}")
+        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)
+                get_target_property(library_path "${TARGET}" ${location_property})
+                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()
-                    get_filename_component(libdir "${propvalue}" DIRECTORY)
+                    get_filename_component(libdir "${library_path}" DIRECTORY)
                     file(GLOB subfiles "${libdir}/*")
-                    set(similar_files "${propvalue}")
+                    set(similar_files "${library_path}")
                     foreach(subfile ${subfiles})
-                        if (IS_SYMLINK "${subfile}")
+                        if(IS_SYMLINK "${subfile}")
                             read_absolute_symlink(subfile_target "${subfile}")
-                            if (subfile_target STREQUAL propvalue)
+                            while (IS_SYMLINK "${subfile_target}")
+                                read_absolute_symlink(subfile_target "${subfile_target}")
+                            endwhile()
+                            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)
-                    if (eq_length GREATER 1)
-                        list(GET similar_files 1 item)
-                    else()
-                        list(GET similar_files 0 item)
+                    set(index 0)
+                    if(APPLE)
+                        list(LENGTH similar_files len)
+                        math(EXPR index "${len}-1")
                     endif()
+                    list(GET similar_files ${index} item)
                     get_filename_component(result "${item}" NAME)
                 endif()
             endif()