summaryrefslogtreecommitdiff
path: root/build-test-kernel
blob: 72990bae3cf2a8128ca8bf26177be53d05f8af55 (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
#!/usr/bin/env bash

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

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

. "$ktest_dir/lib/libktest.sh"

checkdep gcc
checkdep make
checkdep bison
checkdep flex
checkdep bc

ktest_kernel_source="."	# dir of kernel source
			#       set with: -k <path>
			#       defaults: current directory
ktest_njobs=$(nproc)
ktest_precise=false

COVERAGE=""		# doing code coverage?
MAKEARGS=()
DEPMOD=depmod

if ! which depmod > /dev/null; then
    DEPMOD=/sbin/depmod
fi

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 "      -c <dir>        enable coverage for this dir (only valid without -K)"
    echo "      -M <arg>        Extra arguments to be passed to make when building the 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:Pc:M:h${ktest_args}" arg; do
    case $arg in
	k)
	    ktest_kernel_source="$OPTARG"
	    ;;
	P)
	    ktest_precise=true
	    ;;
	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
	    ;;
	M)
	    MAKEARGS+=("$OPTARG")
	    ;;

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

parse_args_post

# default parameters
[[ -z $ktest_kernel_source ]]	&& ktest_kernel_source="."

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")

ktest_kernel_build="$ktest_out/kernel_build.$ktest_arch"
mkdir -p "$ktest_kernel_build"

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

run_ktest()
{
    arg=$1
    shift

    "$KTEST" "$arg" $KTESTARGS "$@"
}

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

    make --jobs="$ktest_njobs"			\
	--directory="$ktest_kernel_source"    	\
	O="$ktest_kernel_build"			\
	INSTALL_MOD_PATH="$ktest_kernel_binary"	\
	"${ktest_kernel_make_append[@]}"	\
	"${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"
    local val=y

    if [[ $opt =~ = ]]; then
	local val=${opt#*=}
	opt="${opt%=*}"
    fi

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

	    [[ $c = undef ]] && c=n

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

build_kernel()
{
    rm -rf "$ktest_kernel_binary"
    mkdir -p "$ktest_kernel_binary"
    local kconfig="$ktest_kernel_build/.config"

    if $ktest_precise && [[ -f "$kconfig" ]]; then
	mv "$kconfig" "$kconfig".bak
    fi

    new_config

    log_verbose "kernel_config_require: ${ktest_kernel_config_require[@]}"

    MAKEARGS+=("LOCALVERSION=-ktest")

    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

    # Preserve timestamp if config didn't change:
    if $ktest_precise && [[ -f "$kconfig".bak ]] && diff -q "$kconfig" "$kconfig".bak; then
	mv "$kconfig".bak "$kconfig"
    fi

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

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

    case $ktest_arch in
	x86*)
	    install -m0644 "$BOOT/bzImage"	"$ktest_kernel_binary/vmlinuz"
	    ;;
	aarch64)
	    install -m0644 "$BOOT/Image"	"$ktest_kernel_binary/vmlinuz"
	    ;;
	mips)
	    install -m0644 "$BOOT/vmlinux.strip"	"$ktest_kernel_binary/vmlinuz"
	    #install -m0644 "$ktest_kernel_build/vmlinux"	"$ktest_kernel_binary/vmlinuz"
	    ;;
	default)
	    echo "Don't know how to install kernel"
	    exit 1
	    ;;
    esac

    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

    local kernel_version=$(cat "$ktest_kernel_build/include/config/kernel.release")
    $DEPMOD -b "$ktest_kernel_binary/" -v $kernel_version
}

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

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

    echo Running test $(basename "$ktest_test") on $(uname -n) at $(pwd)
    parse_test_deps "$ktest_test"

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

    run_quiet "building kernel" build_kernel

    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_faddr2line()
{
    ./scripts/faddr2line "$ktest_kernel_build/vmlinux" $@
}

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 "$@"