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

This commit is contained in:
Anonymous Maarten 2023-11-06 00:40:41 +01:00
parent d2e005ee13
commit 761390b62f
1 changed files with 30 additions and 7 deletions

View File

@ -82,19 +82,42 @@ if(APPLE)
enable_language(OBJC)
endif()
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_supports_version_file VAR)
cmake_push_check_state()
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()
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()
set(${VAR} "${LINKER_SUPPORTS_VERSION_SCRIPT}" PARENT_SCOPE)
endfunction()
if(CMAKE_VERSION VERSION_LESS 3.18)
function(check_linker_flag LANG FLAG VAR)
cmake_push_check_state()
list(APPEND CMAKE_REQUIRED_LINK_OPTIONS ${FLAG} )
cmake_push_check_state(RESET)
list(APPEND CMAKE_REQUIRED_LINK_OPTIONS ${FLAG})
if(LANG STREQUAL "C")
include(CheckCSourceCompiles)
check_c_source_compiles("int main(int argc,char*argv[]){(void)argc;(void)argv;return 0;}" ${VAR} FAIL_REGEX "(unsupported|syntax error)")