aboutsummaryrefslogtreecommitdiff
path: root/kbuildtest.sh
blob: 5b127d6fd7588d2baef9ab8e7fc1c20b770eccb1 (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
#!/bin/sh

GETOPTARGS="a:A:b:C:e:hi:Il:no:P:v"
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 <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>: parameter file, only used with -I"
    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)
            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;;
        -v)
            vflag="-v";
	    ECHO=echo
            shift;;
        --)
            shift;
	    break;;
        esac
    done

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

    # top level check
    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

    ${ECHO} ARCH is ${ARCH}
    ${ECHO} CC is ${CROSS_COMPILE}gcc
}

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

    mkdir ${LOGDIR}
    mkdir ${BUILDDIR}

    ${ECHO} "Log directory is ${LOGDIR}"
    ${ECHO} "Build directory is ${BUILDDIR}"
}

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

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

function do_combo {
    if [ -z "$2" ]
    then
        ${ECHO} Creating config $p/$i
        mkdir ${LOGDIR}/$p/$i
	echo $1 | tr " " "\n" | sed 's/^\(CONFIG_[A-Za-z0-9_]*\)$/\1=y/' > ${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}')
#    echo 1111 - $1 2222 - $1
#    echo head - $head tail - $tail

    if [ -z "${tail}" ]
    then
        ${ECHO} Creating config $p/$i
        mkdir ${LOGDIR}/$p/$i
        echo $1 $tail | tr " " "\n" | sed 's/^\(CONFIG_[A-Za-z0-9_]*\)$/\1=y/' > ${LOGDIR}/$p/$i/kernel_config
        i=$(expr $i + 1)
        ${ECHO} Creating config $p/$i
        mkdir ${LOGDIR}/$p/$i
        echo $1 $head $tail | tr " " "\n" | sed 's/^\(CONFIG_[A-Za-z0-9_]*\)$/\1=y/' > ${LOGDIR}/$p/$i/kernel_config
        i=$(expr $i + 1)

	return
    fi

#    echo do_combo2 "$1" - "$tail"
    do_combo "$1" "$tail"
#    echo do_combo1 "$1 $head" - "$tail"
    do_combo "$1 $head" "$tail"
}

function create_configs {
    ALLCONFIGS=${LOGDIR}/allconfigs
    if [ ! -z "${ADD_CONFIGS}" ]
    then
        ADD_CONFIGS=$(echo ${ADD_CONFIGS} | tr " " "\n" | \
	    awk '{print "CONFIG_"$1}' | xargs echo)
    fi
    if [ ! -z "${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}

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

    i=1
    do_combo "${ADD_CONFIGS}" "$(cat ${ALLCONFIGS})"
}

function run_build_test {
#    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.log
        return
    fi
    cp ${BUILDDIR}/.config ${LOGDIR}/$p/$c/kernel_config_build

    if [ ! -z ${nflag} ]
    then
	return
    fi

    ${ECHO} Do make O=${BUILDDIR} ${OFLAGS}
    make O=${BUILDDIR} ${OFLAGS} > ${LOGDIR}/$p/$c/build.log 2>&1
    if [ $? -eq 0 ]
    then
        echo $p/$c PASS
    else
        echo $p/$c FAIL
    fi >> ${LOGDIR}/results.log
#	./baa
    make O=${BUILDDIR} clean > ${LOGDIR}/$p/$c/clean.log 2>&1
}

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

    create_configs

    error=0
    for c in $(seq 1 $(expr $i - 1))
    do
        run_build_test
	if [ ${error} -ne 0 ]
	then
	    return
	fi
    done
}

function run_build_test_loop {
#    for later git,
#    git rebase -x "${SELF}" -i ${COMMIT}
#      or
#    export GIT_EDITOR="../inscmd.sh \"exec ${SELF} ${nflag} ${vflag} -I -P ${PARAMFILE} \""
    SED_CMD="/^pick /a exec ${SELF} ${nflag} ${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
}

#
# main
#
SELF=$0
parse_args

# for inferior loop
if [ ! -z ${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
setup_env

create_patchlist

save_env

run_build_test_loop

exit 0