-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstyle-format.sh
More file actions
executable file
·161 lines (137 loc) · 5.08 KB
/
Copy pathstyle-format.sh
File metadata and controls
executable file
·161 lines (137 loc) · 5.08 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
#
# Copyright (c) 2024, NXP.
#
#
# The script to check or format source code of Connectivity Framework.
#
# Format c/c++:
#
# style-format.sh format
#
# Check only:
#
# style-format.sh check
#
set -e
# Check bash version. MCUXpresso has added bash in buildtools/bin since 11.9.0, which causes assignment of EXCLUDE_DIRS,
# EXCLUDE_FILES etc.. to fail.
BASH_PATH=`which bash`
SUB_PATH='ide/buildtools'
if [[ "$BASH_PATH" == *"$SUB_PATH"* ]]; then
echo "Rename bash in MCUXpresso $BASH_PATH or change path to force use of /usr/bin/bash"
exit 1
fi
readonly BUILD_JOBS=$(getconf _NPROCESSORS_ONLN)
readonly EXCLUDE_DIRS=(zephyr/subsys zephyr/include zephyr/lib zephyr/port/settings)
# must not be empty
readonly EXCLUDE_FILES=(placeholder.c)
readonly CLANG_SOURCES=('*.c' '*.cpp' '*.h' '*.hpp' '*.ch')
CLANG_VERSION="$(clang-format --version)"
EXPECTED_VERSION="clang-format version 16.0.0"
check_clang_version()
{
command -v clang-format >/dev/null 2>&1 || { echo >&2 "clang is required. Please install LLVM package. Aborting ..."; exit 1; }
case "${CLANG_VERSION}" in "${EXPECTED_VERSION}"*);;
*)
echo "${EXPECTED_VERSION} is required"
exit 1
;;
esac
}
do_clang_format()
{
echo -e '========================================'
echo -e ' format c/c++ (clang-format)'
echo -e '========================================'
echo "Formatting..."
file_list=$(git diff --name-only HEAD --diff-filter=AM "${CLANG_SOURCES[@]}" \
| grep -v -E "^($(echo "${EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
| grep -v -E "*($(echo "${EXCLUDE_FILES[@]}" | tr ' ' '|'))")
echo $file_list
for i in $(echo $file_list); do
clang-format -style=file -i $i
done
echo "Formatting done."
echo "Checking git status:"
echo
git status
}
do_clang_format_check()
{
echo -e '========================================'
echo -e ' check c/c++ (clang-format)'
echo -e '========================================'
echo "Checking..."
set +e
echo $1
if [ "$1" == 'pre-commit-check' ]; then
#the pre commit check applies to the staged files. The diff-filter option is to exclude the deleted files (A Added, M Modified)
git diff --name-only --cached --diff-filter=AM "${CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
| grep -v -E "*($(echo "${EXCLUDE_FILES[@]}" | tr ' ' '|'))" \
| xargs -n3 -P"${BUILD_JOBS}" clang-format -style=file --dry-run --Werror
else
# The clang check on the repository applies to all previous commits becasue a pull request may consist of several commits.
# otherwise "git diff --name-only HEAD HEAD~1 --diff-filter=AM" could have worked for single commit PRs.
git ls-files "${CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
| grep -v -E "*($(echo "${EXCLUDE_FILES[@]}" | tr ' ' '|'))" \
| xargs -n3 -P"${BUILD_JOBS}" clang-format -style=file --dry-run --Werror
fi
error_code=$?
set -e
if [ "$error_code" == "0" ]; then
echo "All files are formatted as expected."
else
echo "Formatting is required, run ./style-format.sh to apply required formatting."
exit $error_code
fi
}
do_clang_format_all()
{
echo -e '========================================'
echo -e ' format c/c++ (clang-format)'
echo -e '========================================'
echo "Formatting..."
file_list=$(git ls-files "${CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
| grep -v -E "*($(echo "${EXCLUDE_FILES[@]}" | tr ' ' '|'))")
for i in $(echo $file_list); do
clang-format -style=file -i $i
done
echo "Formatting done."
echo "Checking git status:"
echo
git status
}
main()
{
check_clang_version || exit 1
PRECOMMIT_SCRIPT=`git rev-parse --git-path hooks`/pre-commit
if [ -f "$PRECOMMIT_SCRIPT" ]; then
local compare_hook=`diff -q "$PRECOMMIT_SCRIPT" "scripts/pre_commit_fwk.template"`
if [ ! "$compare_hook" ]; then
echo "Found fwk pre-commit hook"
else
echo "Existing pre-commit hook but differs"
cp scripts/pre_commit_fwk.template $PRECOMMIT_SCRIPT
chmod +x $PRECOMMIT_SCRIPT
fi
else
echo "Pre-commit hook not existing"
cp scripts/pre_commit_fwk.template $PRECOMMIT_SCRIPT
chmod +x $PRECOMMIT_SCRIPT
fi
if [ $# == 0 ]; then
do_clang_format
elif [ "$1" == 'format' ]; then
do_clang_format
elif [ "$1" == 'check' ] || [ "$1" == 'pre-commit-check' ]; then
do_clang_format_check $1
elif [ "$1" == 'format-all' ]; then
do_clang_format_all
else
echo >&2 "Unsupported action: $1. Supported: format, check"
# 128 for Invalid arguments
exit 128
fi
}
main "$@"