TExcelTAD

compiler_check, Sloppiness & Uncacheable Calls

ccache's defaults are chosen to be safe: when in doubt, it compiles. Three groups of settings decide how often that happens: how it recognises the compiler, which harmless differences it may ignore ("sloppiness"), and which calls it can't cache at all. Settings go in ccache.conf (ccache -o key=value writes it), or in CCACHE_<KEY> environment variables, which win over the file. ccache -p shows every setting and where it came from.

compiler_check: is it the same compiler?

The compiler is part of the hash, but hashing a 100 MB compiler binary on every call would be slow. compiler_check decides what represents it:

Value Hashes Trade-off
mtime (default) the compiler's size and modification time fast; misses everything when a container image or package reinstall gives the same compiler a new mtime
content the compiler binary's content robust across reinstalls; ccache caches the hash, so it's cheap after the first call
none nothing dangerous: a compiler upgrade returns objects built by the old one
string:<value> a string you choose, e.g. string:gcc-13.3.0 fast and stable if you set it from a reliable version source
a command, e.g. %compiler% -v the command's output precise (catches vendor patches), costs one extra process per call

The values aren't interchangeable from one call to the next: the setting is part of the key, so changing it makes earlier entries unreachable. This session shows it:

bash · try it yourself
mkdir -p cc && cd cc
printf 'int f() { return 1; }\n' > f.cpp
st() { ccache --print-stats | awk -F'\t' '$1 ~ /^(direct_cache_hit|cache_miss)$/ { printf "%s=%s ", $1, $2 } END { print "" }'; }
echo "default: $(ccache -k compiler_check)"
ccache -z > /dev/null
ccache g++ -c f.cpp -o f.o; ccache g++ -c f.cpp -o f.o
echo "mtime, twice:          $(st)"
export CCACHE_COMPILERCHECK=content
ccache g++ -c f.cpp -o f.o
echo "switched to content:   $(st)"
ccache g++ -c f.cpp -o f.o
echo "content, again:        $(st)"
default: mtime
mtime, twice:          cache_miss=1 direct_cache_hit=1 
switched to content:   cache_miss=2 direct_cache_hit=1 
content, again:        cache_miss=2 direct_cache_hit=2 

For CI with containers, content (or a string: with the exact compiler version) is the usual choice: the same image pulled onto a new agent then reuses the cache. Pick one value for the whole team and keep it.

Sloppiness: ignoring differences on purpose

Some inputs make ccache refuse or miss by default because they might change the output. sloppiness is a comma-separated list of those checks to relax:

Sloppiness Lets ccache… Only safe if…
time_macros cache code that uses __DATE__/__TIME__/__TIMESTAMP__ you don't care that the stamp is stale (or SOURCE_DATE_EPOCH fixes it)
pch_defines, include_file_mtime, include_file_ctime cache compiles that use precompiled headers the PCH and headers are rebuilt in the normal way
locale ignore LANG/LC_* compiler messages don't matter
system_headers skip hashing system headers in direct mode system headers never change under you (they do, on upgrades)
file_macro ignore __FILE__ differences paths in assertions don't matter
ivfsoverlay, modules, clang_index_store, … special cases for Apple/Clang features see the ccache manual

The most common case is precompiled headers. Without sloppiness, ccache doesn't cache any compile that uses a PCH. With CMake's target_precompile_headers, this session first shows the refusals, then the fix:

bash · try it yourself
mkdir -p pch/src && cd pch
printf '#pragma once\n#include <map>\n#include <string>\n' > src/common.hpp
for n in a b c; do printf '#include "common.hpp"\nint %s() { return int(std::map<std::string, int>{{"%s", 1}}.size()); }\n' $n $n > src/$n.cpp; done
echo 'int a(); int b(); int c(); int main() { return a() + b() + c() - 3; }' > src/main.cpp
printf 'cmake_minimum_required(VERSION 3.25)\nproject(P CXX)\nadd_executable(app src/main.cpp src/a.cpp src/b.cpp src/c.cpp)\ntarget_precompile_headers(app PRIVATE src/common.hpp)\n' > CMakeLists.txt
st() { ccache --print-stats | awk -F'\t' '$2 > 0 && $1 ~ /^(direct_cache_hit|cache_miss|could_not_use_precompiled_header)$/ { printf "%s=%s ", $1, $2 } END { print "" }'; }
clean_build() {
  rm -rf build && cmake -S . -B build -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache > /dev/null
  ccache -z > /dev/null && cmake --build build > /dev/null && echo "  $(st)"
}
echo "default sloppiness:"; clean_build; clean_build
export CCACHE_SLOPPINESS=pch_defines,time_macros,include_file_mtime,include_file_ctime
echo "with PCH sloppiness:"; ccache -C > /dev/null; clean_build; clean_build
default sloppiness:
  could_not_use_precompiled_header=5 
  could_not_use_precompiled_header=5 
with PCH sloppiness:
  cache_miss=5 
  direct_cache_hit=5 

Without the settings, every compile (the PCH itself plus the four sources) was counted as could_not_use_precompiled_header and ran uncached: a clean rebuild got nothing from the cache. With pch_defines,time_macros (the two the ccache manual requires for PCH; the include_file_* pair avoids misses when the freshly built .gch is newer than the compile that uses it) the second clean build is all hits.

Uncacheable calls

Some calls never produce a cache entry. They show up as their own counters in ccache --print-stats and under "Uncacheable" in ccache -sv:

Counter Cause What to do
called_for_link the launcher wraps a link nothing; links are not cached (CMake only uses the launcher for compiles anyway)
called_for_preprocessing -E nothing
unsupported_compiler_option e.g. -ftime-trace, some -Xclang options profile in a separate build dir
could_not_use_precompiled_header PCH without sloppiness set sloppiness as above
could_not_use_modules C++20 modules not cached; see Modules
multiple_source_files, no_input_file unusual command lines check the build system
compiler_produced_stdout a compiler that writes to stdout rare; wrapper scripts
bash · try it yourself
printf 'int f() { return 1; }\n' > f.cpp
ccache -z > /dev/null
ccache clang++ -ftime-trace -c f.cpp -o f.o
ccache g++ -E f.cpp > /dev/null
ccache g++ f.cpp -shared -fPIC -o libf.so
ccache --print-stats | awk -F'\t' '$2 > 0 && $1 ~ /^(called_for|unsupported|cache_miss|direct)/'
called_for_link	1
called_for_preprocessing	1
unsupported_compiler_option	1

A high uncacheable count in CI is a sign that something in the flags (often a profiling or coverage option left on) silently turned the cache off.

Test yourself

Your CI pulls the same compiler image onto fresh agents and ccache misses every time. Which setting is the likely fix?

  1. max_size
  2. compiler_check = content (or string:<version>)
  3. hash_dir = true
  4. depend_mode = false

Which sloppiness values does ccache need to cache compiles that use precompiled headers?

  1. locale
  2. pch_defines,time_macros
  3. system_headers
  4. None

What does a rising unsupported_compiler_option counter usually mean?

  1. The cache is full
  2. A flag such as -ftime-trace makes those compiles uncacheable
  3. The compiler crashed
  4. Links are being cached