@@ -6,7 +6,18 @@ ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6
6
7
7
usage () { echo " Usage: $0 [-j MAX_PROCESS_COUNT] [-f]" 1>&2 ; exit 1; }
8
8
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)
10
21
FIX=
11
22
12
23
while getopts " :j:f" o; do
@@ -30,18 +41,27 @@ if [ -n "${FIX}" ]; then
30
41
echo " Operating in -fix mode!"
31
42
fi
32
43
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.
34
45
# Note that this is totally independent of the version of LLVM that you
35
46
# are using to build Halide itself. If you don't have LLVM17 installed,
36
47
# you can usually install what you need easily via:
37
48
#
38
49
# sudo apt-get install llvm-17 clang-17 libclang-17-dev clang-tidy-17
39
50
# 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
40
61
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} "
43
63
44
- VERSION=$( ${CLANG_TIDY_LLVM_INSTALL_DIR} /bin/clang-tidy --version)
64
+ VERSION=$( " ${CLANG_TIDY_LLVM_INSTALL_DIR} /bin/clang-tidy" --version)
45
65
if [[ ${VERSION} =~ .* version\ 17.* ]]
46
66
then
47
67
echo " clang-tidy version 17 found."
52
72
53
73
54
74
# 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} "
57
77
58
78
# Specify Halide_LLVM_SHARED_LIBS=ON because some installers may provide only that.
59
79
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 \
61
84
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
62
85
-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} " \
67
87
> /dev/null
68
88
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"
70
94
71
95
# We must populate the includes directory to check things outside of src/
72
96
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
74
98
75
99
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
77
101
78
102
RUN_CLANG_TIDY=${CLANG_TIDY_LLVM_INSTALL_DIR} /bin/run-clang-tidy
79
103
@@ -101,19 +125,19 @@ CLANG_TIDY_HEADER_FILTER=".*/src/.*|.*/python_bindings/.*|.*/tools/.*|.*/util/.*
101
125
echo Running clang-tidy...
102
126
${RUN_CLANG_TIDY} \
103
127
${FIX} \
104
- -j ${J} \
128
+ -j " ${J} " \
105
129
-header-filter=" ${CLANG_TIDY_HEADER_FILTER} " \
106
130
-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" \
110
134
${CLANG_TIDY_TARGETS} \
111
135
2>&1 | grep -v " warnings generated" | sed " s|.*/||"
112
136
113
137
RESULT=${PIPESTATUS[0]}
114
138
115
- echo run-clang-tidy finished with status ${RESULT}
139
+ echo " run-clang-tidy finished with status ${RESULT} "
116
140
117
- rm -rf ${CLANG_TIDY_BUILD_DIR}
141
+ rm -rf " ${CLANG_TIDY_BUILD_DIR} "
118
142
119
- exit $ RESULT
143
+ exit " ${ RESULT} "
0 commit comments