aboutsummaryrefslogtreecommitdiff
path: root/kbuildtest.sh
blob: 63a0056c1b79818c7bbb22a9eaa66fc9f79d6a2f (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
#!/bin/sh
#
# copyright 2015, AKASHI Takahiro
# GPL v2

SELF=$0
GETOPTARGS="a:A:b:c:C:e:hi:Il:no:p:Sv"
ARGS=$(getopt ${GETOPTARGS} $*)

function print_usage() {
    echo "Usage: $(basename ${SELF}) [options] [<commit>]"
    echo "options:"
    echo "  -a <config>...: forcedly add these configs"
    echo "  -A <ARCH>: overide ARCH"
    echo "  -b <build dir>: passed to 'make' as O="
    echo "  -c <config target>...: more config targets, like allmodconfig"
    echo "  -C <CROSS_COMPILE>: overide CROSS_COMPILE"
    echo "  -e <config>...: exclude these configs"
    echo "  -h : help messages"
    echo "  -i <config>...: include these configs"
#    echo "  -I : inferior execution of loops"
    echo "  -l <log root dir>"
    echo "  -n : not executing, but creating environment"
    echo "  -o <flag>...: passed to 'make' as flags"
    echo "  -p <param file>: re-use parameter file"
#    echo "  -S : skip creating environment"
    echo "  -v : verbose messages"
    echo "<commit>: commit object name to start from"
}

function parse_args() {
    ECHO=true
    set -- ${ARGS}
    for i
    do
        case $i in
        -a)
            ADD_CONFIGS="$2"
            ADD_CONFIGS=${ADD_CONFIGS//,/ }
            shift 2;;
        -A)
            ARCH="$2"
            shift 2;;
        -b)
            BUILDTMPDIR="$2"
            shift 2;;
        -c)
            CONFIG_TARGETS="$2"
            CONFIG_TARGETS=${CONFIG_TARGETS//,/ }
            shift 2;;
        -C)
            CROSS_COMPILE="$2"
            shift 2;;
        -e)
            EXCL_CONFIGS="$2"
            EXCL_CONFIGS=${EXCL_CONFIGS//,/ }
            shift 2;;
        -h)
	    print_usage
	    exit 2;;
        -i)
            INCL_CONFIGS="$2"
            INCL_CONFIGS=${INCL_CONFIGS//,/ }
            shift 2;;
        -I)
            iflag="y";
            shift;;
        -l)
            LOGROOTDIR="$2"
            shift 2;;
        -n)
            nflag="-n";
            shift;;
        -o)
            OFLAGS="$2"
	    OFLAGS=${OFLAGS//,/ }
            shift 2;;
        -p)
            PARAMFILE="$2"
            shift 2;;
        -S)
            sflag="-S";
            shift;;
        -v)
            vflag="-v";
	    ECHO=echo
            shift;;
        --)
            shift;
	    break;;
        esac
    done

    # Infenior loop check
    if [ -n "${iflag}" ]
    then
	if [ -z "${PARAMFILE}" ]
	then
            echo "Inferior loop, but no -p specified"
            exit 2
	else
	    return
	fi
    fi

    # top level check
    if [ -n "${PARAMFILE}" ]
    then
	if [ ! -f "${PARAMFILE}" ]
	then
	    echo "${PARAMFILE} not found"
	    exit 2
	fi
	sflag="-S"
	return
    fi

    if [ $# -eq 0 ]
    then
        print_usage
        exit 2
    fi
    COMMIT="$1"

    case "${ARCH}" in
    x86|arm64)
        ;;
    *)
        echo "ARCH '${ARCH}' is invalid or NULL"
        exit 2
    esac

    type ${CROSS_COMPILE}gcc > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
        echo "CC '${CROSS_COMPILE}gcc' not found"
        exit 2
    fi

    if [ -z "${LOGROOTDIR}" ]
    then
        LOGROOTDIR="/tmp"
    elif [ ! -d ${LOGROOTDIR} ]
    then
        echo "Log dir '${LOGROOTDIR}' does not exist"
        exit 2
    fi

    if [ -z "${BUILDTMPDIR}" ]
    then
        BUILDTMPDIR="/tmp"
    elif [ ! -d ${BUILDTMPDIR} ]
    then
        echo "Build dir '${BUILDTMPDIR}' does not exist"
        exit 2
    fi

    if [ -z "${OFLAGS}" ]
    then
	case "${ARCH}" in
	x86)
	    OFLAGS=zImage;;
	arm64)
	    OFLAGS=Image;;
	esac
	OFLAGS="-j$(nproc) ${OFLAGS}"
    else
	OFLAGS=${OFLAGS//,/ }
    fi
}

