From 4e97de2149a1d4669b7fefb2e824223a5c659cee Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Sat, 24 Oct 2026 13:29:51 +0200 Subject: [PATCH] ci: persist ccache between runs and use it for monero-cpp and bindings monero-project already picked up ccache, but nothing kept the cache between runs, so every job started cold. Restore/save .ccache in the test, macOS, Windows and deb workflows, and pass the compiler launcher to monero-cpp and monero-python, which didn't use ccache at all. Pull requests only restore the cache, pushes to main and manual runs save it. Add USE_CCACHE (ON) to the root CMakeLists and document the limits in docs/ci-ccache.md. Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/build-deb.yml | 17 ++++++ .github/workflows/build-macos.yml | 30 +++++++++- .github/workflows/build-windows.yml | 28 ++++++++- .github/workflows/test.yml | 88 +++++++++++++++++++++++++++-- .gitignore | 3 +- CMakeLists.txt | 11 ++++ README.md | 2 + docker/build.sh | 13 ++++- docs/ci-ccache.md | 39 +++++++++++++ 9 files changed, 222 insertions(+), 9 deletions(-) create mode 100644 docs/ci-ccache.md diff --git a/.github/workflows/build-deb.yml b/.github/workflows/build-deb.yml index 192544c1..3a1a7e8f 100644 --- a/.github/workflows/build-deb.yml +++ b/.github/workflows/build-deb.yml @@ -113,6 +113,14 @@ jobs: --load \ . + - name: Restore ccache + uses: actions/cache/restore@v4 + with: + path: .ccache + key: ccache-deb-${{ matrix.name }}-${{ matrix.arch }}_${{ github.sha }} + restore-keys: | + ccache-deb-${{ matrix.name }}-${{ matrix.arch }}_ + - name: Build monero-python uses: nick-fields/retry@ad984534de44a9489a53aefd81eb77f87c70dc60 # v4.0.0 with: @@ -129,9 +137,18 @@ jobs: --platform $PLATFORM \ -v $PWD:/monero-python -w /monero-python \ -e PACKAGE_NAME="${PACKAGE_NAME}" \ + -e CCACHE_DIR=/monero-python/.ccache \ + -e CCACHE_MAXSIZE=500M \ monero-python:${{ matrix.name }}-${{ matrix.arch }} \ bash -euxc "./docker/build.sh" + - name: Save ccache + if: ${{ !cancelled() }} + uses: actions/cache/save@v4 + with: + path: .ccache + key: ccache-deb-${{ matrix.name }}-${{ matrix.arch }}_${{ github.sha }} + - name: Upload deb package uses: actions/upload-artifact@v4 with: diff --git a/.github/workflows/build-macos.yml b/.github/workflows/build-macos.yml index b3a520e1..ea803ab8 100644 --- a/.github/workflows/build-macos.yml +++ b/.github/workflows/build-macos.yml @@ -13,6 +13,10 @@ jobs: build: name: ${{ matrix.os }} runs-on: ${{ matrix.os }} + env: + CCACHE_DIR: ${{ github.workspace }}/.ccache + CCACHE_MAXSIZE: 500M + CCACHE_COMPILERCHECK: "%compiler% -v" strategy: fail-fast: false @@ -27,7 +31,7 @@ jobs: - name: Install dependencies run: | - HOMEBREW_NO_AUTO_UPDATE=1 brew install python boost@1.85 hidapi openssl zmq libpgm miniupnpc expat libunwind-headers protobuf unbound + HOMEBREW_NO_AUTO_UPDATE=1 brew install python boost@1.85 hidapi openssl zmq libpgm miniupnpc expat libunwind-headers protobuf unbound ccache brew unlink boost || true brew link boost@1.85 --force pip3 install pytest pytest-cov setuptools wheel scikit-build-core --break-system-packages @@ -40,6 +44,17 @@ jobs: run: | pip3 install delocate --break-system-packages + - name: Restore ccache + uses: actions/cache/restore@v4 + with: + path: .ccache + key: ccache-${{ matrix.os }}_${{ github.sha }} + restore-keys: | + ccache-${{ matrix.os }}_ + + - name: Reset ccache statistics + run: ccache --zero-stats + - name: Build monero run: | cd external/monero-cpp/external/monero-project @@ -54,7 +69,7 @@ jobs: cd external/monero-cpp mkdir -p build cd build - cmake .. + cmake .. -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache cmake --build . make -j3 cd ../../../ @@ -64,6 +79,17 @@ jobs: mkdir -p build python3 -m pip wheel . -w dist --no-build-isolation + - name: Show ccache statistics + if: ${{ !cancelled() }} + run: ccache --show-stats + + - name: Save ccache + if: ${{ !cancelled() }} + uses: actions/cache/save@v4 + with: + path: .ccache + key: ccache-${{ matrix.os }}_${{ github.sha }} + - name: Repair wheel with delocate run: | delocate-wheel -w wheelhouse -v dist/*.whl diff --git a/.github/workflows/build-windows.yml b/.github/workflows/build-windows.yml index 04435a00..897d45e4 100644 --- a/.github/workflows/build-windows.yml +++ b/.github/workflows/build-windows.yml @@ -13,6 +13,9 @@ jobs: msys2-mingw64: name: MSYS2 MinGW64 runs-on: windows-latest + env: + CCACHE_DIR: ${{ github.workspace }}/.ccache + CCACHE_MAXSIZE: 500M defaults: run: @@ -50,6 +53,7 @@ jobs: mingw-w64-x86_64-protobuf mingw-w64-x86_64-libusb mingw-w64-x86_64-ntldd + mingw-w64-x86_64-ccache git make gettext @@ -114,6 +118,17 @@ jobs: echo "5b56ad87bc544336b2d51edb8bb0614f46542571362daf4d7e53bf23120175a1 mingw-w64-x86_64-pybind11-2.13.6-2-any.pkg.tar.zst" | sha256sum -c pacman -U --noconfirm mingw-w64-x86_64-pybind11-2.13.6-2-any.pkg.tar.zst + - name: Restore ccache + uses: actions/cache/restore@v4 + with: + path: .ccache + key: ccache-windows_${{ github.sha }} + restore-keys: | + ccache-windows_ + + - name: Reset ccache statistics + run: ccache --zero-stats + - name: Build monero run: | cd external/monero-cpp/external/monero-project @@ -135,7 +150,7 @@ jobs: cd external/monero-cpp mkdir -p build cd build - cmake .. + cmake .. -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache cmake --build . - name: Build wheel @@ -146,6 +161,17 @@ jobs: -C cmake.define.Python3_FIND_STRATEGY=LOCATION \ -C cmake.define.Python3_FIND_REGISTRY=NEVER + - name: Show ccache statistics + if: ${{ !cancelled() }} + run: ccache --show-stats + + - name: Save ccache + if: ${{ !cancelled() }} + uses: actions/cache/save@v4 + with: + path: .ccache + key: ccache-windows_${{ github.sha }} + - name: Repair wheel with delvewheel run: | export WIN_PYTHON_EXE=$(cygpath -u "$pythonLocation/python.exe") diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7d37f9e2..d038d7e7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,6 +17,9 @@ jobs: linux: runs-on: ubuntu-24.04 name: linux (pytest) + env: + CCACHE_DIR: ${{ github.workspace }}/.ccache + CCACHE_MAXSIZE: 500M steps: - name: Checkout repository @@ -52,6 +55,17 @@ jobs: sudo make install cd ../ + - name: Restore ccache + uses: actions/cache/restore@v4 + with: + path: .ccache + key: ccache-linux-test_${{ github.sha }} + restore-keys: | + ccache-linux-test_ + + - name: Reset ccache statistics + run: ccache --zero-stats + - name: Build monero run: | cd external/monero-cpp/external/monero-project @@ -66,7 +80,7 @@ jobs: cd external/monero-cpp mkdir -p build cd build - cmake .. -DENABLE_COVERAGE=ON + cmake .. -DENABLE_COVERAGE=ON -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache cmake --build . make -j3 cd ../../../ @@ -78,6 +92,18 @@ jobs: cmake --build build cp build/monero.cpython-312-x86_64-linux-gnu.so ~/.local/lib/python3.12/site-packages/ + - name: Show ccache statistics + if: ${{ !cancelled() }} + run: ccache --show-stats + + - name: Save ccache + # Pull requests only read the cache, pushes to main and manual runs populate it. + if: ${{ !cancelled() && github.event_name != 'pull_request' }} + uses: actions/cache/save@v4 + with: + path: .ccache + key: ccache-linux-test_${{ github.sha }} + - name: Setup test environment run: | docker compose -f tests/docker-compose.yml up -d node_1 node_2 xmr_wallet_1 xmr_wallet_2 xmr_wallet_3 @@ -158,6 +184,9 @@ jobs: windows: name: windows (pytest) runs-on: windows-latest + env: + CCACHE_DIR: ${{ github.workspace }}/.ccache + CCACHE_MAXSIZE: 500M defaults: run: @@ -195,6 +224,7 @@ jobs: mingw-w64-x86_64-protobuf mingw-w64-x86_64-libusb mingw-w64-x86_64-ntldd + mingw-w64-x86_64-ccache git make gettext @@ -259,6 +289,17 @@ jobs: echo "5b56ad87bc544336b2d51edb8bb0614f46542571362daf4d7e53bf23120175a1 mingw-w64-x86_64-pybind11-2.13.6-2-any.pkg.tar.zst" | sha256sum -c pacman -U --noconfirm mingw-w64-x86_64-pybind11-2.13.6-2-any.pkg.tar.zst + - name: Restore ccache + uses: actions/cache/restore@v4 + with: + path: .ccache + key: ccache-windows_${{ github.sha }} + restore-keys: | + ccache-windows_ + + - name: Reset ccache statistics + run: ccache --zero-stats + - name: Build monero run: | cd external/monero-cpp/external/monero-project @@ -280,7 +321,7 @@ jobs: cd external/monero-cpp mkdir -p build cd build - cmake .. + cmake .. -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache cmake --build . - name: Build monero-python @@ -300,6 +341,18 @@ jobs: done cd .. + - name: Show ccache statistics + if: ${{ !cancelled() }} + run: ccache --show-stats + + - name: Save ccache + # Pull requests only read the cache, pushes to main and manual runs populate it. + if: ${{ !cancelled() && github.event_name != 'pull_request' }} + uses: actions/cache/save@v4 + with: + path: .ccache + key: ccache-windows_${{ github.sha }} + - name: Run unit tests shell: bash env: @@ -312,6 +365,10 @@ jobs: macos: name: ${{ matrix.os }} (pytest) runs-on: ${{ matrix.os }} + env: + CCACHE_DIR: ${{ github.workspace }}/.ccache + CCACHE_MAXSIZE: 500M + CCACHE_COMPILERCHECK: "%compiler% -v" strategy: fail-fast: false @@ -326,7 +383,7 @@ jobs: - name: Install dependencies run: | - HOMEBREW_NO_AUTO_UPDATE=1 brew install python boost@1.85 hidapi openssl zmq libpgm miniupnpc expat libunwind-headers protobuf unbound + HOMEBREW_NO_AUTO_UPDATE=1 brew install python boost@1.85 hidapi openssl zmq libpgm miniupnpc expat libunwind-headers protobuf unbound ccache brew unlink boost || true brew link boost@1.85 --force pip3 install pytest pytest-rerunfailures pytest-timeout pytest-cov setuptools wheel scikit-build-core --break-system-packages @@ -335,6 +392,17 @@ jobs: run: | pip3 install pybind11==2.13.6 pybind11-stubgen --break-system-packages + - name: Restore ccache + uses: actions/cache/restore@v4 + with: + path: .ccache + key: ccache-${{ matrix.os }}_${{ github.sha }} + restore-keys: | + ccache-${{ matrix.os }}_ + + - name: Reset ccache statistics + run: ccache --zero-stats + - name: Build monero run: | cd external/monero-cpp/external/monero-project @@ -349,7 +417,7 @@ jobs: cd external/monero-cpp mkdir -p build cd build - cmake .. + cmake .. -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache cmake --build . make -j3 cd ../../../ @@ -359,6 +427,18 @@ jobs: mkdir -p build pip3 install --no-build-isolation -vvv . --break-system-packages + - name: Show ccache statistics + if: ${{ !cancelled() }} + run: ccache --show-stats + + - name: Save ccache + # Pull requests only read the cache, pushes to main and manual runs populate it. + if: ${{ !cancelled() && github.event_name != 'pull_request' }} + uses: actions/cache/save@v4 + with: + path: .ccache + key: ccache-${{ matrix.os }}_${{ github.sha }} + - name: Run unit tests run: | pytest -m unit diff --git a/.gitignore b/.gitignore index b5ce8657..3b6f0434 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ coverage* .coverage .cache monero.log -monero_tests_*.log \ No newline at end of file +monero_tests_*.log +.ccache diff --git a/CMakeLists.txt b/CMakeLists.txt index ea9684dc..61618865 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,17 @@ if (WIN32) add_definitions( "-DWIN32_LEAN_AND_MEAN" ) endif() +option(USE_CCACHE "Use ccache if found" ON) + +# Respect a launcher given explicitly (-DCMAKE_CXX_COMPILER_LAUNCHER or env). +if(USE_CCACHE AND NOT CMAKE_CXX_COMPILER_LAUNCHER) + find_program(CCACHE_PROGRAM ccache) + if(CCACHE_PROGRAM) + message(STATUS "Using ccache: ${CCACHE_PROGRAM}") + set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}") + endif() +endif() + option(ENABLE_COVERAGE "Enable coverage" OFF) if(ENABLE_COVERAGE) diff --git a/README.md b/README.md index d7c53c48..fa493efc 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,8 @@ wallet_full.close(True) ``` 6. Or run build script: `./bin/build_libmonero_python.sh` +> **Faster rebuilds:** if [ccache](https://ccache.dev/) is installed it is used automatically for monero and monero-python (pass `-DUSE_CCACHE=OFF` to cmake to disable it). To use it for monero-cpp as well, build that with `-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache`. + ### Linux Docker Build 1. Install [Docker](https://docs.docker.com/engine/install/) diff --git a/docker/build.sh b/docker/build.sh index 16365b8b..5045c6b4 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -26,6 +26,13 @@ echo "Architecture: ${ARCH}" git submodule update --init --recursive mkdir -p build +# monero-project picks ccache up on its own, monero-cpp has to be told to use it +CCACHE_CMAKE_ARGS=() +if command -v ccache >/dev/null 2>&1; then + ccache --zero-stats + CCACHE_CMAKE_ARGS=(-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache) +fi + # build monero-project dependencies cd ./external/monero-cpp/external/monero-project/ git submodule update --init --force @@ -49,13 +56,17 @@ cd ../../../../ # build libmonero-cpp library mkdir -p build && cd build && -cmake .. && +cmake .. "${CCACHE_CMAKE_ARGS[@]}" && cmake --build . && make . cd ../../../ pip3 install -vvv . --no-build-isolation --target build/${PACKAGE_NAME}/usr/lib/python3/dist-packages +if command -v ccache >/dev/null 2>&1; then + ccache --show-stats +fi + cp -R src/python build/${PACKAGE_NAME}/usr/lib/python3/dist-packages/monero rm -rf build/${PACKAGE_NAME}/usr/lib/python3/dist-packages/pybind11* rm -rf build/${PACKAGE_NAME}/usr/lib/python3/dist-packages/bin diff --git a/docs/ci-ccache.md b/docs/ci-ccache.md new file mode 100644 index 00000000..d32457b5 --- /dev/null +++ b/docs/ci-ccache.md @@ -0,0 +1,39 @@ +## ccache in CI + +The workflows keep a ccache directory (`.ccache` in the workspace) between runs so monero-project doesn't get rebuilt from scratch every time. This is what to expect from it and where it stops helping. + +### How it's wired + +- monero-project already uses ccache on its own (`USE_CCACHE`, `FindCcache`). Nothing is passed to it. Don't set `CMAKE_CXX_COMPILER_LAUNCHER` in the environment for that step, it would be wrapped twice. +- monero-cpp is a submodule and doesn't know about ccache. The workflows and `docker/build.sh` pass `-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache` when configuring it. +- monero-python picks ccache up itself if it finds it. `-DUSE_CCACHE=OFF` turns that off. A launcher passed on the command line or in the environment wins. +- `CCACHE_DIR` is `.ccache`, `CCACHE_MAXSIZE` is 500M per job. + +### Where the cache lives + +- Key is `ccache-_`, restore falls back to the newest `ccache-_`. The `_` is there because `macos-15` is a prefix of `macos-15-intel`. +- macOS and Windows share a cache between `test.yml` and the `build-*.yml` workflows, the compile commands are the same. Linux tests have their own, the deb builds have one per distro and arch. +- Pull requests only restore. Pushes to main and manual runs (`workflow_dispatch`) save. This is on purpose, see quota below. +- A cache saved on a branch is only visible to that branch. Main's caches are visible to everyone. So the first run on main after merging this is cold. +- The save step runs after the build and before the tests, so a flaky test doesn't throw the cache away. Failed builds save too, whatever got compiled is still good. + +### What isn't cached + +- Coverage. `ENABLE_COVERAGE` adds `-fprofile-update=atomic`, and ccache 4.9.1 (what Ubuntu 24.04 ships) doesn't support that option. Those compiles go straight to the compiler, they don't fail. In the Linux test job that's the 12 monero-cpp files and the 7 binding files. monero-project is built without coverage and is cached normally. Newer ccache might handle it, not checked. +- Don't drop the flag to get hits. The tests are multithreaded and non-atomic counters give garbage coverage. `--coverage` alone is fine with ccache, a hit restores the `.gcno` and lcov reads it as usual. + +### Per platform + +- macOS: `CCACHE_COMPILERCHECK` is `%compiler% -v`. The default hashes mtime and size of `/usr/bin/c++`, which is a shim and stays the same when Xcode updates, so you'd keep getting objects from an old clang. Costs one extra process per compile. +- Windows: ccache comes from the rolling MSYS2 repo through `setup-msys2`, the toolchain is pinned afterwards with `pacman -U`. If ccache stops starting after you touch the pins, look there first. If it keeps being a problem, remove the ccache steps from the two Windows jobs, nothing else depends on them. +- deb: `.ccache` sits in the workspace that is mounted into the container, `CCACHE_DIR` is passed to `docker run`. Files end up owned by root on the host, the cache action only reads them. The runner has no ccache, so the stats are printed by `docker/build.sh`. Builds under qemu (arm64, armhf) are the slow ones and gain the most. + +### Quota + +GitHub gives 10 GB per repo, least recently used goes first, and anything not touched for 7 days is dropped. The deb matrix alone is 12 caches and the buildx layer cache (`type=gha`) shares the same quota. 500M is a guess. Look at the numbers once there are a few runs and adjust. + +### Checking it works + +Run `test.yml` twice on the same branch with `workflow_dispatch`. Second run should show mostly hits in the "Show ccache statistics" step (for deb it's the end of the `build.sh` output). Stats are zeroed at the start of each job, so they're only for that run. + +To start over: `gh cache list`, then `gh cache delete `.