wiki:howto/ccache

<- Back to the HOWTO section

How to enable ccache

  • Audience: All users
  • Requires: MacPorts >=1.6.0

Introduction

This HOWTO covers how to enable building with ccache. It 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 compile 5-10 times faster than they would otherwise.

Installation

Step 1: Install ccache

$ sudo port install ccache

Step 2: Edit macports.conf

Open /opt/local/etc/macports/macports.conf in your favorite editor. Find the line

configureccache     no

and change it to

configureccache     yes

And you are done. From now on, MacPorts will use ccache for building.

If you want to limit how much disk space ccache may use, you can configure the maximum with ccache_size.

Configuration

When building ports, cache files are stored in a directory inside the MacPorts prefix, which is at /opt/local/var/macports/build/.ccache by default. Additionally, there is also a ~/.ccache directory in your home, which is used if you run manual builds with ccache outside of MacPorts.

To see some statistics how useful ccache is for your ports and how much disk space it takes, use

$ CCACHE_DIR=/opt/local/var/macports/build/.ccache ccache -s

Optional Parts

Use ccache outside MacPorts

MacPorts does not create symlinks to the local compiler therefore you need to create them manually.

One way could be to use the following script that finds the available compilers and create symlinks on /opt/local/libexec/ccache.

#!/bin/sh

##
## run with sudo or adjust prefix/dest_links
##

# adjust if needed
prefix="/opt/local"
dest_links="${prefix}/libexec/ccache"
bin_paths="/usr/bin /opt/local/bin /usr/local/bin"
ccache_bin=$(which ccache)

# set umask to avoid strict starting shell
umask 022

if [ ! -d ${dest_links} ]; then
  install -d ${dest_links}
fi

find $bin_paths \( -name "gcc*" -or -name "cc*" \
  -or -name "g++*" -or -name "c++*" \
  -or -name "clang++-mp*" -or -name "clang-mp*" \) | \
  egrep -v "cmake*|ccache*|ccomps*|c\+\+filt*" | \
while read file; do
  base=$(basename $file)
  ln -s ${ccache_bin} ${dest_links}/${base}
done

echo ">> add $dest_links in front of your \$PATH"

Now to use ccache also outside MacPorts you have to edit your PATH which is set in your .profile or .bash_profile.

First, locate the file you are using. If there is a .bash_profile then edit this file, if there is only .profile then you want to edit this.

There will be this line in there:

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

Now add the ccache binary path /opt/local/ccache/bin in front of already existing paths:

export PATH=/opt/local/libexec/ccache:/opt/local/bin:/opt/local/sbin:$PATH

Important: Reopen your Terminal afterwards.

If you now use commands like gcc or clang they automatically go through ccache.

<- Back to the HOWTO section

Last modified 4 years ago Last modified on Apr 4, 2021, 6:22:41 PM