Skip to content

Commit ff2cfcf

Browse files
move C++ format/tidy checks to GitHub Actions and stop Jenkins blocking on formatting
Signed-off-by: bogdan-petkovic <bogdan.petkovic@htecgroup.com>
1 parent fa7222a commit ff2cfcf

4 files changed

Lines changed: 196 additions & 1 deletion

File tree

.githooks/install.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
repo_root="$(git rev-parse --show-toplevel)"
5+
cd "$repo_root"
6+
7+
hooks_src=".githooks"
8+
hooks_dst=".git/hooks"
9+
10+
if [[ ! -d "$hooks_dst" ]]; then
11+
echo "Error: $hooks_dst not found (is this a git checkout?)"
12+
exit 1
13+
fi
14+
15+
install_hook() {
16+
local hook="$1"
17+
local src="$hooks_src/$hook"
18+
local dst="$hooks_dst/$hook"
19+
20+
if [[ ! -f "$src" ]]; then
21+
echo "Error: missing $src"
22+
exit 1
23+
fi
24+
25+
if [[ -f "$dst" ]] && [[ ! -L "$dst" ]]; then
26+
cp -f "$dst" "$dst.bak.$(date +%s)"
27+
fi
28+
29+
cp -f "$src" "$dst"
30+
chmod +x "$dst"
31+
echo "Installed $dst"
32+
}
33+
34+
install_hook pre-commit
35+
install_hook pre-push
36+
37+
echo
38+
echo "Done. Hooks installed into .git/hooks/"
39+
echo "To uninstall, remove .git/hooks/pre-commit and .git/hooks/pre-push (and any .bak.* backups)."
40+

