-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathrun-clang-tidy.sh
More file actions
executable file
·145 lines (117 loc) · 4.02 KB
/
run-clang-tidy.sh
File metadata and controls
executable file
·145 lines (117 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
set -e
CLEANUP_FILES=()
# shellcheck disable=SC2329
cleanup() { rm -rf "${CLEANUP_FILES[@]}"; }
trap cleanup EXIT
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
##
# We standardize a common LLVM/Clang version for this script.
# Note that this is totally independent of the version of LLVM that you
# are using to build Halide itself. If you don't have the right version
# installed, you can usually install what you need easily via:
#
# sudo apt-get install llvm-X clang-X libclang-X-dev clang-tidy-X
# export CLANG_TIDY_LLVM_INSTALL_DIR=/usr/lib/llvm-X
#
# On macOS:
#
# brew install llvm@X
# export CLANG_TIDY_LLVM_INSTALL_DIR=/opt/homebrew/opt/llvm@X
#
# Where X matches the EXPECTED_VERSION below.
EXPECTED_VERSION=21
##
usage() {
echo "Usage: $0 [-j MAX_PROCESS_COUNT] [-f]" 1>&2
exit 1
}
get_thread_count() {
([ -x "$(command -v nproc)" ] && nproc) ||
([ -x "$(command -v sysctl)" ] && sysctl -n hw.physicalcpu)
}
if [ "$(uname)" == "Darwin" ]; then
_DEFAULT_LLVM_LOCATION="/opt/homebrew/opt/llvm@$EXPECTED_VERSION"
else
_DEFAULT_LLVM_LOCATION="/usr/lib/llvm-$EXPECTED_VERSION"
fi
J=$(get_thread_count)
FIX=
while getopts ":j:f" o; do
case "${o}" in
j)
J="${OPTARG}"
[[ ${J} =~ ^[0-9]+$ ]] || (
echo "-j requires an integer argument"
usage
)
;;
f)
FIX="-fix"
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
echo "Using ${J} processes."
if [ -n "${FIX}" ]; then
echo "Operating in -fix mode!"
fi
if [ -z "$CLANG_TIDY_LLVM_INSTALL_DIR" ]; then
if [ -d "${_DEFAULT_LLVM_LOCATION}" ]; then
CLANG_TIDY_LLVM_INSTALL_DIR="${_DEFAULT_LLVM_LOCATION}"
else
echo "CLANG_TIDY_LLVM_INSTALL_DIR must point to an LLVM installation dir for this script."
exit
fi
fi
echo "CLANG_TIDY_LLVM_INSTALL_DIR = ${CLANG_TIDY_LLVM_INSTALL_DIR}"
VERSION=$("${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-tidy" --version)
if [[ ${VERSION} =~ .*version\ $EXPECTED_VERSION.* ]]; then
echo "clang-tidy version $EXPECTED_VERSION found."
else
echo "CLANG_TIDY_LLVM_INSTALL_DIR must point to an LLVM $EXPECTED_VERSION install!"
exit 1
fi
# Use a temp folder for the CMake stuff here, so it's fresh & correct every time
if [[ -z ${CLANG_TIDY_BUILD_DIR} ]]; then
CLANG_TIDY_BUILD_DIR=$(mktemp -d)
CLEANUP_FILES+=("${CLANG_TIDY_BUILD_DIR}")
else
mkdir -p "${CLANG_TIDY_BUILD_DIR}"
fi
echo "CLANG_TIDY_BUILD_DIR = ${CLANG_TIDY_BUILD_DIR}"
export CC="${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang"
export CXX="${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang++"
export CMAKE_GENERATOR=Ninja
export CMAKE_BUILD_TYPE=Debug
export CMAKE_EXPORT_COMPILE_COMMANDS=ON
export Halide_LLVM_ROOT="${CLANG_TIDY_LLVM_INSTALL_DIR}"
if [[ $(${CC} --version) =~ .*Homebrew.* ]]; then
export CMAKE_TOOLCHAIN_FILE="${ROOT_DIR}/cmake/toolchain.macos-homebrew.cmake"
fi
echo Configuring Halide...
cmake -S "${ROOT_DIR}" -B "${CLANG_TIDY_BUILD_DIR}" -Wno-dev -DWITH_TESTS=OFF
[ -e "${CLANG_TIDY_BUILD_DIR}/compile_commands.json" ]
echo Building Halide...
cmake --build "${CLANG_TIDY_BUILD_DIR}" -j "${J}"
echo Merging runtime compilation database...
jq -s 'add' "${CLANG_TIDY_BUILD_DIR}/compile_commands.json" \
<(sed 's/,$//' "${CLANG_TIDY_BUILD_DIR}"/src/runtime/*.json | jq -s '.') \
>"${CLANG_TIDY_BUILD_DIR}/compile_commands_merged.json"
mv "${CLANG_TIDY_BUILD_DIR}/compile_commands_merged.json" "${CLANG_TIDY_BUILD_DIR}/compile_commands.json"
echo "Running clang-tidy..."
export PYTHONUNBUFFERED=1
export CLANG_TIDY_REAL_BINARY="${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-tidy"
export CLANG_TIDY_ROOT_DIR="${ROOT_DIR}"
"${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/run-clang-tidy" \
${FIX} \
-j "${J}" \
-quiet \
-p "${CLANG_TIDY_BUILD_DIR}" \
-clang-tidy-binary "${ROOT_DIR}/tools/clang-tidy-filter.sh" \
-clang-apply-replacements-binary "${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-apply-replacements" \
"$@"
echo "Success!"