SDL_ttf: cmake: sync cmake/PrivateSDLFunctions.cmake

From 25def80ca74c42bfd4cfb4a4e29b50a830a83771 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Thu, 7 Aug 2025 14:44:29 +0200
Subject: [PATCH] cmake: sync cmake/PrivateSDLFunctions.cmake

---
 cmake/PrivateSdlFunctions.cmake | 198 +++++++++++++++++---------------
 1 file changed, 103 insertions(+), 95 deletions(-)

diff --git a/cmake/PrivateSdlFunctions.cmake b/cmake/PrivateSdlFunctions.cmake
index c0ec4b5a..02695db6 100644
--- a/cmake/PrivateSdlFunctions.cmake
+++ b/cmake/PrivateSdlFunctions.cmake
@@ -111,114 +111,122 @@ endfunction()
 
 function(target_get_dynamic_library DEST TARGET)
     set(result)
-    get_actual_target(TARGET)
-    if(WIN32)
-        # Use the target dll of the import library
-        set(props_to_check IMPORTED_IMPLIB)
-        if(CMAKE_BUILD_TYPE)
-            list(APPEND props_to_check IMPORTED_IMPLIB_${CMAKE_BUILD_TYPE})
+    if(DEFINED ${DEST})
+        if(NOT EXISTS "${${DEST}}")
+            message(FATAL_ERROR "${DEST}=${${DEST}} does not exist")
         endif()
-        list(APPEND props_to_check IMPORTED_LOCATION)
-        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)
-            list(APPEND props_to_check IMPORTED_IMPLIB_${config_type})
-            list(APPEND props_to_check IMPORTED_LOCATION_${config_type})
-        endforeach()
+        get_filename_component(filename ${${DEST}} NAME)
+        set(${DEST} ${filename} PARENT_SCOPE)
+    else()
+        get_actual_target(TARGET)
+        if(WIN32)
+            # Use the target dll of the import library
+            set(props_to_check IMPORTED_IMPLIB)
+            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)
+                list(APPEND props_to_check IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
+            endif()
+            foreach (config_type ${CMAKE_CONFIGURATION_TYPES} RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
+                list(APPEND props_to_check IMPORTED_IMPLIB_${config_type})
+                list(APPEND props_to_check IMPORTED_LOCATION_${config_type})
+            endforeach()
 
-        foreach(prop_to_check ${props_to_check})
-            if(NOT result)
-                get_target_property(propvalue "${TARGET}" ${prop_to_check})
-                if(propvalue AND EXISTS "${propvalue}")
-                    win32_implib_identify_dll(result "${propvalue}" NOTFATAL)
+            foreach(prop_to_check ${props_to_check})
+                if(NOT result)
+                    get_target_property(propvalue "${TARGET}" ${prop_to_check})
+                    if(propvalue AND EXISTS "${propvalue}")
+                        win32_implib_identify_dll(result "${propvalue}" NOTFATAL)
+                    endif()
                 endif()
-            endif()
-        endforeach()
-    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 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 location_properties IMPORTED_LOCATION_${config_type})
-        endforeach()
-        if(APPLE)
-            set(valid_shared_library_regex "\\.[0-9]+\\.dylib$")
+            endforeach()
         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})
-                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}")
-                            read_absolute_symlink(subfile_target "${subfile}")
-                            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}")
+            # 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 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 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)
+                    get_target_property(library_path "${TARGET}" ${location_property})
+                    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}")
+                                read_absolute_symlink(subfile_target "${subfile}")
+                                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)
+                        message(DEBUG "files that are similar to \"${library_path}\"=${similar_files}")
+                        if(APPLE)
+                            list(REVERSE similar_files)
                         endif()
-                    endforeach()
-                    list(SORT similar_files)
-                    message(DEBUG "files that are similar to \"${library_path}\"=${similar_files}")
-                    if(APPLE)
-                        list(REVERSE similar_files)
+                        list(GET similar_files 0 item)
+                        get_filename_component(result "${item}" NAME)
                     endif()
-                    list(GET similar_files 0 item)
-                    get_filename_component(result "${item}" NAME)
                 endif()
-            endif()
-        endforeach()
-    endif()
-    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")
+            endforeach()
+        endif()
+        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()
-            if(NOT result_lower MATCHES ".*so.*")
-                message(FATAL_ERROR "\"${result}\" is not a .so shared library")
+            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()
+            # TARGET_SONAME_FILE is not allowed for DLL target platforms.
+            if(WIN32)
+              set(result "$<TARGET_FILE_NAME:${TARGET}>")
+            else()
+              set(result "$<TARGET_SONAME_FILE_NAME:${TARGET}>")
             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()
-        # TARGET_SONAME_FILE is not allowed for DLL target platforms.
-        if(WIN32)
-          set(result "$<TARGET_FILE_NAME:${TARGET}>")
-        else()
-          set(result "$<TARGET_SONAME_FILE_NAME:${TARGET}>")
-        endif()
+        set(${DEST} ${result} PARENT_SCOPE)
     endif()
-    set(${DEST} ${result} PARENT_SCOPE)
 endfunction()
 
 function(sdl_check_project_in_subfolder relative_subfolder name vendored_option)