|
| 1 | +name: Clang-Tidy |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + workflow_dispatch: |
| 6 | + |
| 7 | +concurrency: |
| 8 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} |
| 9 | + cancel-in-progress: true |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: read |
| 13 | + |
| 14 | +jobs: |
| 15 | + clang_tidy: |
| 16 | + name: Clang-Tidy Checks |
| 17 | + runs-on: ubuntu-24.04 |
| 18 | + steps: |
| 19 | + - name: Checkout code |
| 20 | + uses: actions/checkout@v6 |
| 21 | + with: |
| 22 | + submodules: recursive |
| 23 | + # fetch-depth: 0 is required for tj-actions/changed-files to correctly |
| 24 | + # compute the diff against the base branch on pull_request events. |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Install dependencies |
| 28 | + run: | |
| 29 | + sudo apt-get update |
| 30 | + sudo apt-get install -y clang-tidy=1:18.0-59~exp2 cmake ninja-build |
| 31 | +
|
| 32 | + - name: Configure CMake and export compile commands |
| 33 | + run: | |
| 34 | + cmake -S . -B build -G Ninja \ |
| 35 | + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ |
| 36 | + -DBUILD_TOOLS=ON |
| 37 | +
|
| 38 | + - name: Collect changed C/C++ files |
| 39 | + id: changed_files |
| 40 | + uses: tj-actions/changed-files@v47 |
| 41 | + with: |
| 42 | + files: | |
| 43 | + **/*.c |
| 44 | + **/*.cc |
| 45 | + **/*.cpp |
| 46 | + **/*.cxx |
| 47 | + files_ignore: | |
| 48 | + thirdparty/** |
| 49 | + build/** |
| 50 | +
|
| 51 | + - name: Filter changed files against compile_commands.json |
| 52 | + id: tidy_files |
| 53 | + if: steps.changed_files.outputs.any_changed == 'true' |
| 54 | + run: | |
| 55 | + python3 - <<'PY' |
| 56 | + import json |
| 57 | + import os |
| 58 | + from pathlib import Path |
| 59 | +
|
| 60 | + # all_changed_files is space-separated when output_format is not set; |
| 61 | + # tj-actions v46 defaults to space-separated for the env var form. |
| 62 | + # We read from the file written by the action instead via GITHUB_OUTPUT, |
| 63 | + # but the safest approach is to use the JSON output format. |
| 64 | + raw = os.environ.get("ALL_CHANGED_FILES", "") |
| 65 | + changed = [f for f in raw.split() if f] |
| 66 | +
|
| 67 | + compile_db_path = Path("build/compile_commands.json") |
| 68 | + with compile_db_path.open("r", encoding="utf-8") as fh: |
| 69 | + compile_db = json.load(fh) |
| 70 | +
|
| 71 | + # Build a set of absolute, normalised paths from compile_commands.json. |
| 72 | + compile_entries = set() |
| 73 | + for entry in compile_db: |
| 74 | + file_field = entry.get("file", "") |
| 75 | + if file_field: |
| 76 | + compile_entries.add(os.path.normpath(file_field)) |
| 77 | +
|
| 78 | + cwd = Path.cwd().resolve() |
| 79 | + selected = [] |
| 80 | + skipped = [] |
| 81 | +
|
| 82 | + for rel_path in changed: |
| 83 | + abs_path = os.path.normpath(str((cwd / rel_path).resolve())) |
| 84 | + if abs_path in compile_entries: |
| 85 | + selected.append(rel_path) |
| 86 | + elif not Path(rel_path).is_file(): |
| 87 | + skipped.append(f"{rel_path} (file not found)") |
| 88 | + else: |
| 89 | + skipped.append(f"{rel_path} (not in compile_commands.json)") |
| 90 | +
|
| 91 | + github_output = os.environ["GITHUB_OUTPUT"] |
| 92 | + with open(github_output, "a", encoding="utf-8") as out: |
| 93 | + out.write(f"any_tidy_files={'true' if selected else 'false'}\n") |
| 94 | + out.write("all_tidy_files<<TIDY_EOF\n") |
| 95 | + out.write("\n".join(selected) + "\n") |
| 96 | + out.write("TIDY_EOF\n") |
| 97 | + out.write("skipped_files<<SKIP_EOF\n") |
| 98 | + out.write("\n".join(skipped) + "\n") |
| 99 | + out.write("SKIP_EOF\n") |
| 100 | + PY |
| 101 | + env: |
| 102 | + ALL_CHANGED_FILES: ${{ steps.changed_files.outputs.all_changed_files }} |
| 103 | + |
| 104 | + - name: Show skipped files |
| 105 | + if: steps.changed_files.outputs.any_changed == 'true' && steps.tidy_files.outputs.skipped_files != '' |
| 106 | + run: | |
| 107 | + echo "=== Files skipped by clang-tidy ===" |
| 108 | + printf '%s\n' "${{ steps.tidy_files.outputs.skipped_files }}" |
| 109 | +
|
| 110 | + - name: Compute submodule commits hash |
| 111 | + id: submodule_hash |
| 112 | + run: | |
| 113 | + # Collect each submodule's current commit SHA, sort for a stable order, |
| 114 | + # then SHA-256 the result so the cache key stays a fixed length. |
| 115 | + hash=$(git submodule status --recursive | awk '{print $1}' | sort | sha256sum | awk '{print $1}') |
| 116 | + echo "value=${hash}" >> "$GITHUB_OUTPUT" |
| 117 | +
|
| 118 | + - name: Cache third-party build artifacts |
| 119 | + id: cache_thirdparty |
| 120 | + uses: actions/cache@v5 |
| 121 | + with: |
| 122 | + path: build/external |
| 123 | + # Cache key combines: |
| 124 | + # - thirdparty CMakeLists / cmake / patch file contents |
| 125 | + # - all submodule commit SHAs (upgrading a submodule busts the cache) |
| 126 | + key: thirdparty-${{ runner.os }}-${{ hashFiles('thirdparty/**/*.cmake', 'thirdparty/**/CMakeLists.txt', 'thirdparty/**/*.patch') }}-${{ steps.submodule_hash.outputs.value }} |
| 127 | + |
| 128 | + - name: Build |
| 129 | + if: steps.tidy_files.outputs.any_tidy_files == 'true' |
| 130 | + run: | |
| 131 | + # build so that clang-tidy can find the headers |
| 132 | + ninja -C build zvec_db |
| 133 | +
|
| 134 | + - name: Run clang-tidy on changed files |
| 135 | + if: steps.tidy_files.outputs.any_tidy_files == 'true' |
| 136 | + run: | |
| 137 | + # Read the newline-delimited file list into an array. |
| 138 | + mapfile -t files_to_check <<'TIDY_EOF' |
| 139 | + ${{ steps.tidy_files.outputs.all_tidy_files }} |
| 140 | + TIDY_EOF |
| 141 | +
|
| 142 | + failed=0 |
| 143 | + for file in "${files_to_check[@]}"; do |
| 144 | + # Skip blank lines that mapfile may produce. |
| 145 | + [[ -z "${file// }" ]] && continue |
| 146 | + if [ -f "$file" ]; then |
| 147 | + echo "=== clang-tidy: $file ===" |
| 148 | + clang-tidy -p build --warnings-as-errors='*' "$file" || failed=1 |
| 149 | + fi |
| 150 | + done |
| 151 | +
|
| 152 | + exit $failed |
| 153 | +
|
| 154 | + - name: No C/C++ files changed |
| 155 | + if: steps.changed_files.outputs.any_changed != 'true' || steps.tidy_files.outputs.any_tidy_files != 'true' |
| 156 | + run: echo "No changed source files with compile_commands entries to analyse." |
0 commit comments