Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (c) 2020 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 | set -eEo pipefail |
| 17 | |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 18 | CLANG_FORMAT_VER_REGEX='([0-9]+)\.[0-9]+\.[0-9]+' |
| 19 | CLANG_FORMAT_DIFF="/usr/share/clang/clang-format-diff.py" |
| 20 | |
Dave Wallace | fb1135d | 2021-11-09 13:58:12 -0500 | [diff] [blame] | 21 | # TODO: Remove clang-format-${CLANG_FORMAT_VER} from 'make install-deps' when |
Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 22 | # CLANG_FORMAT_VER default value is upgraded to 15 after Ubuntu-20.04 is deprecated |
Dave Wallace | 4a33231 | 2022-03-17 17:44:35 -0400 | [diff] [blame] | 23 | CLANG_FORMAT_VER=${CLANG_FORMAT_VER:-11} |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 24 | GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1" |
Dave Wallace | 5d0fa2f | 2022-03-23 21:29:54 -0400 | [diff] [blame] | 25 | GIT_DIFF_EXCLUDE_LIST=( |
| 26 | ':!*.patch' |
| 27 | ':(exclude)*src/vppinfra/dlmalloc.*' |
| 28 | ) |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 29 | CLANG_FORMAT_DIFF_ARGS="-style file -p1" |
| 30 | SUFFIX="-${CLANG_FORMAT_VER}" |
| 31 | |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 32 | # Attempt to find clang-format to confirm Clang version. |
Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 33 | if command -v "clang-format${SUFFIX}" &> /dev/null; |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 34 | then |
Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 35 | CLANG_FORMAT="clang-format${SUFFIX}" |
| 36 | else |
| 37 | echo "*******************************************************************" |
| 38 | echo "* CHECKSTYLE FAILED" |
| 39 | echo "* Could not locate the clang-format${SUFFIX} script" |
| 40 | echo "* Run 'make install-deps' to install it" |
| 41 | echo "*******************************************************************" |
| 42 | exit 1 |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 43 | fi |
| 44 | |
| 45 | CLANG_FORMAT_VERSION=$(${CLANG_FORMAT} --version) |
Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 46 | echo "$CLANG_FORMAT_VERSION" |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 47 | |
| 48 | # Confirm that Clang is the expected version. |
| 49 | if [[ ! $CLANG_FORMAT_VERSION =~ $CLANG_FORMAT_VER_REGEX ]]; |
| 50 | then |
| 51 | echo "*******************************************************************" |
| 52 | echo "* CHECKSTYLE VERSION REGEX CHECK FAILED" |
| 53 | echo "* $CLANG_FORMAT_VERSION" |
| 54 | echo "*******************************************************************" |
| 55 | exit 1 |
| 56 | fi |
| 57 | |
| 58 | if [[ ! $CLANG_FORMAT_VER == "${BASH_REMATCH[1]}" ]]; |
| 59 | then |
| 60 | echo "*******************************************************************" |
| 61 | echo "* CHECKSTYLE VERSION CHECK FAILED" |
| 62 | echo "* Expected major version $CLANG_FORMAT_VER, found ${BASH_REMATCH[1]}" |
| 63 | echo "*******************************************************************" |
| 64 | exit 1 |
| 65 | fi |
| 66 | |
| 67 | # Attempt to find clang-format-diff. |
| 68 | if command -v clang-format-diff${SUFFIX} &> /dev/null; |
| 69 | then |
| 70 | CLANG_FORMAT_DIFF=clang-format-diff${SUFFIX} |
Klement Sekera | b9ff03c | 2022-02-18 16:23:33 +0000 | [diff] [blame] | 71 | elif command -v clang-format-diff.py &> /dev/null; |
| 72 | then |
| 73 | CLANG_FORMAT_DIFF=clang-format-diff.py |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 74 | elif command -v clang-format-diff &> /dev/null; |
| 75 | then |
Klement Sekera | b9ff03c | 2022-02-18 16:23:33 +0000 | [diff] [blame] | 76 | CLANG_FORMAT_DIFF=clang-format-diff |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 77 | elif [ ! -f $CLANG_FORMAT_DIFF ] ; |
| 78 | then |
| 79 | echo "*******************************************************************" |
| 80 | echo "* CHECKSTYLE FAILED" |
| 81 | echo "* Could not locate the clang-format-diff script" |
Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 82 | echo "* Run 'make install-deps' to install it" |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 83 | echo "*******************************************************************" |
| 84 | exit 1 |
| 85 | fi |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 86 | |
| 87 | in=$(mktemp) |
Dave Wallace | 5d0fa2f | 2022-03-23 21:29:54 -0400 | [diff] [blame] | 88 | git diff ${GIT_DIFF_ARGS} ${GIT_DIFF_EXCLUDE_LIST[@]} > ${in} |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 89 | |
| 90 | line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l) |
| 91 | if [ ${line_count} -gt 0 ] ; then |
| 92 | echo |
| 93 | sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in} |
| 94 | echo |
| 95 | echo "*******************************************************************" |
| 96 | echo "* CHECKSTYLE FAILED" |
| 97 | echo "* Please remove INDENT-ON and INDENT-OFF from modified lines." |
| 98 | echo "*******************************************************************" |
| 99 | rm ${in} |
| 100 | exit 1 |
| 101 | fi |
| 102 | |
| 103 | if [ "${1}" == "--fix" ]; then |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 104 | cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} -i |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 105 | filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in}) |
| 106 | git status ${filelist} |
| 107 | rm ${in} |
| 108 | exit 0 |
| 109 | fi |
| 110 | |
| 111 | line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l) |
| 112 | if [ ${line_count} -gt 0 ] ; then |
| 113 | echo |
| 114 | sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in} |
| 115 | echo |
| 116 | echo "*******************************************************************" |
| 117 | echo "* CHECKSTYLE FAILED" |
| 118 | echo "* Trailing whitespace detected" |
| 119 | echo "*******************************************************************" |
| 120 | rm ${in} |
| 121 | exit 1 |
| 122 | fi |
| 123 | |
| 124 | out=$(mktemp) |
| 125 | |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 126 | cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} > ${out} |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 127 | rm ${in} |
| 128 | |
| 129 | line_count=$(cat ${out} | wc -l) |
| 130 | |
| 131 | if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \ |
| 132 | command -v highlight &> /dev/null ; then |
| 133 | highlight --syntax diff -O ansi ${out} |
| 134 | else |
| 135 | cat ${out} |
| 136 | fi |
| 137 | |
| 138 | rm ${out} |
| 139 | |
| 140 | if [ ${line_count} -gt 0 ] ; then |
| 141 | echo "*******************************************************************" |
| 142 | echo "* CHECKSTYLE FAILED" |
| 143 | echo "* CONSULT DIFF ABOVE" |
| 144 | echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue" |
| 145 | echo "*******************************************************************" |
| 146 | exit 1 |
| 147 | else |
| 148 | echo "*******************************************************************" |
| 149 | echo "* CHECKSTYLE SUCCESSFULLY COMPLETED" |
| 150 | echo "*******************************************************************" |
| 151 | exit 0 |
| 152 | fi |