Skip to content

Commit

Permalink
CMake: use ccache and sccache for compiler caching
Browse files Browse the repository at this point in the history
First build on Fedora 35 with an Intel Core i7 8550U: 4.5 minutes

Repeat build with ccache after deleting CMake build directory
and creating a new one: 1.5 minutes

This code is adapted from Tenacity, written by myself and
@emabrey, licensed GPLv2 or later.

ccache is easily available from Linux distribution packages and
Homebrew on macOS. sccache is the only currently maintained
compiler cache for MSVC and can be installed easily from
Chocolatey on Windows.

Signed-off-by: Be <[email protected]>
  • Loading branch information
Be-ing committed Nov 3, 2021
1 parent 511da0f commit 602dc3b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cmake/modules/CompilerCaching.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# SPDX-FileCopyrightText: 2021 Tenacity Audio Editor contributors
#
# SPDX-License-Identifier: GPL-2.0-or-later
#[=======================================================================[.rst:
CompilerCaching
---------------

Search for sccache and ccache and use them for compiler caching for C & C++.
ccache is preferred if both are found, but the user can override this by
explicitly setting CCACHE=OFF to use sccache when both are installed.
#]=======================================================================]

# ccache does not support MSVC
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
find_program(CCACHE_PROGRAM ccache)
mark_as_advanced(FORCE CCACHE_PROGRAM)
if("${CCACHE_PROGRAM}" STREQUAL "CCACHE_PROGRAM-NOTFOUND")
message(STATUS "Could NOT find ccache")
else()
message(STATUS "Found ccache: ${CCACHE_PROGRAM}")
option(CCACHE "Use ccache for compiler caching to speed up rebuilds." ON)
endif()
endif()

find_program(SCCACHE_PROGRAM sccache)
mark_as_advanced(FORCE SCCACHE_PROGRAM)
if("${SCCACHE_PROGRAM}" STREQUAL "SCCACHE_PROGRAM-NOTFOUND")
message(STATUS "Could NOT find sccache")
else()
message(STATUS "Found sccache: ${SCCACHE_PROGRAM}")
option(SCCACHE "Use sccache for compiler caching to speed up rebuilds." ON)
endif()

if(CCACHE)
message(STATUS "Using ccache for compiler caching to speed up rebuilds")
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
elseif(SCCACHE)
message(STATUS "Using sccache for compiler caching to speed up rebuilds")
set(CMAKE_C_COMPILER_LAUNCHER "${SCCACHE_PROGRAM}")
set(CMAKE_CXX_COMPILER_LAUNCHER "${SCCACHE_PROGRAM}")

# Instruct MSVC to generate symbolic debug information within object files for sccache
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
if(IS_MULTI_CONFIG)
foreach(config ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER "${config}" config)
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_${config} "${CMAKE_CXX_FLAGS_${config}}")
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_${config} "${CMAKE_C_FLAGS_${config}}")
endforeach()
else()
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE} "${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}")
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE} "${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}}")
endif()
endif()
else()
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
message(STATUS "No compiler caching enabled. Install sccache to speed up rebuilds.")
else()
message(STATUS "No compiler caching enabled. Install ccache or sccache to speed up rebuilds.")
endif()
endif()
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if(NOT TOKEN_AUTH_ONLY)
endif()

include(ECMEnableSanitizers)
include(CompilerCaching)

find_package(Qt5 5.15 COMPONENTS Core Network Xml Concurrent REQUIRED)
find_package(Qt5 5.15 COMPONENTS WebEngineWidgets WebEngine)
Expand Down

0 comments on commit 602dc3b

Please sign in to comment.