ccache

From Gentoo Linux Wiki

Jump to: navigation, search

ccache is a compiler cache. It uses the GCC -E switch and a hash to detect when a compilation can be satisfied from cache. The effect is that packages frequently recompile faster on the condition that GCCs output stays the same. ccache will for instance not speed up compile time if you change your CFLAGS or often even USE flags.

The first time that you emerge a package after setting up ccache, there will be a very slight increase in its compilation time, but thereafter when the compilation is cached you will notice significant reductions in completion times.

Contents

[edit] Installation

Begin with emerging dev-util/ccache:

emerge -av dev-util/ccache

[edit] Portage

Warning: If you also use distcc, be sure you add the ccache option first and distcc second in your FEATURES list to take full advantage of both.

After you've emerged ccache, you'll need to add ccache to the FEATURE line in /etc/make.conf for emerge to use ccache:

File: /etc/make.conf
...
FEATURES="ccache"
CCACHE_DIR="/var/tmp/ccache"
CCACHE_SIZE="2G"
....

Also note the CCACHE_DIR and CCACHE_SIZE variables. They are optional, and are set to the defaults in the example above. See man make.conf for more information.

[edit] Userspace

There are two ways to configure ccache to work for the regular user. The first is to export the CC variable to point to the ccache symlink when you run the autotools configure script:

CC=/usr/lib/ccache/bin/gcc ./configure

Alternatively, set the PATH variable to begin with ccache's symlink directory (/usr/lib/ccache/bin/) in your profile (~/.bash_profile):

File: ~/.bash_profile
...
PATH="/usr/lib/ccache/bin/:${PATH}"
export ${PATH}

[edit] ccache-config

The other way to enable ccache system-wide by default is to have ccache-config install ccaches symlinks. To do this you'll first need to know your systems CHOST:

emerge --info | grep CHOST

This should output something similar to:

CHOST="i686-pc-linux-gnu"

Now run ccache-config with the --install-links argument followed by your CHOST. So, following our example above:

ccache-config --install-links i686-pc-linux-gnu

[edit] Statistics

To view ccache statistics, run ccache with the -s switch. This will report the users ccache usage, for example:

ccache -s
cache directory                     /home/gentoouser/.ccache
cache hit                           2500
cache miss                          1311
called for link                       10
compile failed                         3
couldn't find the compiler             1
autoconf compile/link                 98
no input file                         87
files in cache                      2622
cache size                         114.0 Mbytes
max cache size                     976.6 Mbytes

If you want to see the statistics for another user, for example if you want to see the statistics for your regular user when you're superuser, you'll need to prefix ccache -s with the CCACHE_DIR variable, so:

CCACHE_DIR=/home/gentoouser/.ccache ccache -s

[edit] Portage

To see the Portage ccache statistics, export the CCACHE_DIR variable you set in /etc/make.conf, if you didn't set this, the default is /var/tmp/ccache:

CCACHE_DIR=/var/tmp/ccache ccache -s

[edit] Troubleshooting

[edit] Genkernel

Genkernel doesn't use ccache by default, even when you've enabled it for root. You can easily fix this by appending the --kernel-cc option to your genkernel call:

genkernel --kernel-cc=/usr/lib/ccache/bin/gcc --menuconfig all

You can configure the KERNEL_CC setting to always use ccache with genkernel by editing /etc/genkernel.conf as follows:

Note: You'll need to set the CCACHE_DIR environment variable for this to work correctly.
File: /etc/genkernel.conf
...
KERNEL_CC="/usr/lib/ccache/bin/gcc"
...

[edit] Links

Personal tools