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