Skip to content

Commit

Permalink
cmake: detect linker id, and assume MSVC does not support version scr…
Browse files Browse the repository at this point in the history
…ipts
  • Loading branch information
madebr committed Nov 6, 2023
1 parent 9ca6c7d commit 977ec9f
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions cmake/PrivateSdlFunctions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 977ec9f

Please sign in to comment.