function check_patches {
#    echo doing git log --format=%h ${COMMIT}..HEAD
    PATCHLIST=$(git log --format=%h ${COMMIT}..HEAD 2> /dev/null | xargs echo)
    if [ $? -ne 0 ] || [ -z "${PATCHLIST}" ]
    then
        echo doubious commit: ${COMMIT}
        exit 2
    fi
}

function show_info {
    ${ECHO} "ARCH:" ${ARCH}
    ${ECHO} "CC:  " ${CROSS_COMPILE}gcc

    ${ECHO} "Log dir:  " ${LOGDIR}
    ${ECHO} "Build dir:" ${BUILDDIR}

    if [ -n "${vflag}" ]
    then
        n=$(echo ${PATCHLIST} | wc -w)
        echo "Commits:"
        git log --oneline --reverse ${COMMIT}..HEAD | \
            awk "BEGIN {i=1} {printf \"  [%d/%d] %s\n\", i, $n, \$p; i++}"
    fi
    ${ECHO} ======
}

function setup_env {
    DIRNAME=$(date +%y%m%d%H%M)_$$
    LOGDIR=${LOGROOTDIR}/${DIRNAME}
    PARAMFILE=${LOGDIR}/params
    BUILDDIR=${BUILDTMPDIR}/${DIRNAME}_build

    mkdir ${LOGDIR}
    mkdir ${BUILDDIR}
}

function save_env {
    echo ADD_CONFIGS=\"${ADD_CONFIGS}\"		>> ${PARAMFILE}
    if [ -n "${ARCH}" ] ; then
    echo ARCH=${ARCH}				>> ${PARAMFILE}
    fi
    if [ -n "${CROSS_COMPILE}" ] ; then
    echo CROSS_COMPILE=${CROSS_COMPILE}		>> ${PARAMFILE}
    fi
    echo EXCL_CONFIGS=\"${EXCL_CONFIGS}\"	>> ${PARAMFILE}
    echo INCL_CONFIGS=\"${INCL_CONFIGS}\"	>> ${PARAMFILE}
    echo CONFIG_TARGETS=\"${CONFIG_TARGETS}\"	>> ${PARAMFILE}
    echo LOGDIR=${LOGDIR}			>> ${PARAMFILE}
    echo BUILDDIR=${BUILDDIR}			>> ${PARAMFILE}
    echo OFLAGS=\"${OFLAGS}\"			>> ${PARAMFILE}
    echo COMMIT=${COMMIT}			>> ${PARAMFILE}
    echo PATCHLIST=\"${PATCHLIST}\"		>> ${PARAMFILE}
}

