summaryrefslogtreecommitdiff
path: root/build-test-kernel
blob: 331407518526a4869ba3df02059dea28a255b580 (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
#!/bin/bash

# Dec 2019: requires additional validation via https://github.com/koalaman/shellcheck

set -o nounset
set -o errexit
set -o errtrace

ktest_dir=$(dirname "$(readlink -f "$0")")
KTEST=$ktest_dir/ktest

source "$ktest_dir/lib/libktest.sh"

checkdep gcc
checkdep make

ktest_kernel_source="." # dir of kernel source
#       set with: -k <path>
#       defaults: current directory
ktest_kernel_build=""  # kernel build dir
# defaults to
# $ktest_kernel_source/.build-test-kernel-$ktest_arch
ktest_kernel_binary="" # kernel output dir
# defaults to $ktest_kernel_build/kpkg

BUILD=1 # if set to 1, kernel will build
DEPS=1
COVERAGE="" # doing code coverage?
MAKEARGS=()
ktest_njobs=$(($(grep -c '^processor' /proc/cpuinfo) * 2))
# number of jobs to passed to make during kernel compile
#       sets with: -j
#       defaults to 2 * number of processor

usage() {
	echo "build-test-kernel: Run generic virtual machine tests"
	echo "Usage: build-test-kernel cmd [options]"
	ktest_usage_cmds
	echo "  oldconfig   Run make oldconfig"
	echo "  config      Run make nconfig"
	echo
	echo "  options:"
	ktest_usage_opts
	echo
	echo " options for build-test-kernel run:"
	ktest_usage_run_opts
	echo "      -k <dir>    kernel source dir"
	echo "      -b <dir>    build directory for kernel (default: kernel_source/.build-test-kernel)"
	echo "      -o <dir>    output directory for kernel binary (default: kernel_build/kpkg"
	echo "      -K          don't rebuild kernel"
	echo "      -D          don't modify kernel .config"
	echo "      -c <dir>    enable coverage for this dir (only valid without -K)"
	echo "      -j <num>    j option to make"
	echo "      -K          don't rebuild kernel"
	echo
	ktest_usage_post
}

if [[ $# == 0 ]]; then
	usage
	exit 1
fi

#parse command and shift for rest of arg parsing
CMD="$1"
shift

while getopts "k:b:o:KDc:j:h${ktest_args}" arg; do
	case $arg in
	k)
		ktest_kernel_source="$OPTARG"
		;;
	b)
		ktest_kernel_build="$OPTARG"
		;;
	o)
		ktest_kernel_binary="$OPTARG"
		;;
	K)
		BUILD=""
		;;
	D)
		DEPS=""
		;;
	c)
		if [[ ! -d $OPTARG ]]; then
			echo "$OPTARG must be a directory"
			exit 1
		fi

		checkdep lcov

		# Strip trailing / from directory name, substitute _ for /
		OPTARG=$(echo "${OPTARG%/}" | tr / _)
		MAKEARGS+=("GCOV_PROFILE_$OPTARG=y")
		COVERAGE=1
		;;
	j)
		ktest_njobs=$OPTARG
		;;

	h)
		usage
		exit 0
		;;
	esac
	parse_ktest_arg "$arg"
done
shift $((OPTIND - 1))

# default parameters
[[ -z $ktest_kernel_source ]] && ktest_kernel_source="."
[[ -z $ktest_kernel_build ]] && ktest_kernel_build="$ktest_kernel_source/.build_test_kernel-$ktest_arch"
[[ -z $ktest_kernel_binary ]] && ktest_kernel_binary="$ktest_kernel_build/kpgk"

if [[ ! -d $ktest_kernel_source ]]; then
	echo "kernel source directory $ktest_kernel_source does not exist"
	exit 1
fi

ktest_kernel_source=$(readlink -e "$ktest_kernel_source")

mkdir -p "$ktest_kernel_build"

ktest_kernel_build=$(readlink -e "$ktest_kernel_build")
ktest_kernel_binary=$(readlink -f "$ktest_kernel_binary")

parse_args_post

if [[ -n $CROSS_COMPILE ]]; then
	checkdep "$ARCH_TRIPLE-gcc" "gcc-$ARCH_TRIPLE"
fi

run_ktest() {
	arg=$1
	shift

	"$KTEST" "$arg" "$KTESTARGS" -k "$ktest_kernel_binary" "$@"
}