.githooks/pre-push

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Optional: set ROCMLIR_PREPUSH_BASE_REF to override the default base ref.
5+
# Example: ROCMLIR_PREPUSH_BASE_REF=origin/release/rocm-6.2
6+
BASE_REF="${ROCMLIR_PREPUSH_BASE_REF:-origin/develop}"
7+
8+
# Optional: if set to 1, apply formatting fixes (then abort push so you can commit).
9+
AUTO_APPLY="${ROCMLIR_PREPUSH_AUTO_APPLY:-0}"
10+
11+
git_clang_format_py="external/llvm-project/clang/tools/clang-format/git-clang-format"
12+
if [[ ! -f "$git_clang_format_py" ]]; then
13+
echo "[pre-push] Cannot find $git_clang_format_py"
14+
echo "[pre-push] Make sure submodules/deps are present."
15+
exit 1
16+
fi
17+
18+
python_bin="${PYTHON_BIN:-python3}"
19+
if ! command -v "$python_bin" >/dev/null 2>&1; then
20+
echo "[pre-push] $python_bin not found."
21+
exit 1
22+
fi
23+
24+
if ! git rev-parse --verify "$BASE_REF" >/dev/null 2>&1; then
25+
if git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}' >/dev/null 2>&1; then
26+
BASE_REF="$(git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}')"
27+
fi
28+
fi
29+
30+
if ! git rev-parse --verify "$BASE_REF" >/dev/null 2>&1; then
31+
echo "[pre-push] Base ref '$BASE_REF' not found. Fetch it or set ROCMLIR_PREPUSH_BASE_REF."
32+
exit 1
33+
fi
34+
35+
BASE_SHA="$(git merge-base HEAD "$BASE_REF")"
36+
37+
readarray -d '' files < <(
38+
git diff --name-only -z --diff-filter=AM "$BASE_SHA"...HEAD \
39+
| grep -zE '\.(c|cc|cpp|cxx|h|hh|hpp|hxx|inc|td)$' \
40+
| grep -zEv '^external/' \
41+
|| true
42+
)
43+
44+
if (( ${#files[@]} == 0 )); then
45+
exit 0
46+
fi
47+
48+
diff_out="$("$python_bin" "$git_clang_format_py" --diff "$BASE_SHA" "${files[@]}" || true)"
49+
if ! grep -q '^diff --git ' <<<"$diff_out"; then
50+
exit 0
51+
fi
52+
53+
echo "$diff_out"
54+
echo
55+
echo "[pre-push] clang-format issues detected in your outgoing commits."
56+
57+
if [[ "$AUTO_APPLY" == "1" ]]; then
58+
echo "[pre-push] AUTO_APPLY enabled. Applying git-clang-format fixes..."
59+
"$python_bin" "$git_clang_format_py" "$BASE_SHA" "${files[@]}" || true
60+
git add -- "${files[@]}" || true
61+
echo
62+
echo "[pre-push] Formatting fixes were applied and staged."
63+
echo "[pre-push] Please review, commit the changes, then retry the push."
64+
exit 1
65+
fi
66+
67+
echo "[pre-push] Fix locally with one of:"
68+
echo " $python_bin $git_clang_format_py $BASE_SHA"
69+
echo " git clang-format $BASE_SHA (if available on your PATH)"
70+
echo
71+
echo "[pre-push] Then commit the result and retry the push."
72+
echo "[pre-push] To auto-apply fixes during pre-push (still aborting the push):"
73+
echo " ROCMLIR_PREPUSH_AUTO_APPLY=1 git push"
74+
exit 1
75+

.github/workflows/ci.yml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,87 @@ on:
2222
- ".github/workflows/**"
2323
- "pip_requirements.txt"
2424
jobs:
25+
cpp-static-checks:
26+
name: C/C++ clang-format + clang-tidy (diff-based)
27+
runs-on: ubuntu-latest
28+
container:
29+
image: rocm/mlir:rocm7.2-latest
30+
options: --user root
31+
steps:
32+
- uses: actions/checkout@v4
33+
with:
34+
fetch-depth: 0
35+
36+
- name: Fix git ownership
37+
run: |
38+
git config --global --add safe.directory "$GITHUB_WORKSPACE"
39+
40+
- name: Install Python dependencies for premerge checker
41+
shell: bash
42+
run: |
43+
python3 -m pip install --upgrade pip
44+
python3 -m pip install pathspec unidiff GitPython
45+
46+
- name: Determine base branch ref
47+
id: base
48+
shell: bash
49+
run: |
50+
if [ "${{ github.event_name }}" = "pull_request" ]; then
51+
BASE_REF="origin/${{ github.base_ref }}"
52+
BASE_BRANCH="${{ github.base_ref }}"
53+
else
54+
BASE_REF="origin/${{ github.ref_name }}"
55+
BASE_BRANCH="${{ github.ref_name }}"
56+
fi
57+
58+
git fetch origin "$BASE_BRANCH:refs/remotes/origin/$BASE_BRANCH" || \
59+
git fetch origin "$BASE_BRANCH" || true
60+
61+
if ! git rev-parse --verify "$BASE_REF" >/dev/null 2>&1; then
62+
echo "Error: Base ref '$BASE_REF' does not exist locally. Fetch may have failed." >&2
63+
exit 1
64+
fi
65+
66+
echo "ref=$BASE_REF" >> "$GITHUB_OUTPUT"
67+
echo "Base ref: $BASE_REF"
68+
69+
- name: Cache CMake build directory (compile_commands.json generation)
70+
uses: actions/cache@v4
71+
with:
72+
path: |
73+
build
74+
key: cpp-static-checks-${{ runner.os }}-${{ github.ref_name }}-${{ hashFiles('CMakeLists.txt', 'cmake/**', 'mlir/**/CMakeLists.txt') }}
75+
restore-keys: |
76+
cpp-static-checks-${{ runner.os }}-${{ github.ref_name }}-
77+
cpp-static-checks-${{ runner.os }}-
78+
79+
- name: Generate compile_commands.json (CMake configure)
80+
shell: bash
81+
run: |
82+
set -euo pipefail
83+
cmake -S . -B build -G Ninja \
84+
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
85+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
86+
-DCMAKE_C_COMPILER=/opt/rocm/llvm/bin/clang \
87+
-DCMAKE_CXX_COMPILER=/opt/rocm/llvm/bin/clang++
88+
89+
test -f build/compile_commands.json
90+
ln -sf build/compile_commands.json compile_commands.json
91+
92+
- name: Run Jenkins premerge static checks (format + tidy)
93+
shell: bash
94+
run: |
95+
set -euo pipefail
96+
export PATH="/opt/rocm/llvm/bin:$PATH"
97+
98+
# premerge-checks.py invokes `git-clang-format`; ensure it's on PATH.
99+
chmod +x external/llvm-project/clang/tools/clang-format/git-clang-format || true
100+
export PATH="$GITHUB_WORKSPACE/external/llvm-project/clang/tools/clang-format:$PATH"
101+
102+
python3 ./mlir/utils/jenkins/static-checks/premerge-checks.py \
103+
--base-commit="${{ steps.base.outputs.ref }}" \
104+
--ignore-external
105+
25106
format-and-lint-checks:
26107
name: Python format and lint checks
27108
runs-on: ubuntu-latest

mlir/utils/jenkins/Jenkinsfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,6 @@ pipeline {
11001100
showEnv()
11011101

11021102
build_fixedE2ETests("${CODEPATH}")
1103-
preMergeCheck("${CODEPATH}")
11041103
timeout(time: 60, activity: true, unit: 'MINUTES') {
11051104
sh 'cd build; ninja check-mlir check-rocmlir'
11061105
}

0 commit comments

Comments
 (0)