SDL_ttf: cmake: detect linker id, and assume MSVC does not support version scripts

From 977ec9f3a654a2e22143509c5d604f4e54866970 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Mon, 6 Nov 2023 01:00:39 +0100
Subject: [PATCH] cmake: detect linker id, and assume MSVC does not support
 version scripts

---
 cmake/PrivateSdlFunctions.cmake | 34 ++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/cmake/PrivateSdlFunctions.cmake b/cmake/PrivateSdlFunctions.cmake
index 5b33fcc..e2ff173 100644
--- a/cmake/PrivateSdlFunctions.cmake
+++ b/cmake/PrivateSdlFunctions.cmake
@@ -240,15 +240,35 @@ macro(sdl_check_linker_flag flag var)
     cmake_pop_check_state()
 endmacro()
 
+function(SDL_detect_linker)
+    if(CMAKE_VERSION VERSION_LESS 3.29)
+        if(NOT DEFINED SDL_CMAKE_C_COMPILER_LINKER_ID)
+            execute_process(COMMAND ${CMAKE_LINKER} -v OUTPUT_VARIABLE LINKER_OUTPUT ERROR_VARIABLE LINKER_OUTPUT)
+            string(REGEX REPLACE "[\r\n]" " " LINKER_OUTPUT "${LINKER_OUTPUT}")
+            if(LINKER_OUTPUT MATCHES ".*Microsoft.*")
+                set(linker MSVC)
+            else()
+                set(linker GNUlike)
+            endif()
+            message(STATUS "Linker identification: ${linker}")
+            set(SDL_CMAKE_C_COMPILER_LINKER_ID "${linker}" CACHE STRING "Linker identification")
+            mark_as_advanced(SDL_CMAKE_C_COMPILER_LINKER_ID)
+        endif()
+        set(CMAKE_C_COMPILER_LINKER_ID "${SDL_CMAKE_C_COMPILER_LINKER_ID}")
+    endif()
+endfunction()
+
 function(check_linker_support_version_script VAR)
-    cmake_push_check_state(RESET)
-    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/dummy.sym" "n_0 {\n global:\n  func;\n local: *;\n};\n")
-    list(APPEND CMAKE_REQUIRED_LINK_OPTIONS "-Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/dummy.sym")
-    if(MSVC)
-        list(APPEND CMAKE_REQUIRED_LINK_OPTIONS "/WX")
+    SDL_detect_linker()
+    if(CMAKE_C_COMPILER_LINKER_ID MATCHES "^(MSVC)$")
+        set(LINKER_SUPPORTS_VERSION_SCRIPT FALSE)
+    else()
+        cmake_push_check_state(RESET)
+        file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/dummy.sym" "n_0 {\n global:\n  func;\n local: *;\n};\n")
+        list(APPEND CMAKE_REQUIRED_LINK_OPTIONS "-Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/dummy.sym")
+        check_c_source_compiles("int func(void) {return 0;} int main(int argc,char*argv[]){(void)argc;(void)argv;return func();}" LINKER_SUPPORTS_VERSION_SCRIPT FAIL_REGEX "(unsupported|syntax error|unrecognized option)")
+        cmake_pop_check_state()
     endif()
-    check_c_source_compiles("int func(void) {return 0;} int main(int argc,char*argv[]){(void)argc;(void)argv;return func();}" LINKER_SUPPORTS_VERSION_SCRIPT FAIL_REGEX "(unsupported|syntax error|unrecognized option)")
-    cmake_pop_check_state()
     set(${VAR} "${LINKER_SUPPORTS_VERSION_SCRIPT}" PARENT_SCOPE)
 endfunction()