Add C++ API
[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 command -v clang-format > /dev/null
43 if [ $? != 0 ]; then
44     echo "Could not find command \"clang-format\". Checking C++ files will cause abort"
45     HAVE_CLANG_FORMAT=0
46 else
47     HAVE_CLANG_FORMAT=1
48     clang-format --version
49 fi
50
51 cd ${VPP_DIR}
52 git status
53 for i in ${FILELIST}; do
54     if [ -f ${i} ] && [ ${i} != "build-root/scripts/checkstyle.sh" ] && [ ${i} != "extras/emacs/fix-coding-style.el" ]; then
55         grep -q "fd.io coding-style-patch-verification: ON" ${i}
56         if [ $? == 0 ]; then
57             EXTENSION=`basename ${i} | sed 's/^\w\+.//'`
58             case ${EXTENSION} in
59                 hpp|cpp|cc|hh)
60                     CMD="clang-format"
61                     if [ ${HAVE_CLANG_FORMAT} == 0 ]; then
62                             echo "C++ file detected. Abort. (missing clang-format)"
63                             exit ${EXIT_CODE}
64                     fi
65                     ;;
66                 *)
67                     CMD="indent"
68                     ;;
69             esac
70             CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}"
71             if [ ${FIX} == 0 ]; then
72                 if [ "${CMD}" == "clang-format" ]
73                 then
74                     clang-format ${i} > ${i}.out2
75                 else
76                     indent ${i} -o ${i}.out1 > /dev/null 2>&1
77                     indent ${i}.out1 -o ${i}.out2 > /dev/null 2>&1
78                 fi
79                 # Remove trailing whitespace
80                 sed -i -e 's/[[:space:]]*$//' ${i}.out2
81                 diff -q ${i} ${i}.out2
82             else
83                 if [ "${CMD}" == "clang-format" ]; then
84                     clang-format -i ${i} > /dev/null 2>&1
85                 else
86                     indent ${i}
87                     indent ${i}
88                 fi
89                 # Remove trailing whitespace
90                 sed -i -e 's/[[:space:]]*$//' ${i}
91             fi
92             if [ $? != 0 ]; then
93                 EXIT_CODE=1
94                 echo
95                 echo "Checkstyle failed for ${i}."
96                 if [ "${CMD}" == "clang-format" ]; then
97                     echo "Run clang-format as shown to fix the problem:"
98                     echo "clang-format -i ${VPP_DIR}${i}"
99                 else
100                     echo "Run indent (twice!) as shown to fix the problem:"
101                     echo "indent ${VPP_DIR}${i}"
102                     echo "indent ${VPP_DIR}${i}"
103                 fi
104             fi
105             if [ -f ${i}.out1 ]; then
106                 rm ${i}.out1
107             fi
108             if [ -f ${i}.out2 ]; then
109                 rm ${i}.out2
110             fi
111         else
112             UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
113         fi
114     else
115         UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
116     fi
117 done
118
119 if [ ${EXIT_CODE} == 0 ]; then
120     echo "*******************************************************************"
121     echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED"
122     echo "*******************************************************************"
123 else
124     echo "*******************************************************************"
125     echo "* VPP CHECKSTYLE FAILED"
126     echo "* CONSULT FAILURE LOG ABOVE"
127     echo "* NOTE: Running 'build-root/scripts/checkstyle.sh --fix' *MAY* fix the issue"
128     echo "*******************************************************************"
129 fi
130 exit ${EXIT_CODE}