TExcelTAD

Path-Independent Hits: base_dir & hash_dir

CMake passes absolute paths to the compiler: -I/home/ada/work/game/src, -c /home/ada/work/game/src/util.cpp. Those paths are part of ccache's hash, so the same commit built in /home/ada/work/game and in /home/ada/review/game produces different keys and no shared hits. The same goes for CI agents that check out into /builds/agent-7/…, and for two build directories of one checkout.

Two settings fix it:

  • base_dir (CCACHE_BASEDIR): absolute paths below this directory are rewritten to paths relative to the current working directory before hashing. Set it to a directory that contains all checkouts (often $HOME on a workstation, or the CI workspace root).
  • hash_dir (CCACHE_HASHDIR, default true): when compiling with -g, ccache also hashes the current working directory, because GCC and Clang write it into the debug info. Turning it off lets debug builds share hits across directories, at the price of debug info that may name the other directory.

Two checkouts, four experiments

bash · try it yourself
st() { ccache --print-stats | awk -F'\t' '$1 ~ /^(direct_cache_hit|preprocessed_cache_hit|cache_miss)$/ && $2 > 0 { printf "%s=%s ", $1, $2 } END { print "" }'; }
make_checkout() {
  mkdir -p $1/src
  printf 'cmake_minimum_required(VERSION 3.25)\nproject(P CXX)\nadd_executable(app src/main.cpp src/util.cpp)\ntarget_include_directories(app PRIVATE src)\n' > $1/CMakeLists.txt
  printf '#pragma once\nint util();\n' > $1/src/util.hpp
  printf '#include "util.hpp"\n#include <string>\nint util() { return int(std::string("u").size()); }\n' > $1/src/util.cpp
  printf '#include "util.hpp"\nint main() { return util() - 1; }\n' > $1/src/main.cpp
}
make_checkout work/game
make_checkout review/game
build() {   # build one checkout from scratch and print what ccache did
  rm -rf $1/build
  cmake -S $1 -B $1/build -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache $2 > /dev/null
  ccache -z > /dev/null
  cmake --build $1/build > /dev/null
  printf '  %-12s %s\n' "$1:" "$(st)"
}
echo "A. defaults";                  ccache -C > /dev/null; build work/game; build review/game
echo "B. base_dir=\$HOME";           ccache -C > /dev/null; export CCACHE_BASEDIR=$HOME
                                     build work/game; build review/game
echo "C. base_dir, Debug (-g)";      ccache -C > /dev/null
                                     build work/game -DCMAKE_BUILD_TYPE=Debug; build review/game -DCMAKE_BUILD_TYPE=Debug
echo "D. base_dir, -g, hash_dir off"; ccache -C > /dev/null; export CCACHE_NOHASHDIR=1
                                     build work/game -DCMAKE_BUILD_TYPE=Debug; build review/game -DCMAKE_BUILD_TYPE=Debug
A. defaults
  work/game:   cache_miss=2 
  review/game: cache_miss=2 
B. base_dir=$HOME
  work/game:   cache_miss=2 
  review/game: direct_cache_hit=2 
C. base_dir, Debug (-g)
  work/game:   cache_miss=2 
  review/game: cache_miss=2 
D. base_dir, -g, hash_dir off
  work/game:   cache_miss=2 
  review/game: direct_cache_hit=2 
  • A. Identical code, different paths: the second checkout misses everything.
  • B. With base_dir, -I/home/learner/review/game/src becomes -I../src relative to the build directory, the same string as in the other checkout. Both compiles are direct hits.
  • C. With -g, the build directory itself (/home/learner/review/game/build) is hashed because it ends up in the debug info. Misses again.
  • D. With hash_dir off, debug builds hit too. The objects then carry the first checkout's directory in DW_AT_comp_dir; debuggers may need a source-path mapping (set substitute-path in gdb) when that matters.

Making paths irrelevant in the output too

base_dir only changes what ccache hashes. To make the objects themselves path-independent (good for reproducible builds as well as caching), map the directories in the compiler with -ffile-prefix-map (GCC 8+, Clang 10+; it implies -fdebug-prefix-map and -fmacro-prefix-map). ccache recognises a mapping of the current directory and then doesn't need to hash it, so debug builds share hits with hash_dir left on, and the debug info no longer names either checkout:

bash · try it yourself
st() { ccache --print-stats | awk -F'\t' '$1 ~ /^(direct_cache_hit|preprocessed_cache_hit|cache_miss)$/ && $2 > 0 { printf "%s=%s ", $1, $2 } END { print "" }'; }
make_checkout() {
  mkdir -p $1/src
  printf 'cmake_minimum_required(VERSION 3.25)\nproject(P CXX)\nadd_executable(app src/main.cpp src/util.cpp)\ntarget_include_directories(app PRIVATE src)\n' > $1/CMakeLists.txt
  printf '#pragma once\nint util();\n' > $1/src/util.hpp
  printf '#include "util.hpp"\n#include <string>\nint util() { return int(std::string("u").size()); }\n' > $1/src/util.cpp
  printf '#include "util.hpp"\nint main() { return util() - 1; }\n' > $1/src/main.cpp
}
make_checkout work/game
make_checkout review/game
build() {   # build one checkout from scratch and print what ccache did
  rm -rf $1/build
  cmake -S $1 -B $1/build -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache $2 > /dev/null
  ccache -z > /dev/null
  cmake --build $1/build > /dev/null
  printf '  %-12s %s\n' "$1:" "$(st)"
}
export CCACHE_BASEDIR=$HOME
for d in work/game review/game; do
  sed -i 's#^add_executable#add_compile_options("-ffile-prefix-map=${CMAKE_SOURCE_DIR}=." "-ffile-prefix-map=${CMAKE_BINARY_DIR}=build")\nadd_executable#' $d/CMakeLists.txt
done
grep prefix-map work/game/CMakeLists.txt
echo "E. base_dir, -g, prefix maps, hash_dir on"; ccache -C > /dev/null
build work/game -DCMAKE_BUILD_TYPE=Debug; build review/game -DCMAKE_BUILD_TYPE=Debug
add_compile_options("-ffile-prefix-map=${CMAKE_SOURCE_DIR}=." "-ffile-prefix-map=${CMAKE_BINARY_DIR}=build")
E. base_dir, -g, prefix maps, hash_dir on
  work/game:   cache_miss=2
  review/game: direct_cache_hit=2

In a real project, put those options in a toolchain file or CI preset rather than in every CMakeLists.txt.

Rules of thumb

Situation Setting
Developer machine, several worktrees base_dir = /home/<you> (or the parent of all worktrees) in ~/.config/ccache/ccache.conf
CI agents with per-job directories CCACHE_BASEDIR=$CI_WORKSPACE_ROOT ($GITHUB_WORKSPACE, $CI_BUILDS_DIR, …)
Debug builds should share hits add hash_dir = false, or map the build directory with prefix maps
Never base_dir = /: it rewrites system paths too, so different compilers' headers can look alike

Test yourself

Why did the second checkout miss every compile with default settings?

  1. The code was different
  2. Absolute -I and source paths differed, and they are part of the hash
  3. ccache only caches one directory
  4. Ninja disabled the cache

With base_dir set, why did Debug builds still miss?

  1. -g disables ccache
  2. With -g, ccache hashes the current directory (hash_dir), which differs per checkout
  3. Debug builds use a different compiler
  4. base_dir only works for Release

Which base_dir value is a mistake?

  1. $HOME
  2. The CI workspace root
  3. /
  4. The parent of all worktrees