zvec/.github/workflows/clang_tidy.yml

157 lines
5.7 KiB
YAML

name: Clang-Tidy
on:
workflow_call:
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
permissions:
contents: read
jobs:
clang_tidy:
name: Clang-Tidy Checks
runs-on: ubuntu-24.04
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
submodules: recursive
# fetch-depth: 0 is required for tj-actions/changed-files to correctly
# compute the diff against the base branch on pull_request events.
fetch-depth: 0
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y clang-tidy=1:18.0-59~exp2 cmake ninja-build libomp-dev
- name: Configure CMake and export compile commands
run: |
cmake -S . -B build -G Ninja \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DBUILD_TOOLS=ON
- name: Collect changed C/C++ files
id: changed_files
uses: tj-actions/changed-files@v47
with:
files: |
**/*.c
**/*.cc
**/*.cpp
**/*.cxx
files_ignore: |
thirdparty/**
build/**
- name: Filter changed files against compile_commands.json
id: tidy_files
if: steps.changed_files.outputs.any_changed == 'true'
run: |
python3 - <<'PY'
import json
import os
from pathlib import Path
# all_changed_files is space-separated when output_format is not set;
# tj-actions v46 defaults to space-separated for the env var form.
# We read from the file written by the action instead via GITHUB_OUTPUT,
# but the safest approach is to use the JSON output format.
raw = os.environ.get("ALL_CHANGED_FILES", "")
changed = [f for f in raw.split() if f]
compile_db_path = Path("build/compile_commands.json")
with compile_db_path.open("r", encoding="utf-8") as fh:
compile_db = json.load(fh)
# Build a set of absolute, normalised paths from compile_commands.json.
compile_entries = set()
for entry in compile_db:
file_field = entry.get("file", "")
if file_field:
compile_entries.add(os.path.normpath(file_field))
cwd = Path.cwd().resolve()
selected = []
skipped = []
for rel_path in changed:
abs_path = os.path.normpath(str((cwd / rel_path).resolve()))
if abs_path in compile_entries:
selected.append(rel_path)
elif not Path(rel_path).is_file():
skipped.append(f"{rel_path} (file not found)")
else:
skipped.append(f"{rel_path} (not in compile_commands.json)")
github_output = os.environ["GITHUB_OUTPUT"]
with open(github_output, "a", encoding="utf-8") as out:
out.write(f"any_tidy_files={'true' if selected else 'false'}\n")
out.write("all_tidy_files<<TIDY_EOF\n")
out.write("\n".join(selected) + "\n")
out.write("TIDY_EOF\n")
out.write("skipped_files<<SKIP_EOF\n")
out.write("\n".join(skipped) + "\n")
out.write("SKIP_EOF\n")
PY
env:
ALL_CHANGED_FILES: ${{ steps.changed_files.outputs.all_changed_files }}
- name: Show skipped files
if: steps.changed_files.outputs.any_changed == 'true' && steps.tidy_files.outputs.skipped_files != ''
run: |
echo "=== Files skipped by clang-tidy ==="
printf '%s\n' "${{ steps.tidy_files.outputs.skipped_files }}"
- name: Compute submodule commits hash
id: submodule_hash
run: |
# Collect each submodule's current commit SHA, sort for a stable order,
# then SHA-256 the result so the cache key stays a fixed length.
hash=$(git submodule status --recursive | awk '{print $1}' | sort | sha256sum | awk '{print $1}')
echo "value=${hash}" >> "$GITHUB_OUTPUT"
- name: Cache third-party build artifacts
id: cache_thirdparty
uses: actions/cache@v5
with:
path: build/external
# Cache key combines:
# - thirdparty CMakeLists / cmake / patch file contents
# - all submodule commit SHAs (upgrading a submodule busts the cache)
key: thirdparty-${{ runner.os }}-${{ hashFiles('thirdparty/**/*.cmake', 'thirdparty/**/CMakeLists.txt', 'thirdparty/**/*.patch') }}-${{ steps.submodule_hash.outputs.value }}
- name: Build
if: steps.tidy_files.outputs.any_tidy_files == 'true'
run: |
# build so that clang-tidy can find the headers
ninja -C build zvec_db
- name: Run clang-tidy on changed files
if: steps.tidy_files.outputs.any_tidy_files == 'true'
run: |
# Read the newline-delimited file list into an array.
mapfile -t files_to_check <<'TIDY_EOF'
${{ steps.tidy_files.outputs.all_tidy_files }}
TIDY_EOF
failed=0
for file in "${files_to_check[@]}"; do
# Skip blank lines that mapfile may produce.
[[ -z "${file// }" ]] && continue
if [ -f "$file" ]; then
echo "=== clang-tidy: $file ==="
clang-tidy -p build --warnings-as-errors='*' "$file" || failed=1
fi
done
exit $failed
- name: No C/C++ files changed
if: steps.changed_files.outputs.any_changed != 'true' || steps.tidy_files.outputs.any_tidy_files != 'true'
run: echo "No changed source files with compile_commands entries to analyse."