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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 VPP_DIR=`dirname $0`/../../
21 UNCHECKSTYLED_FILES=""
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
38 if [ "${FULL}" == "1" ]; then
39 FILELIST=$(git ls-tree -r HEAD --name-only)
41 FILELIST=$((git diff HEAD~1.. --name-only; git ls-files -m ) | sort -u)
44 # Check to make sure we have indent. Exit if we don't with an error message, but
46 command -v indent > /dev/null
48 echo "Cound not find required command \"indent\". Checkstyle aborted"
53 # Check to make sure we have clang-format. Exit if we don't with an error message, but
56 command -v clang-format > /dev/null
58 echo "Could not find command \"clang-format\". Checking C++ files will cause abort"
60 clang-format --version
61 x=$(echo "" | clang-format 2>&1)
62 if [[ "$x" == "" ]]; then
65 echo "Output produced while formatting empty file (expected empty string):"
67 echo "Could not find working \"clang-format\". Checking C++ files will cause abort"
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}
77 EXTENSION=`basename ${i} | sed 's/^\w\+.//'`
81 if [ ${HAVE_CLANG_FORMAT} == 0 ]; then
82 echo "C++ file detected. Abort. (missing clang-format)"
90 CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}"
91 if [ ${FIX} == 0 ]; then
92 if [ "${CMD}" == "clang-format" ]
94 clang-format ${i} > ${i}.out2
96 indent ${i} -o ${i}.out1 > /dev/null 2>&1
97 indent ${i}.out1 -o ${i}.out2 > /dev/null 2>&1
99 # Remove trailing whitespace
100 sed -i -e 's/[[:space:]]*$//' ${i}.out2
101 diff -q ${i} ${i}.out2
103 if [ "${CMD}" == "clang-format" ]; then
104 clang-format -i ${i} > /dev/null 2>&1
109 # Remove trailing whitespace
110 sed -i -e 's/[[:space:]]*$//' ${i}
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}"
120 echo "Run indent (twice!) as shown to fix the problem:"
121 echo "indent ${VPP_DIR}${i}"
122 echo "indent ${VPP_DIR}${i}"
125 if [ -f ${i}.out1 ]; then
128 if [ -f ${i}.out2 ]; then
132 UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
135 UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
139 if [ ${EXIT_CODE} == 0 ]; then
140 echo "*******************************************************************"
141 echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED"
142 echo "*******************************************************************"
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 "*******************************************************************"