Skip to content

Instantly share code, notes, and snippets.

@gglin001
Forked from cgmb/AutoCCache.cmake
Created February 3, 2023 09:56
Show Gist options
  • Save gglin001/0983b25038b644e518df46c5b49126f3 to your computer and use it in GitHub Desktop.
Save gglin001/0983b25038b644e518df46c5b49126f3 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