Skip to content

Commit cf3827a

Browse files
committed
Reuse built clang-tidy plugin across the shards
1 parent dbf4dca commit cf3827a

File tree

6 files changed

+120
-86
lines changed

6 files changed

+120
-86
lines changed

.github/workflows/clang-tidy.yml

+44-29
Original file line numberDiff line numberDiff line change
@@ -27,56 +27,71 @@ jobs:
2727
uses: fkirc/skip-duplicate-actions@master
2828
with:
2929
cancel_others: 'true'
30-
paths: '[ "**.cpp", "**.h", "**.c", "**/CMakeLists.txt", "**/Makefile", "**.hpp", "**.cmake", "build-scripts/clang-tidy.sh", "build-scripts/clang-tidy-wrapper.sh", "build-scripts/get_affected_files.py", ".github/workflows/clang-tidy.yml" ]'
31-
build:
30+
paths: '[ "**.cpp", "**.h", "**.c", "**/CMakeLists.txt", "**/Makefile", "**.hpp", "**.cmake", "build-scripts/clang-tidy-build.sh", "build-scripts/clang-tidy-run.sh", "build-scripts/clang-tidy-wrapper.sh", "build-scripts/get_affected_files.py", ".github/workflows/clang-tidy.yml" ]'
31+
32+
33+
build-clang-tidy:
3234
needs: skip-duplicates
35+
strategy:
36+
fail-fast: true
37+
runs-on: ubuntu-24.04
38+
env:
39+
COMPILER: clang++-17
40+
steps:
41+
- name: install LLVM 17
42+
if: ${{ needs.skip-duplicates.outputs.should_skip != 'true' && github.event_name != 'pull_request' || github.event.pull_request.draft == false }}
43+
run: |
44+
sudo apt install llvm-17 llvm-17-dev llvm-17-tools clang-17 clang-tidy-17 clang-tools-17 libclang-17-dev
45+
sudo apt install python3-pip ninja-build cmake
46+
pip3 install --user lit
47+
- name: checkout repository
48+
uses: actions/checkout@v4
49+
- uses: ammaraskar/gcc-problem-matcher@master
50+
- name: build clang-tidy plugin
51+
if: ${{ needs.skip-duplicates.outputs.should_skip != 'true' && github.event_name != 'pull_request' || github.event.pull_request.draft == false }}
52+
run: bash ./build-scripts/clang-tidy-build.sh
53+
- name: upload plugin
54+
uses: actions/upload-artifact@v4
55+
with:
56+
name: cata-analyzer-plugin
57+
path: build/tools/clang-tidy-plugin/libCataAnalyzerPlugin.so
58+
retention-days: 1
59+
3360

61+
run-clang-tidy:
62+
needs: build-clang-tidy
3463
strategy:
3564
fail-fast: false
3665
matrix:
37-
# To make the run finish in the run time limit, we split it up into two
38-
# parts: the src directory and everything else
66+
# To make the run finish in the run time limit, we split it up into three parts:
67+
# the files explicitly changed in the pr, the src directory and everything else
3968
subset: [
4069
'directly-changed',
4170
'indirectly-changed-src',
4271
'indirectly-changed-other'
4372
]
4473

