nat: use correct data types for memory sizes
[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 "fd.io coding-style-patch-verification: ON" ${i}
76         if [ $? == 0 ]; then
77             EXTENSION=`basename ${i} | sed 's/^\w\+.//'`
78             case ${EXTENSION} in
79                 hpp|cpp|cc|hh)
80                     CMD="clang-format"
81                     if [ ${HAVE_CLANG_FORMAT} == 0 ]; then
82                             echo "C++ file detected. Abort. (missing clang-format)"
83                             exit ${EXIT_CODE}
84                     fi
85                     ;;
86                 *)
87                     CMD="indent"
88                     ;;
89             esac
90             CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}"
91             if [ ${FIX} == 0 ]; then
92                 if [ "${CMD}" == "clang-format" ]
93                 then
94                     clang-format ${i} > ${i}.out2
95                 else
96                     indent ${i} -o ${i}.out1 > /dev/null 2>&1
97                     indent ${i}.out1 -o ${i}.out2 > /dev/null 2>&1
98                 fi
99                 # Remove trailing whitespace
100                 sed -i -e 's/[[:space:]]*$//' ${i}.out2
101                 diff -q ${i} ${i}.out2
102             else
103                 if [ "${CMD}" == "clang-format" ]; then
104                     clang-format -i ${i} > /dev/null 2>&1
105                 else
106                     indent ${i}
107                     indent ${i}
108                 fi
109                 # Remove trailing whitespace
110                 sed -i -e 's/[[:space:]]*$//' ${i}
111             fi
112             if [ $? != 0 ]; then
113                 EXIT_CODE=1
114                 echo
115                 echo "Checkstyle failed for ${i}."
116                 if [ "${CMD}" == "clang-format" ]; then
117                     echo "Run clang-format as shown to fix the problem:"
118                     echo "clang-format -i ${VPP_DIR}${i}"
119                 else
120                     echo "Run indent (twice!) as shown to fix the problem:"
121                     echo "indent ${VPP_DIR}${i}"
122                     echo "indent ${VPP_DIR}${i}"
123                 fi
124             fi
125             if [ -f ${i}.out1 ]; then
126                 rm ${i}.out1
127             fi
128             if [ -f ${i}.out2 ]; then
129                 rm ${i}.out2
130             fi
131         else
132             UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
133         fi
134     else
135         UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
136     fi
137 done
138
139 if [ ${EXIT_CODE} == 0 ]; then
140     echo "*******************************************************************"
141     echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED"
142     echo "*******************************************************************"
143 else
144     echo "*******************************************************************"
145     echo "* VPP CHECKSTYLE FAILED"
146     echo "* CONSULT FAILURE LOG ABOVE"
147     echo "* NOTE: Running 'build-root/scripts/checkstyle.sh --fix' *MAY* fix the issue"
148     echo "*******************************************************************"
149 fi
150 exit ${EXIT_CODE}