devtools: test pkg-config file
[dpdk.git] / devtools / checkpatches.sh
1 #! /bin/sh
2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright 2015 6WIND S.A.
4
5 # Load config options:
6 # - DPDK_CHECKPATCH_PATH
7 # - DPDK_CHECKPATCH_CODESPELL
8 # - DPDK_CHECKPATCH_LINE_LENGTH
9 . $(dirname $(readlink -e $0))/load-devel-config
10
11 VALIDATE_NEW_API=$(dirname $(readlink -e $0))/check-symbol-change.sh
12
13 # Enable codespell by default. This can be overwritten from a config file.
14 # Codespell can also be enabled by setting DPDK_CHECKPATCH_CODESPELL to a valid path
15 # to a dictionary.txt file if dictionary.txt is not in the default location.
16 codespell=${DPDK_CHECKPATCH_CODESPELL:-enable}
17 length=${DPDK_CHECKPATCH_LINE_LENGTH:-80}
18
19 # override default Linux options
20 options="--no-tree"
21 if [ "$codespell" = "enable" ] ; then
22     options="$options --codespell"
23 elif [ -f "$codespell" ] ; then
24     options="$options --codespell"
25     options="$options --codespellfile $codespell"
26 fi
27 options="$options --max-line-length=$length"
28 options="$options --show-types"
29 options="$options --ignore=LINUX_VERSION_CODE,\
30 FILE_PATH_CHANGES,MAINTAINERS_STYLE,SPDX_LICENSE_TAG,\
31 VOLATILE,PREFER_PACKED,PREFER_ALIGNED,PREFER_PRINTF,\
32 PREFER_KERNEL_TYPES,BIT_MACRO,CONST_STRUCT,\
33 SPLIT_STRING,LONG_LINE_STRING,\
34 LINE_SPACING,PARENTHESIS_ALIGNMENT,NETWORKING_BLOCK_COMMENT_STYLE,\
35 NEW_TYPEDEFS,COMPARISON_TO_NULL"
36
37 clean_tmp_files() {
38         if echo $tmpinput | grep -q '^checkpatches\.' ; then
39                 rm -f "$tmpinput"
40         fi
41 }
42
43 trap "clean_tmp_files" INT
44
45 print_usage () {
46         cat <<- END_OF_HELP
47         usage: $(basename $0) [-q] [-v] [-nX|-r range|patch1 [patch2] ...]]
48
49         Run Linux kernel checkpatch.pl with DPDK options.
50         The environment variable DPDK_CHECKPATCH_PATH must be set.
51
52         The patches to check can be from stdin, files specified on the command line,
53         latest git commits limited with -n option, or commits in the git range
54         specified with -r option (default: "origin/master..").
55         END_OF_HELP
56 }
57
58 check_forbidden_additions() { # <patch>
59         res=0
60
61         # refrain from new additions of rte_panic() and rte_exit()
62         # multiple folders and expressions are separated by spaces
63         awk -v FOLDERS="lib drivers" \
64                 -v EXPRESSIONS="rte_panic\\\( rte_exit\\\(" \
65                 -v RET_ON_FAIL=1 \
66                 -v MESSAGE='Using rte_panic/rte_exit' \
67                 -f $(dirname $(readlink -e $0))/check-forbidden-tokens.awk \
68                 "$1" || res=1
69
70         # svg figures must be included with wildcard extension
71         # because of png conversion for pdf docs
72         awk -v FOLDERS='doc' \
73                 -v EXPRESSIONS='::[[:space:]]*[^[:space:]]*\\.svg' \
74                 -v RET_ON_FAIL=1 \
75                 -v MESSAGE='Using explicit .svg extension instead of .*' \
76                 -f $(dirname $(readlink -e $0))/check-forbidden-tokens.awk \
77                 "$1" || res=1
78
79         return $res
80 }
81
82 check_experimental_tags() { # <patch>
83         res=0
84
85         cat "$1" |awk '
86         BEGIN {
87                 current_file = "";
88                 ret = 0;
89         }
90         /^+++ b\// {
91                 current_file = $2;
92         }
93         /^+.*__rte_experimental/ {
94                 if (current_file ~ ".c$" ) {
95                         print "Please only put __rte_experimental tags in " \
96                                 "headers ("current_file")";
97                         ret = 1;
98                 }
99                 if ($1 != "+__rte_experimental" || $2 != "") {
100                         print "__rte_experimental must appear alone on the line" \
101                                 " immediately preceding the return type of a function."
102                         ret = 1;
103                 }
104         }
105         END {
106                 exit ret;
107         }' || res=1
108
109         return $res
110 }
111
112 number=0
113 range='origin/master..'
114 quiet=false
115 verbose=false
116 while getopts hn:qr:v ARG ; do
117         case $ARG in
118                 n ) number=$OPTARG ;;
119                 q ) quiet=true ;;
120                 r ) range=$OPTARG ;;
121                 v ) verbose=true ;;
122                 h ) print_usage ; exit 0 ;;
123                 ? ) print_usage ; exit 1 ;;
124         esac
125 done
126 shift $(($OPTIND - 1))
127
128 if [ ! -f "$DPDK_CHECKPATCH_PATH" ] || [ ! -x "$DPDK_CHECKPATCH_PATH" ] ; then
129         print_usage >&2
130         echo
131         echo 'Cannot execute DPDK_CHECKPATCH_PATH' >&2
132         exit 1
133 fi
134
135 print_headline() { # <title>
136         printf '\n### %s\n\n' "$1"
137         headline_printed=true
138 }
139
140 total=0
141 status=0
142
143 check () { # <patch> <commit> <title>
144         local ret=0
145         headline_printed=false
146
147         total=$(($total + 1))
148         ! $verbose || print_headline "$3"
149         if [ -n "$1" ] ; then
150                 tmpinput=$1
151         elif [ -n "$2" ] ; then
152                 tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
153                 git format-patch --find-renames \
154                 --no-stat --stdout -1 $commit > "$tmpinput"
155         else
156                 tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
157                 cat > "$tmpinput"
158         fi
159
160         ! $verbose || printf 'Running checkpatch.pl:\n'
161         report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
162         if [ $? -ne 0 ] ; then
163                 $headline_printed || print_headline "$3"
164                 printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
165                 ret=1
166         fi
167
168         ! $verbose || printf '\nChecking API additions/removals:\n'
169         report=$($VALIDATE_NEW_API "$tmpinput")
170         if [ $? -ne 0 ] ; then
171                 $headline_printed || print_headline "$3"
172                 printf '%s\n' "$report"
173                 ret=1
174         fi
175
176         ! $verbose || printf '\nChecking forbidden tokens additions:\n'
177         report=$(check_forbidden_additions "$tmpinput")
178         if [ $? -ne 0 ] ; then
179                 $headline_printed || print_headline "$3"
180                 printf '%s\n' "$report"
181                 ret=1
182         fi
183
184         ! $verbose || printf '\nChecking __rte_experimental tags:\n'
185         report=$(check_experimental_tags "$tmpinput")
186         if [ $? -ne 0 ] ; then
187                 $headline_printed || print_headline "$3"
188                 printf '%s\n' "$report"
189                 ret=1
190         fi
191
192         clean_tmp_files
193         [ $ret -eq 0 ] && return 0
194
195         status=$(($status + 1))
196 }
197
198 if [ -n "$1" ] ; then
199         for patch in "$@" ; do
200                 # Subject can be on 2 lines
201                 subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
202                 check "$patch" '' "$subject"
203         done
204 elif [ ! -t 0 ] ; then # stdin
205         subject=$(while read header value ; do
206                 if [ "$header" = 'Subject:' ] ; then
207                         IFS= read next
208                         continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
209                         echo $value$continuation
210                         break
211                 fi
212         done)
213         check '' '' "$subject"
214 else
215         if [ $number -eq 0 ] ; then
216                 commits=$(git rev-list --reverse $range)
217         else
218                 commits=$(git rev-list --reverse --max-count=$number HEAD)
219         fi
220         for commit in $commits ; do
221                 subject=$(git log --format='%s' -1 $commit)
222                 check '' $commit "$subject"
223         done
224 fi
225 pass=$(($total - $status))
226 $quiet || printf '\n%d/%d valid patch' $pass $total
227 $quiet || [ $pass -le 1 ] || printf 'es'
228 $quiet || printf '\n'
229 exit $status