-
Notifications
You must be signed in to change notification settings - Fork 71
/
haskell-ci.sh
executable file
·556 lines (445 loc) · 14.8 KB
/
haskell-ci.sh
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#!/bin/bash
# shellcheck disable=SC2086,SC2016,SC2046
# REGENDATA ["--config=cabal.haskell-ci","bash","cabal.project"]
set -o pipefail
# Mode
##############################################################################
if [ "$1" = "indocker" ]; then
INDOCKER=true
shift
else
INDOCKER=false
fi
# Run configuration
##############################################################################
CFG_CABAL_STORE_CACHE=""
CFG_CABAL_REPO_CACHE=""
CFG_JOBS="9.10.1 9.8.3 9.8.2 9.6.6 9.4.8 9.2.8 9.0.2 8.10.7 8.8.4 8.6.5"
CFG_CABAL_UPDATE=false
SCRIPT_NAME=$(basename "$0")
START_TIME="$(date +'%s')"
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
# Job configuration
##############################################################################
GHC_VERSION="non-existing"
CABAL_VERSION=3.2
HEADHACKAGE=false
# Locale
##############################################################################
export LC_ALL=C.UTF-8
# Utilities
##############################################################################
SGR_RED='\033[1;31m'
SGR_GREEN='\033[1;32m'
SGR_BLUE='\033[1;34m'
SGR_CYAN='\033[1;96m'
SGR_RESET='\033[0m' # No Color
put_info() {
printf "$SGR_CYAN%s$SGR_RESET\n" "### $*"
}
put_error() {
printf "$SGR_RED%s$SGR_RESET\n" "!!! $*"
}
run_cmd() {
local PRETTYCMD="$*"
local PROMPT
if $INDOCKER; then
PROMPT="$(pwd) >>>"
else
PROMPT=">>>"
fi
printf "$SGR_BLUE%s %s$SGR_RESET\n" "$PROMPT" "$PRETTYCMD"
local start_time end_time cmd_duration total_duration
start_time=$(date +'%s')
"$@"
local RET=$?
end_time=$(date +'%s')
cmd_duration=$((end_time - start_time))
total_duration=$((end_time - START_TIME))
cmd_min=$((cmd_duration / 60))
cmd_sec=$((cmd_duration % 60))
total_min=$((total_duration / 60))
total_sec=$((total_duration % 60))
if [ $RET -eq 0 ]; then
printf "$SGR_GREEN%s$SGR_RESET (%dm%02ds; %dm%02ds)\n" "<<< $PRETTYCMD" "$cmd_min" "$cmd_sec" "$total_min" "$total_sec"
else
printf "$SGR_RED%s$SGR_RESET\n" "!!! $PRETTYCMD"
exit 1
fi
}
run_cmd_if() {
local COND=$1
shift
if [ $COND -eq 1 ]; then
run_cmd "$@"
else
local PRETTYCMD="$*"
local PROMPT
PROMPT="$(pwd) (skipping) >>>"
printf "$SGR_BLUE%s %s$SGR_RESET\n" "$PROMPT" "$PRETTYCMD"
fi
}
run_cmd_unchecked() {
local PRETTYCMD="$*"
local PROMPT
if $INDOCKER; then
PROMPT="$(pwd) >>>"
else
PROMPT=">>>"
fi
printf "$SGR_BLUE%s %s$SGR_RESET\n" "$PROMPT" "$PRETTYCMD"
local start_time end_time cmd_duration total_duration cmd_min cmd_sec total_min total_sec
start_time=$(date +'%s')
"$@"
end_time=$(date +'%s')
cmd_duration=$((end_time - start_time))
total_duration=$((end_time - START_TIME))
cmd_min=$((cmd_duration / 60))
cmd_sec=$((cmd_duration % 60))
total_min=$((total_duration / 60))
total_sec=$((total_duration % 60))
printf "$SGR_GREEN%s$SGR_RESET (%dm%02ds; %dm%02ds)\n" "<<< $PRETTYCMD" "$cmd_min" "$cmd_sec" "$total_min" "$total_sec"
}
change_dir() {
local DIR=$1
if [ -d "$DIR" ]; then
printf "$SGR_BLUE%s$SGR_RESET\n" "change directory to $DIR"
cd "$DIR" || exit 1
else
printf "$SGR_RED%s$SGR_RESET\n" "!!! cd $DIR"
exit 1
fi
}
change_dir_if() {
local COND=$1
local DIR=$2
if [ $COND -ne 0 ]; then
change_dir "$DIR"
fi
}
echo_to() {
local DEST=$1
local CONTENTS=$2
echo "$CONTENTS" >> "$DEST"
}
echo_if_to() {
local COND=$1
local DEST=$2
local CONTENTS=$3
if [ $COND -ne 0 ]; then
echo_to "$DEST" "$CONTENTS"
fi
}
install_cabalplan() {
put_info "installing cabal-plan"
if [ ! -e $CABAL_REPOCACHE/downloads/cabal-plan ]; then
curl -L https://github.com/haskell-hvr/cabal-plan/releases/download/v0.6.2.0/cabal-plan-0.6.2.0-x86_64-linux.xz > /tmp/cabal-plan.xz || exit 1
(cd /tmp && echo "de73600b1836d3f55e32d80385acc055fd97f60eaa0ab68a755302685f5d81bc cabal-plan.xz" | sha256sum -c -)|| exit 1
mkdir -p $CABAL_REPOCACHE/downloads
xz -d < /tmp/cabal-plan.xz > $CABAL_REPOCACHE/downloads/cabal-plan || exit 1
chmod a+x $CABAL_REPOCACHE/downloads/cabal-plan || exit 1
fi
mkdir -p $CABAL_DIR/bin || exit 1
ln -s $CABAL_REPOCACHE/downloads/cabal-plan $CABAL_DIR/bin/cabal-plan || exit 1
}
# Help
##############################################################################
show_usage() {
cat <<EOF
./haskell-ci.sh - build & test
Usage: ./haskell-ci.sh [options]
A script to run automated checks locally (using Docker)
Available options:
--jobs JOBS Jobs to run (default: $CFG_JOBS)
--cabal-store-cache PATH Directory to use for cabal-store-cache
--cabal-repo-cache PATH Directory to use for cabal-repo-cache
--skip-cabal-update Skip cabal update (useful with --cabal-repo-cache)
--no-skip-cabal-update
--help Print this message
EOF
}
# getopt
#######################################################################
process_cli_options() {
while [ $# -gt 0 ]; do
arg=$1
case $arg in
--help)
show_usage
exit
;;
--jobs)
CFG_JOBS=$2
shift
shift
;;
--cabal-store-cache)
CFG_CABAL_STORE_CACHE=$2
shift
shift
;;
--cabal-repo-cache)
CFG_CABAL_REPO_CACHE=$2
shift
shift
;;
--skip-cabal-update)
CFG_CABAL_UPDATE=false
shift
;;
--no-skip-cabal-update)
CFG_CABAL_UPDATE=true
shift
;;
*)
echo "Unknown option $arg"
exit 1
esac
done
}
process_indocker_options () {
while [ $# -gt 0 ]; do
arg=$1
case $arg in
--ghc-version)
GHC_VERSION=$2
shift
shift
;;
--cabal-version)
CABAL_VERSION=$2
shift
shift
;;
--start-time)
START_TIME=$2
shift
shift
;;
--cabal-update)
CABAL_UPDATE=$2
shift
shift
;;
*)
echo "Unknown option $arg"
exit 1
esac
done
}
if $INDOCKER; then
process_indocker_options "$@"
else
if [ -f "$XDG_CONFIG_HOME/haskell-ci/bash.config" ]; then
process_cli_options $(cat "$XDG_CONFIG_HOME/haskell-ci/bash.config")
fi
process_cli_options "$@"
put_info "jobs: $CFG_JOBS"
put_info "cabal-store-cache: $CFG_CABAL_STORE_CACHE"
put_info "cabal-repo-cache: $CFG_CABAL_REPO_CACHE"
put_info "cabal-update: $CFG_CABAL_UPDATE"
fi
# Constants
##############################################################################
SRCDIR=/hsci/src
BUILDDIR=/hsci/build
CABAL_DIR="$BUILDDIR/cabal"
CABAL_REPOCACHE=/hsci/cabal-repocache
CABAL_STOREDIR=/hsci/store
# Docker invoke
##############################################################################
# if cache directory is specified, use it.
# Otherwise use another tmpfs host
if [ -z "$CFG_CABAL_STORE_CACHE" ]; then
CABALSTOREARG="--tmpfs $CABAL_STOREDIR:exec"
else
CABALSTOREARG="--volume $CFG_CABAL_STORE_CACHE:$CABAL_STOREDIR"
fi
if [ -z "$CFG_CABAL_REPO_CACHE" ]; then
CABALREPOARG="--tmpfs $CABAL_REPOCACHE:exec"
else
CABALREPOARG="--volume $CFG_CABAL_REPO_CACHE:$CABAL_REPOCACHE"
fi
echo_docker_cmd() {
local GHCVER=$1
# TODO: mount /hsci/src:ro (readonly)
echo docker run \
--tty \
--interactive \
--rm \
--label haskell-ci \
--volume "$(pwd):/hsci/src" \
$CABALSTOREARG \
$CABALREPOARG \
--tmpfs /tmp:exec \
--tmpfs /hsci/build:exec \
--workdir /hsci/build \
"phadej/ghc:$GHCVER-bionic" \
"/bin/bash" "/hsci/src/$SCRIPT_NAME" indocker \
--ghc-version "$GHCVER" \
--cabal-update "$CFG_CABAL_UPDATE" \
--start-time "$START_TIME"
}
# if we are not in docker, loop through jobs
if ! $INDOCKER; then
for JOB in $CFG_JOBS; do
put_info "Running in docker: $JOB"
run_cmd $(echo_docker_cmd "$JOB")
done
run_cmd echo "ALL OK"
exit 0
fi
# Otherwise we are in docker, and the rest of script executes
put_info "In docker"
# Environment
##############################################################################
GHCDIR=/opt/ghc/$GHC_VERSION
HC=$GHCDIR/bin/ghc
HCPKG=$GHCDIR/bin/ghc-pkg
HADDOCK=$GHCDIR/bin/haddock
CABAL=/opt/cabal/$CABAL_VERSION/bin/cabal
CABAL="$CABAL -vnormal+nowrap"
export CABAL_DIR
export CABAL_CONFIG="$BUILDDIR/cabal/config"
PATH="$CABAL_DIR/bin:$PATH"
# HCNUMVER
HCNUMVER=$(${HC} --numeric-version|perl -ne '/^(\d+)\.(\d+)\.(\d+)(\.(\d+))?$/; print(10000 * $1 + 100 * $2 + ($3 == 0 ? $5 != 1 : $3))')
GHCJSARITH=0
put_info "HCNUMVER: $HCNUMVER"
# Args for shorter/nicer commands
if [ 1 -ne 0 ] ; then ARG_TESTS=--enable-tests; else ARG_TESTS=--disable-tests; fi
if [ 1 -ne 0 ] ; then ARG_BENCH=--enable-benchmarks; else ARG_BENCH=--disable-benchmarks; fi
ARG_COMPILER="--ghc --with-compiler=$HC"
put_info "tests/benchmarks: $ARG_TESTS $ARG_BENCH"
# Apt dependencies
##############################################################################
# Cabal config
##############################################################################
mkdir -p $BUILDDIR/cabal
cat > $BUILDDIR/cabal/config <<EOF
remote-build-reporting: anonymous
write-ghc-environment-files: always
remote-repo-cache: $CABAL_REPOCACHE
logs-dir: $CABAL_DIR/logs
world-file: $CABAL_DIR/world
extra-prog-path: $CABAL_DIR/bin
symlink-bindir: $CABAL_DIR/bin
installdir: $CABAL_DIR/bin
build-summary: $CABAL_DIR/logs/build.log
store-dir: $CABAL_STOREDIR
install-dirs user
prefix: $CABAL_DIR
repository hackage.haskell.org
url: http://hackage.haskell.org/
EOF
if $HEADHACKAGE; then
put_error "head.hackage is not implemented"
exit 1
fi
run_cmd cat "$BUILDDIR/cabal/config"
# Version
##############################################################################
put_info "Versions"
run_cmd $HC --version
run_cmd_unchecked $HC --print-project-git-commit-id
run_cmd $CABAL --version
# Build script
##############################################################################
# update cabal index
if $CABAL_UPDATE; then
put_info "Updating Hackage index"
run_cmd $CABAL v2-update -v
fi
# install cabal-plan
install_cabalplan
run_cmd cabal-plan --version
# install doctest
put_info "install doctest"
# install doctest
run_cmd_if $((HCNUMVER < 90000)) $CABAL v2-install $ARG_COMPILER --ignore-project -j doctest --constraint='doctest ^>=0.22.0'
run_cmd_if $((HCNUMVER < 90000)) doctest --version
# initial cabal.project for sdist
put_info "initial cabal.project for sdist"
change_dir "$BUILDDIR"
run_cmd touch cabal.project
echo_to cabal.project "packages: $SRCDIR/."
echo_to cabal.project "packages: $SRCDIR/cabal-install-parsers"
run_cmd cat cabal.project
# sdist
put_info "sdist"
run_cmd mkdir -p "$BUILDDIR/sdist"
run_cmd $CABAL sdist all --output-dir "$BUILDDIR/sdist"
# unpack
put_info "unpack"
change_dir "$BUILDDIR"
run_cmd mkdir -p "$BUILDDIR/unpacked"
run_cmd find "$BUILDDIR/sdist" -maxdepth 1 -type f -name '*.tar.gz' -exec tar -C "$BUILDDIR/unpacked" -xzvf {} \;
# generate cabal.project
put_info "generate cabal.project"
PKGDIR_haskell_ci="$(find "$BUILDDIR/unpacked" -maxdepth 1 -type d -regex '.*/haskell-ci-[0-9.]*')"
PKGDIR_cabal_install_parsers="$(find "$BUILDDIR/unpacked" -maxdepth 1 -type d -regex '.*/cabal-install-parsers-[0-9.]*')"
run_cmd touch cabal.project
run_cmd touch cabal.project.local
echo_to cabal.project "packages: ${PKGDIR_haskell_ci}"
echo_to cabal.project "packages: ${PKGDIR_cabal_install_parsers}"
cat >> cabal.project <<EOF
package *
ghc-options: -Werror=missing-methods
EOF
cat >> cabal.project <<EOF
allow-newer: ShellCheck:filepath
package haskell-ci
ghc-options: -Werror
package cabal-install-parsers
ghc-options: -Werror
keep-going: False
package bytestring
tests: False
EOF
$HCPKG list --simple-output --names-only | perl -ne 'for (split /\s+/) { print "constraints: $_ installed\n" unless /^(Cabal|Cabal-syntax|cabal-install-parsers|haskell-ci|parsec)$/; }' >> cabal.project.local
run_cmd cat cabal.project
run_cmd cat cabal.project.local
# dump install plan
put_info "dump install plan"
run_cmd $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH --dry-run all
run_cmd cabal-plan
# install dependencies
put_info "install dependencies"
run_cmd $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --dependencies-only -j all
run_cmd $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH --dependencies-only -j all
# build w/o tests
put_info "build w/o tests"
run_cmd $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks all
# build
put_info "build"
run_cmd $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH all
# tests
put_info "tests"
run_cmd $CABAL v2-test $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --test-show-details=direct
# doctest
put_info "doctest"
run_cmd perl -i -e 'while (<ARGV>) { print unless /package-id\s+(base-compat-batteries|bs-cmpt-bttrs)-\d+(\.\d+)*/; }' .ghc.environment.*
change_dir_if $((HCNUMVER < 90000)) ${PKGDIR_haskell_ci}
run_cmd_if $((HCNUMVER < 90000)) doctest --fast -XHaskell2010 -XBangPatterns -XConstraintKinds -XDataKinds -XDeriveAnyClass -XDeriveFoldable -XDeriveFunctor -XDeriveGeneric -XDeriveTraversable -XDerivingStrategies -XFlexibleContexts -XFlexibleInstances -XFunctionalDependencies -XGADTs -XGeneralizedNewtypeDeriving -XMultiWayIf -XNoImplicitPrelude -XQuantifiedConstraints -XRankNTypes -XScopedTypeVariables -XStandaloneDeriving -XTypeApplications -XTypeOperators -XUndecidableInstances -XUndecidableSuperClasses -XViewPatterns src
change_dir_if $((HCNUMVER < 90000)) ${PKGDIR_cabal_install_parsers}
run_cmd_if $((HCNUMVER < 90000)) doctest --fast -XHaskell2010 src
# cabal check
put_info "cabal check"
change_dir "${PKGDIR_haskell_ci}"
run_cmd ${CABAL} -vnormal check
change_dir "${PKGDIR_cabal_install_parsers}"
run_cmd ${CABAL} -vnormal check
change_dir "$BUILDDIR"
# haddock
put_info "haddock"
run_cmd $CABAL v2-haddock --haddock-all $ARG_COMPILER --with-haddock $HADDOCK $ARG_TESTS $ARG_BENCH all
# unconstrained build
put_info "unconstrained build"
run_cmd rm -f cabal.project.local
run_cmd $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks all
# constraint sets
put_info "constraint sets"
run_cmd rm -f cabal.project.local
put_info "constraint set deepseq-1.4"
# Done
run_cmd echo OK