ccache with FreeBSD
Using ccache with FreeBSD and portupgrade to speed up compilation of port builds.
The flexibility given by source based[1] installation system of FreeBSD also has a downside in terms of long compile times for some large packages such as FireFox Often minor updates or patches can result in the time consuming process of recompiling an entire port. To avoid this there is ccache which is a compiler cache utility that caches previous compiles and detects whether another compile is required.
Installing ccache
Install ccache as you would any other port, it is available in
devel/ccache
Configuring ccache
ccache requires a cache directory to store cached compilations. As
the default is to store it the users directory, this will result
in the cache being stored in the root user directory /root as normally
ports are installed by the root user. This is your root partition, and
it is good practice to do as few read/write operations on that partition
as possible.
Cache directory
Edit your root's /root/.cshrc file and put in a different cache directory
setenv CCACHE_DIR /usr/local/ccache
/var/cache/ccache or /tmp/ccache are good options to put your cached compiles if those partitions are quite large on your system (1-2GB)
Cache directory size
Simply run ccache with the -M option. To set a 2G cache directory:
ccache -M 2G
ccache usage
Please read the following file for the latest up to date information on usage first.
/usr/local/share/doc/ccache/ccache-howto-freebsd.txt
Configuring ccache use via changing order of paths
Inserting the path to ccache symbolic links to cc, and other compilers ahead of /usr/bin/ will result in ccache being used transparently by ports. This is the least problematic solution to using ccache by default. It does not however work for kernel and world builds, for this you need to set ccache options in make.conf
/root/.cshrc
set path = (/sbin /usr/local/libexec/ccache /bin ...
Configuring ccache for building world and kernel
Add the following lines to /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
Configuring ccache use via make.conf
By default the ccache port will install compiler links for ccache in /usr/local/libexec/ccache. You can then change your /etc/make.conf settings by adding these lines to it:
CC=/usr/local/libexec/ccache/cc
CXX=/usr/local/libexec/ccache/c++
CPP=/usr/local/libexec/ccache/cpp
It will not work for ports that do not respect
global environment variables, and a lot of ports will have problems.
Configuring portupgrade to use ccache
Unfortunately universally setting CC (C), CXX (C++) and CPP options in /etc/make.conf such as stated before may result in some port builds breaking as well as possibly kernel and world builds.
The another way to enable ccache is by doing it on a port by port basis, using /usr/local/etc/pkgtools.conf which sets port build environment of individual ports when using portupgrade (sysutils/portupgrade).
MAKE_ARGS = {
'editors/openoffice-1.1' => 'WITHOUT_MOZILLA=1 WITH_CCACHE=1',
'net/samba3' => 'CC="ccache cc" CXX="ccache c++" CPP="ccache cpp"'
}
In pkgtools.conf example above, you can see that openoffice already has
support for ccache in it's build option, which you can enable by passing
the WITH_CCACHE=1 option. However samba3 doesn't, so here I've defined
the compiler variables to use ccache. You can use those variables for
any other ports that does not have problems with ccache for example,
x11/xorg* for all xorg ports, or qt* for all qt ports.
A recent upgrade and recomplie of xorg-clients and xorg-server to fix a bug for i830 chipset took little more than 5 minutes, as only certain drivers need to be recompiled, with the rest alrady stored in cache and picked up by ccache.
[1] FreeBSD also supports binary based packages if users are tracking a release version.



