]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Faster compile times (#759)
authorAlexander Lanin <alex@lanin.de>
Sun, 17 Jan 2021 16:17:11 +0000 (17:17 +0100)
committerGitHub <noreply@github.com>
Sun, 17 Jan 2021 16:17:11 +0000 (17:17 +0100)
CMakeLists.txt
cmake/UseCcache.cmake [new file with mode: 0644]
cmake/UseFastestLinker.cmake [new file with mode: 0644]
doc/CMakeLists.txt

index b4a0ddcb9a526a23e94bb5feb04f8a31edf4197a..ca873c78b02831b0bac788b6ca5f373717259d17 100644 (file)
@@ -60,6 +60,8 @@ else()
 endif()
 message(STATUS "Ccache dev mode: ${CCACHE_DEV_MODE}")
 
+include(UseCcache)
+include(UseFastestLinker)
 include(StandardSettings)
 include(StandardWarnings)
 include(CIBuildType)
diff --git a/cmake/UseCcache.cmake b/cmake/UseCcache.cmake
new file mode 100644 (file)
index 0000000..49015c7
--- /dev/null
@@ -0,0 +1,68 @@
+# Note: Compiling ccache via ccache is fine, because this uses a stable version which
+# is installed on the system.
+
+
+# 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)
+  if(NOT ${CMAKE_VERSION} VERSION_LESS "3.15")
+    message(VERBOSE ${msg})
+  endif()
+endfunction()
+
+# Modified version of Craig Scott's "Professional CMake: A Practical Guide", 8th Edition
+function(use_ccache)
+  if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
+    message(WARNING "use_ccache() disabled, as it is not called from the project top level")
+    return()
+  endif()
+
+  find_program(CCACHE_PROGRAM ccache)
+  if(NOT CCACHE_PROGRAM)
+    message_verbose("Ccache program not found, not enabling ccache for faster recompilation")
+    return()
+  endif()
+
+  message_verbose("Ccache enabled for faster recompilation")
+
+  # This will override any config and environment settings.
+  # Worst case it's overriding better suited user defined values.
+  set(ccacheEnv
+    # Another option would be CMAKE_BINARY_DIR, however currently only one basedir is supported.
+    CCACHE_BASEDIR=${CMAKE_SOURCE_DIR}
+
+    # In case of very old ccache versions (pre 3.3)
+    CCACHE_CPP2=true
+
+    # This has been turned on by default in ccache 4.0
+    # CCACHE_COMPRESS=1
+  )
+
+  if(CMAKE_GENERATOR MATCHES "Ninja|Makefiles")
+    foreach(lang IN ITEMS C CXX OBJC OBJCXX CUDA)
+      set(CMAKE_${lang}_COMPILER_LAUNCHER
+        ${CMAKE_COMMAND} -E env ${ccacheEnv} ${CCACHE_PROGRAM}
+        PARENT_SCOPE)
+    endforeach()
+  elseif(CMAKE_GENERATOR STREQUAL Xcode)
+    foreach(lang IN ITEMS C CXX)
+      set(launcher ${CMAKE_BINARY_DIR}/launch-${lang})
+      file(WRITE ${launcher} "#!/bin/bash\n\n")
+      foreach(keyVal IN LISTS ccacheEnv)
+        file(APPEND ${launcher} "export ${keyVal}\n")
+      endforeach()
+      file(APPEND ${launcher}
+        "exec \"${CCACHE_PROGRAM}\" \"${CMAKE_${lang}_COMPILER}\" \"$@\"\n")
+      execute_process(COMMAND chmod a+rx ${launcher})
+    endforeach()
+    set(CMAKE_XCODE_ATTRIBUTE_CC ${CMAKE_BINARY_DIR}/launch-C PARENT_SCOPE)
+    set(CMAKE_XCODE_ATTRIBUTE_CXX ${CMAKE_BINARY_DIR}/launch-CXX PARENT_SCOPE)
+    set(CMAKE_XCODE_ATTRIBUTE_LD ${CMAKE_BINARY_DIR}/launch-C PARENT_SCOPE)
+    set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS ${CMAKE_BINARY_DIR}/launch-CXX PARENT_SCOPE)
+  endif()
+endfunction()
+
+option(USE_CCACHE "Use ccache to speed up recompilation time" TRUE)
+if(USE_CCACHE)
+  use_ccache()
+endif()
diff --git a/cmake/UseFastestLinker.cmake b/cmake/UseFastestLinker.cmake
new file mode 100644 (file)
index 0000000..8f392aa
--- /dev/null
@@ -0,0 +1,33 @@
+# 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)
+  if(NOT ${CMAKE_VERSION} VERSION_LESS "3.15")
+    message(VERBOSE ${msg})
+  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)
+    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)
+      message_verbose("Using gold linker for faster linking")
+    else()
+      message_verbose("Using default linker")
+    endif()
+  endif()
+endfunction()
+
+option(USE_FASTER_LINKER "Use the lld or gold linker instead of the default for faster linking" TRUE)
+if(USE_FASTER_LINKER)
+  use_fastest_linker()
+endif()
index c5ce224d9e99ff317e45f9ccf627107169da3fa6..dda47033aa60698d380248e63150f2066d38c371 100644 (file)
@@ -2,7 +2,7 @@ find_program(ASCIIDOC_EXE asciidoc)
 mark_as_advanced(ASCIIDOC_EXE) # Don't show in CMake UIs
 
 if(NOT ASCIIDOC_EXE)
-  message(WARNING "Could not find asciidoc; documentation will not be generated")
+  message(NOTICE "Could not find asciidoc; documentation will not be generated")
 else()
   #
   # HTML documentation