8

As of version 4.6.1, ccache supports compilation with msvc.

On my Windows environment, I have ccache installed and available via the command line. I try to integrate ccache to my cmake project in the following way:

Root CMakeLists.txt:

find_program(CCACHE_FOUND ccache) 
if(CCACHE_FOUND)
    message("CCACHE is found")
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) # Less useful to do it for linking, see edit2
else(CCACHE_FOUND)
    message("CCACHE is NOT found")
endif(CCACHE_FOUND)

Here is my cmake configuration in CMakePresets.json:

{
"name": ",
"hidden": false,
"generator": "Visual Studio 17 2022",
"toolset": {
"value": "host=x64",
"strategy": "external"
},
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
"CMAKE_TOOLCHAIN_FILE": {
value": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
"type": "FILEPATH"
},
"VCPKG_INSTALLED_DIR": "${sourceDir}/build/packages",
"VCPKG_TARGET_TRIPLET": "x64-windows-static-md"
},
"vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } }
}

When Running the build, I can see ccache is found but I see no indication that it works or called by the build system.

Running ccache -s shows every stat is 0 as if ccache is never called.

Questions:

  • How to correctly configure ccache with MSVC & cmake?
  • How can I ensure ccache is working and the right commands are being used by the build system? is there a "verbose" option I can provide to cmake / ccache to debug this?
Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37

1 Answers1

2

The <LANG>_COMPILER_LAUNCHER property does not work with the Visual Studio generator. According do the docs, it is only available for the Makefile and Ninja generators: https://cmake.org/cmake/help/v3.24/prop_tgt/LANG_COMPILER_LAUNCHER.html

Specify a semicolon-separated list containing a command line for a compiler launching tool. The Makefile Generators and the Ninja generator will run this tool and pass the compiler and its arguments to the tool. Some example tools are distcc and ccache.

To make it work you need to specify the generator when configuring your CMake project, e.g.

cmake -S C:\project\src -B C:\project\build -G "Ninja"