function do_combo {
#    echo do_combo $1 - $2
    if [ -z "$2" ]
    then
        mkdir ${LOGDIR}/$p/$i
        ${ECHO} "$i:" $1
	echo $1 | tr " " "\n" > ${LOGDIR}/$p/$i/kernel_config
        i=$(expr $i + 1)

	return
    fi

    local head=$(echo $2 | awk '{print $1}')
    local tail=$(echo $2 | awk '{$1=""; print}')
    local head_0=$(echo ${head} | sed 's/\(CONFIG_[A-Za-z0-9_]*\)=.*/\1/')
    if [ "${head}" = "${head_0}" ]
    then
	local head_y="${head_0}=y"
    else
	local head_y=${head}
    fi
    local head_n="${head_0}=n"

    if [ -z "${tail}" ]
    then
        mkdir ${LOGDIR}/$p/$i
        ${ECHO}  "$i:" $1 $head_n
        echo $1 $head_n | tr " " "\n" > ${LOGDIR}/$p/$i/kernel_config
        i=$(expr $i + 1)

        mkdir ${LOGDIR}/$p/$i
        ${ECHO} "$i:" $1 $head_y
        echo $1 $head_y | tr " " "\n" > ${LOGDIR}/$p/$i/kernel_config
        i=$(expr $i + 1)

	return
    fi

    do_combo "$1 $head_n" "$tail"
    do_combo "$1 $head_y" "$tail"
}

function create_configs {
    ${ECHO} Creating configs for $p

    ALLCONFIGS=${LOGDIR}/allconfigs
    if [ -n "${ADD_CONFIGS}" ]
    then
        ADD_CONFIGS=$(echo ${ADD_CONFIGS} | tr " " "\n" | \
		awk '{print "CONFIG_"$1}' | \
		sed -n -e '/\([^=]*\)=./p' -e 's/^\([^=]*\)$/&=y/p' | \
		xargs echo)
    fi
    if [ -n "${INCL_CONFIGS}" ]
    then
        INCL_CONFIGS=$(echo ${INCL_CONFIGS} | tr " " "\n" | \
	    awk '{print "CONFIG_"$1}' | xargs echo)
    fi

    git diff $p^..$p | \
    grep '^+' | grep CONFIG_ | \
    sed -n	':again
		    s/\(.*\)CONFIG_\([A-Za-z0-9_]*\)\(.*\)/config_\2 \1/
		    t again
		    s/\(.*config_[A-Za-z0-9_]*\)\(.*\)/\1/
		 s/config_/CONFIG_/gp
		' - | (cat - ; echo ${INCL_CONFIGS}) | \
    tr -s " " "\n" | sort | uniq | comm -23 - ${ALLCONFIGS} >> ${ALLCONFIGS}
    l=$(tempfile -d /tmp)
    sort ${ALLCONFIGS} | sed '/^$/d' > $l
    mv -f $l ${ALLCONFIGS}

    for e in ${EXCL_CONFIGS}
    do
        sed -i -e "/CONFIG_$e/d" ${ALLCONFIGS}
    done

    COMBO_CONFIGS=$(cat ${ALLCONFIGS} | \
	sed -n -e '/\([^=]*\)=./p' -e 's/^\([^=]*\)$/&=y/p' | \
	xargs echo)
    i=1
    do_combo "${ADD_CONFIGS}" "${COMBO_CONFIGS}"

    for j in $(seq 1 $(expr $i - 1))
    do
        make O=${BUILDDIR} defconfig > ${LOGDIR}/$p/$j/config.log 2>&1
        cat ${LOGDIR}/$p/$c/kernel_config >> ${BUILDDIR}/.config
        make O=${BUILDDIR} olddefconfig > ${LOGDIR}/$p/$j/config.log 2>&1
        if [ $? -eq 0 ]
        then
            cp ${BUILDDIR}/.config ${LOGDIR}/$p/$j/kernel_config_build
        fi
    done

    for t in ${CONFIG_TARGETS}
    do
        mkdir ${LOGDIR}/$p/$i
        ${ECHO} "$i:" $t
        make O=${BUILDDIR} $t > ${LOGDIR}/$p/$i/config.log 2>&1
        if [ $? -eq 0 ]
        then
            # just for marking
            echo $t > ${LOGDIR}/$p/$i/kernel_config

            cp ${BUILDDIR}/.config ${LOGDIR}/$p/$i/kernel_config_build
        fi
        i = $(expr $i + 1)
    done
}

