blob: f2f118ca20d8cb477e4d61a7e95ea8bfad8e337b [file] [log] [blame]
Ed Warnickebe053b82016-08-05 11:43:58 -07001#!/bin/bash
2
Dave Barach8d0f2f02018-03-12 09:31:36 -04003# 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
Ed Warnickebe053b82016-08-05 11:43:58 -070016VPP_DIR=`dirname $0`/../../
17EXIT_CODE=0
18FIX="0"
Damjan Marion24704852016-09-07 13:10:50 +020019FULL="0"
Ed Warnickebe053b82016-08-05 11:43:58 -070020CHECKSTYLED_FILES=""
21UNCHECKSTYLED_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
Damjan Marion24704852016-09-07 13:10:50 +020026while true; do
27 case ${1} in
28 --fix)
29 FIX="1"
30 ;;
31 --full)
32 FULL="1"
33 ;;
34 esac
35 shift || break
36done
37
38if [ "${FULL}" == "1" ]; then
39 FILELIST=$(git ls-tree -r HEAD --name-only)
40else
41 FILELIST=$((git diff HEAD~1.. --name-only; git ls-files -m ) | sort -u)
Ed Warnickebe053b82016-08-05 11:43:58 -070042fi
43
44# Check to make sure we have indent. Exit if we don't with an error message, but
45# don't *fail*.
46command -v indent > /dev/null
47if [ $? != 0 ]; then
Klement Sekeradc15be22017-06-12 06:49:33 +020048 echo "Cound not find required command \"indent\". Checkstyle aborted"
Ed Warnickebe053b82016-08-05 11:43:58 -070049 exit ${EXIT_CODE}
50fi
51indent --version
52
Klement Sekeradc15be22017-06-12 06:49:33 +020053# Check to make sure we have clang-format. Exit if we don't with an error message, but
54# don't *fail*.
Klement Sekera8a398bb2017-09-26 02:39:40 +020055HAVE_CLANG_FORMAT=0
Klement Sekera6b6bd9d2017-10-04 06:26:36 +020056command -v clang-format > /dev/null
Klement Sekeradc15be22017-06-12 06:49:33 +020057if [ $? != 0 ]; then
58 echo "Could not find command \"clang-format\". Checking C++ files will cause abort"
Klement Sekeradc15be22017-06-12 06:49:33 +020059else
Klement Sekeradc15be22017-06-12 06:49:33 +020060 clang-format --version
Klement Sekera8a398bb2017-09-26 02:39:40 +020061 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
Klement Sekeradc15be22017-06-12 06:49:33 +020069fi
70
Ed Warnickebe053b82016-08-05 11:43:58 -070071cd ${VPP_DIR}
72git status
Damjan Marion24704852016-09-07 13:10:50 +020073for i in ${FILELIST}; do
Damjan Marion757585d2017-04-20 11:42:28 +020074 if [ -f ${i} ] && [ ${i} != "build-root/scripts/checkstyle.sh" ] && [ ${i} != "extras/emacs/fix-coding-style.el" ]; then
Ed Warnickebe053b82016-08-05 11:43:58 -070075 grep -q "fd.io coding-style-patch-verification: ON" ${i}
76 if [ $? == 0 ]; then
Klement Sekeradc15be22017-06-12 06:49:33 +020077 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
Ed Warnickebe053b82016-08-05 11:43:58 -070090 CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}"
91 if [ ${FIX} == 0 ]; then
Klement Sekeradc15be22017-06-12 06:49:33 +020092 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
Ed Warnickebe053b82016-08-05 11:43:58 -0700101 diff -q ${i} ${i}.out2
102 else
Klement Sekeradc15be22017-06-12 06:49:33 +0200103 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}
Ed Warnickebe053b82016-08-05 11:43:58 -0700111 fi
112 if [ $? != 0 ]; then
113 EXIT_CODE=1
114 echo
115 echo "Checkstyle failed for ${i}."
Klement Sekeradc15be22017-06-12 06:49:33 +0200116 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
Ed Warnickebe053b82016-08-05 11:43:58 -0700124 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
137done
138
139if [ ${EXIT_CODE} == 0 ]; then
140 echo "*******************************************************************"
141 echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED"
142 echo "*******************************************************************"
143else
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 "*******************************************************************"
149fi
150exit ${EXIT_CODE}