Skip to content

Commit dff8b7d

Browse files
authored
Merge pull request cpp-best-practices#62 from patrickelectric/add_cache
cmake: Add Cache file
2 parents e2f3cc4 + 9702018 commit dff8b7d

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ endif()
1818
# Link this 'library' to use the warnings specified in CompilerWarnings.cmake
1919
add_library(project_warnings INTERFACE)
2020

21+
# enable cache system
22+
include(cmake/Cache.cmake)
23+
2124
# standard compiler warnings
2225
include(cmake/CompilerWarnings.cmake)
2326
set_project_warnings(project_warnings)

cmake/Cache.cmake

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
option(ENABLE_CACHE "Enable cache if available" ON)
2+
if(NOT ENABLE_CACHE)
3+
return()
4+
endif()
5+
6+
set(CACHE_OPTION "ccache" CACHE STRING "Compiler cache to be used")
7+
set(CACHE_OPTION_VALUES "ccache" "sccache")
8+
set_property(CACHE CACHE_OPTION PROPERTY STRINGS ${CACHE_OPTION_VALUES})
9+
list(FIND CACHE_OPTION_VALUES ${CACHE_OPTION} CACHE_OPTION_INDEX)
10+
11+
if(${CACHE_OPTION_INDEX} EQUAL -1)
12+
message(STATUS "Using custom compiler cache system: '${CACHE_OPTION}', explicitly supported entries are ${CACHE_OPTION_VALUES}")
13+
endif()
14+
15+
find_program(CACHE_BINARY ${CACHE_OPTION})
16+
if(CACHE_BINARY)
17+
message(STATUS "${CACHE_OPTION} found and enabled")
18+
set(CMAKE_CXX_COMPILER_LAUNCHER ${CACHE_BINARY})
19+
else()
20+
message(WARNING "${CACHE_OPTION} is enabled but was not found. Not using it")
21+
endif()

cmake/StandardProjectSettings.cmake

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
1010
"MinSizeRel" "RelWithDebInfo")
1111
endif()
1212

13-
find_program(CCACHE ccache)
14-
if(CCACHE)
15-
message("using ccache")
16-
set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE})
17-
else()
18-
message("ccache not found cannot use")
19-
endif()
20-
2113
# Generate compile_commands.json to make it easier to work with clang based
2214
# tools
2315
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

0 commit comments

Comments
 (0)