45-
runs-on: ubuntu-22.04
74+
runs-on: ubuntu-24.04
4675
env:
47-
CMAKE: 1
48-
CLANG: clang++-17
4976
COMPILER: clang++-17
50-
CATA_CLANG_TIDY: plugin
77+
CATA_CLANG_TIDY: clang-tidy-17
5178
CATA_CLANG_TIDY_SUBSET: ${{ matrix.subset }}
5279
TILES: 1
5380
SOUND: 1
54-
RELEASE: 1
81+
LOCALIZE: 1
5582
steps:
56-
- name: install LLVM 17
57-
if: ${{ needs.skip-duplicates.outputs.should_skip != 'true' && github.event_name != 'pull_request' || github.event.pull_request.draft == false }}
58-
run: |
59-
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
60-
sudo apt-add-repository "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main"
61-
sudo apt update
62-
sudo apt install llvm-17 llvm-17-dev llvm-17-tools clang-17 clang-tidy-17 clang-tools-17 \
63-
libclang-17-dev libflac-dev libsdl2-dev libsdl2-ttf-dev libsdl2-image-dev libsdl2-mixer-dev \
64-
libpulse-dev ccache gettext jq
6583
- name: install dependencies
66-
run: |
67-
sudo apt install python3-pip libncursesw5-dev ninja-build cmake gettext
68-
pip3 install --user lit
69-
- name: ensure clang-tidy and FileCheck commands point to LLVM 17
7084
if: ${{ needs.skip-duplicates.outputs.should_skip != 'true' && github.event_name != 'pull_request' || github.event.pull_request.draft == false }}
7185
run: |
72-
mkdir ~/llvm-command-override
73-
ln -s /usr/bin/clang-tidy-17 ~/llvm-command-override/clang-tidy
74-
ln -s /usr/bin/FileCheck-17 ~/llvm-command-override/FileCheck
75-
echo "$HOME/llvm-command-override" >> $GITHUB_PATH
86+
sudo apt install clang-17 clang-tidy-17 cmake ccache jq
87+
sudo apt install libflac-dev libsdl2-dev libsdl2-ttf-dev libsdl2-image-dev libsdl2-mixer-dev libpulse-dev gettext
7688
- name: checkout repository
7789
uses: actions/checkout@v4
78-
- name: prepare
79-
run: bash ./build-scripts/requirements.sh
90+
- name: download plugin from the previous job in this workflow run
91+
uses: actions/download-artifact@v4
92+
with:
93+
name: cata-analyzer-plugin
94+
path: build/tools/clang-tidy-plugin/
8095
- name: determine changed files
8196
if: ${{ github.event_name == 'pull_request' }}
8297
uses: actions/github-script@v7
@@ -98,7 +113,7 @@ jobs:
98113
- uses: ammaraskar/gcc-problem-matcher@master
99114
- name: run clang-tidy
100115
if: ${{ needs.skip-duplicates.outputs.should_skip != 'true' && github.event_name != 'pull_request' || github.event.pull_request.draft == false }}
101-
run: bash ./build-scripts/clang-tidy.sh
116+
run: bash ./build-scripts/clang-tidy-run.sh
102117
- name: show most time consuming checks
103118
if: always()
104119
run: | # the folder may not exist if there is no file to analyze

build-scripts/clang-tidy-build.sh

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
# Shell script intended for clang-tidy check
4+
5+
echo "Using bash version $BASH_VERSION"
6+
set -exo pipefail
7+
8+
num_jobs=3
9+
10+
# We might need binaries installed via pip, so ensure that our personal bin dir is on the PATH
11+
#export PATH=$HOME/.local/bin:$PATH
12+
build_type=MinSizeRel
13+
14+
cmake_extra_opts=()
15+
cmake_extra_opts+=("-DCATA_CLANG_TIDY_PLUGIN=ON")
16+
# Need to specify the particular LLVM / Clang versions to use, lest it
17+
# use the older LLVM that comes by default on Ubuntu.
18+
cmake_extra_opts+=("-DLLVM_DIR=/usr/lib/llvm-17/lib/cmake/llvm")
19+
cmake_extra_opts+=("-DClang_DIR=/usr/lib/llvm-17/lib/cmake/clang")
20+
21+
22+
mkdir -p build
23+
cd build
24+
cmake \
25+
${COMPILER:+-DCMAKE_CXX_COMPILER=$COMPILER} \
26+
-DCMAKE_BUILD_TYPE="$build_type" \
27+
-DLOCALIZE=OFF \
28+
"${cmake_extra_opts[@]}" \
29+
..
30+
31+
32+
echo "Compiling clang-tidy plugin"
33+
make -j$num_jobs CataAnalyzerPlugin
34+
#export PATH=$PWD/tools/clang-tidy-plugin/clang-tidy-plugin-support/bin:$PATH
35+
# add FileCheck to the search path
36+
export PATH=/usr/lib/llvm-17/bin:$PATH
37+
if ! which FileCheck
38+
then
39+
echo "Missing FileCheck"
40+
exit 1
41+
fi
42+
CATA_CLANG_TIDY=clang-tidy
43+
# lit might be installed via pip, so ensure that our personal bin dir is on the PATH
44+
export PATH=$HOME/.local/bin:$PATH
45+
lit -v tools/clang-tidy-plugin/test
46+
cd ..
47+
48+
# show that it works
49+
50+
echo "version:"
51+
LD_PRELOAD=build/tools/clang-tidy-plugin/libCataAnalyzerPlugin.so clang-tidy --version
52+
echo "all enabled checks:"
53+
LD_PRELOAD=build/tools/clang-tidy-plugin/libCataAnalyzerPlugin.so clang-tidy --list-checks
54+
echo "cata-specific checks:"
55+
LD_PRELOAD=build/tools/clang-tidy-plugin/libCataAnalyzerPlugin.so clang-tidy --list-checks --checks="cata-*" | grep "cata"
56+

