check_style_cpplint.sh
3.4 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
#!/bin/bash
#
# Copyright 2020 Mobvoi Inc. (authors: Fangjun Kuang)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Usage:
#
# (1) To check files of the last commit
# ./scripts/check_style_cpplint.sh
#
# (2) To check changed files not committed yet
# ./scripts/check_style_cpplint.sh 1
#
# (3) To check all files in the project
# ./scripts/check_style_cpplint.sh 2
cpplint_version="1.5.4"
cur_dir=$(cd $(dirname $BASH_SOURCE) && pwd)
sherpa_onnx_dir=$(cd $cur_dir/.. && pwd)
build_dir=$sherpa_onnx_dir/build
mkdir -p $build_dir
cpplint_src=$build_dir/cpplint-${cpplint_version}/cpplint.py
if [ ! -d "$build_dir/cpplint-${cpplint_version}" ]; then
pushd $build_dir
if command -v wget &> /dev/null; then
wget https://github.com/cpplint/cpplint/archive/${cpplint_version}.tar.gz
elif command -v curl &> /dev/null; then
curl -O -SL https://github.com/cpplint/cpplint/archive/${cpplint_version}.tar.gz
else
echo "Please install wget or curl to download cpplint"
exit 1
fi
tar xf ${cpplint_version}.tar.gz
rm ${cpplint_version}.tar.gz
# cpplint will report the following error for: __host__ __device__ (
#
# Extra space before ( in function call [whitespace/parens] [4]
#
# the following patch disables the above error
sed -i "3490i\ not Search(r'__host__ __device__\\\s+\\\(', fncall) and" $cpplint_src
popd
fi
source $sherpa_onnx_dir/scripts/utils.sh
# return true if the given file is a c++ source file
# return false otherwise
function is_source_code_file() {
case "$1" in
*.cc|*.h|*.cu)
echo true;;
*)
echo false;;
esac
}
function check_style() {
if [[ $1 == mfc-example* ]]; then
return
fi
python3 $cpplint_src $1 || abort $1
}
function check_last_commit() {
files=$(git diff HEAD^1 --name-only --diff-filter=ACDMRUXB)
echo $files
}
function check_current_dir() {
files=$(git status -s -uno --porcelain | awk '{
if (NF == 4) {
# a file has been renamed
print $NF
} else {
print $2
}}')
echo $files
}
function do_check() {
case "$1" in
1)
echo "Check changed files"
files=$(check_current_dir)
;;
2)
echo "Check all files"
files=$(find $sherpa_onnx_dir/cxx-api-examples $sherpa_onnx_dir/c-api-examples $sherpa_onnx_dir/sherpa-onnx/csrc $sherpa_onnx_dir/sherpa-onnx/python $sherpa_onnx_dir/scripts/node-addon-api/src $sherpa_onnx_dir/sherpa-onnx/jni $sherpa_onnx_dir/sherpa-onnx/c-api -name "*.h" -o -name "*.cc")
files2=$(find $sherpa_onnx_dir/harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/ -name "*.cc")
;;
*)
echo "Check last commit"
files=$(check_last_commit)
;;
esac
for f in $files $files2; do
need_check=$(is_source_code_file $f)
if $need_check; then
[[ -f $f ]] && check_style $f
fi
done
}
function main() {
do_check $1
ok "Great! Style check passed!"
}
cd $sherpa_onnx_dir
main $1