summaryrefslogtreecommitdiffstats
path: root/aiaiai-test-patchset
blob: a17d1241ef4451b4a1d5645791f4fcaef7fe7d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/bin/sh -efu

# Copyright 2011-2012 Intel Corporation
# Author: Artem Bityutskiy
# License: GPLv2

srcdir="$(readlink -ev -- ${0%/*})"
PATH="$srcdir:$srcdir/helpers/libshell:$srcdir/helpers:$PATH"

. shell-error
. shell-args
. shell-signal
. aiaiai-sh-functions

PROG="${0##*/}"
message_time="yes"

# This is a small trick to make sure the script is portable - check if 'dash'
# is present, and if yes - use it.
if can_switch_to_dash; then
	exec dash -euf -- "$srcdir/$PROG" "$@"
	exit $?
fi

show_usage()
{
	cat <<-EOF
Usage: $PROG [options] <kernel-tree> <defconfig,[arch[,cross]] ...>

<kernel-tree>                   - directory with kernel sources
<defconfig,[arch[,cross]] ...>  - list of configurations to test.

The mbox file containing the patches to test is expected to come from stdin or
-i option can be used instead.

The configurations are specified as follows:

  defconfig[,arch[,cross]]

where

  * "defconfig" is the name kernel defconfig file to test (or "randconfig")
  * "arch" is the architecture (translates to Kbuild's ARCH variable value)
  * "cross" is the cross-compiler prefix (translates to Kbuild's CROSS_COMPILE
     variable value).

For example, the following list of configurations:

  omap2_defconfig,arm,arm-eabi- i386_defconfig,i386 defconfig,s390,s390x-linux- randconfig,arm

will make $PROG to test the patch in 3 configurations:

  1. defconfig "omap2_defconfig" with ARCH=arm and CROSS_COMPILE=arm-eabi-
  2. defconfig "i386_defconfig" with ARCH=arm and the default CROSS_COMPILE
  3. defconfig "defconfig" with ARCH=s390 and CROSS_COMPILE=s390x-linux-
  4. a randomly generated configuration with ARCH=arm

Note that randconfig pre-generates a single random configuration rather than
using randconfig for each kernel compile. In this way, we guarantee that the
same kernel configuration is used each build, rather than completely random
each time.

By default, $PROG assumes that defconfigs are part of the
kernel tree, unless --confdir option is specified, in which case the defconfig
files will be searched for only in the specified directory.

Several static checking utilities can be enabled to provide enhanced
information, if desired. These include sparse, smatch, cppcheck, and
coccinelle. In addition scripts/checkpatch.pl is run if found in the project
tree. You can disable a checker with --no[checker] or enable it with
--[checker]. The last provided option on the command line wins, and supersedes
other settings.

Options:
  -j, --jobs=N           allow to run N jobs simultaneously (default is 1);
  -c, --commit-id=ID     the commit id to test against (default is the head of
                         the git tree (HEAD));
  -i, --input=MBOX       use the MBOX file instead of stdin;
  -w, --workdir=WDIR     path to the work directory where the kernel will
                         be built (default: a temporary directory is created
                         using mktemp);
      --logdir=LOGDIR    build logs will be put to this directory (by default
                         the build logs are stored in the script's temporary
                         directory and removed upon exit, unless -p is
                         specified);
  -C, --confdir=CDIR     path to the directory containing the defconfig files
                         (those you specify at the end); by default the
                         defconfig files are assumed to be part of the
                         <kernel-tree>; this option makes it possible to use
                         stand-alone defconfig files instead;
  -p, --preserve         preserve all the temporary files - do not clean up;
      --bisectability    test bisectability;
      --[no]sparse       check with sparse while building;
      --[no]smatch       check with smatch while building;
      --[no]cppcheck     check with cppcheck while building;
      --[no]coccinelle   check with coccinelle (spatch) while building;
      --[no]checkpatch   run scripts/checkpatch.pl, if found in the project;
  -Q  --quick-fixes=F    sometimes it is necessary to carry out-of-tree patches
                         like quick build fixes and this option allows to pass
                         an mbox file with quick fixes which will be applied
                         first and the user patch-set will be tested on top of
                         the fixes;
      --targets          list of make targets to build (defaults to all)
  -K  --keywords=FILE    match keywords from FILE against the patch;
  -M, --kmake-opts       additional options to append to the final kernel
                         compilation 'make' command
                         (e.g., W=2 KALLSYMS_EXTRA_PASS=1);
  -v, --verbose          be verbose;
  -h, --help             show this text and exit.
EOF
}

fail_usage()
{
	[ -z "$1" ] || printf "%s\n" "$1"
	show_usage
	exit 1
}

test_checkpatch()
{
	local n=0 patch_cnt
	local tmp_out="$tmpdir/checkpatch.tmp_out"
	local tmp_mbox="$tmpdir/checkpatch.tmp_mbox"
	local opts="--no-tree -q --show-types --ignore PREFER_PACKED,PREFER_ALIGNED,CONST_STRUCT"

	patch_cnt="$(formail -s echo 1 < "$mbox" | wc -l)"

	# Iterate over all patches and check each one individually
	while [ "$n" -lt "$patch_cnt" ]; do
		local subj

		formail +$n -1 -s < "$mbox" > "$tmp_mbox"
		truncate -s0 "$tmp_out"
		"$checkpatch_pl" $opts - < "$tmp_mbox" > "$tmp_out" ||:
		n="$(($n+1))"

		[ -s "$tmp_out" ] || continue

		fetch_header_or_die subj "Subject" < "$tmp_mbox"

		printf "\n" >> "$tmpdir/checkpatch.results"
		print_separator >> "$tmpdir/checkpatch.results"
		printf "\n%s\n\n" "checkpatch.pl results for patch \"$subj\"" \
			>> "$tmpdir/checkpatch.results"
		cat < "$tmp_out" >> "$tmpdir/checkpatch.results"
	done

	if [ "$patch_cnt" -gt 1 ]; then
		# Check the squashed patch
		truncate -s0 "$tmp_out"
		"$checkpatch_pl" --no-signoff $opts - < "$tmpdir/diff-for-checkpatch" > "$tmp_out" ||:
		[ -s "$tmp_out" ] || return 0

		printf "\n" >> "$tmpdir/checkpatch.results"
		print_separator >> "$tmpdir/checkpatch.results"

		printf "\n%s\n\n" "checkpatch.pl results for the entire squashed patch-set" \
			>> "$tmpdir/checkpatch.results"

		cat < "$tmp_out" >> "$tmpdir/checkpatch.results"
	fi

}

test_configuration()
{
	local config="$1"
	local defconfig="$(leave_first "$1")";
	local arch="$(leave_second "$1")";
	local cross="$(leave_third "$1")";
	local arch_opt=
	local pid_bisect=

	[ -z "$arch" ] || arch_opt="-a $arch${cross:+",$cross"}"

	git --git-dir="$(git_dir "$cloned_kernel1")" reset $quiet --hard "$commit_id1" >&2

	# Enable use of a random condiguration that is stable between the
	# before and after tests. If we just used 'randconfig' by default, then
	# we wouldn't be testing the before and after kernels with the same
	# configuration. We do this even if we have specified a confdir.
	if [ "$defconfig" = "randconfig" ]; then
			# Generate a random configuration
			make -C "$cloned_kernel1" ${arch:+ARCH="$arch"} \
				${cross:+CROSS_COMPILE="$cross"} -- "$defconfig" >&2

			# Store this configuration in our temporary work dir
			defconfig="$tmpdir/$config.random"
			verbose "Copying random configuration to $defconfig"
			cp .config "$defconfig" >&2

			make -C "$cloned_kernel1" mrproper >&2
	elif [ -n "$confdir" ]; then
		defconfig="$confdir/$defconfig"
		[ -f "$defconfig" ] || die "$defconfig is not available"
	fi

	verbose "Build non-patched kernel (\"$cloned_kernel1\", configuration \"$config\")"

	local log1="$logdir1/$config"
	local obj1="$tmpdir1/obj.$config"

	# Note, we use the --keep-going option assuming that the base commit
	# build may fail, which is OK for continuing as long as the patches
	# under test fix it.
	aiaiai-make-kernel $verbose $sparse $smatch $cppcheck $coccinelle -o "$obj1" \
		--check-only "$mod_files" -j "$jobs" $arch_opt -D "$defconfig" \
		-O "$log1.stdout.log" -E "$log1.stderr.log" --keep-going \
		${kmake_opts:+-M "$kmake_opts"} -- "$cloned_kernel1" "$targets"

	verbose "Done with non-patched kernel"

	# Run the bisectability test, re-use our kernel sources and built
	# objects.
	if [ -n "$bisectability" ]; then
		aiaiai-test-bisectability $verbose $preserve -j "$jobs" -c "$commit_id1" \
			--dont-clone -i "$mbox" -w "$tmpdir" -o "$obj1" \
			${confdir:+-C $confdir} ${kmake_opts:+-M "$kmake_opts"} -- \
			"$cloned_kernel1" "$config" > "$tmpdir/$config.bisect.results" &
		pid_bisect="$!"
	fi

	verbose "Build patched kernel (\"$cloned_kernel2\", configuration \"$config\")"

	local log2="$logdir2/$config"
	local obj2="$tmpdir2/obj.$config"

	aiaiai-make-kernel $verbose $sparse $smatch $cppcheck $coccinelle -o "$obj2" \
		--check-only "$mod_files" -j "$jobs" $arch_opt -D "$defconfig" \
		-O "$log2.stdout.log" -E "$log2.stderr.log" \
		${kmake_opts:+-M "$kmake_opts"} -- "$cloned_kernel2" "$targets"

	verbose "Done with patched kernel"

	aiaiai-diff-log $verbose $preserve "$tmpdir/diff-for-diff-log" "$log1.stderr.log" \
		        -w "$tmpdir" "$log2.stderr.log" > "$tmpdir/$config.stderr.diff"

	if [ -n "$bisectability" ]; then
		# MUST sync with bisect test before removing build artifacts
		wait "$pid_bisect" || die "aiaiai-test-bisectability failed"

		# Clean-up the git tree after the test
		rm $verbose -rf "$cloned_kernel1/.git/rebase-apply" >&2
		verbose "Done with bisectability test"
	fi

	if [ -n "$preserve" ]; then
		message "Preserving objdirs: $obj1 $obj2"
	else
		[ -d "$obj1" ] && (verbose "Removing $obj1"; rm -rf -- "$obj1" >&2)
		[ -d "$obj2" ] && (verbose "Removing $obj2"; rm -rf -- "$obj2" >&2)
	fi
}

tmpdir=
preserve=
cleanup_handler()
{
	if [ -n "$preserve" ]; then
		message "Preserved tmpdir: $tmpdir"
	else
		[ -z "$tmpdir" ] || verbose "Removing $tmpdir";
		rm -rf -- "$tmpdir" >&2
	fi

	# Just in case we were interrupted, kill all our children
	local child
	for child in $(ps -o pid --no-headers --ppid "$$"); do
		kill "$child" > /dev/null 2>&1 ||:
	done
}
set_cleanup_handler cleanup_handler

TEMP=`getopt -n $PROG -o j:,c:,i:,w:,C:,p,Q:,K:,M:,v,h --long jobs:,commit-id:,input:,workdir:,logdir:,confdir:,preserve,bisectability,sparse,smatch,cppcheck,coccinelle,checkpatch,quick-fixes:,targets:,keywords:,kmake-opts:,verbose,help -- "$@"` ||
	fail_usage ""
eval set -- "$TEMP"

jobs=1
logdir=
confdir=
commit_id1=HEAD
mbox=
bisectability=
sparse=
smatch=
cppcheck=
coccinelle=
checkpatch="yes" # Keep enabled by default for backwards compatibility
quick_fixes=
targets="all"
keywords=
kmake_opts=
verbose=
quiet="-q"

while true; do
	case "$1" in
	-j|--jobs)
		jobs="$(opt_check_number "$1" "$2")"
		shift
		;;
	-c|--commit-id)
		commit_id1="$2"
		shift
		;;
	-i|--input)
		mbox="$(opt_check_read "$1" "$2")"
		shift
		;;
	-w|--workdir)
		mkdir $verbose -p -- "$2" >&2
		tmpdir="$(mktemp --tmpdir="$(readlink -fv -- "$2")" -dt "$PROG.XXXX")"
		shift
		;;
	--logdir)
		logdir="$(opt_check_dir "$1" "$2")"
		shift
		;;
	-C|--confdir)
		confdir="$(opt_check_dir "$1" "$2")"
		shift
		;;
	-p|--preserve)
		preserve="--preserve"
		;;
	--bisectability)
		bisectability="--bisectability"
		;;
	--sparse)
		sparse="--sparse"
		program_required "sparse" "See section 'sparse' in doc/README"
		;;
	--smatch)
		smatch="--smatch"
		program_required "smatch" "See section 'smatch' in doc/README"
		;;
	--cppcheck)
		cppcheck="--cppcheck"
		program_required "cppcheck" "Usually Linux distribution provide a cppcheck package"
		;;
	--coccinelle)
		coccinelle="yes"
		program_required "spatch" "Usually Linux distribution provide a 'spatch' or 'coccinelle' package"
		;;
	--checkpatch)
		checkpatch="yes"
		;;
	--nosparse)
		sparse=
		;;
	--nosmatch)
		smatch=
		;;
	--nocppcheck)
		cppcheck=
		;;
	--nococcinelle)
		coccinelle=
		;;
	--nocheckpatch)
		checkpatch=
		;;
	-Q|--quick-fixes)
		quick_fixes="$(opt_check_read "$1" "$2")"
		shift
		;;
	--targets)
		targets="$2"
		shift
		;;
	-K|--keywords)
		keywords="$(opt_check_read "$1" "$2")"
		shift
		;;
	-M|--kmake-opts)
		kmake_opts="$2"
		shift
		;;
	-v|--verbose)
		verbose=-v
		quiet=
		;;
	-h|--help)
		show_usage
		exit 0
		;;
	--) shift; break
                ;;
	*) fail_usage "Unrecognized option: $1"
		;;
	esac
	shift
