|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# tidy.sh -- Run clang-tidy locally using the same file set and config as CI. |
| 4 | +# |
| 5 | +# Matches the file filter used by the cpp-linter GitHub Action in |
| 6 | +# .github/workflows/builds.yml: only src/**/*.{c,cpp,cc,cxx} excluding |
| 7 | +# src/tests/. Picks up checks from the repo-root .clang-tidy automatically. |
| 8 | +# |
| 9 | +# Usage: |
| 10 | +# ./tidy.sh # run on full src/ tree |
| 11 | +# ./tidy.sh -j 4 # override parallelism |
| 12 | +# ./tidy.sh -fix # auto-apply fixes (forwarded to run-clang-tidy) |
| 13 | +# |
| 14 | +# Requires CMake to have generated build-release/compile_commands.json. |
| 15 | +# Run once: cmake --preset macos-release (or linux-release) |
| 16 | + |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | +BUILD_DIR="build-release" |
| 20 | +# Positive match for top-level src/*.{c,cpp,cc,cxx}; negative lookahead excludes |
| 21 | +# dep paths (_deps/, build-*/, -src/src/) and other top-level dirs that CI's |
| 22 | +# cpp-linter `ignore:` list filters out. Python regex (PCRE-ish) supports |
| 23 | +# lookahead; this regex is evaluated by run-clang-tidy. |
| 24 | +FILE_REGEX='^(?!.*/(_deps|build-[^/]*|bridge|examples|client-sdk-rust|cpp-example-collection|vcpkg_installed|docker|docs|data)/).*/src/(?!tests/).*\.(c|cpp|cc|cxx)$' |
| 25 | + |
| 26 | +if [[ ! -f "${BUILD_DIR}/compile_commands.json" ]]; then |
| 27 | + echo "ERROR: ${BUILD_DIR}/compile_commands.json not found." >&2 |
| 28 | + echo "Run: cmake --preset macos-release (or linux-release)" >&2 |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +if ! command -v run-clang-tidy >/dev/null 2>&1; then |
| 33 | + echo "ERROR: run-clang-tidy not found in PATH." >&2 |
| 34 | + echo "Install LLVM: brew install llvm (macOS)" >&2 |
| 35 | + echo " apt install clang-tidy (Linux)" >&2 |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +extra_args=() |
| 40 | +if [[ "$(uname)" == "Darwin" ]]; then |
| 41 | + sdk_path="$(xcrun --show-sdk-path 2>/dev/null || true)" |
| 42 | + if [[ -n "${sdk_path}" ]]; then |
| 43 | + extra_args+=(-extra-arg="-isysroot${sdk_path}") |
| 44 | + fi |
| 45 | +fi |
| 46 | + |
| 47 | +if command -v nproc >/dev/null 2>&1; then |
| 48 | + jobs=$(nproc) |
| 49 | +else |
| 50 | + jobs=$(sysctl -n hw.ncpu 2>/dev/null || echo 4) |
| 51 | +fi |
| 52 | + |
| 53 | +run-clang-tidy \ |
| 54 | + -p "${BUILD_DIR}" \ |
| 55 | + -quiet \ |
| 56 | + -j "${jobs}" \ |
| 57 | + "${extra_args[@]}" \ |
| 58 | + "$@" \ |
| 59 | + "${FILE_REGEX}" |
0 commit comments