Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use CCache with CMake?

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).

like image 504
amit kumar Avatar asked Nov 29 '09 14:11

amit kumar


People also ask

How do I use ccache?

You can use ccache in two ways. The first is just to prefix your compile commands with "ccache". For example, you could change the "CC=gcc" line in your Makefile to be "CC=ccache gcc". Alternatively, you can create symbolic links from your compilers name to ccache.

How do I enable ccache?

To enable ccache, simply add '/usr/lib/ccache' to the beginning of your PATH. This directory contains symlinks to ccache, and ccache is smart enough to look at the name of the calling executable to determine which real executable to run.

How do I install ccache?

To install ccache with Homebrew, run brew install ccache . With MacPorts, run port install ccache . You can also download and install yourself, using the instructions in the repository. Make sure ccache can be found in your $PATH .


2 Answers

As of CMAKE 3.4 you can do:

-DCMAKE_CXX_COMPILER_LAUNCHER=ccache 
like image 119
Jacob Avatar answered Sep 22 '22 10:09

Jacob


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 :

find_program(CCACHE_FOUND ccache) if(CCACHE_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 endif(CCACHE_FOUND) 

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.

Edit2: @Emilio Cobos recommended to avoid doing that for the linking part as ccache doesn't improve linking speed and can mess with other types of cache like sccache

like image 21
Babcool Avatar answered Sep 23 '22 10:09

Babcool