done

[ "$#" -ge 2 ] || fail_usage "Insufficient arguments"

kernel_tree="$(readlink -ev -- "$1")"; shift
defconfigs="$@"

# Make sure external programs we depend on are installed
program_required "git" ""
program_required "make" ""
program_required "patch" ""
program_required "formail" ""
program_required "python" ""
program_required "perl" ""
program_required "diff" ""
program_required "tar" ""

for defconfig in $defconfigs; do
	cross="$(leave_third "$defconfig")";
	program_required "${cross}gcc" ""
done

[ -n "$tmpdir" ] || tmpdir="$(mktemp -dt "$PROG.XXXX")"
tmpdir1="$tmpdir/before"
tmpdir2="$tmpdir/after"
mkdir -p $verbose "$tmpdir1" "$tmpdir2" >&2

if [ -n "$logdir" ]; then
	logdir1="$logdir/before"
	logdir2="$logdir/after"
	mkdir -p $verbose "$logdir1" "$logdir2" >&2
else
	logdir="$tmpdir"
	logdir1="$tmpdir1"
	logdir2="$tmpdir2"
fi

# Save the mbox to a temporary file if it comes from stdin
if [ -z "$mbox" ]; then
	mbox="$tmpdir/mbox"
	cat > "$mbox"
else
	cp $verbose "$mbox" "$tmpdir/mbox" >&2
	mbox="$tmpdir/mbox"
