diff options
author | Joel Rosdahl <joel@rosdahl.net> | 2021-03-27 14:28:13 +0100 |
---|---|---|
committer | Joel Rosdahl <joel@rosdahl.net> | 2021-03-27 15:30:44 +0100 |
commit | 0da8a1181da478259d7a500d3df7e6441fe8558d (patch) | |
tree | b91355122a22765df63b26f5113a1b20c81e6c3b /cmake | |
parent | 5b9a5b84ebefc4baafc784a36099945d9f2de19f (diff) | |
download | ccache-0da8a1181da478259d7a500d3df7e6441fe8558d.tar.gz |
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”.
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/UseFastestLinker.cmake | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/cmake/UseFastestLinker.cmake b/cmake/UseFastestLinker.cmake index 8f392aac..c3c6bdbf 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) |