Skip to content

Commit

Permalink
Enable automatic compiler caching
Browse files Browse the repository at this point in the history
  • Loading branch information
daljit46 committed Aug 7, 2023
1 parent cca4e95 commit 63379df
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ set(CMAKE_INSTALL_RPATH ${base} ${base}/${relDir})

include(BuildType)
include(FindFFTW)
include(CompilerCache)

use_compiler_cache()

add_compile_definitions(
MRTRIX_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
Expand Down
6 changes: 1 addition & 5 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
"displayName" : "Base configuration",
"hidden" : true,
"generator" : "Ninja",
"binaryDir": "${sourceParentDir}/mrtrix3-build-${presetName}",
"cacheVariables" : {
"CMAKE_CXX_COMPILER_LAUNCHER" : "ccache"
}
"binaryDir": "${sourceParentDir}/mrtrix3-build-${presetName}"
},
{
"name": "debug",
Expand All @@ -33,7 +30,6 @@
"inherits" : "base",
"cacheVariables" : {
"CMAKE_BUILD_TYPE" : "Release",
"CMAKE_CXX_COMPILER_LAUNCHER" : "sccache",
"MRTRIX_STL_DEBUGGING" : "ON",
"MRTRIX_WARNINGS_AS_ERRORS" : "ON"
}
Expand Down
11 changes: 7 additions & 4 deletions cmake-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ For example, on Ubuntu you can add the following line to your `~/.bashrc`:
$ cmake -B build mrtrix3

If all the required dependencies are installed, `CMake` should correctly configure the project.
It's highly recommended that you use `Ninja` and `ccache` (or `sccache`) to configure the project,
to do this run `cmake -G Ninja -D CMAKE_CXX_COMPILER_LAUNCHER=ccache -B build mrtrix3` instead of
It's **highly** recommended that you use `Ninja` and `ccache` (or `sccache`) to configure the project,
to do this run `cmake -G Ninja -B build mrtrix3` instead of
second step above. This will provide you faster compilations speeds. You can install `Ninja` and
`ccache` using your system's package manager (e.g. `brew install ninja ccache` or `apt install ninja ccache`).

The project will automatically detect if `ccache` (or `sccache`) is installed on your system. You can also specify your own custom compiler caching tool by setting the `CACHE_OPTION` variable
(by specifying `-DCACHE_OPTION=custom_cache_tool`).

If you wish, we provide some default CMake presets that automatically configure the project using predefined
settings. You can view the list of available presets in [CMakePresets.json](https://github.com/MRtrix3/mrtrix3/blob/cmake_experimental_shared/CMakePresets.json). To configure the project using a given preset run
settings. You can view the list of available presets in [CMakePresets.json](https://github.com/MRtrix3/mrtrix3/blob/cmake_experimental_shared/CMakePresets.json)
or run `cmake --list-presets` in the source directory. To configure the project using a given preset run

$ cmake --preset name_of_configure_preset

Expand Down
33 changes: 33 additions & 0 deletions cmake/CompilerCache.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Automatically enable compiler caching if available
function(use_compiler_cache)
set(CACHE_OPTION
"ccache"
CACHE STRING "Compiler cache to be used")
set(CACHE_OPTION_VALUES "ccache" "sccache")
set_property(CACHE CACHE_OPTION PROPERTY STRINGS ${CACHE_OPTION_VALUES})
list(
FIND
CACHE_OPTION_VALUES
${CACHE_OPTION}
CACHE_OPTION_INDEX)

if(${CACHE_OPTION_INDEX} EQUAL -1)
message(
STATUS
"Using custom compiler cache system: '${CACHE_OPTION}', explicitly supported entries are ${CACHE_OPTION_VALUES}"
)
endif()

find_program(CACHE_BINARY NAMES ${CACHE_OPTION_VALUES})
if(CACHE_BINARY)
message(STATUS "${CACHE_BINARY} found and enabled")
set(CMAKE_CXX_COMPILER_LAUNCHER
${CACHE_BINARY}
CACHE FILEPATH "CXX compiler cache used")
set(CMAKE_C_COMPILER_LAUNCHER
${CACHE_BINARY}
CACHE FILEPATH "C compiler cache used")
else()
message(WARNING "${CACHE_OPTION} is enabled but was not found. Not using it")
endif()
endfunction()

0 comments on commit 63379df

Please sign in to comment.