|
| 1 | +name: 'Set up ccache' |
| 2 | +description: > |
| 3 | + Install ccache (on Ubuntu), restore the ccache directory from a previous |
| 4 | + run, and prepend the ccache compiler-symlink dir to PATH. Subsequent |
| 5 | + gcc/cc/g++/c++ invocations are transparently intercepted by ccache, so |
| 6 | + no other workflow step needs to change. macOS is not supported yet. |
| 7 | +
|
| 8 | +inputs: |
| 9 | + workflow-id: |
| 10 | + description: 'Cache namespace - typically the calling workflow name.' |
| 11 | + required: true |
| 12 | + config-hash: |
| 13 | + description: > |
| 14 | + Optional short string distinguishing matrix entries. Each unique |
| 15 | + value gets its own primary cache key. Leave empty to share one |
| 16 | + cache across all entries in the workflow. |
| 17 | + required: false |
| 18 | + default: 'shared' |
| 19 | + max-size: |
| 20 | + description: 'Per-job ccache max size (passed to ccache -M).' |
| 21 | + required: false |
| 22 | + default: '500M' |
| 23 | + |
| 24 | +runs: |
| 25 | + using: 'composite' |
| 26 | + steps: |
| 27 | + - name: Install ccache (Ubuntu) |
| 28 | + shell: bash |
| 29 | + run: | |
| 30 | + if command -v ccache >/dev/null 2>&1; then |
| 31 | + echo "ccache already installed: $(ccache --version | head -1)" |
| 32 | + else |
| 33 | + sudo apt-get update -q |
| 34 | + sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \ |
| 35 | + --no-install-recommends ccache |
| 36 | + fi |
| 37 | +
|
| 38 | + - name: Restore + save ccache |
| 39 | + uses: actions/cache@v4 |
| 40 | + with: |
| 41 | + path: ~/.ccache |
| 42 | + # Unique per run+attempt+config so each job persists its own |
| 43 | + # contribution; restore-keys falls back to the most recent |
| 44 | + # cache for this workflow/os/config. |
| 45 | + key: ccache-${{ inputs.workflow-id }}-${{ runner.os }}-${{ runner.arch }}-${{ inputs.config-hash }}-${{ github.run_id }}-${{ github.run_attempt }} |
| 46 | + restore-keys: | |
| 47 | + ccache-${{ inputs.workflow-id }}-${{ runner.os }}-${{ runner.arch }}-${{ inputs.config-hash }}- |
| 48 | + ccache-${{ inputs.workflow-id }}-${{ runner.os }}-${{ runner.arch }}- |
| 49 | +
|
| 50 | + - name: Configure ccache and PATH |
| 51 | + shell: bash |
| 52 | + run: | |
| 53 | + ccache -M "${{ inputs.max-size }}" |
| 54 | + # base_dir lets ccache reuse hits across different workspace |
| 55 | + # checkout paths (different runs use different /home/.../work/ dirs). |
| 56 | + ccache --set-config=base_dir="$GITHUB_WORKSPACE" |
| 57 | + ccache --set-config=hash_dir=false |
| 58 | + ccache -z # zero stats so the post-build summary is per-job |
| 59 | + # /usr/lib/ccache contains gcc, g++, cc, c++ symlinks that resolve |
| 60 | + # to ccache. Prepending to PATH makes the build transparently use |
| 61 | + # ccache without changing any configure/make invocation. |
| 62 | + echo "/usr/lib/ccache" >> "$GITHUB_PATH" |
| 63 | + echo "CCACHE_DIR=$HOME/.ccache" >> "$GITHUB_ENV" |
| 64 | +
|
| 65 | + - name: Show ccache stats (initial) |
| 66 | + shell: bash |
| 67 | + run: ccache -s |
0 commit comments