-
Notifications
You must be signed in to change notification settings - Fork 9
/
packcheck.sh
executable file
·2135 lines (1914 loc) · 61.2 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
#------------------------------------------------------------------------------
PACKCHECK_VERSION=0.7.0
show_version() {
echo "packcheck version $PACKCHECK_VERSION"
}
# $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\""
}
# Rewrite deprecated environment variables
rewrite_deprecated() {
local oldvar=$1
local oldval=$(eval "echo \$$1")
local newvar=$2
if test -n "$oldval"
then
echo "DEPRECATED [$oldvar] please use [$newvar] instead"
eval "export $newvar=$oldval"
unset $oldvar
fi
}
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 "$*" 1>&2
bash -c "$*"
}
function run_verbose_errexit_with() {
local errScript="$1";
shift
if ! run_verbose "$*";
then
eval "$errScript"
die "Command [$*] failed. Exiting."
fi
}
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|FreeBSD)
OS_HAS_TOOLS=tar
OS_UNGZTAR_CMD="run_verbose_errexit tar xmzvf"
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: $os"
lscpu | grep "^Archi\|^CPU\|^Bogo\|^Hyper\|^Virtualiz"
echo "Memory:"
run_verbose free -h || true
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 -n 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 ;;
FreeBSD)
echo "OS: FreeBSD" ;;
Darwin)
echo "OS: MacOS" ;;
MINGW*)
echo "OS: Windows (MINGW)" ;;
*) die "OS: Unknown OS [$os]" ;;
esac
}
#------------------------------------------------------------------------------
# Build config show and determine
#------------------------------------------------------------------------------
SAFE_ENVVARS="\
RESOLVER \
ENABLE_GHCJS \
GHCUP_VERSION \
GHCVER \
CABALVER \
GHC_OPTIONS \
GHCUP_GHC_OPTIONS \
SDIST_OPTIONS \
DISABLE_SDIST_BUILD \
DISABLE_SDIST_PROJECT_CHECK \
DISABLE_SDIST_GIT_CHECK \
DISABLE_BENCH \
DISABLE_TEST \
DISABLE_DOCS \
ENABLE_DOCSPEC \
DISABLE_DIST_CHECKS \
PATH \
TOOLS_DIR \
STACKVER \
STACK_YAML \
STACK_OPTIONS \
STACK_BUILD_OPTIONS \
CABAL_PROJECT \
CABAL_CHECK_RELAX \
CABAL_USE_STACK_SDIST \
CABAL_BUILD_OPTIONS \
CABAL_TEST_OPTIONS \
CABAL_DISABLE_DEPS \
CABAL_BUILD_TARGETS \
COVERAGE \
COVERALLS_OPTIONS \
HLINT_VERSION \
HLINT_BUILD \
HLINT_COMMANDS \
HLINT_OPTIONS \
HLINT_TARGETS \
DOCSPEC_URL \
DOCSPEC_OPTIONS \
CHECK_ENV \
LANG \
LC_ALL \
BASE_TIME \
"
UNSAFE_ENVVARS="\
ENABLE_INSTALL \
STACK_UPGRADE \
CABAL_REINIT_CONFIG \
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]\nTry --help for supported parameters"
}
error_clean_env() {
echo "Error: environment variable [$1] is set."
echo "Error: No environment variables except [$ALLOW_ENVVARS] are allowed to be set when using CHECK_ENV=y"
die "Please use a clean environment (e.g. env -i) with CHECK_ENV."
}
error_clean_param() {
die "Unknown parameter [$1] specified on command line.\nTry --help for supported parameters"
}
ALLOW_ENVVARS="CHECK_ENV STACK_ROOT APPDATA PWD SHLVL _"
check_clean_env() {
local vars=$(env | cut -f1 -d=)
for i in $vars
do
find_var $i "$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 name
check_boolean_var() {
error_novar "$1"
local var=$(eval "echo \$$1")
if test -n "$var"
then
case "$var" in
y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON) export $1=y ;;
n|N|no|No|NO|false|False|FALSE|off|Off|OFF) export $1= ;;
*) die "Boolean parameter or environment variable [$1=$var] can only be set to either an empty value or one of the following boolean values: y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON|n|N|no|No|NO|false|False|FALSE|off|Off|OFF"
esac
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() {
local script=$(basename $0)
echo "$script COMMAND [PARAMETER=VALUE ...]"
echo
echo "For example:"
echo "$script cabal GHCVER=9.8.1"
echo "$script stack RESOLVER=lts GHC_OPTIONS=\"-O0 -Werror\""
echo "$script hlint"
echo
echo "Ask questions: https://app.gitter.im/#/room/#composewell_streamly:gitter.im"
echo "Report issues: https://github.com/composewell/packcheck/issues/new"
}
# Please add the boolean options to check_boolean_var as well
show_help() {
show_step1 "Usage"
short_help
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."
echo
echo "Boolean parameters can be specified as "
echo "y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON for an affirmative value and as"
echo "n|N|no|No|NO|false|False|FALSE|off|Off|OFF or empty for a negative value."
show_step1 "Commands and flags"
#help_cmd cabal-v2 "build using cabal v2-build"
help_cmd cabal "build using cabal"
#help_cmd cabal-new "Deprecated alias to cabal-v2"
#help_cmd cabal-v1 "Deprecated: build using cabal v1-build"
help_cmd stack "build using stack"
help_cmd hlint "run hlint"
# TODO add hlint as a tool
help_cmd clean "remove the .packcheck directory"
help_cmd cleanall "remove .packcheck, .stack-work directories"
help_cmd "help | --help | -h" "show this help message"
help_cmd "--version" "show packcheck version"
show_step1 "Selecting tool versions"
# untested/unsupported
#help_envvar ENABLE_GHCJS "[y] Use GHCJS instead of GHC to build"
help_envvar GHCUP_VERSION "[a.b.c.d] ghcup version to install at $GHCUP_PATH (see $GHCUP_URL_PREFIX)"
help_envvar GHCVER "[a.b.c | head] GHC version prefix (may not be enforced when using stack)"
help_envvar CABALVER "[a.b.c.d] Cabal version (prefix) to use"
help_envvar STACKVER "[a.b.c.d] Stack version (prefix) to use"
help_envvar STACK_UPGRADE "[y] DESTRUCTIVE! Upgrades stack to latest version"
help_envvar RESOLVER "Stack resolver to use for stack builds or cabal builds using stack"
help_envvar HLINT_VERSION "hlint version to install at $HLINT_PATH (see $HLINT_URL_PREFIX)"
help_envvar DOCSPEC_URL "cabal-docspec release URL to install at $DOCSPEC_PATH (see $DOCSPEC_URL_PREFIX)"
show_step1 "Where to find the required tools"
help_envvar PATH "[path] Set PATH explicitly for predictable builds"
# Untested/unsupported
#help_envvar TOOLS_DIR "[dir] Find ghc|cabal by version as in TOOLS_DIR/ghc/<version>/bin"
show_step1 "Specifying common tool options"
# TODO
# help_envvar TOOL_OPTIONS "Specify the tool specific (stack or cabal) options to use."
# help_envvar BUILD_OPTIONS "Specify the tool specific build (stack build or cabal new-build) options to use."
help_envvar GHCUP_GHC_OPTIONS "Used as in \"ghcup install ghc <GHCUP_GHC_OPTIONS> <version>\""
help_envvar GHC_OPTIONS "Specify GHC options to use"
help_envvar SDIST_OPTIONS "Arguments to stack/cabal sdist command"
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 ENABLE_DOCSPEC "[y] Run cabal-docspec after the cabal build"
help_envvar DISABLE_SDIST_BUILD "[y] Do not build from source distribution"
help_envvar DISABLE_SDIST_PROJECT_CHECK "[y] Ignore project file and continue"
help_envvar DISABLE_SDIST_GIT_CHECK "[y] Do not compare source distribution with git repo"
help_envvar DISABLE_DIST_CHECKS "[y] Do not perform source distribution checks"
#help_envvar ENABLE_INSTALL "[y] DESTRUCTIVE! Install the package after building"
show_step1 "cabal options"
# XXX this applies to both stack and cabal builds
help_envvar CABAL_REINIT_CONFIG "[y] DESTRUCTIVE! Remove old config to avoid incompatibility issues"
help_envvar CABAL_PROJECT "Alternative cabal project file, path relative to project root"
#help_envvar CABAL_USE_STACK_SDIST "[y] Use stack sdist (to use --pvp-bounds)"
help_envvar CABAL_BUILD_OPTIONS "ADDITIONAL cabal v2-build options to append to defaults"
help_envvar CABAL_DISABLE_DEPS "[y] Do not install dependencies, do not do cabal update"
help_envvar CABAL_BUILD_TARGETS "cabal v2-build targets, default is 'all'"
help_envvar CABAL_CHECK_RELAX "[y] Do not fail if cabal check fails on the package."
help_envvar CABAL_HACKAGE_MIRROR "DESTRUCTIVE! Specify an alternative mirror, modifies the cabal config file."
show_step1 "stack options"
help_envvar STACK_YAML "Alternative stack config file path relative to project root"
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 "hlint options"
#help_envvar HLINT_COMMANDS "hlint commands e.g.'hlint lint src; hlint lint test'"
# XXX this is broken
#help_envvar HLINT_BUILD "Build latest hlint from hackage source"
help_envvar HLINT_OPTIONS "hlint arguments e.g.'--datadir=. lint'"
help_envvar HLINT_TARGETS "target directories to run hlint on e.g. 'src test'"
show_step1 "Coverage options"
# Untested/unsupported should be removed
#help_envvar COVERALLS_OPTIONS "hpc-coveralls args and options, usually just test suite names"
help_envvar COVERAGE "[y] Just generate coverage information"
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 DISABLE_DIST_CHECKS
check_boolean_var DISABLE_SDIST_PROJECT_CHECK
check_boolean_var DISABLE_SDIST_GIT_CHECK
check_boolean_var CABAL_USE_STACK_SDIST
check_boolean_var CABAL_REINIT_CONFIG
check_boolean_var CABAL_CHECK_RELAX
check_boolean_var CABAL_DISABLE_DEPS
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_GHCJS
check_boolean_var ENABLE_INSTALL
if test -n "$ENABLE_INSTALL"
then
echo "WARNING! ENABLE_INSTALL is deprecated and will be removed in future"
echo "WARNING! ENABLE_INSTALL is a no op, it will do nothing"
unset ENABLE_INSTALL
fi
check_boolean_var DISABLE_BENCH
check_boolean_var DISABLE_TEST
check_boolean_var DISABLE_DOCS
check_boolean_var ENABLE_DOCSPEC
check_boolean_var COVERAGE
check_boolean_var CHECK_ENV
}
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-v2"
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
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 -n "$ENABLE_GHCJS"
then
test "$BUILD" = "cabal-v2" || die "ENABLE_GHCJS works only with build type 'cabal-v2'"
COMPILER=ghcjs
GHCJS_FLAG=--ghcjs
else
if test "$GHCVER" = "head"
then
COMPILER=ghc-head
else
COMPILER=ghc
fi
fi
if test "$BUILD" = stack
then
STACK_DEP_OPTIONS="$STACK_BUILD_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-v2"
then
test -n "$CABAL_BUILD_TARGETS" || CABAL_BUILD_TARGETS=all
CABAL_BUILD_OPTIONS=$(cat << EOF
$(test -n "$DISABLE_TEST" || echo "--enable-tests")
$(test -n "$DISABLE_BENCH" || echo "--enable-benchmarks")
$(test -z "${GHC_OPTIONS}" || echo --ghc-options=\"$GHC_OPTIONS\")
$CABAL_BUILD_OPTIONS
EOF
)
CABAL_DEP_OPTIONS="$CABAL_BUILD_OPTIONS --only-dependencies --reorder-goals --max-backjumps=-1"
test -z "${COVERAGE}" || \
CABAL_BUILD_OPTIONS="$CABAL_BUILD_OPTIONS --enable-coverage"
else
die "Bug: Unknown build type [$BUILD]"
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-v2" || \
die "build [$BUILD] can only be 'stack', or 'cabal-v2'"
if test -n "$CHECK_ENV"
then
if test "$BUILD" != "cabal-v2"
then
cabal_only_var CABAL_PROJECT
cabal_only_var CABALVER
cabal_only_var CABAL_USE_STACK_SDIST
cabal_only_var CABAL_CHECK_RELAX
cabal_only_var CABAL_HACKAGE_MIRROR
cabal_only_var CABAL_BUILD_OPTIONS
cabal_only_var CABAL_TEST_OPTIONS
cabal_only_var CABAL_DISABLE_DEPS
cabal_only_var CABAL_BUILD_TARGETS
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 --fail -sSkL https://www.stackage.org/stack/osx-x86_64 \
| tar xz --strip-components=1 -C $1 --include '*/stack'
}
fetch_stack_linux() {
curl --fail -sSkL https://www.stackage.org/stack/linux-x86_64 \
| tar xz --strip-components=1 -C $1 --wildcards '*/stack'
}
fetch_stack_windows() {
curl --fail -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
# Empty STACK_YAML variable does not go well
if test -z "$STACK_YAML"
then
unset STACK_YAML
fi
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
echo "Adding [$GHCPATHS] in front of PATH..."
export PATH=$GHCPATHS:$PATH
fi
fi
if test -n "$BINPATH"
then
echo "Adding [$BINPATH] in front of PATH..."
export PATH=$BINPATH:$PATH
fi
}
#------------------------------------------------------------------------------
# Ensure ghc, cabal are available and the right versions when requested
#------------------------------------------------------------------------------
# $1: version to compare
# $2: version to compare with
# true (returns 0) if $1 <= $2
verlte () {
[ "$1" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ]
}
# $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 the real version.
# 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
local removed_path
echo "Looking for binary [$1] in PATH..."
binary=$(which_cmd $1)
while test -n "$binary"
do
if test -z "$2" || check_version $binary $2
then
PATH=$PATH$removed_path
echo "Found. PATH set to [$PATH]..."
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"
removed_path=$removed_path:$(dirname $binary)
path_remove $(dirname $binary)
binary="$(which_cmd $1)"
fi
done
echo "Not found."
# If we did not find the binary, restore the PATH for better error reporting
PATH=$path
if test -n "$TOOLS_DIR"
then
echo "Looking for binary [$1] in [$TOOLS_DIR]..."
# 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
echo "Found. PATH set to [$PATH]..."
export PATH
return 0
fi
fi
echo "Not found."
fi
STACK_ROOT_PATH="~/.stack"
if test -n "$STACK_ROOT"
then
STACK_ROOT_PATH=$STACK_ROOT
fi
# XXX what if there are multiple arch dirs in programs?
if test -d $STACK_ROOT_PATH -a "$1" = "ghc"
then
echo "Looking for binary [$1] in [$STACK_ROOT_PATH]..."
# XXX We should pick the highest version by default
local dir=$(echo $STACK_ROOT_PATH/programs/*/${1}-${2}*/)
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
echo "Found. PATH set to [$PATH]..."
export PATH
return 0
fi
fi
echo "Not found."
fi
return 1
}
ghcup_install() {
local tool=$1
local tool_ver=$2
local GHCUP_ARCH
local ghcup_path
# XXX add only if not already on PATH
PATH=$GHCUP_BIN:$PATH
echo "Added $GHCUP_BIN to PATH [$PATH]"
ghcup_path=$(which_cmd ghcup)
if test -n "$ghcup_path"
then
echo "Using existing $ghcup_path in PATH"
else
# User can either add it in the PATH or we can use the full path of the
# tool either as found in PATH or use GHCUP_PATH directly. We should
# probably fix each tool's location as found and use that rather using it
# from PATH.
if test -e "$GHCUP_PATH"
then
die "$GHCUP_PATH already exists, not overwriting."
fi
# Determine GHCUP_ARCH
os=$(uname -s -m)
case "$os" in
"Linux x86_64") GHCUP_ARCH="x86_64-linux" ;;
"FreeBSD x86_64" | "FreeBSD amd64") GHCUP_ARCH=x86_64-portbld-freebsd ;;
"Darwin x86_64") GHCUP_ARCH="x86_64-apple-darwin" ;;
"Darwin arm64") GHCUP_ARCH="aarch64-apple-darwin" ;;
*) echo "Unknown OS/Arch: $os"; exit 1;;
esac
# Check available versions here: https://downloads.haskell.org/~ghcup/
URL="$GHCUP_URL_PREFIX/$GHCUP_VERSION/${GHCUP_ARCH}-ghcup-$GHCUP_VERSION"
echo "Downloading $URL to $GHCUP_PATH"
# XXX Download to a temp location and move when successful?
#TEMP=$(mktemp -d .ghcup-XXXXXX)
#cleanup(){
# rm -r $TEMP
#}
#trap cleanup EXIT
mkdir -p $(dirname $GHCUP_PATH)
retry_cmd curl --fail --progress-bar --location -o $GHCUP_PATH $URL
if test $? -ne 0
then
rm -f $GHCUP_PATH
echo "Failed to download ghcup"
exit 1
fi
chmod +x $GHCUP_PATH
# Avoid changing the user's setup
#$GHCUP_PATH set $tool $tool_ver
fi
if test "$tool" = "ghc"
then
run_verbose_errexit_with "cat /usr/local/.ghcup/logs/*" \
ghcup install ghc $GHCUP_GHC_OPTIONS $tool_ver
else
run_verbose_errexit ghcup install $tool $tool_ver
fi
}
ensure_default_ghc() {
local ghc
ghc="$(which_cmd ghc)"
if test -z "$ghc" -a -n "$GHCUP_VERSION"
then
echo "No default ghc found in PATH. Setting it using ghcup"
ghcup set ghc $GHCVER
fi
}
ensure_ghc() {
local found
local compiler
# If there is a ghc in PATH then use that otherwise install it using
# ghcup or stack
find_binary "$COMPILER" "$GHCVER"
found=$?
if test "$found" -ne 0
then
if test -n "$GHCVER"
then
find_binary "$COMPILER-$GHCVER" "$GHCVER"
found=$?
if test "$found" -eq 0
then
COMPILER="$COMPILER-$GHCVER"
fi
fi
if test "$found" -ne 0
then
if test -n "$(need_stack)"
then
# Use stack supplied ghc
echo "$STACKCMD setup"
retry_cmd $STACKCMD setup || die "stack setup falied"
use_stack_paths