api: clean up use of deprecated flag
[vpp.git] / build-root / scripts / checkstyle.sh
1 #!/bin/bash
2
3 # Copyright (c) 2015 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 VPP_DIR=`dirname $0`/../../
17 EXIT_CODE=0
18 FIX="0"
19 FULL="0"
20 CHECKSTYLED_FILES=""
21 UNCHECKSTYLED_FILES=""
22
23 # If the user provides --fix, then actually fix things
24 # Note: this is meant for use outside of the CI Jobs, by users cleaning things up
25
26 while true; do
27         case ${1} in
28                 --fix)
29                         FIX="1"
30                         ;;
31                 --full)
32                         FULL="1"
33                         ;;
34         esac
35         shift || break
36 done
37
38 if [ "${FULL}" == "1" ]; then
39         FILELIST=$(git ls-tree -r HEAD --name-only)
40 else
41         FILELIST=$((git diff HEAD~1.. --name-only; git ls-files -m ) | sort -u)
42 fi
43
44 # Check to make sure we have indent.  Exit if we don't with an error message, but
45 # don't *fail*.
46 command -v indent > /dev/null
47 if [ $? != 0 ]; then
48     echo "Cound not find required command \"indent\".  Checkstyle aborted"
49     exit ${EXIT_CODE}
50 fi
51 indent --version
52
53 # Check to make sure we have clang-format.  Exit if we don't with an error message, but
54 # don't *fail*.
55 HAVE_CLANG_FORMAT=0
56 command -v clang-format > /dev/null
57 if [ $? != 0 ]; then
58     echo "Could not find command \"clang-format\". Checking C++ files will cause abort"
59 else
60     clang-format --version
61     x=$(echo "" | clang-format 2>&1)
62     if [[ "$x" == "" ]]; then
63         HAVE_CLANG_FORMAT=1
64     else
65         echo "Output produced while formatting empty file (expected empty string):"
66         echo "$x"
67         echo "Could not find working \"clang-format\". Checking C++ files will cause abort"
68     fi
69 fi
70
71 cd ${VPP_DIR}
72 git status
73 for i in ${FILELIST}; do
74     if [ -f ${i} ] && [ ${i} != "build-root/scripts/checkstyle.sh" ] && [ ${i} != "extras/emacs/fix-coding-style.el" ]; then
75         grep -q '>>>>>>>' ${i}
76         if [ $? == 0 ]; then
77             echo "Unresolved merge conflict detected in" ${i} "... Abort."
78             exit 1
79         fi
80         grep -q "fd.io coding-style-patch-verification: ON" ${i}
81         if [ $? == 0 ]; then
82             EXTENSION=`basename ${i} | sed 's/^\w\+.//'`
83             case ${EXTENSION} in
84                 hpp|cpp|cc|hh)
85                     CMD="clang-format"
86                     if [ ${HAVE_CLANG_FORMAT} == 0 ]; then
87                             echo "C++ file detected. Abort. (missing clang-format)"
88                             exit ${EXIT_CODE}
89                     fi
90                     ;;
91                 *)
92                     CMD="indent"
93                     ;;
94             esac
95             CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}"
96             if [ ${FIX} == 0 ]; then
97                 if [ "${CMD}" == "clang-format" ]
98                 then
99                     clang-format ${i} > ${i}.out2
100                 else
101                     indent ${i} -o ${i}.out1 > /dev/null 2>&1
102                     indent ${i}.out1 -o ${i}.out2 > /dev/null 2>&1
103                 fi
104                 # Remove trailing whitespace
105                 sed -i -e 's/[[:space:]]*$//' ${i}.out2
106                 diff -q ${i} ${i}.out2
107             else
108                 if [ "${CMD}" == "clang-format" ]; then
109                     clang-format -i ${i} > /dev/null 2>&1
110                 else
111                     indent ${i}
112                     indent ${i}
113                 fi
114                 # Remove trailing whitespace
115                 sed -i -e 's/[[:space:]]*$//' ${i}
116             fi
117             if [ $? != 0 ]; then
118                 EXIT_CODE=1
119                 echo
120                 echo "Checkstyle failed for ${i}."
121                 if [ "${CMD}" == "clang-format" ]; then
122                     echo "Run clang-format as shown to fix the problem:"
123                     echo "clang-format -i ${VPP_DIR}${i}"
124                 else
125                     echo "Run indent (twice!) as shown to fix the problem:"
126                     echo "indent ${VPP_DIR}${i}"
127                     echo "indent ${VPP_DIR}${i}"
128                 fi
129             fi
130             if [ -f ${i}.out1 ]; then
131                 rm ${i}.out1
132             fi
133             if [ -f ${i}.out2 ]; then
134                 rm ${i}.out2
135             fi
136         else
137             UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
138         fi
139     else
140         UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
141     fi
142 done
143
144 if [ ${EXIT_CODE} == 0 ]; then
145     echo "*******************************************************************"
146     echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED"
147     echo "*******************************************************************"
148 else
149     echo "*******************************************************************"
150     echo "* VPP CHECKSTYLE FAILED"
151     echo "* CONSULT FAILURE LOG ABOVE"
152     echo "* NOTE: Running 'build-root/scripts/checkstyle.sh --fix' *MAY* fix the issue"
153     echo "*******************************************************************"
154 fi
155 exit ${EXIT_CODE}