forked from informatikr/aeson-pretty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packcheck.sh
1323 lines (1159 loc) · 36.4 KB
/
packcheck.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#------------------------------------------------------------------------------
# Skip to the end for main flow of script
#------------------------------------------------------------------------------
# See the Readme.md file for details on how it works and for the user guide.
#------------------------------------------------------------------------------
# TODO
#------------------------------------------------------------------------------
# FORCE_TOOL_INSTALL: an option to not use cached installs, force fresh
# installs of tools What if we are using a tool from system path -
# invert path?
# NO_TOOL_INSTALL: do not download/install any tools
# cabal build can use stack installed ghc if GHCVER is not specified
#------------------------------------------------------------------------------
# Utility functions
#------------------------------------------------------------------------------
# $1: varname
show_var() {
echo "$1=$(eval \"echo \$$1\")"
}
# $1: varname
show_nonempty_var() {
local var=$(eval "echo \$$1")
if test -n "$var"
then
printf "%q\n" "$1=$var"
fi
}
# $1: var name
# $2: default value
init_default() {
local var=$(eval "echo \$$1")
test -n "$var" || eval "export $1=\"$2\""
}
require_file () {
if test ! -f "$1"
then
echo "Required file [$1] does not exist."
exit 1
fi
}
# $1: message
die () {
>&2 echo -e "Error: $1"
exit 1
}
retry_cmd() {
cmd=$*
$cmd || (sleep 2 && $cmd) || (sleep 10 && $cmd)
}
# MINGW 'which' does not seem to work when there are spaces in the
# PATH. Note that "type" returns a cached path, so if something got
# deleted we might still be returning a stale value (we can use hash -r
# to clear the cache if needed).
which_cmd() {
hash -r && type -P "$1" || true
}
require_cmd () {
if test -z "$(which_cmd $1)"
then
echo "Required command [$1] not found in PATH [$PATH]."
exit 1
else
echo "Using [$1] at [$(which_cmd $1)]"
fi
}
# $1: command
function run_verbose() {
echo "$*"
bash -c "$*"
}
function run_verbose_errexit() {
run_verbose "$*" || die "Command [$*] failed. Exiting."
}
# $1: msg
show_step1() {
echo
echo "--------------------------------------------------"
echo "$1"
echo "--------------------------------------------------"
}
# $1: msg
show_step() {
local reltime
local disptime
reltime=$(get_rel_time)
if test -n "$reltime"
then
disptime="[$reltime sec]"
fi
show_step1 "$disptime $1"
}
# $1: file to ungztar
win_ungztar() {
local output=$(basename ${1%.gz})
run_verbose_errexit 7z e -y $1 && run_verbose_errexit 7z x -y $output
run_verbose_errexit rm -f $output
}
set_os_specific_vars() {
local os=$(uname)
case "$os" in
Darwin|Linux)
OS_HAS_TOOLS=tar
OS_UNGZTAR_CMD="run_verbose_errexit tar xzvf"
OS_LOCAL_DIR=.local
OS_CABAL_DIR=.cabal
OS_APP_HOME=$HOME ;;
MINGW*)
require_cmd cygpath
require_envvar APPDATA
OS_HAS_TOOLS=cygpath
OS_UNGZTAR_CMD=win_ungztar
OS_LOCAL_DIR=local
OS_CABAL_DIR=cabal
OS_APP_HOME=`cygpath $APPDATA` ;;
*) die "Unknown OS [$os]" ;;
esac
}
show_machine_info() {
local os=$(uname)
case "$os" in
Linux)
echo "OS: Linux"
lscpu | grep "^Archi\|^CPU\|^Bogo\|^Hyper\|^Virtualiz"
echo "Memory:"
run_verbose free -h
show_step "Container/cgroup information"
# See https://stackoverflow.com/questions/20010199/determining-if-a-process-runs-inside-lxc-docker
# For comprehensive detection see container-detect.conf in ubuntu
#if test -f /.dockerenv
#then
# echo "Running inside Docker (found /.dockerenv)";
#fi
#run_verbose head -n 1 /proc/1/cgroup
sudo cat /proc/1/environ | tr '\0' '\n' | grep "^container=" || true
run_verbose cat /sys/fs/cgroup/cpu/cpu.cfs_period_us || true
run_verbose cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us || true
run_verbose cat /sys/fs/cgroup/memory/memory.limit_in_bytes || true ;;
Darwin)
echo "OS: MacOS" ;;
MINGW*)
echo "OS: Windows (MINGW)" ;;
*) die "OS: Unknown OS [$os]" ;;
esac
}
#------------------------------------------------------------------------------
# Build config show and determine
#------------------------------------------------------------------------------
SAFE_ENVVARS="\
RESOLVER \
GHCVER \
CABALVER \
GHC_OPTIONS \
SDIST_OPTIONS \
DISABLE_SDIST_BUILD \
DISABLE_BENCH \
DISABLE_TEST \
DISABLE_DOCS \
PATH \
TOOLS_DIR \
STACKVER \
STACK_YAML \
STACK_OPTIONS \
STACK_BUILD_OPTIONS \
CABAL_CHECK_RELAX \
CABAL_USE_STACK_SDIST \
CABAL_CONFIGURE_OPTIONS \
CABAL_NEWBUILD_OPTIONS \
COVERAGE \
COVERALLS_OPTIONS \
HLINT_COMMANDS \
CHECK_ENV \
LANG \
LC_ALL \
BASE_TIME \
"
UNSAFE_ENVVARS="\
ENABLE_INSTALL \
STACK_UPGRADE \
CABAL_REINIT_CONFIG \
CABAL_NO_SANDBOX \
CABAL_HACKAGE_MIRROR \
"
ENVVARS="$SAFE_ENVVARS $UNSAFE_ENVVARS"
# $1: varname
# $2: list of vars to find in
find_var() {
for v in $2
do
test $v != "$1" || return 0
done
return 1
}
error_novar() {
find_var "$1" "$ENVVARS" || die "Unknown parameter or environment variable [$1]"
}
error_clean_env() {
echo "Error: Unknown parameter or environment variable [$1]."
die "Please check spelling mistakes and use a clean environment (e.g. env -i) with CHECK_ENV."
}
ALLOW_ENVVARS="STACK_ROOT APPDATA PWD SHLVL _"
check_clean_env() {
local vars=$(env | cut -f1 -d=)
for i in $vars
do
find_var $i "$ENVVARS $ALLOW_ENVVARS" || error_clean_env "$i"
done
}
# $1: varname
require_envvar() {
local var=$(eval "echo \$$1")
test -n "$var" || die "Parameter or environment variable [$1] must be set. Try --help for usage."
}
# $1: envvar
check_boolean_var() {
error_novar "$1"
local var=$(eval "echo \$$1")
if test -n "$var" -a "$var" != y
then
die "Boolean parameter or environment variable [$1] can only be empty or 'y'"
fi
}
# $1: cmdname
# $2: help text
help_cmd() {
printf "%-24s: %s\n" "$1" "$2"
}
# $1: varname
# $2: help text
help_envvar() {
error_novar $1
printf "%-24s: %s\n" "$1" "$2"
}
short_help() {
echo "$0 COMMAND [PARAMETER=VALUE ...]"
echo
echo "For example:"
echo "$0 stack RESOLVER=lts GHC_OPTIONS=\"-O0 -Werror\""
echo "$0 cabal-new GHC_OPTIONS=\"-O0 -Werror\""
echo
echo "Control parameters can either be passed on command line or exported"
echo "as environment variables. Parameters marked DESTRUCTIVE may modify"
echo "your global user config or state."
}
show_help() {
show_step1 "Usage"
short_help
show_step1 "Commands"
help_cmd stack "build using stack"
help_cmd cabal "build using cabal"
help_cmd cabal-new "build using cabal new-build"
help_cmd clean "remove the .packcheck directory"
help_cmd cleanall "remove .packcheck, .stack-work, .cabal-sandbox directories"
help_cmd help "show this help message"
show_step1 "Selecting tool versions"
help_envvar GHCVER "[a.b.c] GHC version prefix (may not be enforced when using stack)"
help_envvar CABALVER "[a.b.c.d] Cabal version (prefix) to use"
help_envvar RESOLVER "Stack resolver to use for stack builds or cabal builds using stack"
help_envvar STACKVER "[a.b.c.d] Stack version (prefix) to use"
help_envvar STACK_UPGRADE "[y] DESTRUCTIVE! Upgrades stack to latest version"
show_step1 "Where to find the required tools"
help_envvar PATH "[path] Set PATH explicitly for predictable builds"
help_envvar TOOLS_DIR "[dir] Find ghc|cabal by version as in TOOLS_DIR/ghc/8.4.1/bin"
show_step1 "Specifying common tool options"
help_envvar GHC_OPTIONS "Specify GHC options to use"
help_envvar SDIST_OPTIONS "Arguments to stack/cabal sdist command"
# XXX this applies to both stack and cabal builds
help_envvar CABAL_REINIT_CONFIG "[y] DESTRUCTIVE! Remove old config to avoid incompatibility issues"
show_step1 "Specifying what to build"
help_envvar DISABLE_BENCH "[y] Do not build benchmarks, default is to build but not run"
help_envvar DISABLE_TEST "[y] Do not run tests, default is to run tests"
help_envvar DISABLE_DOCS "[y] Do not build haddocks, default is to build docs"
help_envvar DISABLE_SDIST_BUILD "[y] Do not build from source distribution"
help_envvar ENABLE_INSTALL "[y] DESTRUCTIVE! Install the package after building"
show_step1 "stack options"
help_envvar STACK_YAML "Alternative stack config, cannot be a path, just the file name"
help_envvar STACK_OPTIONS "ADDITIONAL stack global options (e.g. -v) to append"
help_envvar STACK_BUILD_OPTIONS "ADDITIONAL stack build command options to append"
show_step1 "cabal options"
#help_envvar CABAL_USE_STACK_SDIST "[y] Use stack sdist (to use --pvp-bounds)"
help_envvar CABAL_NEWBUILD_OPTIONS "ADDITIONAL cabal new-build options to append"
help_envvar CABAL_CONFIGURE_OPTIONS "ADDITIONAL cabal old style configure options to append"
help_envvar CABAL_CHECK_RELAX "[y] Do not fail if cabal check fails on the package."
# The sandbox mode is a bit expensive because a sandbox is used and
# dependencies have to be installed twice in two separate sandboxes, once to
# create an sdist and once to build the sdist. For a CI NO sandbox mode
# makes more sense as long as multiple builds running simultaneously will not
# try to install conflicting packages.
help_envvar CABAL_NO_SANDBOX "[y] DESTRUCTIVE! Clobber (force install) global cabal ghc package db"
help_envvar CABAL_HACKAGE_MIRROR "[y] DESTRUCTIVE! Specify an alternative mirror, modifies the cabal config file."
show_step1 "Coverage options"
help_envvar COVERALLS_OPTIONS "hpc-coveralls args and options, usually just test suite names"
help_envvar COVERAGE "[y] Just generate coverage information"
show_step1 "hlint options"
help_envvar HLINT_COMMANDS "hlint commands e.g.'hlint lint src; hlint lint test'"
show_step1 "Diagnostics options"
# To catch spelling mistakes in envvar names passed, otherwise they will be
# silently ignored and we will be wondering why the script is not working.
help_envvar CHECK_ENV "[y] Treat unknown env variables as error, used with env -i"
help_envvar BASE_TIME "System time to be used as base for timeline reporting"
}
check_all_boolean_vars () {
check_boolean_var STACK_UPGRADE
check_boolean_var DISABLE_SDIST_BUILD
check_boolean_var CABAL_USE_STACK_SDIST
check_boolean_var CABAL_REINIT_CONFIG
check_boolean_var CABAL_CHECK_RELAX
check_boolean_var CABAL_NO_SANDBOX
if test -n "$TEST_INSTALL"
then
echo "WARNING! TEST_INSTALL is deprecated. Please use ENABLE_INSTALL instead"
ENABLE_INSTALL="$TEST_INSTALL"
unset TEST_INSTALL
fi
check_boolean_var ENABLE_INSTALL
check_boolean_var DISABLE_BENCH
check_boolean_var DISABLE_TEST
check_boolean_var DISABLE_DOCS
check_boolean_var COVERAGE
}
show_build_command() {
check_all_boolean_vars
echo "You can use the following command to reproduce this build:"
echo
echo -n "$0 $BUILD "
for i in $SAFE_ENVVARS
do
local val="$(show_nonempty_var $i)"
test -z "$val" || echo -n "$val "
done
echo
local unsafe
for i in $UNSAFE_ENVVARS
do
if test -n "$(show_nonempty_var $i)"
then
unsafe=y
fi
done
if test -n "$unsafe"
then
echo
echo "The above command has omitted the following unsafe options."
echo "If you know what you are doing, you can also add these to the"
echo "above command to reproduce this build more faithfully."
echo
echo "Unsafe options may modify your config and "
echo "are usually meant to be used in a CI setup:"
for i in $UNSAFE_ENVVARS
do
show_nonempty_var $i
done
echo
fi
}
show_build_config() {
check_all_boolean_vars
for i in $ENVVARS
do
show_nonempty_var $i
done
}
show_build_env() {
show_nonempty_var HOME
show_nonempty_var APPDATA
show_nonempty_var STACK_ROOT
}
need_stack() {
if test "$BUILD" = stack -o -n "$RESOLVER" -o -n "$CABAL_USE_STACK_SDIST"
then
echo true
fi
}
# $1: be verbose about why we need cabal
dont_need_cabal() {
if test "$BUILD" = cabal -o "$BUILD" = "cabal-new"
then
test -z "$1" || echo "Need cabal-install because 'BUILD=$BUILD'"
return 1
fi
if test -z "$DISABLE_SDIST_BUILD"
then
test -z "$1" || echo "Need cabal-install because 'DISABLE_SDIST_BUILD=$DISABLE_SDIST_BUILD'"
return 1
fi
if test -n "$ENABLE_INSTALL"
then
test -z "$1" || echo "Need cabal-install because 'ENABLE_INSTALL=$ENABLE_INSTALL'"
return 1
fi
return 0
}
# $1: varname
cabal_only_var() {
error_novar $1
local var=$(eval "echo \$$1")
test -z "$var" || die "[$1] is meaningful only for cabal build"
}
# $1: varname
stack_only_var() {
error_novar $1
local var=$(eval "echo \$$1")
test -z "$var" || die "[$1] is meaningful only when stack is used"
}
verify_build_config() {
test -z "$COVERALLS_OPTIONS" || COVERAGE=y
if test "$BUILD" = stack
then
STACK_DEP_OPTIONS="--only-dependencies"
test -n "$DISABLE_TEST" || STACK_DEP_OPTIONS="$STACK_DEP_OPTIONS --test"
test -n "$DISABLE_BENCH" || STACK_DEP_OPTIONS="$STACK_DEP_OPTIONS --bench"
STACK_BUILD_OPTIONS_ORIG=$STACK_BUILD_OPTIONS
STACK_BUILD_OPTIONS=$(cat << EOF
$(test -n "$DISABLE_DOCS" || echo "--haddock --no-haddock-deps")
$(test -n "$DISABLE_TEST" || echo "--test")
$(test -n "$DISABLE_BENCH" || echo "--bench --no-run-benchmarks")
$(test -z "${COVERAGE}" || echo --coverage)
$(test -z "${GHC_OPTIONS}" || echo --ghc-options=\"$GHC_OPTIONS\")
$STACK_BUILD_OPTIONS
EOF
)
elif test "$BUILD" = cabal
then
CABAL_DEP_OPTIONS="--only-dependencies --reorder-goals --max-backjumps=-1"
test -n "$DISABLE_TEST" || \
CABAL_DEP_OPTIONS="$CABAL_DEP_OPTIONS --enable-tests"
test -n "$DISABLE_BENCH" || \
CABAL_DEP_OPTIONS="$CABAL_DEP_OPTIONS --enable-benchmarks"
CABAL_CONFIGURE_OPTIONS=$(cat << EOF
$(test -n "$DISABLE_TEST" || echo "--enable-tests")
$(test -n "$DISABLE_BENCH" || echo "--enable-benchmarks")
$(test -z "$COVERAGE" || echo --enable-coverage)
$(test -z "$GHC_OPTIONS" || echo --ghc-options=\"$GHC_OPTIONS\")
$CABAL_CONFIGURE_OPTIONS
EOF
)
else
CABAL_DEP_OPTIONS="--only-dependencies"
test -n "$DISABLE_TEST" || \
CABAL_DEP_OPTIONS="$CABAL_DEP_OPTIONS --enable-tests"
test -n "$DISABLE_BENCH" || \
CABAL_DEP_OPTIONS="$CABAL_DEP_OPTIONS --enable-benchmarks"
CABAL_NEWBUILD_OPTIONS=$(cat << EOF
$(test -n "$DISABLE_TEST" || echo "--enable-tests")
$(test -n "$DISABLE_BENCH" || echo "--enable-benchmarks")
$(test -z "${COVERAGE}" || echo --enable-coverage)
$(test -z "${GHC_OPTIONS}" || echo --ghc-options=\"$GHC_OPTIONS\")
$CABAL_NEWBUILD_OPTIONS
EOF
)
fi
# These variables are now combined with other options so clear them
# so that we do not show them in the effective config
COVERAGE=
GHC_OPTIONS=
test "$BUILD" = stack -o "$BUILD" = cabal -o "$BUILD" = "cabal-new" || \
die "build [$BUILD] can only be 'stack','cabal' or 'cabal-new'"
if test -n "$CHECK_ENV"
then
if test "$BUILD" != cabal -a "$BUILD" != "cabal-new"
then
cabal_only_var CABALVER
cabal_only_var CABAL_USE_STACK_SDIST
cabal_only_var CABAL_CHECK_RELAX
cabal_only_var CABAL_CONFIGURE_OPTIONS
cabal_only_var CABAL_NEWBUILD_OPTIONS
cabal_only_var CABAL_NO_SANDBOX
cabal_only_var CABAL_HACKAGE_MIRROR
fi
if test -z "$(need_stack)"
then
stack_only_var STACK_YAML
stack_only_var STACK_UPGRADE
stack_only_var STACK_OPTIONS
stack_only_var STACK_BUILD_OPTIONS
fi
fi
}
#------------------------------------------------------------------------------
# Stack fetch and install etc.
#------------------------------------------------------------------------------
ensure_msys_tools() {
if [[ `uname` = MINGW* ]]
then
# retry??
for i in "$1"
do
if test -z "$(which_cmd $i)"
then
stack exec pacman -- -S --noconfirm $i
fi
done
fi
}
fetch_stack_osx() {
curl -sSkL https://www.stackage.org/stack/osx-x86_64 \
| tar xz --strip-components=1 -C $1 --include '*/stack'
}
fetch_stack_linux() {
curl -sSkL https://www.stackage.org/stack/linux-x86_64 \
| tar xz --strip-components=1 -C $1 --wildcards '*/stack'
}
fetch_stack_windows() {
curl -sSkL http://www.stackage.org/stack/windows-i386 \
| 7z x -si stack.exe
}
# $1: directory to place stack executable in
fetch_stack() {
mkdir -p $1
local os=$(uname)
case "$os" in
Darwin) retry_cmd fetch_stack_osx $1 ;;
Linux) retry_cmd fetch_stack_linux $1 ;;
MINGW*) retry_cmd fetch_stack_windows $1 ;;
*) die "Unknown OS [$os]" ;;
esac
}
# $1: directory to place stack executable in
ensure_stack() {
if test -z "$(which_cmd stack)"
then
echo "Downloading stack to [$1]..."
fetch_stack $1
fi
require_cmd stack
if test -n "$STACK_UPGRADE"
then
echo "Upgrading stack to the required version"
if test -n "$STACKVER"
then
local curver=$(stack --numeric-version)
if test "${curver#$STACKVER}" = ${curver}
then
run_verbose stack --no-terminal upgrade --binary-only --binary-version $STACKVER
fi
else
run_verbose stack --no-terminal upgrade --binary-only || fetch_stack $1
fi
fi
test -z "$STACKVER" || check_version_die stack $STACKVER
# Set the real version of stack
STACKVER=$(stack --numeric-version) || exit 1
STACKCMD="stack --no-terminal $STACK_OPTIONS"
STACKCMD_TOOL_INSTALL="$STACKCMD"
$STACKCMD --version
if test -n "$RESOLVER"
then
STACKCMD="$STACKCMD --resolver $RESOLVER"
fi
}
use_stack_paths() {
# Need the bin path (not just compiler-path) on mingw to find gcc
# some packages may have a configure script looking for gcc, so we need to
# use bin path so that on windows we will find the stack installed mingw gcc
run_verbose_errexit $STACKCMD path --bin-path
local BINPATH=`$STACKCMD path --bin-path`
if [[ `uname` = MINGW* ]]
then
# Need for 7z on windows
local GHCPATHS=`$STACKCMD path --programs`
# Convert the path to MINGW format from windows native format
BINPATH=$(cygpath -u -p $BINPATH)
GHCPATHS=$(cygpath -u -p $GHCPATHS)
if test -n "$GHCPATHS"
then
export PATH=$GHCPATHS:$PATH
fi
fi
if test -n "$BINPATH"
then
export PATH=$BINPATH:$PATH
fi
}
#------------------------------------------------------------------------------
# Ensure ghc, cabal are available and the right versions when requested
#------------------------------------------------------------------------------
# $1: tool name (used only for ghc, cabal and stack)
# $2: expected version
check_version() {
local real_ver=$($1 --numeric-version)
# Match that the expected version is a prefix of real
# Do not check when the expected version is head
if test "${real_ver#$2}" != ${real_ver} -o $2 = head
then
return 0
else
echo "Found $1 version [$real_ver] expecting [$2]"
return 1
fi
}
# $1: tool name (used only for ghc, cabal and stack)
# $2: expected version
check_version_die() {
check_version $1 $2 || \
die "Wrong $1 version [$($1 --numeric-version)] expected [$2]"
}
# Remove a path from the PATH envvar
# $1: path component to remove
function path_remove {
# Delete path by parts so we can never accidentally remove sub paths
PATH=${PATH//":$1:"/":"} # delete any instances in the middle
PATH=${PATH/#"$1:"/} # delete any instance at the beginning
PATH=${PATH/%":$1"/} # delete any instance at the end
}
# Find ghc/cabal having the given version prefix in PATH or in a version
# suffixed directory at TOOLS_DIR, and check if it matches the requested
# version. If we found the requested binary we make sure that it is in the
# PATH.
#
# $1: binary name (e.g. ghc or cabal)
# $2: binary version prefix (e.g. 8 or 8.0 or 8.0.1)
find_binary () {
local binary
local path=$PATH
binary=$(which_cmd $1)
while test -n "$binary"
do
if test -z "$2" || check_version $binary $2
then
return 0
else
# remove it from the path and search again
echo "Mismatching $1 version found at [$(dirname $binary)], removing it from PATH and trying again"
path_remove $(dirname $binary)
binary="$(which_cmd $1)"
fi
done
test -n "$TOOLS_DIR" || return 0
# Find if we have a binary in TOOLS_DIR
local dir
if test -n "$2"
then
dir=$(echo ${TOOLS_DIR}/$1/$2*/ | tr ' ' '\n' | sort | tail -1)
else
dir=$(echo ${TOOLS_DIR}/$1/[0-9]*/ | tr ' ' '\n' | sort | tail -1)
test -x "${dir}/bin/$1" || dir="${TOOLS_DIR}/$1/head"
fi
if test -x "${dir}/bin/$1"
then
if test -z "$2" || check_version "${dir}/bin/$1" $2
then
if [[ $dir != /* ]]
then
dir=`pwd`/$dir
fi
PATH=$dir/bin:$PATH
export PATH
return 0
fi
fi
# If we did not find the binary, restore the PATH for better error reporting
PATH=$path
# This command does never fails even if we could not find the binary.
return 0
}
ensure_ghc() {
local found
local compiler
# If there is a ghc in PATH then use that otherwise install it using stack
find_binary ghc "$GHCVER"
found="$(which_cmd ghc)"
if test -n "$(need_stack)" -a -z "$found"
then
# Use stack supplied ghc
echo "$STACKCMD setup"
retry_cmd $STACKCMD setup || die "stack setup falied"
use_stack_paths
echo
fi
compiler="$(which_cmd ghc)"
test -n "$compiler" || die "ghc $GHCVER not found in PATH [$PATH]"
echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]"
if test -n "$GHCVER"
then
check_version_die ghc $GHCVER
# If the user specified GHCVER then use it as system-ghc
# Stack will still silently choose its own ghc if the ghc does not match
# the snapshot.
STACKCMD="$STACKCMD --system-ghc"
fi
# Use the real version, the user might have specified a version prefix in
# GHCVER
GHCVER=$(ghc --numeric-version) || exit 1
}
# XXX/TODO this may not work for cabal 1.24 config
# $1: mirror URL
cabal_use_mirror() {
local CABAL_CONFIG=${OS_APP_HOME}/${OS_CABAL_DIR}/config
if test -f $CABAL_CONFIG
then
local inplace
if [ `uname` = "Darwin" ]
then
inplace="-i orig"
else
inplace="--in-place"
fi
echo "Adding hackage mirror [$1] to [$CABAL_CONFIG]"
sed $inplace -e "s%^remote-repo:.*%remote-repo: $1%" $CABAL_CONFIG
else
die "cabal config file [$CABAL_CONFIG] not found."
fi
}
# We need to ignore the project's stack.yaml when installing
# cabal-install, otherwise it may fail due to dependency conflicts.
#
# On CI machines if our repo is cloned in the top dir then there is no way to
# teach stack to ignore the project's stack.yaml. We cannot change our
# directory to go above it. Because we have a project stack.yaml, stack does
# not even create a global stack.yaml. So we work it around by creating a new
# package and installing cabal via that.
#
# $1 tool name
stack_install_tool () {
rm -rf .packcheck/tool-install
mkdir -p .packcheck/tool-install || exit 1
cd .packcheck/tool-install || exit 1
local res
# Where possible we want to use the same resolver for build as well as
# tools to share the snapshot when building.
# But nightlies may not have cabal-install all the time
if [[ "$RESOLVER" != "" && "$RESOLVER" != nightly* ]]
then
res="--resolver $RESOLVER"
fi
if test ! -e stack.yaml
then
run_verbose_errexit $STACKCMD_TOOL_INSTALL $res \
new --bare tool-install
fi
run_verbose_errexit $STACKCMD_TOOL_INSTALL $res install $1
cd ../..
}
# $1: Directory to install cabal in
# We are not using the param as currently it is always the dir where stack
# installs binaries.
ensure_cabal() {
# We can only do this after ghc is installed.
# We need cabal to retrieve the package version as well as for the solver
# We are assuming CI cache will be per resolver so we can cache the bin
find_binary cabal "$CABALVER"
if test -z "$(which_cmd cabal)" -a -n "$(need_stack)"
then
stack_install_tool cabal-install
fi
require_cmd cabal
cabal --version
test -z "$CABALVER" || check_version_die cabal $CABALVER
# Set the real version of cabal
CABALVER=$(cabal --numeric-version) || exit 1
}
ensure_stack_yaml() {
if test -n "$STACK_YAML"
then
require_file $STACK_YAML
elif test ! -e stack.yaml
then
echo "Need cabal-install for 'stack init' to generate a stack.yaml"
ensure_cabal $OS_APP_HOME/$OS_LOCAL_DIR/bin
# solver seems to be broken with latest cabal
echo "Trying to generate a stack.yaml"
run_verbose $STACKCMD init --solver --ignore-subdirs || die "Solver failed to generate a stack.yaml.\n\
Please provide a working stack.yaml or use cabal build."
require_file stack.yaml
fi
SDIST_STACKCMD=$STACKCMD
test -z "$STACK_YAML" || SDIST_STACKCMD="$STACKCMD --stack-yaml $STACK_YAML"
# We run the stack command from .packcheck/<package> dir after unpacking
# sdist
test -z "$STACK_YAML" || STACKCMD="$STACKCMD --stack-yaml ../../$STACK_YAML"
echo "Using stack command [$STACKCMD]"
}
#------------------------------------------------------------------------------
# Create a dist, install deps and test
#------------------------------------------------------------------------------
get_pkg_name() {
local name=$(echo *.cabal)
test -f "$name" || die "One and only one .cabal file is required in the current directory."
name=${name%.cabal}
test -f "${name}.cabal" || \
die "Cannot determine package name. File [${name}.cabal] does not exist"
echo $name
}
get_pkg_full_name() {
local pkgname
local full_name
pkgname=$(get_pkg_name) || exit 1
full_name=$(cabal info . | awk '{ if ($1 == "*") {print $2; exit}}') || die "cabal info failed"
if test "${pkgname}${full_name#$pkgname}" != "${full_name}"
then
die "Inconsistent package name [$pkgname] and package full name [$full_name]"
fi
echo $full_name
}
ensure_cabal_config() {
# When cabal versions change across builds on a CI host its safer to remove
# the old config so that the build does not error out.
if test "$CABAL_REINIT_CONFIG" = y
then
local cfg="${OS_APP_HOME}/${OS_CABAL_DIR}/config"
echo "Removing old cabal config [$cfg]"
run_verbose_errexit rm -f "$cfg"
fi
local name=$(echo *.cabal)
if test ! -f "$name"
then
if test -n "$name"
then
die "There should be exactly one .cabal file in the project dir. Found: $name"
else
if test -f "package.yaml" -a -n "$STACKCMD"
then
echo "Generating cabal file from package.yaml"
# Generate cabal file from package.yaml
run_verbose "$STACKCMD query > /dev/null 2>&1"
else
die "No cabal file found in the package directory"
fi
fi
fi
PACKAGE_FULL_NAME=$(get_pkg_full_name) || die "PACKAGE_FULL_NAME"
echo "Package name and version: [$PACKAGE_FULL_NAME]"
# cabal 1.22 and earlier do not support this command
# We rely on the cabal info command to create the config above.
#run_verbose cabal user-config init || true
if test "$BUILD" = cabal -o "$BUILD" = "cabal-new"
then
if test -n "$CABAL_HACKAGE_MIRROR"
then
cabal_use_mirror $CABAL_HACKAGE_MIRROR
fi
echo
echo "cabal update"
retry_cmd cabal update
fi
}
# $1: dir where they are installed
remove_pkg_executables() {
test -n "$(which_cmd cabal)" || return 0
exes=$(cabal info . | awk '{if ($1 == "Executables:") { print $2; exit }}') || exit 1
echo "Remove installed binaries"
for i in $exes
do
# The executables may be under a cabal flag and may not have been installed
# unless we used that flag. So just use "rm -f" to remove silently.
run_verbose_errexit rm -f "$1"/"$i"
done
}
install_cabal_deps() {
if test "$CABAL_NO_SANDBOX" != "y"
then
run_verbose_errexit cabal sandbox init
fi
echo
run_verbose_errexit cabal install $CABAL_DEP_OPTIONS
}
cabal_configure() {
echo
run_verbose_errexit cabal configure $CABAL_CONFIGURE_OPTIONS
}
# $1: package full name (name + ver)
create_and_unpack_pkg_dist() {
local pkgtar=${1}.tar.gz
local opts
local SDIST_DIR
local SDIST_CMD
test -z "$SDIST_OPTIONS" || opts="$SDIST_OPTIONS"
if test "$BUILD" = stack
then
# When custom setup is used we need to configure before we can use sdist.
run_verbose_errexit $SDIST_STACKCMD build --only-configure
SDIST_CMD="$SDIST_STACKCMD sdist $opts"
SDIST_DIR=$($SDIST_STACKCMD path --dist-dir) || exit 1
elif test -n "$CABAL_USE_STACK_SDIST"
then
# When custom setup is used we need to configure before we can use sdist.
run_verbose_errexit $SDIST_STACKCMD --compiler=ghc-$GHCVER build --only-configure
SDIST_CMD="$SDIST_STACKCMD --compiler=ghc-$GHCVER sdist $opts"
SDIST_DIR=$($SDIST_STACKCMD --compiler=ghc-$GHCVER path --dist-dir) || exit 1
else
# XXX We need to configure to use sdist and we need to install