ccache

Updated 2 February 2020

Introduction

ccache is a С / С++ compiler cache It makes recompilation faster by caching the results of previous compilations and checking when the same compilation is done again. The supported languages are C, C++, Objective-C and Objective-C++. Ccache was carefully written to always generate the exact same output that you would get without caching. The only way to know you are using ccache is the compilation speed.

Features

  • Keeps statistics on hits and misses
  • Automatic cache size adjustment
  • Able to cache compilations that have generated warnings.
  • Easy to install
  • Low load charges
  • Can compress cached files to reduce used disk space.

Limitations

  • Can cache compilation of only one file. For multiple file compilation, the layout will still rely on a real compiler.
  • Some compilation flags are not supported. In this case,

Using ccache in Calculate Linux

  • Package recompilation with custom USE flags
  • Package recompilation, including with patches
  • Using the live (9999) packages
  • With ccache, large packages such as Chromium, Libreoffice, Firefox can be safely installed from source with no risk of losing the result of hours of compilation due to an error.
  • Kernel recompilation

Installation

To install ccache, run:

emerge -a dev-util/ccache

Once installed, ccache will be tuned to work with Portage and cl-kernel. Its working directory is /var/calculate/ccache.

Configuration

The default ccache size is limited to 5 GB~. To set the maximum cache size, set the desired value for Portage:

/etc/portage/make.conf/custom

# Max ccache size
CCACHE_SIZE=10G

Note

The ccache size in /var/calculate/ccache/ccache.conf will be adjusted after any package compilation.

Usage

To view cache statistics, run:

ccache -s
cache directory                     /var/calculate/ccache
primary config                      /var/calculate/ccache/ccache.conf
secondary config      (readonly)    /etc/ccache.conf
stats updated                       Fri Jan 31 11:54:54 2020
stats zeroed                        Fri Jan 31 11:54:54 2020
cache hit (direct)                     0
cache hit (preprocessed)               0
cache miss                             0
cache hit rate                      0.00 %
cleanups performed                     0
files in cache                         0
cache size                           0.0 kB
max cache size                       5.0 GB

For instance, disable the binary repository for app-editors/vile to build this package from source code:

FEATURES="-getbinpkg" emerge -a app-editors/vile

As you browse the new statistics, you can see that a number of items were cached at compilation time, but not yet been reused:

ccache -s
cache directory                     /var/calculate/ccache
primary config                      /var/calculate/ccache/ccache.conf
secondary config      (readonly)    /etc/ccache.conf
stats updated                       Fri Jan 31 11:57:52 2020
stats zeroed                        Fri Jan 31 11:54:54 2020
cache hit (direct)                     0
cache hit (preprocessed)               0
cache miss                           194
cache hit rate                      0.00 %
called for link                      174
called for preprocessing              44
compile failed                         6
preprocessor error                     1
bad compiler arguments                 2
autoconf compile/link                100
no input file                          3
cleanups performed                    10
files in cache                       342
cache size                           3.0 MB
max cache size                       5.0 GB

Start to build vile again and check the cache:

FEATURES="-getbinpkg" emerge -a app-editors/vile

ccache -s
cache directory                     /var/calculate/ccache
primary config                      /var/calculate/ccache/ccache.conf
secondary config      (readonly)    /etc/ccache.conf
stats updated                       Fri Jan 31 12:03:17 2020
stats zeroed                        Fri Jan 31 11:54:54 2020
cache hit (direct)                   146
cache hit (preprocessed)              47
cache miss                           195
cache hit rate                     49.74 %
called for link                      348
called for preprocessing              88
compile failed                        12
preprocessor error                     2
bad compiler arguments                 4
autoconf compile/link                200
no input file                          6
cleanups performed                    10
files in cache                       344
cache size                           3.0 MB
max cache size                       5.0 GB

Now you can see that cache is in full use. When the maximum cache size is reached, ccache will delete the less used records.

To reset the stats, run:

ccache -z

To clear the cache, run:

ccache -C

Ccache will call the package manager and cl-kernel automatically. If you want to use ccache for any GCC compilation, reset the PATH variable:

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

If you want to always use ccache, add this to .bashrc:

echo 'export PATH="/usr/lib/ccache/bin:$PATH"' >> ~/.bashrc

An unauthorized user may need to specify their cache directory. Otherwise it will default to ~/.ccache.

echo 'export CCACHE_DIR="/var/calculate/userccache"' >> ~/.bashrc