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 6590b82
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,50 @@ elseif(UNIX AND NOT APPLE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,relro -Wl,-z,now")
endif()

# Compiler caching
find_program(CCACHE_PROGRAM ccache)
mark_as_advanced(FORCE CCACHE_PROGRAM)
if(NOT "${CCACHE_PROGRAM}" STREQUAL "CCACHE_PROGRAM-NOTFOUND")
option(CCACHE "Use ccache for compiler caching to speed up rebuilds." ON)
endif()

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

if(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()
elseif(CCACHE AND NOT WIN32)
# Don't use ccache on Windows because it is probably mingw and it will muck things up
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}")
else()
if(WIN32)
message(STATUS "No compiler caching enabled. Install sccache to enable compiler caching.")
else()
message(STATUS "No compiler caching enabled. Install sccache or ccache to enable compiler caching.")
endif()
endif()

set(QML_IMPORT_PATH ${CMAKE_SOURCE_DIR}/theme CACHE STRING "" FORCE)

add_subdirectory(csync)
Expand Down

0 comments on commit 6590b82

Please sign in to comment.