build-scripts/clang-tidy.sh build-scripts/clang-tidy-run.sh

+16-49
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,41 @@ set -exo pipefail
77

88
num_jobs=3
99

10-
# We might need binaries installed via pip, so ensure that our personal bin dir is on the PATH
11-
export PATH=$HOME/.local/bin:$PATH
12-
13-
if [ "$RELEASE" = "1" ]
14-
then
15-
build_type=MinSizeRel
16-
else
17-
build_type=Debug
18-
fi
19-
20-
cmake_extra_opts=()
21-
22-
if [ "$CATA_CLANG_TIDY" = "plugin" ]
23-
then
24-
cmake_extra_opts+=("-DCATA_CLANG_TIDY_PLUGIN=ON")
25-
# Need to specify the particular LLVM / Clang versions to use, lest it
26-
# use the older LLVM that comes by default on Ubuntu.
27-
cmake_extra_opts+=("-DLLVM_DIR=/usr/lib/llvm-17/lib/cmake/llvm")
28-
cmake_extra_opts+=("-DClang_DIR=/usr/lib/llvm-17/lib/cmake/clang")
29-
fi
30-
10+
# create compilation database (compile_commands.json)
3111
mkdir -p build
3212
cd build
3313
cmake \
34-
-DBACKTRACE=ON \
35-
${COMPILER:+-DCMAKE_CXX_COMPILER=$COMPILER} \
3614
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
37-
-DCMAKE_BUILD_TYPE="$build_type" \
15+
${COMPILER:+-DCMAKE_CXX_COMPILER=$COMPILER} \
16+
-DCMAKE_BUILD_TYPE="Release" \
17+
-DBACKTRACE=ON \
3818
-DTILES=${TILES:-0} \
3919
-DSOUND=${SOUND:-0} \
40-
"${cmake_extra_opts[@]}" \
20+
-DLOCALIZE=${LOCALIZE:-0} \
4121
..
22+
cd ..
23+
ln -s build/compile_commands.json .
4224

43-
if [ "$CATA_CLANG_TIDY" = "plugin" ]
25+
if [ ! -f build/tools/clang-tidy-plugin/libCataAnalyzerPlugin.so ]
4426
then
45-
echo "Compiling clang-tidy plugin"
46-
make -j$num_jobs CataAnalyzerPlugin
47-
export PATH=$PWD/tools/clang-tidy-plugin/clang-tidy-plugin-support/bin:$PATH
48-
if ! which FileCheck
49-
then
50-
ls -l tools/clang-tidy-plugin/clang-tidy-plugin-support/bin
51-
ls -l /usr/bin
52-
echo "Missing FileCheck"
53-
exit 1
54-
fi
55-
if ! which python && which python3
56-
then
57-
ln -s `which python3` $PWD/tools/clang-tidy-plugin/clang-tidy-plugin-support/bin/python
58-
fi
59-
CATA_CLANG_TIDY=clang-tidy
60-
lit -v tools/clang-tidy-plugin/test
27+
echo "Cata plugin not found. Assuming we're in CI and bailing out."
28+
exit 1
6129
fi
6230

