How to Use Ccache with Cmake

How to Use CCache with CMake?

I personally have /usr/lib/ccache in my $PATH. This directory contains loads of symlinks for every possible name the compiler could be called from (like gcc and gcc-4.3), all pointing to ccache.

And I didn't even create the symlinks. That directory comes pre-filled when I install ccache on Debian.

CMake + ccache: RULE_LAUNCH_COMPILE or CMAKE_LANG_COMPILER_LAUNCHER

The first one will use the launcher for all languages in the build. The second one will use the same launcher for C and CXX but not for other languages. The two methods are slightly different as the second one would allow for a different launcher for different languages. The second method allows for finer control and may be preferred. There are also environment variables and directory and target properties for control which items use a launcher.

https://crascit.com/2016/04/09/using-ccache-with-cmake/

How ccache improves building speed?

Consider the case where you switch to some older branch of your project - that you did compile in the past and that ccache has cached, but CMake sees as "almost all files have changed and must be recompiled" - that's where you see a massive gain.

Another situation is where you have deleted your build directory (for some good reason) and now have to rebuild everything. ccache is also a huge help there.

Also; ccache is trivial to set up and is thenceforth completely invisible / transparent, so there really is no reason to not use it. When it helps it usually helps a lot, when it does not help it doesn't hurt.

CMake wants to use ccache instead of gcc

You are not supposed to change CMAKE_C_COMPILER and CMAKE_CXX_COMPILER from within the CMakeLists. Instead you should give them at the command line when running CMake for the first time:

cmake -D CMAKE_CXX_COMPILER=/path/to/g++ ..

Note that this value has no effect after the first run of CMake! So if you want to switch to a different compiler you will either have to delete the CMake cache or switch to a new build directory.

On the other hand, not having to specify this lengthy option for subsequent CMake runs of course also saves a lot of typing.

Using compiler prefix command(s) with CMake (distcc, ccache)

Since CMake 3.4.0 there has been a CMAKE_<LANG>_COMPILER_LAUNCHER variable and corresponding target property <LANG>_COMPILER_LAUNCHER. So if your project is C-only you would do something like:

cmake -DCMAKE_C_COMPILER_LAUNCHER=ccache /path/to/source
CCACHE_PREFIX=distcc make -j`distcc -j`

If you have a C++ project, use -DCMAKE_CXX_COMPILER_LAUNCHER=ccache.

Or, make your CMakeLists.txt smart and use ccache automatically if it can be found:

#-----------------------------------------------------------------------------
# Enable ccache if not already enabled by symlink masquerading and if no other
# CMake compiler launchers are already defined
#-----------------------------------------------------------------------------
find_program(CCACHE_EXECUTABLE ccache)
mark_as_advanced(CCACHE_EXECUTABLE)
if(CCACHE_EXECUTABLE)
foreach(LANG C CXX)
if(NOT DEFINED CMAKE_${LANG}_COMPILER_LAUNCHER AND NOT CMAKE_${LANG}_COMPILER MATCHES ".*/ccache")
message(STATUS "Enabling ccache for ${LANG}")
set(CMAKE_${LANG}_COMPILER_LAUNCHER ${CCACHE_EXECUTABLE} CACHE STRING "")
endif()
endforeach()
endif()


Related Topics



Leave a reply



Submit