]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Probe for working faster linker
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 27 Mar 2021 13:28:13 +0000 (14:28 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 27 Mar 2021 14:30:44 +0000 (15:30 +0100)
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”.

CMakeLists.txt
cmake/UseFastestLinker.cmake

index 40e21a57120414e9b88dec92fc6c85676a84d280..fc2ff6657352429bd8bd82ea04500b04fe592175 100644 (file)
@@ -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)
index 8f392aac3ed4e7c40b9e0772f2aa378b929afef2..c3c6bdbfebc84b5445e91e83c584c9295472f851 100644 (file)
@@ -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)