Skip to content

Instantly share code, notes, and snippets.

@cgmb
Last active August 8, 2023 17:21
Show Gist options
  • Save cgmb/c48bfa77da6c825a27bf05d25001d321 to your computer and use it in GitHub Desktop.
Save cgmb/c48bfa77da6c825a27bf05d25001d321 to your computer and use it in GitHub Desktop.
Script to enable ccache in CMake by default

To use, put AutoCCache.cmake in a directory named cmake at your project root, and add these two lines to CMakeLists.txt:

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
include(AutoCCache)

The AutoCCache script will automatically detect if ccache is available at CMake configuration time, and use it when available (unless -DAUTO_CCACHE=OFF is passed to CMake). To install ccache on Ubuntu, use sudo apt install ccache.

option(AUTO_CCACHE "Use ccache to speed up rebuilds" ON)
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM AND ${AUTO_CCACHE})
message(STATUS "Using ${CCACHE_PROGRAM} as compiler launcher")
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
# requires at least CMake 3.9 to be any use
set(CMAKE_CUDA_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment