forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-clang-format.sh
More file actions
executable file
·100 lines (83 loc) · 2.86 KB
/
run-clang-format.sh
File metadata and controls
executable file
·100 lines (83 loc) · 2.86 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
#!/bin/bash
set -e
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 -e "Usage: $0 [-c]" 1>&2; exit 1; }
if [ "$(uname)" == "Darwin" ]; then
_DEFAULT_LLVM_LOCATION="/opt/homebrew/opt/llvm@$EXPECTED_VERSION"
else
_DEFAULT_LLVM_LOCATION="/usr/lib/llvm-$EXPECTED_VERSION"
fi
# Fix the formatting in-place
MODE_FLAGS=(-i --sort-includes)
while getopts "c" o; do
case "${o}" in
c)
# Only check the files and print formatting errors
MODE_FLAGS=(--dry-run -Werror)
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
if [[ "${MODE_FLAGS[*]}" =~ "-i" ]]; then
if ! git diff-files --quiet --ignore-submodules; then
echo -e "\033[0;31m" # RED
echo "WARNING: There are still uncommited changes in your working tree."
echo " Reverting this formatting action will be difficult."
echo -e "\033[0m" # RESET
git diff-files --ignore-submodules
echo
read -p "Do you wish to continue (Y/N)? " -r
if ! [[ $REPLY =~ ^[Yy]$ ]]; then
exit
fi
fi
fi
if [ -z "$CLANG_FORMAT_LLVM_INSTALL_DIR" ]; then
if [ -d "${_DEFAULT_LLVM_LOCATION}" ]; then
CLANG_FORMAT_LLVM_INSTALL_DIR="${_DEFAULT_LLVM_LOCATION}"
else
echo "CLANG_FORMAT_LLVM_INSTALL_DIR must point to an LLVM installation dir for this script."
exit 1
fi
fi
echo "CLANG_FORMAT_LLVM_INSTALL_DIR=${CLANG_FORMAT_LLVM_INSTALL_DIR}"
CLANG_FORMAT="${CLANG_FORMAT_LLVM_INSTALL_DIR}/bin/clang-format"
VERSION=$("${CLANG_FORMAT}" --version)
if [[ ${VERSION} =~ .*version\ $EXPECTED_VERSION.* ]]; then
echo "clang-format version $EXPECTED_VERSION found."
else
echo "CLANG_FORMAT_LLVM_INSTALL_DIR must point to an LLVM $EXPECTED_VERSION install!"
exit 1
fi
# Note that we specifically exclude files starting with . in order
# to avoid finding emacs backup files
find "${ROOT_DIR}/apps" \
"${ROOT_DIR}/src" \
"${ROOT_DIR}/tools" \
"${ROOT_DIR}/test" \
"${ROOT_DIR}/util" \
"${ROOT_DIR}/python_bindings" \
-not -path "${ROOT_DIR}/src/runtime/hexagon_remote/bin/src/*" \
\( -name "*.cpp" -o -name "*.h" -o -name "*.c" \) -and -not -wholename "*/.*" \
-print0 | xargs -0 "${CLANG_FORMAT}" "${MODE_FLAGS[@]}" -style=file
exit "${PIPESTATUS[1]}"