63-
"$CATA_CLANG_TIDY" --version
64-
6531
# Show compiler C++ header search path
6632
${COMPILER:-clang++} -v -x c++ /dev/null -c
6733
# And the same for clang-tidy
68-
"$CATA_CLANG_TIDY" ../src/version.cpp -- -v
69-
70-
cd ..
71-
ln -s build/compile_commands.json
34+
./build-scripts/clang-tidy-wrapper.sh --version
35+
# list of checks
36+
./build-scripts/clang-tidy-wrapper.sh --list-checks
7237

7338
# We want to first analyze all files that changed in this PR, then as
7439
# many others as possible, in a random order.
7540
set +x
7641

7742
# Check for changes to any files that would require us to run clang-tidy across everything
7843
changed_global_files="$( ( cat ./files_changed || echo 'unknown' ) | \
79-
egrep -i "clang-tidy.sh|clang-tidy-wrapper.sh|clang-tidy.yml|.clang-tidy|files_changed|get_affected_files.py|CMakeLists.txt|CMakePresets.json|unknown" || true )"
44+
egrep -i "clang-tidy-build.sh|clang-tidy-run.sh|clang-tidy-wrapper.sh|clang-tidy.yml|.clang-tidy|files_changed|get_affected_files.py|CMakeLists.txt|CMakePresets.json|unknown" || true )"
8045
if [ -n "$changed_global_files" ]
8146
then
8247
first_changed_file="$(echo "$changed_global_files" | head -n 1)"
@@ -135,6 +100,8 @@ case "$CATA_CLANG_TIDY_SUBSET" in
135100
;;
136101
esac
137102

103+
printf "full list of files to analyze (they might get shuffled around in practice):\n%s\n" "$tidyable_cpp_files"
104+
138105
function analyze_files_in_random_order
139106
{
140107
if [ -n "$1" ]

build-scripts/clang-tidy-wrapper.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ plugin=build/tools/clang-tidy-plugin/libCataAnalyzerPlugin.so
88
if [ -f "$plugin" ]
99
then
1010
set -x
11-
LD_PRELOAD=$plugin "$CATA_CLANG_TIDY" --enable-check-profile --store-check-profile=clang-tidy-trace "$@"
11+
LD_PRELOAD=$plugin ${CATA_CLANG_TIDY} --enable-check-profile --store-check-profile=clang-tidy-trace "$@"
1212
else
1313
set -x
14-
"$CATA_CLANG_TIDY" "$@"
14+
${CATA_CLANG_TIDY} "$@"
1515
fi

build-scripts/requirements.sh

-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ if [ -n "${CODE_COVERAGE}" ]; then
5454
export LDFLAGS="$LDFLAGS --coverage"
5555
fi
5656

57-
if [ -n "$CATA_CLANG_TIDY" ]; then
58-
$travis_retry pip install --user wheel --upgrade
59-
$travis_retry pip install --user compiledb lit
60-
fi
61-
6257
# Influenced by https://github.com/zer0main/battleship/blob/master/build/windows/requirements.sh
6358
if [ -n "${MXE_TARGET}" ]; then
6459
sudo apt update

doc/DEVELOPER_TOOLING.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ sudo apt install build-essential cmake clang-12 libclang-12-dev llvm-12 llvm-12-
101101
sudo pip install compiledb lit
102102
test -f /usr/bin/python || sudo ln -s /usr/bin/python3 /usr/bin/python
103103
# The following command invokes clang-tidy exactly like CI does
104-
COMPILER=clang++-12 CLANG=clang++-12 CMAKE=1 CATA_CLANG_TIDY=plugin TILES=1 LOCALIZE=0 ./build-scripts/clang-tidy.sh
104+
COMPILER=clang++-12 CLANG=clang++-12 CMAKE=1 CATA_CLANG_TIDY=plugin TILES=1 LOCALIZE=0 ./build-scripts/clang-tidy-build.sh
105+
COMPILER=clang++-12 CLANG=clang++-12 CMAKE=1 CATA_CLANG_TIDY=plugin TILES=1 LOCALIZE=0 ./build-scripts/clang-tidy-run.sh
105106
```
106107

107108
#### Ubuntu Focal

0 commit comments

Comments
 (0)