Skip to content

Commit 07fecc9

Browse files
authored
Make run-clang-tidy.sh work on macOS (#8416)
1 parent f658eec commit 07fecc9

File tree

3 files changed

+72
-48
lines changed

3 files changed

+72
-48
lines changed

.github/workflows/presubmit.yml

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
name: Halide Presubmit Checks
22
on:
3-
# We don't want 'edited' (that's basically just the description, title, etc)
3+
# We don't want 'edited' (that's basically just the description, title, etc.)
44
# We don't want 'review_requested' (that's redundant to the ones below for our purposes)
55
pull_request:
6-
types: [opened, synchronize, reopened]
6+
types: [ opened, synchronize, reopened ]
77
paths:
88
- '**.h'
99
- '**.c'
1010
- '**.cpp'
11+
- 'run-clang-tidy.sh'
1112

1213
permissions:
1314
contents: read
@@ -23,29 +24,17 @@ jobs:
2324
source: '.'
2425
extensions: 'h,c,cpp'
2526
clangFormatVersion: 17
26-
# As of Aug 2023, the macOS runners have more RAM (14GB vs 7GB) and CPU (3 cores vs 2)
27-
# than the Linux and Windows runners, so let's use those instead, since clang-tidy is
28-
# a bit of a sluggard
2927
check_clang_tidy:
3028
name: Check clang-tidy
31-
runs-on: ubuntu-20.04
29+
runs-on: macos-14
3230
steps:
3331
- uses: actions/checkout@v3
3432
- name: Install clang-tidy
35-
run: |
36-
# from apt.llvm.org
37-
# wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -
38-
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 15CF4D18AF4F7421
39-
sudo apt-add-repository "deb https://apt.llvm.org/$(lsb_release -sc)/ llvm-toolchain-$(lsb_release -sc)-17 main"
40-
sudo apt-get update
41-
sudo apt-get install llvm-17 clang-17 liblld-17-dev libclang-17-dev clang-tidy-17 ninja-build
33+
run: brew install llvm@17 ninja
4234
- name: Run clang-tidy
43-
run: |
44-
export CC=clang-17
45-
export CXX=clang++-17
46-
export CLANG_TIDY_LLVM_INSTALL_DIR=/usr/lib/llvm-17
47-
export CMAKE_GENERATOR=Ninja
48-
./run-clang-tidy.sh
35+
run: ./run-clang-tidy.sh
36+
env:
37+
CLANG_TIDY_LLVM_INSTALL_DIR: /opt/homebrew/opt/llvm@17
4938
check_cmake_file_lists:
5039
name: Check CMake file lists
5140
runs-on: ubuntu-20.04

run-clang-format.sh

+18-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
1111
#
1212
# sudo apt-get install llvm-17 clang-17 libclang-17-dev clang-tidy-17
1313
# export CLANG_FORMAT_LLVM_INSTALL_DIR=/usr/lib/llvm-17
14+
#
15+
# On macOS:
16+
#
17+
# brew install llvm@17
18+
# export CLANG_FORMAT_LLVM_INSTALL_DIR=/opt/homebrew/opt/llvm@17
19+
20+
if [ -z "$CLANG_FORMAT_LLVM_INSTALL_DIR" ]; then
21+
echo "CLANG_FORMAT_LLVM_INSTALL_DIR must point to an LLVM installation dir for this script."
22+
exit 1
23+
fi
24+
25+
echo "CLANG_FORMAT_LLVM_INSTALL_DIR = ${CLANG_FORMAT_LLVM_INSTALL_DIR}"
1426

15-
[ -z "$CLANG_FORMAT_LLVM_INSTALL_DIR" ] && echo "CLANG_FORMAT_LLVM_INSTALL_DIR must point to an LLVM installation dir for this script." && exit
16-
echo CLANG_FORMAT_LLVM_INSTALL_DIR = ${CLANG_FORMAT_LLVM_INSTALL_DIR}
27+
CLANG_FORMAT="${CLANG_FORMAT_LLVM_INSTALL_DIR}/bin/clang-format"
1728

18-
VERSION=$(${CLANG_FORMAT_LLVM_INSTALL_DIR}/bin/clang-format --version)
19-
if [[ ${VERSION} =~ .*version\ 17.* ]]
20-
then
29+
VERSION=$("${CLANG_FORMAT}" --version)
30+
if [[ ${VERSION} =~ .*version\ 17.* ]]; then
2131
echo "clang-format version 17 found."
2232
else
2333
echo "CLANG_FORMAT_LLVM_INSTALL_DIR must point to an LLVM 17 install!"
@@ -33,5 +43,6 @@ find "${ROOT_DIR}/apps" \
3343
"${ROOT_DIR}/util" \
3444
"${ROOT_DIR}/python_bindings" \
3545
-not -path "${ROOT_DIR}/src/runtime/hexagon_remote/bin/src/*" \
36-
\( -name "*.cpp" -o -name "*.h" -o -name "*.c" \) -and -not -wholename "*/.*" | \
37-
xargs ${CLANG_FORMAT_LLVM_INSTALL_DIR}/bin/clang-format -i -style=file
46+
\( -name "*.cpp" -o -name "*.h" -o -name "*.c" \) -and -not -wholename "*/.*" \
47+
-print0 | \
48+
xargs -0 "${CLANG_FORMAT}" -i -style=file

run-clang-tidy.sh

+46-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
66

77
usage() { echo "Usage: $0 [-j MAX_PROCESS_COUNT] [-f]" 1>&2; exit 1; }
88

9-
J=$(nproc)
9+
get_thread_count () {
10+
([ -x "$(command -v nproc)" ] && nproc) ||
11+
([ -x "$(command -v sysctl)" ] && sysctl -n hw.physicalcpu)
12+
}
13+
14+
if [ "$(uname)" == "Darwin" ]; then
15+
patch_file () { sed -i '' -E "$@"; }
16+
else
17+
patch_file () { sed -i -E "$@"; }
18+
fi
19+
20+
J=$(get_thread_count)
1021
FIX=
1122

1223
while getopts ":j:f" o; do
@@ -30,18 +41,27 @@ if [ -n "${FIX}" ]; then
3041
echo "Operating in -fix mode!"
3142
fi
3243

33-
# We are currently standardized on using LLVM/Clang17 for this script.
44+
# We are currently standardized on using LLVM/Clang 17 for this script.
3445
# Note that this is totally independent of the version of LLVM that you
3546
# are using to build Halide itself. If you don't have LLVM17 installed,
3647
# you can usually install what you need easily via:
3748
#
3849
# sudo apt-get install llvm-17 clang-17 libclang-17-dev clang-tidy-17
3950
# export CLANG_TIDY_LLVM_INSTALL_DIR=/usr/lib/llvm-17
51+
#
52+
# On macOS:
53+
#
54+
# brew install llvm@17
55+
# export CLANG_TIDY_LLVM_INSTALL_DIR=/opt/homebrew/opt/llvm@17
56+
57+
if [ -z "$CLANG_TIDY_LLVM_INSTALL_DIR" ]; then
58+
echo "CLANG_TIDY_LLVM_INSTALL_DIR must point to an LLVM installation dir for this script."
59+
exit
60+
fi
4061

41-
[ -z "$CLANG_TIDY_LLVM_INSTALL_DIR" ] && echo "CLANG_TIDY_LLVM_INSTALL_DIR must point to an LLVM installation dir for this script." && exit
42-
echo CLANG_TIDY_LLVM_INSTALL_DIR = ${CLANG_TIDY_LLVM_INSTALL_DIR}
62+
echo "CLANG_TIDY_LLVM_INSTALL_DIR = ${CLANG_TIDY_LLVM_INSTALL_DIR}"
4363

44-
VERSION=$(${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-tidy --version)
64+
VERSION=$("${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-tidy" --version)
4565
if [[ ${VERSION} =~ .*version\ 17.* ]]
4666
then
4767
echo "clang-tidy version 17 found."
@@ -52,28 +72,32 @@ fi
5272

5373

5474
# Use a temp folder for the CMake stuff here, so it's fresh & correct every time
55-
CLANG_TIDY_BUILD_DIR=`mktemp -d`
56-
echo CLANG_TIDY_BUILD_DIR = ${CLANG_TIDY_BUILD_DIR}
75+
CLANG_TIDY_BUILD_DIR=$(mktemp -d)
76+
echo "CLANG_TIDY_BUILD_DIR = ${CLANG_TIDY_BUILD_DIR}"
5777

5878
# Specify Halide_LLVM_SHARED_LIBS=ON because some installers may provide only that.
5979
echo Building compile_commands.json...
60-
cmake -DCMAKE_BUILD_TYPE=Debug \
80+
cmake -G Ninja -S "${ROOT_DIR}" -B "${CLANG_TIDY_BUILD_DIR}" -Wno-dev \
81+
-DCMAKE_C_COMPILER="${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang" \
82+
-DCMAKE_CXX_COMPILER="${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang++" \
83+
-DCMAKE_BUILD_TYPE=Debug \
6184
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
6285
-DHalide_CLANG_TIDY_BUILD=ON \
63-
-DHalide_LLVM_SHARED_LIBS=ON \
64-
-DLLVM_DIR=${CLANG_TIDY_LLVM_INSTALL_DIR}/lib/cmake/llvm \
65-
-S ${ROOT_DIR} \
66-
-B ${CLANG_TIDY_BUILD_DIR} \
86+
-DHalide_LLVM_ROOT="${CLANG_TIDY_LLVM_INSTALL_DIR}" \
6787
> /dev/null
6888

69-
[ -a ${CLANG_TIDY_BUILD_DIR}/compile_commands.json ]
89+
[ -a "${CLANG_TIDY_BUILD_DIR}/compile_commands.json" ]
90+
91+
# We need to remove -arch flags where -target flags also exist. These break our fake runtime compilation steps on macOS
92+
echo Patching compile_commands.json...
93+
patch_file '/-target/ s/-arch *[^ ]+//' "${CLANG_TIDY_BUILD_DIR}/compile_commands.json"
7094

7195
# We must populate the includes directory to check things outside of src/
7296
echo Building HalideIncludes...
73-
cmake --build ${CLANG_TIDY_BUILD_DIR} -j $(nproc) --target HalideIncludes
97+
cmake --build "${CLANG_TIDY_BUILD_DIR}" -j "${J}" --target HalideIncludes
7498

7599
echo Building flatbuffer stuff...
76-
cmake --build ${CLANG_TIDY_BUILD_DIR} -j $(nproc) --target generate_fb_header
100+
cmake --build "${CLANG_TIDY_BUILD_DIR}" -j "${J}" --target generate_fb_header
77101

78102
RUN_CLANG_TIDY=${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/run-clang-tidy
79103

@@ -101,19 +125,19 @@ CLANG_TIDY_HEADER_FILTER=".*/src/.*|.*/python_bindings/.*|.*/tools/.*|.*/util/.*
101125
echo Running clang-tidy...
102126
${RUN_CLANG_TIDY} \
103127
${FIX} \
104-
-j ${J} \
128+
-j "${J}" \
105129
-header-filter="${CLANG_TIDY_HEADER_FILTER}" \
106130
-quiet \
107-
-p ${CLANG_TIDY_BUILD_DIR} \
108-
-clang-tidy-binary ${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-tidy \
109-
-clang-apply-replacements-binary ${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-apply-replacements \
131+
-p "${CLANG_TIDY_BUILD_DIR}" \
132+
-clang-tidy-binary "${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-tidy" \
133+
-clang-apply-replacements-binary "${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-apply-replacements" \
110134
${CLANG_TIDY_TARGETS} \
111135
2>&1 | grep -v "warnings generated" | sed "s|.*/||"
112136

113137
RESULT=${PIPESTATUS[0]}
114138

115-
echo run-clang-tidy finished with status ${RESULT}
139+
echo "run-clang-tidy finished with status ${RESULT}"
116140

117-
rm -rf ${CLANG_TIDY_BUILD_DIR}
141+
rm -rf "${CLANG_TIDY_BUILD_DIR}"
118142

119-
exit $RESULT
143+
exit "${RESULT}"

0 commit comments

Comments
 (0)