I would like to do the following: If CCache is present in PATH, use "ccache g++" for compilation, else use g++. I tried writing a small my-cmake script containing
CC="ccache gcc" CXX="ccache g++" cmake $*
but it does not seem to work (running make still does not use ccache; I checked this using CMAKE_VERBOSE_MAKEFILE on).
Update:
As per this link I tried changing my script to
cmake -D CMAKE_CXX_COMPILER="ccache" -D CMAKE_CXX_COMPILER_ARG1="g++" -D CMAKE_C_COMPILER="ccache" -D CMAKE_C_COMPILER_ARG1="gcc" $*
but cmake bails out complaining that a test failed on using the compiler ccache (which can be expected).
From CMake 3.1, it is possible to use ccache with the Xcode generator and Ninja is supported from CMake 3.4 onwards. Ninja will honour
RULE_LAUNCH_COMPILE
just like the Unix Makefiles generator (so @Babcool's answer gets you there for Ninja too), but getting ccache working for the Xcode generator takes a little more work. The following article explains the method in detail, focussing on a general implementation which works for all three CMake generators and making no assumptions about setting up ccache symlinks or the underlying compiler used (it still lets CMake decide the compiler):https://crascit.com/2016/04/09/using-ccache-with-cmake/
The general gist of the article is as follows. The start of your
CMakeLists.txt
file should be set up something like this:The two script template files
launch-c.in
andlaunch-cxx.in
look like this (they should be in the same directory as theCMakeLists.txt
file):launch-c.in:
launch-cxx.in:
The above uses
RULE_LAUNCH_COMPILE
alone for Unix Makefiles and Ninja, but for the Xcode generator it relies on help from CMake'sCMAKE_XCODE_ATTRIBUTE_...
variables support. The setting of theCC
andCXX
user-defined Xcode attributes to control the compiler command andLD
andLDPLUSPLUS
for the linker command is not, as far as I can tell, a documented feature of Xcode projects, but it does seem to work. If anyone can confirm it is officially supported by Apple, I'll update the linked article and this answer accordingly.As of CMAKE 3.4 you can do:
In my opinion the best way is to symlink gcc,g++ to ccache, but if you would like to use within cmake, try this:
I didn't like to set a symlink from
g++
toccache
. AndCXX="ccache g++"
didn't work for me as some cmake test case wanted to have just the compiler program without attributes.So I used a small bash script instead:
and saved it as an executable in
/usr/bin/ccache-g++
.Then C configured cmake to use
/usr/bin/ccache-g++
as C++ compiler. This way it passes the cmake test cases and I feel more comfortable than having symlinks that I might forget about in 2 or 3 weeks and then maybe wonder if something doesn't work...It is now possible to specify ccache as a launcher for compile commands and link commands (since cmake 2.8.0). That works for Makefile and Ninja generator. To do this, just set the following properties :
It is also possible to set these properties only for specific directories or targets.
For Ninja, this is possible since version 3.4. For XCode, Craig Scott gives a workaround in his answer.
Edit : Thanks to uprego and Lekensteyn's comment, I edited the answer to check if ccache is available before using it as launcher and for which generators is it possible to use a compile launcher.
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 (likegcc
andgcc-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.