Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ on:
jobs:
format-check:
runs-on: ubuntu-latest
env:
CLANG_FORMAT_VERSION: "20"
CLANG_FORMAT_PYPI_VERSION: "20.1.8"
CLANG_FORMAT_INSTALL_DIR: "clang-format-venv"
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install clang-format 17
- name: Install clang-format 20
run: |
sudo apt-get update
sudo apt-get install -y clang-format-17
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-17 100
python3 -m venv "${RUNNER_TEMP}/${CLANG_FORMAT_INSTALL_DIR}"
"${RUNNER_TEMP}/${CLANG_FORMAT_INSTALL_DIR}/bin/python" -m pip install --upgrade pip
"${RUNNER_TEMP}/${CLANG_FORMAT_INSTALL_DIR}/bin/python" -m pip install "clang-format==${CLANG_FORMAT_PYPI_VERSION}"
echo "CLANG_FORMAT=${RUNNER_TEMP}/${CLANG_FORMAT_INSTALL_DIR}/bin/clang-format" >> "${GITHUB_ENV}"
"${RUNNER_TEMP}/${CLANG_FORMAT_INSTALL_DIR}/bin/clang-format" --version

- name: Run format-check
run: ./scripts/format-check.sh
32 changes: 32 additions & 0 deletions scripts/clang-format-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

CLANG_FORMAT_VERSION="${CLANG_FORMAT_VERSION:-20}"
CLANG_FORMAT="${CLANG_FORMAT:-clang-format-${CLANG_FORMAT_VERSION}}"

resolve_clang_format() {
if [[ ! "${CLANG_FORMAT_VERSION}" =~ ^[0-9]+$ ]]; then
echo "error: CLANG_FORMAT_VERSION must be a numeric major version." >&2
return 1
fi

local candidate=""
if command -v "${CLANG_FORMAT}" >/dev/null 2>&1; then
candidate="${CLANG_FORMAT}"
elif [ "${CLANG_FORMAT}" = "clang-format-${CLANG_FORMAT_VERSION}" ] && command -v clang-format >/dev/null 2>&1; then
candidate="clang-format"
else
echo "error: clang-format ${CLANG_FORMAT_VERSION}.x was not found." >&2
echo "Set CLANG_FORMAT to the clang-format binary to use." >&2
return 1
fi

local version_output
version_output="$("${candidate}" --version)"
if [[ ! "${version_output}" =~ (^|[[:space:]])version[[:space:]]+${CLANG_FORMAT_VERSION}\. ]]; then
echo "error: expected clang-format ${CLANG_FORMAT_VERSION}.x, got: ${version_output}" >&2
echo "Set CLANG_FORMAT_VERSION to override the required major version." >&2
return 1
fi

printf '%s\n' "${candidate}"
}
5 changes: 4 additions & 1 deletion scripts/format-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
source "${SCRIPT_DIR}/clang-format-common.sh"

CLANG_FORMAT_BIN="$(resolve_clang_format)"

files=()
while IFS= read -r -d '' file; do
Expand All @@ -19,7 +22,7 @@ fi
echo "Checking formatting on ${#files[@]} files..."
failed=0
for file in "${files[@]}"; do
if ! clang-format --dry-run --Werror "${file}"; then
if ! "${CLANG_FORMAT_BIN}" --dry-run --Werror "${file}"; then
failed=1
fi
done
Expand Down
7 changes: 5 additions & 2 deletions scripts/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
source "${SCRIPT_DIR}/clang-format-common.sh"

CLANG_FORMAT_BIN="$(resolve_clang_format)"

files=()
while IFS= read -r -d '' file; do
Expand All @@ -16,5 +19,5 @@ if [ "${#files[@]}" -eq 0 ]; then
exit 0
fi

echo "Formatting ${#files[@]} files with clang-format (style from ${REPO_ROOT}/.clang-format)..."
clang-format -i "${files[@]}"
echo "Formatting ${#files[@]} files with ${CLANG_FORMAT_BIN} (style from ${REPO_ROOT}/.clang-format)..."
"${CLANG_FORMAT_BIN}" -i "${files[@]}"
Loading