function run_build_test {
    if [ -n "${sflag}" ]
    then
        if [ -f "${LOGDIR}/$p/$c/kernel_config_build" ]
        then
	    cp ${LOGDIR}/$p/$c/kernel_config_build ${BUILDDIR}/.config
        else
            if [ ! -f "${LOGDIR}/$p/$c/kernel_config" ]
            then
                echo $p/$c FAIL config >> ${LOGDIR}/results
                return
            fi
            make O=${BUILDDIR} defconfig > ${LOGDIR}/$p/$c/config.log 2>&1
            cat ${LOGDIR}/$p/$c/kernel_config >> ${BUILDDIR}/.config
            make O=${BUILDDIR} olddefconfig > ${LOGDIR}/$p/$c/config.log 2>&1
            if [ $? -ne 0 ]
            then
                echo $p/$c FAIL config >> ${LOGDIR}/results
                return
            fi
            cp ${BUILDDIR}/.config ${LOGDIR}/$p/$c/kernel_config_build
        fi
    else
        if [ -f "${LOGDIR}/$p/$c/kernel_config_build" ]
        then
	    cp ${LOGDIR}/$p/$c/kernel_config_build ${BUILDDIR}/.config
        else
            echo $p/$c FAIL config >> ${LOGDIR}/results
            return
        fi
    fi

    ${ECHO} $c: make O=${BUILDDIR} ${OFLAGS}
    make O=${BUILDDIR} ${OFLAGS} > ${LOGDIR}/$p/$c/build.log 2>&1
    if [ $? -eq 0 ]
    then
        if [ $(cat ${LOGDIR}/$p/$c/build.log | \
                grep -E "(note:|warning:)" | wc -l) -ne 0 ]
        then
            echo $p/$c PASS warning
        else
            echo $p/$c PASS
        fi
    else
        echo $p/$c FAIL
    fi >> ${LOGDIR}/results
#	test run
#	./baa
    make O=${BUILDDIR} clean > ${LOGDIR}/$p/$c/clean.log 2>&1
}

function run_build_test_configs {
    p=$(git log --format=%h -1)
    mkdir ${LOGDIR}/$p

    if [ -z "${sflag}" ]
    then
        create_configs
    fi

    TESTS=$(cd ${LOGDIR}/$p ; ls -v)
#    echo TESTS are ${TESTS}

    error=0
    if [ -n "${nflag}" ]
    then
	return
    else
	${ECHO} Processing patch $p "($(echo ${TESTS} | xargs echo))"
    fi

    for c in ${TESTS}
    do
        run_build_test
	if [ ${error} -ne 0 ]
	then
	    return
	fi
    done
}

function run_build_test_loop {
    make mrproper > /dev/null 2>&1

#    for later git,
#    git rebase -x "${SELF} ..." -i ${COMMIT}
#      or
    SED_CMD="/^pick /a exec ${SELF} ${nflag} ${sflag} ${vflag} -I -p ${PARAMFILE}"
    export GIT_EDITOR="sed -i -e '${SED_CMD}'"
#    echo GIT_EIDTOR is ${GIT_EDITOR}
    git rebase -i ${COMMIT} 2> /dev/null

    if [ -n "${vflag}" ] && [ -z "${nflag}" ]
    then
	echo ======
	echo results:
	cat ${LOGDIR}/results
    fi
}

#
# main
#
parse_args

# for inferior loop
if [ -n "${iflag}" ]
then
    trap "git rebase --abort" SIGINT SIGTERM

    . ${PARAMFILE}
    run_build_test_configs
    if [ ${error} -eq 0 ]
    then
	exit 0
    else
	git rebase --abort
        exit 1
    fi
fi

# top level
if [ -z "${sflag}" ]
then
    check_patches

    setup_env

    save_env
else
    . ${PARAMFILE}
    mv ${LOGDIR}/results ${LOGDIR}/results.old > /dev/null 2>&1
fi

show_info

run_build_test_loop

exit 0