fi

cloned_kernel2="$tmpdir2/src"
git clone --shared "${verbose:--q}" "$kernel_tree" "$cloned_kernel2" >&2
cd "$cloned_kernel2"

# Reset to the base commit id
commit_id1="$(git --git-dir="$(git_dir "$kernel_tree")" rev-parse "$commit_id1^{commit}")"
verbose "Base commit: $(git log -1 --oneline "$commit_id1")"
git reset $quiet --hard "$commit_id1" >&2

# Apply quick fixes
if [ -n "$quick_fixes" ]; then
	verbose "Applying quick fixes from $quick_fixes"
	git apply "$quick_fixes" >&2
	git add -A >&2
	git commit $quiet -m "Quick fixes - applied by aiaiai" >&2
	commit_id1="$(git rev-parse "HEAD^{commit}")"
fi

# Apply the patch
apply_patch < "$mbox" || exit 0

commit_id2="$(git rev-parse "HEAD^{commit}")"
verbose "Commit after applying the patch(es): $(git log -1 --oneline "$commit_id2")"

# Re-generate the patch. Note, we prefer to not use the input mbox directly
# because it may be encoded (e.g., with quoted-printable).
git format-patch -M --patience --stdout "$commit_id1".."$commit_id2" > "$mbox"

# Generate a diff for aiaiai-diff-log
git diff -U0 -M "$commit_id1".."$commit_id2" > "$tmpdir/diff-for-diff-log"

