summaryrefslogtreecommitdiff
path: root/tcwg_kernel-build.sh
blob: 2d5fc93398fc19d34bf50b6c8fe19f5a1385970c (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
#!/bin/bash

set -euf -o pipefail

scripts=$(dirname $0)
# shellcheck source=jenkins-helpers.sh
. $scripts/jenkins-helpers.sh
# shellcheck source=round-robin.sh
. $scripts/round-robin.sh

convert_args_to_variables "$@"

obligatory_variables rr[ci_config]

# Execution mode: baseline, bisect, jenkins-full
# shellcheck disable=SC2154
rr[mode]="${rr[mode]-baseline}"

# Set custom revision for one of the projects, and use baseline revisions
# for all other projects.
# shellcheck disable=SC2154
rr[ci_project]="${rr[ci_project]-tcwg_kernel}"
# shellcheck disable=SC2154
rr[baseline_branch]="${rr[baseline_branch]-linaro-local/ci/${rr[ci_project]}/${rr[ci_config]}}"
# shellcheck disable=SC2154
rr[update_baseline]="${rr[update_baseline]-update}"
# shellcheck disable=SC2154
rr[top_artifacts]="${rr[top_artifacts]-$(pwd)/artifacts}"

# {toolchain_name}-{toolchain_ver}-{target}-{linux}-{linux_config}
IFS=- read -a ci_config <<EOF
${rr[ci_config]}
EOF
# shellcheck disable=SC2154
rr[toolchain]=${rr[toolchain]-${ci_config[0]}}
# shellcheck disable=SC2154
rr[release]=${rr[release]-${ci_config[1]}}
# shellcheck disable=SC2154
rr[target]=${rr[target]-${ci_config[2]}}
# shellcheck disable=SC2154
rr[linux_config]=${rr[linux_config]-${ci_config[4]}}

case "${rr[toolchain]}" in
    llvm)
        # shellcheck disable=SC2154
        rr[components]="binutils llvm linux qemu" ;;
    gnu)
        rr[components]="binutils gcc linux qemu" ;;
    *) assert_with_msg "Unknown toolchain \"${rr[toolchain]}\"" false ;;
esac

# Use baseline branches by default.
for c in ${rr[components]}; do
    rr[${c}_branch]=${rr[${c}_branch]-baseline}
    if [ x"${rr[${c}_branch]}" != x"baseline" ]; then
	obligatory_variables rr[${c}_url]
    fi
done

start_at="${start_at-default}"
finish_at="${finish_at-default}"
verbose="${verbose-true}"
verbose2="${verbose2-false}"

if $verbose2; then set -x; fi

trap print_traceback EXIT

# Set start and finish steps for different modes.
default_start_at=""
default_finish_at=""
case "${rr[mode]}" in
    "baseline")
	default_finish_at="update_baseline"
	;;
    "bisect")
	case "${rr[toolchain]}:$(print_single_updated_component)" in
	    gnu:binutils) default_start_at="build_abe-binutils" ;;
	    gnu:gcc) default_start_at="build_abe-stage1" ;;
	    llvm:llvm) default_start_at="build_llvm" ;;
	    *:linux) default_start_at="build_linux" ;;
	    *:qemu) default_start_at="build_abe-qemu" ;;
	    *) assert false ;;
	esac
	default_finish_at="check_regression"
	;;
    "jenkins-full") ;;
esac
if [ x"$start_at" = x"default" ]; then
    start_at="$default_start_at"
fi
if [ x"$finish_at" = x"default" ]; then
    finish_at="$default_finish_at"
fi

run_step_init "$start_at" "$finish_at" "${rr[top_artifacts]}" "$verbose"

# Build Linux kernel
build_linux_1 ()
{
    (
    set -euf -o pipefail

    sanity_check_pwd

    clone_repo linux

    # shellcheck disable=SC2115
    rm -rf "$(pwd)"/bin
    mkdir "$(pwd)"/bin

    local abe_bin cc ld_opt=""

    abe_bin="$(pwd)/abe/builds/destdir/x86_64-pc-linux-gnu/bin"
    # Use binutils $abe_bin for both GNU and LLVM builds.
    # For LLVM builds at the very least we need target assembler, since,
    # otherwise, clang will try to use /usr/bin/as for cases that integrated
    # assembler can't handle.
    export PATH="$abe_bin:$PATH"

    case "${rr[toolchain]}" in
	llvm)
	    local llvm_bin
	    llvm_bin="$(pwd)/llvm-install/bin"
	    export PATH="$llvm_bin:$PATH"

	    cc="$llvm_bin/clang"
	    ld_opt="LD=$llvm_bin/ld.lld"
	    ;;
	gnu)
	    cc="$abe_bin/$(print_gnu_target ${rr[target]})-gcc"
	    ;;
    esac

    # Use ccache only when bisecting linux or qemu (or preparing to).
    # Otherwise the compiler is new in every build and we would
    # only clobber ccache volume.
    local ccache=""
    case "${rr[mode]}:$(print_single_updated_component)" in
	"baseline:") ccache="ccache" ;;
	"bisect:linux") ccache="ccache" ;;
	"bisect:qemu") ccache="ccache" ;;
    esac
    cat > "$(pwd)"/bin/${rr[target]}-cc <<EOF
