Pages

Tuesday, June 16, 2015

[How to] Speed up compilation time with CCache

CCache stores the compiled version of your source files into a cache. If you tries to compile the same source files again, you will get a cache hit and will retrieve the compiled objects from the cache instead of compiling them again.

How it works?


 Instead of calling the GNU Gcc compiler directly, you will point your PATH to another Gcc binary provided by CCache that will check the cache first and them call the GNU Gcc if necessary:



How to install it?


Easy on Ubuntu:

$ sudo apt-get install ccache

Then change you PATH to point to the Ccache gcc (not just gcc) version:

$ export PATH="/usr/lib/ccache:$PATH"

Check with:

$ which gcc
/usr/lib/ccache/gcc

Done!!!

How can I know if it works? 


You can re-compile something and check if CCache is working with ccache -s command, you should have some cache hits:

You can increase/decrease your cache size with:

$ ccache --max-size=5G


Troubleshooting: Re-compiling Linux kernel never hit the cache?

Check if the flag CONFIG_LOCALVERSION_AUTO is set in you menuconfig, disable it and try again.
This flag seems to change a core header file as it appends the git version to the version string automatically, forcing CCache to recompile almost it all.


CCache with Icecc (edited):

If you want to use CCache with Icecc (that I'll explain about it in another post) to speed up even more your compilation time, use CCACHE_PREFIX=icecc (thanks Joel Rosdahl who commented about this)

$ export CCACHE_PREFIX=icecc

NOTE: You don't need to add icecc /usr/lib/icecc/bin in your PATH

2 comments:

  1. Hi!

    Just a note on combining ccache and icecream: There are a couple of drawbacks with using the masquerading technique for both tools. The preferred way of using ccache + icecream is to set CCACHE_PREFIX=icecc and then use ccache like before. Here are some more information and details: https://ccache.samba.org/manual.html#_using_ccache_with_other_compiler_wrappers

    Regards,
    Joel - ccache maintainer :-)

    ReplyDelete
    Replies
    1. Hi!

      Thank you a lot for your comment (and sorry for my late reply), I just updated the post (and my working environment) with this information.

      Thanks again
      Regards

      Delete