# Generate a diff for checkpatch.pl
git diff -M "$commit_id1".."$commit_id2" > "$tmpdir/diff-for-checkpatch"

# Make a copy of 'checkpatch.pl' and coccinelle scripts. This is safer than
# using them directly from the git tree because we'll apply patches under test
# there, and the patches may also change 'checkpatch.pl' or coccinelle scripts.
if [ -n "$checkpatch" ]; then
	if git cat-file -e "$commit_id1:scripts/checkpatch.pl" 2>/dev/null; then
		mkdir -p $verbose "$tmpdir/checkpatch" >&2
		checkpatch_pl="$tmpdir/checkpatch/checkpatch.pl"
		git show "$commit_id1:scripts/checkpatch.pl" > "$checkpatch_pl"
		chmod $verbose u+x "$checkpatch_pl" >&2

		# Grab the typo corrections if the kernel under test has
		# one (no need to report errors... if something is weird
		# checkpatch will let us know).
		if git cat-file -e "$commit_id1:scripts/spelling.txt" 2>/dev/null; then
			git show "$commit_id1:scripts/spelling.txt" > "$tmpdir/checkpatch/spelling.txt"
		fi

		# Also grab the list of constant structures (if present in this kernel)
		if git cat-file -e "$commit_id1:scripts/const_structs.checkpatch" 2>/dev/null; then
			git show "$commit_id1:scripts/const_structs.checkpatch" > "$tmpdir/checkpatch/const_structs.checkpatch"
		fi

	else
		verbose "Can't find checkpatch.pl.. disabling checkpatch tests."
		checkpatch=
	fi