#!/bin/sh
exec $ccache $cc "\$@"
EOF
    chmod +x "$(pwd)"/bin/${rr[target]}-cc

    # Define make variables.
    local opts
    opts="CC=$(pwd)/bin/${rr[target]}-cc $ld_opt SUBLEVEL=0 EXTRAVERSION=-bisect"
    if [ x"${rr[target]}" != x"$(uname -m)" ]; then
	opts="$opts ARCH=$(print_kernel_target ${rr[target]})"
	opts="$opts CROSS_COMPILE=$(print_gnu_target ${rr[target]})-"
	opts="$opts HOSTCC=gcc"
    fi

    cd linux

    make $opts distclean

    make $opts ${rr[linux_config]}
    sed -i -e 's:CONFIG_LOCALVERSION_AUTO=y:# CONFIG_LOCALVERSION_AUTO is not set:' .config
    set +f; rm -f localversion*; set -f
    make $opts oldconfig

    ccache -z
    KBUILD_BUILD_TIMESTAMP=0 make $opts -j"$(nproc --all)" -s -k &
    local res=0 && wait $! || res=$?
    ccache -s
    return $res
    )
}

# Build linux and count number successfully built .o files in linux
build_linux ()
{
    (
    set -euf -o pipefail

    build_linux_1 &
    local res=0 && wait $! || res=$?

    # Number of .o files created is the main success metric.
    local linux_n_obj
    linux_n_obj=$(find linux -name "*.o" | wc -l)
    echo "# linux_n_obj:" >> ${rr[top_artifacts]}/results
    echo "$linux_n_obj" >> ${rr[top_artifacts]}/results

    if [ $res = 0 ]; then
	echo "# linux build successful:" >> ${rr[top_artifacts]}/results
	echo "all" >> ${rr[top_artifacts]}/results
    fi

    return $res
    )
}

# Boot linux kernel
boot_linux ()
{
    (
    set -euf -o pipefail

    local image cpu
    case ${rr[target]} in
	aarch64)
	    image=linux/arch/arm64/boot/Image.gz
	    cpu="-cpu cortex-a53"
	    ;;
	arm)
	    image=linux/arch/arm/boot/zImage
	    cpu=""
	    ;;
	*) assert false ;;
    esac

    local qemu
    qemu="$(pwd)/abe/builds/hosttools/x86_64-pc-linux-gnu/bin/qemu-system-${rr[target]}"

    timeout --foreground 60s "$qemu" \
	    -kernel $image -machine virt $cpu -m 512 \
	    -serial stdio -display none \
	    -append "console=ttyAMA0 panic=-1" -no-reboot

    echo "# linux boot successful:" >> ${rr[top_artifacts]}/results
    echo "boot" >> ${rr[top_artifacts]}/results
    )
}

# Exit with code 0 if no regression compared to base-artifacts/results.
no_regression_p ()
{
    (
    set -euf -o pipefail

    local ref_artifacts=$1
    local new_artifacts=$2

    local linux_n_obj
    linux_n_obj=$(grep -v "^#" $new_artifacts/results | tail -n1)

    # Assume worst for non-existent baseline.
    local base_linux_n_obj="-10"
    if [ -f $ref_artifacts/results ]; then
	base_linux_n_obj=$(grep -v "^#" $ref_artifacts/results | tail -n1)
    fi

    # In log scan for errors below
    #   - sed -e 's/"[^"]*"//g' -- removes quoted "error: app diagnostics" strings
    #   - " error:" detects compiler errors from GCC and Clang (including GCC ICEs)
    #   - "^ERROR:" detects linker errors
    #   - ": undefined reference" detects missing symbols during linking
    #   - "] Error " detects GNU make errors
    # Then grep for "grep" to exclude other uses of this search.
    # shellcheck disable=SC2154
    # (defined by init_step in jenkins-helpers)
    cat > $run_step_artifacts/results.regressions <<EOF
# First few build errors in logs:
$(cat $new_artifacts/console.log | sed -e 's/"[^"]*"//g' | grep " error:\|^ERROR:\|: undefined reference\|\] Error " | grep -v "grep" | head | sed -e "s/^/# /")
EOF

    case "$linux_n_obj:$base_linux_n_obj" in
	boot:*) return 0 ;;
	*:boot) return 1 ;;
	all:*) return 0 ;;
	*:all) return 1 ;;
	*)
	    if ! [ "$linux_n_obj" -ge "-10" ]; then
		# Something is very wrong with result (e.g., it's not a number).
		return 1
	    fi
	    if ! [ "$base_linux_n_obj" -ge "-10" ]; then
		# Something is very wrong with result (e.g., it's not a number).
		return 0
	    fi

	    if [ $linux_n_obj -ge $base_linux_n_obj ]; then
		return 0
	    else
		return 1
	    fi
	    ;;
    esac
    )
}

# Implement rr[breakup_updated_components] hook.
tcwg_kernel_breakup_updated_components ()
{
    (
    set -euf -o pipefail

    # Linux changes tend to cause the most regressions.
    # Breakup updated components into linux and the rest of components
    # to reduce the number of builds.
    if print_updated_components "\n" | grep -q "^linux\$"; then
	echo "linux"
	print_updated_components "\n" | grep -v "^linux\$" | tr '\n' ' ' | sed -e "s/ \$//g"
	echo
    else
	print_updated_components "\n"
    fi
    )
}
# shellcheck disable=SC2154
rr[breakup_updated_components]=tcwg_kernel_breakup_updated_components

run_step stop_on_fail -10 reset_artifacts
run_step stop_on_fail x prepare_abe
run_step skip_on_fail -9 build_abe binutils
case "${rr[toolchain]}" in
    gnu)
	run_step skip_on_fail -5 build_abe stage1
	;;
    llvm)
	run_step skip_on_fail -5 build_llvm
	;;
esac
run_step skip_on_fail -2 build_abe qemu
run_step skip_on_fail x build_linux
run_step skip_on_fail x boot_linux
run_step reset_on_fail x check_regression
run_step stop_on_fail x update_baseline
run_step stop_on_fail x push_baseline

trap "" EXIT