From: Joel Rosdahl Date: Sat, 27 Mar 2021 13:28:13 +0000 (+0100) Subject: Probe for working faster linker X-Git-Tag: v4.2.1~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0da8a1181da478259d7a500d3df7e6441fe8558d;p=thirdparty%2Fccache.git Probe for working faster linker As noted by Nicholas Hutchinson in #794, the availability of a linker program is not enough to conclude that it works for some older compilers and platforms like macOS and Windows. Improve this by probing if it works to pass “-fuse-ld=$LINKER”. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 40e21a571..fc2ff6657 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,7 +66,9 @@ endif() message(STATUS "Ccache dev mode: ${CCACHE_DEV_MODE}") include(UseCcache) -include(UseFastestLinker) +if(NOT MSVC) + include(UseFastestLinker) +endif() include(StandardSettings) include(StandardWarnings) include(CIBuildType) diff --git a/cmake/UseFastestLinker.cmake b/cmake/UseFastestLinker.cmake index 8f392aac3..c3c6bdbfe 100644 --- a/cmake/UseFastestLinker.cmake +++ b/cmake/UseFastestLinker.cmake @@ -1,3 +1,6 @@ +include(CMakePushCheckState) +include(CheckCSourceRuns) + # Calls `message(VERBOSE msg)` if and only if VERBOSE is available (since CMake 3.15). # Call CMake with --loglevel=VERBOSE to view those messages. function(message_verbose msg) @@ -6,25 +9,40 @@ function(message_verbose msg) endif() endfunction() +function(try_linker linker) + string(TOUPPER ${linker} upper_linker) + find_program(HAS_LD_${upper_linker} "ld.${linker}") + if(HAS_LD_${upper_linker}) + cmake_push_check_state(RESET) + set(CMAKE_REQUIRED_LIBRARIES "-fuse-ld=${linker}") + check_c_source_runs("int main() { return 0; }" HAVE_LD_${upper_linker}) + cmake_pop_check_state() + endif() +endfunction() + function(use_fastest_linker) if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) message(WARNING "use_fastest_linker() disabled, as it is not called at the project top level") return() endif() - find_program(HAS_LD_LLD ld.lld) - if(HAS_LD_LLD) - link_libraries(-fuse-ld=lld) + set(use_default_linker 1) + try_linker(lld) + if (HAVE_LD_LLD) + link_libraries("-fuse-ld=lld") + set(use_default_linker 0) message_verbose("Using lld linker for faster linking") else() - find_program(HAS_LD_GOLD ld.gold) - if(HAS_LD_GOLD) - link_libraries(-fuse-ld=gold) + try_linker(gold) + if(HAVE_LD_GOLD) + link_libraries("-fuse-ld=gold") + set(use_default_linker 0) message_verbose("Using gold linker for faster linking") - else() - message_verbose("Using default linker") endif() endif() + if(use_default_linker) + message_verbose("Using default linker") + endif() endfunction() option(USE_FASTER_LINKER "Use the lld or gold linker instead of the default for faster linking" TRUE)