name: Compiler cache description: Restore or save the weekly compiler cache inputs: mode: description: Whether to restore or save the cache required: true path: description: Local compiler-cache directory required: true key-prefix: description: Cache family prefix used for restore required: false default: colmap-compiler key-suffix: description: Configuration-specific cache key suffix used for restore required: false cache-version: description: Cache schema version required: false default: "2" key: description: Exact cache key returned by an earlier restore required: false cache-hit: description: Whether the exact cache key was restored before the build required: false default: "false" ccache-dir: description: Optional ccache directory to prune and recompress before saving required: false evict-older-than: description: Remove ccache entries unused for this long before saving required: false default: 1d recompression-level: description: Zstandard level used to recompress ccache entries before saving required: false default: "6" outputs: cache-hit: description: Whether the exact weekly cache was restored value: ${{ steps.restore.outputs.cache-hit }} key: description: Exact weekly key to use when saving the cache value: ${{ inputs.key-prefix }}-v${{ inputs.cache-version }}-${{ inputs.key-suffix }}-${{ steps.epoch.outputs.value }} runs: using: composite steps: - name: Validate mode if: ${{ inputs.mode != 'restore' && inputs.mode != 'save' }} shell: bash run: | echo "Unsupported compiler-cache mode: ${{ inputs.mode }}" >&2 exit 1 - name: Set cache epoch if: ${{ inputs.mode == 'restore' }} id: epoch shell: bash # GitHub cache entries are immutable. Rotate weekly so changed objects # are incorporated without creating a new archive for every run. run: echo "value=$(date -u +%G-%V)" >> "$GITHUB_OUTPUT" - name: Restore compiler cache if: ${{ inputs.mode == 'restore' }} id: restore uses: actions/cache/restore@v6 with: key: ${{ inputs.key-prefix }}-v${{ inputs.cache-version }}-${{ inputs.key-suffix }}-${{ steps.epoch.outputs.value }} restore-keys: ${{ inputs.key-prefix }}-v${{ inputs.cache-version }}-${{ inputs.key-suffix }}- path: ${{ inputs.path }} - name: Compact ccache (Linux/macOS) if: ${{ success() && inputs.mode == 'save' && runner.os != 'Windows' && (github.event_name == 'push' || github.event_name == 'release') && inputs.cache-hit != 'true' && inputs.ccache-dir != '' }} shell: bash run: | set -euo pipefail cache_root="${{ inputs.path }}" if [ -x "${cache_root}/bin/ccache" ]; then ccache="${cache_root}/bin/ccache" elif [ -x "${cache_root}/bin/ccache.exe" ]; then ccache="${cache_root}/bin/ccache.exe" elif command -v ccache >/dev/null 2>&1; then ccache="$(command -v ccache)" else echo "ccache executable not found" >&2 exit 1 fi # CCACHE_DIR is remapped to a container path by the PyCOLMAP Linux job, # so it must be overridden here. CCACHE_MAXSIZE needs no override: the # job-level value is inherited by composite action steps as-is. export CCACHE_DIR="${{ inputs.ccache-dir }}" "${ccache}" --evict-older-than "${{ inputs.evict-older-than }}" "${ccache}" --recompress "${{ inputs.recompression-level }}" "${ccache}" --cleanup "${ccache}" --show-compression "${ccache}" --show-stats --verbose - name: Compact ccache (Windows) if: ${{ success() && inputs.mode == 'save' && runner.os == 'Windows' && (github.event_name == 'push' || github.event_name == 'release') && inputs.cache-hit != 'true' && inputs.ccache-dir != '' }} shell: pwsh run: | $ErrorActionPreference = "Stop" $cacheRoot = "${{ inputs.path }}" $ccache = @( (Join-Path $cacheRoot "bin/ccache"), (Join-Path $cacheRoot "bin/ccache.exe") ) | Where-Object { Test-Path -PathType Leaf $_ } | Select-Object -First 1 if (!$ccache) { $ccache = (Get-Command ccache -ErrorAction SilentlyContinue).Source } if (!$ccache) { throw "ccache executable not found" } # CCACHE_DIR is remapped to a container path by the PyCOLMAP Linux job, # so it must be overridden here. CCACHE_MAXSIZE needs no override: the # job-level value is inherited by composite action steps as-is. $env:CCACHE_DIR = "${{ inputs.ccache-dir }}" & $ccache --evict-older-than "${{ inputs.evict-older-than }}" if ($LASTEXITCODE -ne 0) { throw "ccache eviction failed" } & $ccache --recompress "${{ inputs.recompression-level }}" if ($LASTEXITCODE -ne 0) { throw "ccache recompression failed" } & $ccache --cleanup if ($LASTEXITCODE -ne 0) { throw "ccache cleanup failed" } & $ccache --show-compression if ($LASTEXITCODE -ne 0) { throw "ccache compression stats failed" } & $ccache --show-stats --verbose if ($LASTEXITCODE -ne 0) { throw "ccache stats failed" } - name: Save compiler cache if: ${{ success() && inputs.mode == 'save' && (github.event_name == 'push' || github.event_name == 'release') && inputs.cache-hit != 'true' }} uses: actions/cache/save@v6 with: key: ${{ inputs.key }} path: ${{ inputs.path }}