summaryrefslogtreecommitdiff
path: root/round-robin.sh
blob: abe27dfe96ab5cbb55bdc82808a5ff2d8a4bde43 (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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/bin/bash

# Pretty name for PROJECT's version
#rr[PROJECT_version]

# PROJECT's git url
#rr[PROJECT_url]

# PROJECT's git branch or SHA1 revision parsable by git rev-parse.
# A special value "baseline" means that PROJECT is not being updated
# in this build, and its baseline branch should be used.
# In a successful build "update_baseline" step will update baseline
# branches of all PROJECTs to the current values, thus setting
# a baseline for the next build.
#rr[PROJECT_branch]

# PROJECT's git SHA1 revision.  These are mostly used in manifests.
#rr[PROJECT_rev]

# Baseline branch name for current configuration.  These branches should
# be present in all above git repos (if ${rr[init_configuration]} is false).
#rr[baseline_branch]="${rr[ci_project]}/${rr[ci_config]}"

# Run mode: bisect or non-bisect.  In bisect mode we do a couple things
# slightly differently (e.g., don't touch repo in clone_repo() ).
#rr[mode]="$mode"

# Ignore failures in check_regression, which forces update_baseline().
#rr[reset_baseline]="$reset_baseline"

# Target architecture to build for: arm or aarch64.
#rr[target]="$target"

# Top-level artifacts directory.
#rr[top_artifacts]="$top_artifacts"

# Set rr[init_configuration] to 'true' for new projects/configurations
# to initialize baseline branches in git repos.
#rr[init_configuration]=false

# Print round-robin components that are being updated in this build
# (the ones using non-baseline branches).
print_updated_components ()
{
    (
    set -euf -o pipefail

    local delim=""
    local c
    for c in ${rr[components]}; do
	if [ x"${rr[${c}_branch]}" != x"baseline" ]; then
	    printf "%s%s" "$delim" "$c"
	    delim=${1- }
	fi
    done
    )
}

# Print the single round-robin component being updated in this build.
# Print nothing if multiple components are being updated.
print_single_updated_component ()
{
    (
    set -euf -o pipefail

    local -a updated_components
    updated_components=($(print_updated_components))

    if [ ${#updated_components[@]} = 1 ]; then
	echo "${updated_components[0]}"
    fi
    )
}

# Reset artifacts to an empty state.  ${rr[top_artifacts]}/results is the most
# important artifact, since it records the metric of how successful the build
# is.
reset_artifacts ()
{
    (
    set -euf -o pipefail

    # Clean ${rr[top_artifacts]} but preserve
    # - ${rr[top_artifacts]}/console.log and $run_step_artifacts/console.log, which
    # are being written to by run_step().
    # - ${rr[top_artifacts]}/jenkins/*, which is cleaned by tcwg_kernel.yaml.
    fresh_dir $run_step_top_artifacts \
	      $run_step_top_artifacts/console.log \
	      $run_step_artifacts/console.log \
	      "$run_step_top_artifacts/jenkins/*"

    local single_branch=${rr[baseline_branch]}

    # Clone base-artifacts here so that bisect runs (which skip this step)
    # don't overwrite it.
    # base-artifacts repo is big and changes all the time, so we
    # fetch only the $baseline_branch, instead of all branches.
    if ${rr[init_configuration]-false}; then
	rr[base-artifacts_rev]=empty
	single_branch=empty
    elif [ x"${rr[base-artifacts_rev]+set}" = x"set" ] && ${rr[reset_baseline]}; then
	# Since we re-write history of base-artifacts all the time we can't
	# rely on SHA1 stored in manifests (aka ${rr[base-artifacts_rev]}) to
	# exist.  The important case here is reproduction of regressions from
	# email instructions: baseline run with reset_baseline set.  We could
	# have tagged such SHA1s to keep them stored in the repo, but it is easier
	# to just ignore base-artifacts_rev when reset_baseline is set.
	rr[base-artifacts_rev]=empty
	single_branch=empty
    else
	rr[base-artifacts_rev]="${rr[base-artifacts_rev]-${rr[baseline_branch]}}"
    fi

    clone_or_update_repo base-artifacts ${rr[base-artifacts_rev]} https://git-us.linaro.org/toolchain/ci/base-artifacts.git auto $single_branch
    cat <<EOF | manifest_out
rr[base-artifacts_rev]=$(git -C base-artifacts rev-parse HEAD)
EOF
    )
}

# Clone repository for $project
# $1: Repo / project name
clone_repo ()
{
    (
    set -euf -o pipefail
    local project="$1"

    if [ x"${rr[mode]}" = x"bisect" ]; then
	# Cleanup current checkout in bisect mode.
	git_clean "$project"
	return 0
    fi

    local branch

    # Resolve "baseline" branch specifier.
    if [ x"${rr[${project}_branch]}" = x"baseline" ]; then
	branch="refs/remotes/baseline/${rr[baseline_branch]}"
    else
	branch="${rr[${project}_branch]}"
    fi

    # Allow manifest override
    branch="${rr[${project}_rev]-$branch}"

    # Decide on whether to use read-only or read-write mode for
    # refs/remotes/baseline.  We use read-only wherever possible to allow
    # developers without ssh keys on Linaro git servers to reproduce builds
    # in --mode "baseline".
    local read_only="true"
    if [ x"${rr[${project}_branch]}" != x"baseline" -a \
	  x"${rr[mode]}" = x"jenkins-full" ]; then
	read_only=false
    fi

    # Clone origin remote
    clone_or_update_repo_no_checkout $project ${rr[${project}_url]} auto "" > /dev/null
    # Add baseline remote
    git_init_linaro_local_remote $project baseline $read_only
    # Checkout, now that we have both origin and baseline remotes ready.
    clone_or_update_repo $project $branch ${rr[${project}_url]} > /dev/null

    local cur_rev
    cur_rev=$(git -C $project rev-parse HEAD)

    cat <<EOF | manifest_out
rr[${project}_rev]=$cur_rev
EOF
    )
}

# Prepare ABE tree for [partial] GNU builds
prepare_abe ()
{
    (
    set -euf -o pipefail

    clone_or_update_repo abe tested https://git-us.linaro.org/toolchain/abe.git > /dev/null

    cd abe

    # Add ccache wrappers.
    rm -rf $(pwd)/bin
    mkdir $(pwd)/bin

    cat > $(pwd)/bin/gcc <<EOF
#!/bin/sh
exec ccache /usr/bin/gcc "\$@"
EOF
    chmod +x $(pwd)/bin/gcc

    cat > $(pwd)/bin/g++ <<EOF
#!/bin/sh
exec ccache /usr/bin/g++ "\$@"
EOF
    chmod +x $(pwd)/bin/g++

    export PATH=$(pwd)/bin:$PATH

    ./configure --with-git-reference-dir=/home/tcwg-buildslave/snapshots-ref
    )
}

# Build ABE component
# $1: Component -- ABE component to build.
build_abe ()
{
    (
    set -euf -o pipefail

    local component="$1"

    local project stage action
    action="build"

    case "$component" in
	stage1)
	    project=gcc
	    stage="--stage 1"
	    ;;
	stage2)
	    project=gcc
	    stage="--stage 2"
	    ;;
	bootstrap)
	    project=gcc
	    stage="--enable bootstrap"
	    ;;
	bootstrap_ubsan)
	    project=gcc
	    stage="--set buildconfig=bootstrap-ubsan"
	    ;;
	bootstrap_O3)
	    project=gcc
	    stage="--set buildconfig=bootstrap-O3"
	    ;;
	bootstrap_O1)
	    project=gcc
	    stage="--set buildconfig=bootstrap-O1"
	    ;;
	bootstrap_lto)
	    project=gcc
	    stage="--set buildconfig=bootstrap-lto"
	    ;;
	bootstrap_debug)
	    project=gcc
	    stage="--set buildconfig=bootstrap-debug"
	    ;;
	bootstrap_profiled)
	    project=gcc
	    stage="--set buildconfig=bootstrap --set makeflags=profiledbootstrap"
	    ;;
	bootstrap_profiled_lto)
	    project=gcc
	    stage="--set buildconfig=bootstrap-lto --set makeflags=profiledbootstrap"
	    ;;
	*)
	    project=$component
	    stage=""
	    ;;
    esac

    # Use our custom sources for everything, but kernel headers.
    local custom_abe_src_opt=""
    local git_dir="$project"
    if [ x"$component" != x"linux" ]; then
	clone_repo $project

	# Don't use ABE's repo clone functions and setup abe/snapshots/ directory
	# to have the right entries.
	local git_dir git_path
	git_dir="$(basename ${rr[${project}_url]})"
	git_path="abe/snapshots/$git_dir"
	rm -rf $git_path $git_path~master
	ln -s $(pwd)/$project $git_path
	ln -s $(pwd)/$project $git_path~master

	custom_abe_src_opt="$project=${rr[${project}_url]}~master --disable update"
    fi

    cd abe

    local gnu_target
    gnu_target=$(print_gnu_target ${rr[target]})

    # Remove previous build directories and .stamp files.
    # We rely on ccache for fast rebuilds.
    set +f; rm -rf builds/*/*/$git_dir*; set -f

    export PATH=$(pwd)/bin:$PATH

    if [ x"$component" != x"stage2" ]; then
	# TODO: Fix install_sysroot logic in ABE.
	# ABE tries to install sysroot even for partial builds, e.g.,
	# with "--build binutils".  Workaround by patching ABE.
	sed -i -e "s/do_install_sysroot/:/" lib/control.sh
    else
	git checkout -- lib/control.sh
    fi

    ccache -z
    local target_opt=""
    if [ x"${rr[target]}" != x"native" ]; then
        target_opt="--target $gnu_target"
    fi

    # Run "./abe.sh --build $project".
    ./abe.sh \
	--$action $project \
	$target_opt \
	--extraconfigdir config/master \
	$custom_abe_src_opt \
	$stage
    ccache -s
    )
}

