Monday, January 19, 2009

HowTo: Using ccache on FreeBSD

ccache is a compiler cache. It speeds up re-compilation of C/C++ code by caching previous compiles and detecting when the same compile is being done again.

The following is a step by step guide to how to enable and use ccache on FreeBSD 7.1:
  1. % su
  2. # cd /usr/ports/devel/ccache
  3. # make install clean
  4. # vim /etc/make.conf
.if (!empty(.CURDIR:M/usr/src*) || !empty(.CURDIR:M/usr/obj*)) && !defined(NOCCACHE)
CC=/usr/local/libexec/ccache/world-cc
CXX=/usr/local/libexec/ccache/world-c++
.endif
Basically we've started by installing ccache (steps 1 through 3) and proceeded by editing /etc/make.conf as to enable ccache on builds.

Now we need to update the environment.

If you are using csh/tcsh shell add the following to /root/.cshrc:
setenv PATH /usr/local/libexec/ccache:$PATH
setenv CCACHE_PATH /usr/bin:/usr/local/bin
setenv CCACHE_DIR /var/tmp/ccache
setenv CCACHE_LOGFILE /var/log/ccache.log
If you are using zsh add the following to your /root/.zshrc file:
export PATH=/usr/local/libexec/ccache:$PATH
export CCACHE_PATH=/usr/bin:/usr/local/bin
export CCACHE_DIR=/var/tmp/ccache
export CCACHE_LOGFILE=/var/log/ccache.log
After updating the dotfiles we update the environment. Users of csh/tcsh shells can update by:
  • # source /root/.cshrc
Anyone using zsh can update the environment by running the following command:
  • # source /root/.zshrc
And that's it: ccache is installed and the environment is updated. Your next build will be performed with ccache enabled.

To display statistics summary:
  • % ccache -s
To zero statistics:
  • % ccache -z
To check out the help file for a list of ccache options:
  • % ccache -h
If you come across a port that fails to build, disable ccache and try again:
  • # make NOCCACHE=yes install clean
You can find more information regarding ccache through:

2 comments:

cykyc said...

I noticed in your make.conf settings, you have the variable "NOCCACHE" used in the define but on the command line you're using "NO_CACHE". A grep for NO_CACHE in /usr/ports/Mk/* returns 0 results. Is one of these a typo?

tangram said...

Yep you're right ;)

It should read make NOCCACHE=yes as the port's own documentation states. I've fixed the post.

Thanks for the feedback.