build: Enhance Ccache performance across worktrees and build trees
To maximize performance, it is essential to follow the recommendations in the Productivity Notes: set the base_dir Ccache configuration option or use the CCACHE_BASEDIR environment variable:
$ export CCACHE_BASEDIR=$HOME
Here are examples of usage on Ubuntu 24.04:
- Across different worktrees:
$ export CCACHE_DIR=$(mktemp -d)
$ git worktree add ../wt_1 4d3f360e2af94f4ed46dc0943196d548da82003e
$ pushd ../wt_1
$ cmake -B build -DWITH_CCACHE=ON
$ cmake --build build -t bitcoind -j $(nproc)
$ popd
$ git worktree add ../wt_2 4d3f360e2af94f4ed46dc0943196d548da82003e
$ pushd ../wt_2
$ cmake -B build -DWITH_CCACHE=ON
$ ccache --zero-stats
$ cmake --build build -t bitcoind -j $(nproc)
$ popd
$ ccache --show-stats
Cacheable calls: 302 / 302 (100.0%)
Hits: 302 / 302 (100.0%)
Direct: 302 / 302 (100.0%)
Preprocessed: 0 / 302 ( 0.00%)
Misses: 0 / 302 ( 0.00%)
Local storage:
Cache size (GiB): 0.2 / 5.0 ( 3.31%)
Hits: 302 / 302 (100.0%)
Misses: 0 / 302 ( 0.00%)
- Across different build trees:
$ export CCACHE_DIR=$(mktemp -d)
$ cmake -B build_1 -DWITH_CCACHE=ON
$ cmake --build build_1 -t bitcoind -j $(nproc)
$ cmake -B build_2 -DWITH_CCACHE=ON
$ ccache --zero-stats
$ cmake --build build_2 -t bitcoind -j $(nproc)
$ ccache --show-stats
Cacheable calls: 302 / 302 (100.0%)
Hits: 302 / 302 (100.0%)
Direct: 302 / 302 (100.0%)
Preprocessed: 0 / 302 ( 0.00%)
Misses: 0 / 302 ( 0.00%)
Local storage:
Cache size (GiB): 0.2 / 5.0 ( 3.31%)
Hits: 302 / 302 (100.0%)
Misses: 0 / 302 ( 0.00%)
This PR addresses this comment:
However, ccache does not hit across two different build dirs, compiling the same commit.
Fixes https://github.com/bitcoin/bitcoin/issues/31771.
Form https://github.com/bitcoin/bitcoin/actions/runs/13215680798/job/36894673006:
...
Treat compiler warnings as errors ..... ON
Use ccache for compiling .............. Built-in ccache support for the 'Visual Studio 17 2022' generator is not available.
-- Configuring done (300.1s)
-- Generating done (1.2s)
-- Build files have been written to: D:/a/bitcoin/bitcoin/build