# Build LLVM
build_llvm ()
{
    (
    set -euf -o pipefail

    local use_abe=${1-false}

    clone_repo llvm

    # Copy only components from the monorepo that are required for kernel build
    rsync -a --del --exclude /tools/clang --exclude /tools/lld llvm/llvm/ llvm-src/
    rsync -a --del llvm/clang/ llvm-src/tools/clang/
    rsync -a --del llvm/lld/ llvm-src/tools/lld/

    # Setup ccache and ninja wrappers.
    rm -rf $(pwd)/bin
    mkdir $(pwd)/bin

    cat > $(pwd)/bin/cc <<EOF
#!/bin/sh
exec ccache /usr/bin/gcc "\$@"
EOF
    chmod +x $(pwd)/bin/cc

    cat > $(pwd)/bin/c++ <<EOF
#!/bin/sh
exec ccache /usr/bin/g++ "\$@"
EOF
    chmod +x $(pwd)/bin/c++

    if [ -f /usr/local/bin/ninja.bin ]; then
	# Use ninja configuration from llvm buildbots to avoid running out of RAM.
	cat > $(pwd)/bin/ninja <<EOF
#!/bin/sh
exec /usr/local/bin/ninja.bin -m 30 -M 50 -D 5000 "\$@"
EOF
	chmod +x $(pwd)/bin/ninja
    fi

    export PATH=$(pwd)/bin:$PATH

    # Freshen up build and install directories.  We rely on ccache for fast rebuilds.
    if $use_abe; then
	rsync -a --del abe/builds/destdir/x86_64-unknown-linux-gnu/ llvm-install/
    else
	rm -rf llvm-install
    fi
    rm -rf llvm-build
    mkdir -p llvm-build
    cd llvm-build

    cmake -G Ninja  ../llvm-src -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=True -DCMAKE_INSTALL_PREFIX=../llvm-install -DLLVM_TARGETS_TO_BUILD=$(print_llvm_target ${rr[target]}) -DLLVM_BINUTILS_INCDIR=/usr/include
    ccache -z
    ninja
    ninja install
    ccache -s
    )
}