do_make() {
	if [[ -n $CROSS_COMPILE ]]; then
		export ARCH="$KERNEL_ARCH"
		export CROSS_COMPILE="$ARCH_TRIPLE-"
	fi

	# work around a bug in make - maybe not needed anymore?
	#    if [[ $# = 0 || $1 != nconfig ]]; then
	#	MAKEARGS+=("--output-sync=target")
	#    fi

	make --jobs="$ktest_njobs" \
		--directory="$ktest_kernel_source" \
		O="$ktest_kernel_build" \
		INSTALL_MOD_PATH="$ktest_kernel_binary" \
		SKIP_STACK_VALIDATION=1 \
		"${MAKEARGS[@]}" \
		"$@"
}

new_config() {
	local kconfig="$ktest_kernel_build/.config"
	local config_tool="$ktest_kernel_source/scripts/config"

	if [[ ! -f $kconfig ]]; then
		do_make allnoconfig

		# Really undefine everything:
		sed -i -e 's/\(CONFIG_.*\)=.*/# \1 is not set/' "$kconfig"
	fi
}

kernel_opt() {
	local cmd=$1
	local opt=$2
	local kconfig="$ktest_kernel_build/.config"
	local config_tool="$ktest_kernel_source/scripts/config"

	if [[ $opt =~ = ]]; then
		local val=${opt: -1}
		opt="${opt%=?}"
	else
		local val=y
	fi

	case $cmd in
	set)
		"$config_tool" --file "$kconfig" --set-val "$opt" "$val"
		;;
	check)
		local c=$("$config_tool" --file "$kconfig" -s "$opt")

		if [[ $c != "$val" ]]; then
			echo "Kernel config option $opt is $c; should be $val"
			exit 1
		fi
		;;
	esac
}

build_kernel() {
	local magic=$ktest_kernel_binary/ktest-kernel-binary-dir

	if [[ -e $ktest_kernel_binary ]] && [[ ! -d $ktest_kernel_binary ]]; then
		echo "$ktest_kernel_binary already exists and is not a directory"
		exit 1
	fi

	if [[ -d $ktest_kernel_binary ]] && [[ -n $(ls -A "$ktest_kernel_binary") ]]; then
		if [[ ! -f $magic ]]; then
			echo "$ktest_kernel_binary already exists and has non ktest kernel contents"
			exit 1
		fi

		rm -rf "$ktest_kernel_binary"
	fi

	mkdir -p "$ktest_kernel_binary"
	touch "$magic"

	if [[ -n $DEPS ]]; then
		new_config

		for opt in "${ktest_kernel_config_require[@]}"; do
			[[ -n $opt ]] && kernel_opt set "$opt"
		done

		do_make olddefconfig

		for opt in "${ktest_kernel_config_require[@]}"; do
			[[ -n $opt ]] && kernel_opt check "$opt"
		done
	fi

	case $KERNEL_ARCH in
	mips)
		do_make vmlinuz
		;;
	*)
		do_make
		;;
	esac

	local BOOT=$ktest_kernel_build/arch/$KERNEL_ARCH/boot

	if [[ -f "$BOOT/bzImage" ]]; then
		install -m0644 "$BOOT/bzImage" "$ktest_kernel_binary/vmlinuz"
	elif [[ -f "$BOOT/vmlinux.strip" ]]; then
		install -m0644 "$BOOT/vmlinux.strip" "$ktest_kernel_binary/vmlinuz"
	else
		install -m0644 "$ktest_kernel_build/vmlinux" "$ktest_kernel_binary/vmlinuz"
	fi

	install -m0644 "$ktest_kernel_build/vmlinux" "$ktest_kernel_binary/vmlinux"
	install -m0644 "$ktest_kernel_build/.config" "$ktest_kernel_binary/config"

	# if there weren't actually any modules selected, make modules_install gets
	# confused:
	touch "$ktest_kernel_build/modules.order"
	touch "$ktest_kernel_build/modules.builtin"

	do_make modules_install
}

cmd_run() {
	if [[ $# == 0 ]]; then
		echo "build-test-kernel: missing test"
		usage
		exit 1
	fi

	ktest_test=$1
	shift
	ktest_testargs=("$@")

	parse_test_deps "$ktest_test"

	if [[ -n $COVERAGE ]]; then
		ktest_kernel_config_require+=(GCOV_KERNEL)
	fi

	if [[ -n $BUILD ]]; then
		run_quiet "building kernel" build_kernel
	fi

	start_vm
}

cmd_boot() {
	cmd_run "$ktest_dir/boot.ktest"
}

cmd_oldconfig() {
	new_config
	do_make oldconfig
}

cmd_config() {
	new_config
	do_make nconfig
}

cmd_help() {
	usage
}

if [[ $(type -t "cmd_$CMD") == function ]]; then
	CMD="cmd_$CMD"
elif [[ $(type -t "ktest_$CMD") == function ]]; then
	CMD="ktest_$CMD"
else
	usage
	exit 1
fi

$CMD "$@"