-
Notifications
You must be signed in to change notification settings - Fork 17
/
format.sh
executable file
·64 lines (55 loc) · 1.71 KB
/
format.sh
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
#!/bin/bash
LLVM_VERSION=13
validate() {
utility="clang-format"
utility_version="$("$utility" --version 2> /dev/null)" || {
echo "$utility not found in path!"
exit 1
}
regex="version ([0-9]+).([0-9]+).([0-9]+)"
declare -i major=0
declare -i minor=0
declare -i patch=0
declare -i required_major=$LLVM_VERSION
declare -i required_minor=0
declare -i required_patch=0
if [[ "$utility_version" =~ $regex ]]; then
major="${BASH_REMATCH[1]}"
minor="${BASH_REMATCH[2]}"
patch="${BASH_REMATCH[3]}"
fi
if ((major >= required_major)) && ((minor >= required_minor)) && ((patch >= required_patch)); then
echo "$utility $required_major.$required_minor.$required_patch required. Satisfied by $major.$minor.$patch"
else
echo "$utility $required_major.$required_minor.$required_patch required. Not satisfied by $major.$minor.$patch"
exit 1
fi
}
help() {
echo "Usage: $0 [OPTION]..."
echo "Formats source code in the repository, applying formatting in place by default"
echo ""
echo "-h, --help Print Help"
echo "-d, --dry-run Dry run formatters, returning 0 when code passes, 1 otherwise"
echo ""
echo "Exit status:"
echo "0 if code required no formatting"
echo "non-zero exit status otherwise"
}
dry_run() {
find runtime \
\( -path "runtime/thirdparty" \) -prune -false -o \
-type f \( -iname \*.h -o -iname \*.c -o -iname \*.s \) -print0 \
| xargs --null clang-format -Werror -n -ferror-limit=1
}
format() {
find runtime \
\( -path "runtime/thirdparty" \) -prune -false -o \
-type f \( -iname \*.h -o -iname \*.c -o -iname \*.s \) -print0 \
| xargs --null clang-format -i
}
case $1 in
"-h" | "--help") help ;;
"-d" | "--dry-run") validate && dry_run ;;
"") validate && format ;;
esac