# Check if current build regressed compared to the baseline
# (unless ${rr[reset_baseline]} is set).
check_regression ()
{
    (
    set -euf -o pipefail

    if ! ${rr[reset_baseline]} && ! no_regression_p; then
	local single_component=$(print_single_updated_component)

	if [ x"$single_component" = x"" -o x"${rr[mode]}" = x"bisect" ]; then
	    local c
	    for c in $(print_updated_components); do
		local c2
		for c2 in ${rr[components]}; do
		    if [ x"$c" = x"$c2" ]; then
			echo "${c2}_branch=${rr[${c}_branch]}"
		    else
			echo "${c2}_branch=baseline"
		    fi
		done >> ${rr[top_artifacts]}/trigger-build-$c
	    done
	else
	    local baseline_rev cur_rev
	    baseline_rev=$(git_rev_parse_long $single_component ${rr[baseline_branch]} baseline)
	    cur_rev=$(git -C $single_component rev-parse HEAD)

	    cat > ${rr[top_artifacts]}/trigger-bisect <<EOF
current_project=$single_component
baseline_branch=$baseline_rev
bad_branch=$cur_rev
EOF
	fi

	# Fail.
	false
    fi
    )
}

# Commit current result and artifacts to the baseline repository
update_baseline ()
{
    (
    set -euf -o pipefail

    local amend=""
    local prev_head=""

    # We discard baseline entries for results worse or same than
    # the current one, but keep entries for results that are better
    # (so that we have a record of pending regressions).
    while no_regression_p; do
	prev_head=""
	if git -C base-artifacts rev-parse HEAD^ >/dev/null 2>&1; then
	    # For every regression we want to keep artifacts for the first-bad
	    # build, so reset to the most relevant regression (marked by reset-baseline).
	    if [ -f base-artifacts/reset-baseline ] && ! ${rr[reset_baseline]}; then
		prev_head=$(git -C base-artifacts rev-parse HEAD)
	    fi
	    git -C base-artifacts reset --hard HEAD^
	else
	    # We got to the beginning of git history, so amend the current
	    # commit.  The initial state of baseline is "empty" branch,
	    # which we treat as worst possible in no_regression_p().
	    amend="--amend"
	    break
	fi
    done

    if [ x"$prev_head" != x"" ]; then
	git -C base-artifacts reset --hard $prev_head
    fi

    # Rsync current artifacts.  Make sure to use -I rsync option since
    # quite often size and timestamp on artifacts/results will be the same
    # as on base-artifacts/results due to "git reset --hard HEAD^" below.
    # This caused rsync's "quick check" heuristic to skip "results" file.
    # !!! From this point on, logs and other artifacts won't be included
    # in base-artifacts.git repo (though they will be uploaded to jenkins).
    rsync -aI --del --exclude /.git ${rr[top_artifacts]}/ base-artifacts/

    local rev_count
    if [ x"$amend" = x"" ]; then
	rev_count=$(git -C base-artifacts rev-list --count HEAD)
    else
	rev_count="0"
    fi

    local msg_title="$rev_count"

    if ${rr[reset_baseline]}; then
	# Create a marker for builds that reset baselines (these are builds
	# for bisected regressions).
	touch base-artifacts/reset-baseline
	msg_title="$msg_title: first-bad"
    else
	msg_title="$msg_title: good"
    fi

    local single_component
    single_component=$(print_single_updated_component)
    if [ x"$single_component" != x"" ]; then
	local single_rev
	single_rev=$(git -C $single_component rev-parse HEAD)
	msg_title="$msg_title: $single_component-$single_rev"
    else
	msg_title="$msg_title: $(print_updated_components "-")"
    fi
    msg_title="$msg_title: $(tail -n1 ${rr[top_artifacts]}/results)"

    git -C base-artifacts add .
    git -C base-artifacts commit $amend -m "$msg_title

$(cat ${rr[top_artifacts]}/results)"
    )
}

# Push to baseline branches and to base-artifacts repo.
push_baseline ()
{
    (
    set -euf -o pipefail

    git_init_linaro_local_remote base-artifacts baseline false
    git_push base-artifacts baseline ${rr[baseline_branch]}

    local c
    for c in $(print_updated_components); do
	git_push $c baseline ${rr[baseline_branch]}
    done
    )
}