fi

if [ -n "$coccinelle" ]; then
	if git cat-file -e "$commit_id1:scripts/coccinelle"; then
		mkdir -p $verbose "$tmpdir/coccinelle" >&2
		git archive "$commit_id1" scripts/coccinelle | \
			tar $verbose --strip-components=2 -C "$tmpdir/coccinelle" -x >&2
		coccinelle="--coccinelle=$tmpdir/coccinelle"
	else
		verbose "Can't find coccinelle scripts.. disabling coccinelle tests."
		coccinelle=
	fi
fi

# Run checkpatch.pl in backgound.
if [ -n "$checkpatch" ]; then
	test_checkpatch &
	pid_checkpatch="$!"
fi

# Search for keywords
if [ -n "$keywords" ]; then
	aiaiai-match-keywords $verbose "$mbox" < "$keywords" > "$tmpdir/keywords.results" &
	pid_keywords="$!"
fi

# Generate the list of modified files
mod_files="$tmpdir/mod_files"
git diff --numstat $commit_id1..$commit_id2 |
	sed -e 's/[[:digit:][:space:]]\+//' > "$mod_files"

# Clone another copy of the kernel tree
cloned_kernel1="$tmpdir1/src"
git clone --shared "${verbose:--q}" "$cloned_kernel2" "$cloned_kernel1" >&2
cd "$cloned_kernel1"

# Main loop for testing all configurations
for defconfig in $defconfigs; do
	test_configuration "$defconfig"
done

[ -z "$checkpatch" ]   || wait "$pid_checkpatch" || die "checkpatch failed"
[ -z "$keywords" ]     || wait "$pid_keywords" || die "aiaiai-match-keywords failed"

# Print the results

echo "Tested the patch(es) on top of the following commits:"
git --no-pager log -4 --oneline "$commit_id1"

for defconfig in $defconfigs; do
	log1="$logdir1/$defconfig.stderr.log"
	log2="$logdir2/$defconfig.stderr.log"

	printf "\n"
	print_separator

	# Make kernel appends a "FAILURE" string at the end when builds fail
	base_commit_failed=""
	if build_failed "$log1"; then
		build_failure "$defconfig" "$commit_id1" "the base commit" < "$log1"
		base_commit_failed="yes"
		print_separator
	fi

	if build_failed "$log2"; then
		build_failure "$defconfig" "$commit_id2" < "$log2"
		continue
	fi

	if [ -n "$bisectability" ] && [ -s "$tmpdir/$defconfig.bisect.results" ]; then
		printf "\n%s\n\n" "Bisectability test results for configuration \"$defconfig\""
		cat "$tmpdir/$defconfig.bisect.results"
		printf "\n"
		print_separator
	fi

	if [ -s "$tmpdir/$defconfig.stderr.diff" ]; then
		printf "\n%s\n" "Successfully built configuration \"$defconfig\", the resulting"
		printf "%s" "build diff "

		if [ -n "$base_commit_failed" ]; then
			printf "%s\n\n" "is rather messy because the base commit failed to build:"
		else
			printf "%s\n\n" "is:"
		fi

		cat "$tmpdir/$defconfig.stderr.diff"

	else
		printf "\n%s\n" "Successfully built configuration \"$defconfig\", no issues."
	fi
done;

if [ -s "$tmpdir/keywords.results" ]; then
	printf "\n"
	print_separator
	printf "\n%s\n\n" "Your patch contains unwanted keywords:"
	cat "$tmpdir/keywords.results"
fi

if [ -s "$tmpdir/checkpatch.results" ]; then
	printf "\n"
	print_separator
	printf "\n%s\n" "checkpatch.pl has some complaints:"
	cat "$tmpdir/checkpatch.results"
fi

printf "\n"
print_separator