build: exclude dlmalloc.[ch] from checkstyle verification
[vpp.git] / extras / scripts / checkstyle.sh
1 #!/bin/bash
2
3 # Copyright (c) 2020 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -eEo pipefail
17
18 CLANG_FORMAT_VER_REGEX='([0-9]+)\.[0-9]+\.[0-9]+'
19 CLANG_FORMAT_DIFF="/usr/share/clang/clang-format-diff.py"
20
21 # TODO: Remove clang-format-${CLANG_FORMAT_VER} from 'make install-deps' when
22 #       CLANG_FORMAT_VER default value is upgraded
23 CLANG_FORMAT_VER=${CLANG_FORMAT_VER:-11}
24 GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
25 GIT_DIFF_EXCLUDE_LIST=(
26     ':!*.patch'
27     ':(exclude)*src/vppinfra/dlmalloc.*'
28 )
29 CLANG_FORMAT_DIFF_ARGS="-style file -p1"
30 SUFFIX="-${CLANG_FORMAT_VER}"
31
32 # Attempt to find clang-format to confirm Clang version.
33 if command -v clang-format${SUFFIX} &> /dev/null;
34 then
35     CLANG_FORMAT=clang-format${SUFFIX}
36 elif command -v clang-format &> /dev/null;
37 then
38     CLANG_FORMAT=clang-format
39 fi
40
41 CLANG_FORMAT_VERSION=$(${CLANG_FORMAT} --version)
42 echo $CLANG_FORMAT_VERSION
43
44 # Confirm that Clang is the expected version.
45 if [[ ! $CLANG_FORMAT_VERSION =~ $CLANG_FORMAT_VER_REGEX ]];
46 then
47     echo "*******************************************************************"
48     echo "* CHECKSTYLE VERSION REGEX CHECK FAILED"
49     echo "* $CLANG_FORMAT_VERSION"
50     echo "*******************************************************************"
51     exit 1
52 fi
53
54 if [[ ! $CLANG_FORMAT_VER == "${BASH_REMATCH[1]}" ]];
55 then
56     echo "*******************************************************************"
57     echo "* CHECKSTYLE VERSION CHECK FAILED"
58     echo "* Expected major version $CLANG_FORMAT_VER, found ${BASH_REMATCH[1]}"
59     echo "*******************************************************************"
60     exit 1
61 fi
62
63 # Attempt to find clang-format-diff.
64 if command -v clang-format-diff${SUFFIX} &> /dev/null;
65 then
66     CLANG_FORMAT_DIFF=clang-format-diff${SUFFIX}
67 elif command -v clang-format-diff.py &> /dev/null;
68 then
69     CLANG_FORMAT_DIFF=clang-format-diff.py
70 elif command -v clang-format-diff &> /dev/null;
71 then
72     CLANG_FORMAT_DIFF=clang-format-diff
73 elif [ ! -f $CLANG_FORMAT_DIFF ] ;
74 then
75     echo "*******************************************************************"
76     echo "* CHECKSTYLE FAILED"
77     echo "* Could not locate the clang-format-diff script"
78     echo "*******************************************************************"
79     exit 1
80 fi
81
82 in=$(mktemp)
83 git diff ${GIT_DIFF_ARGS} ${GIT_DIFF_EXCLUDE_LIST[@]} > ${in}
84
85 line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
86 if [ ${line_count} -gt 0 ] ; then
87     echo
88     sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in}
89     echo
90     echo "*******************************************************************"
91     echo "* CHECKSTYLE FAILED"
92     echo "* Please remove INDENT-ON and INDENT-OFF from modified lines."
93     echo "*******************************************************************"
94     rm ${in}
95     exit 1
96 fi
97
98 if [ "${1}" == "--fix" ]; then
99   cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} -i
100   filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in})
101   git status ${filelist}
102   rm ${in}
103   exit 0
104 fi
105
106 line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
107 if [ ${line_count} -gt 0 ] ; then
108     echo
109     sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in}
110     echo
111     echo "*******************************************************************"
112     echo "* CHECKSTYLE FAILED"
113     echo "* Trailing whitespace detected"
114     echo "*******************************************************************"
115     rm ${in}
116     exit 1
117 fi
118
119 out=$(mktemp)
120
121 cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
122 rm ${in}
123
124 line_count=$(cat ${out} | wc -l)
125
126 if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \
127    command -v highlight &> /dev/null ; then
128   highlight --syntax diff -O ansi ${out}
129 else
130   cat ${out}
131 fi
132
133 rm ${out}
134
135 if [ ${line_count} -gt 0 ] ; then
136     echo "*******************************************************************"
137     echo "* CHECKSTYLE FAILED"
138     echo "* CONSULT DIFF ABOVE"
139     echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue"
140     echo "*******************************************************************"
141     exit 1
142 else
143     echo "*******************************************************************"
144     echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
145     echo "*******************************************************************"
146     exit 0
147 fi