/
ghcup
executable file
·2428 lines (2064 loc) · 72 KB
/
ghcup
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
#!/bin/sh
#
# Copyright (c) 2018, Julian Ospald <hasufell@posteo.de>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the <ORGANIZATION> nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
##########################
#--[ Global Variables ]--#
##########################
# @VARIABLE: VERSION
# @DESCRIPTION:
# Version of this script.
VERSION=0.0.8
# @VARIABLE: SCRIPT
# @DESCRIPTION:
# Name of this script. This will be the
# shell name if this script is sourced, so
# only rely on this for echos and trivial things.
SCRIPT="$(basename "$0")"
# @VARIABLE: VERBOSE
# @DESCRIPTION:
# Whether to print verbose messages in this script.
VERBOSE=false
# @VARIABLE: FORCE
# @DESCRIPTION:
# Whether to force installation and overwrite files.
FORCE=false
# @VARIABLE: GHCUP_INSTALL_BASE_PREFIX
# @DESCRIPTION:
# The main install directory prefix, under which .ghcup
# directory will be created. This directory is user
# configurable via the environment variable of the
# same name. It must be non-empty and the path
# it points to must exist.
: "${GHCUP_INSTALL_BASE_PREFIX:=$HOME}"
# @VARIABLE: INSTALL_BASE
# @DESCRIPTION:
# The main install directory where all ghcup stuff happens.
INSTALL_BASE="$GHCUP_INSTALL_BASE_PREFIX/.ghcup"
# @VARIABLE: GHC_LOCATION
# @DESCRIPTION:
# The location where ghcup will install different ghc versions.
# This is expected to be a subdirectory of INSTALL_BASE.
GHC_LOCATION="$INSTALL_BASE/ghc"
# @VARIABLE: BIN_LOCATION
# @DESCRIPTION:
# The location where ghcup will create symlinks for GHC binaries.
# This is expected to be a subdirectory of INSTALL_BASE.
BIN_LOCATION="$INSTALL_BASE/bin"
# @VARIABLE: CACHE_LOCATION
# @DESCRIPTION:
# The location where ghcup will put tarballs for caching.
# This is expected to be a subdirectory of INSTALL_BASE.
CACHE_LOCATION="$INSTALL_BASE/cache"
# @VARIABLE: DOWNLOADER
# @DESCRIPTION:
# What program to use for downloading files.
DOWNLOADER="curl"
# @VARIABLE: DOWNLOADER_OPTS
# @DESCRIPTION:
# Options passed to the download program.
DOWNLOADER_OPTS="-L --fail -O"
# @VARIABLE: DOWNLOADER_STDOUT_OPTS
# @DESCRIPTION:
# Options passed to the download program when printing the content to stdout.
DOWNLOADER_STDOUT_OPTS="-L --fail"
# @VARIABLE: GHC_DOWNLOAD_BASEURL
# @DESCRIPTION:
# Base URL for all GHC tarballs.
GHC_DOWNLOAD_BASEURL="https://downloads.haskell.org/~ghc"
# @VARIABLE: JOBS
# @DESCRIPTION:
# How many jobs to use for compiling GHC.
JOBS="1"
# @VARIABLE: SOURCE
# @DESCRIPTION:
# The $0 argument, which contains
# the script name.
SOURCE="$0"
# @VARIABLE: BASE_DOWNLOAD_URL
# DESCRIPTION:
# The base url for downloading stuff like meta files, requirements files etc.
BASE_DOWNLOAD_URL="https://gitlab.haskell.org/haskell/ghcup/raw/master/"
# @VARIABLE: SCRIPT_UPDATE_URL
# @DESCRIPTION:
# Location to update this script from.
SCRIPT_UPDATE_URL="${BASE_DOWNLOAD_URL}/ghcup"
# @VARIABLE: META_DOWNLOAD_URL
# DESCRIPTION:
# The url of the meta file for getting
# download information for ghc/cabal-install etc.
META_DOWNLOAD_URL="${GHCUP_META_DOWNLOAD_URL:=${BASE_DOWNLOAD_URL}/.download-urls}"
# @VARIABLE: META_DOWNLOAD_FORMAT
# DESCRIPTION:
# The version of the meta file format.
# This determines whether this script can read the
# file from "${META_DOWNLOAD_URL}".
META_DOWNLOAD_FORMAT="1"
# @VARIABLE: META_VERSION_URL
# DESCRIPTION:
# The url of the meta file for getting
# available versions for ghc/cabal-install etc.
META_VERSION_URL="${GHCUP_META_VERSION_URL:=${BASE_DOWNLOAD_URL}/.available-versions}"
# @VARIABLE: META_VERSION_FORMAT
# DESCRIPTION:
# The version of the meta file format.
# This determines whether this script can read the
# file from "${META_VERSION_URL}".
META_VERSION_FORMAT="1"
# @VARIABLE: BUG_URL
# DESCRIPTION:
# The url to report bugs to.
BUG_URL="https://gitlab.haskell.org/haskell/ghcup/issues"
# @VARIABLE: CACHING
# @DESCRIPTION:
# Whether to cache tarballs in $CACHE_LOCATION.
CACHING=false
####################
#--[ Print Help ]--#
####################
# @FUNCTION: usage
# @DESCRIPTION:
# Print the help message for 'ghcup' to STDERR
# and exit the script with status code 1.
usage() {
(>&2 echo "ghcup ${VERSION}
GHC up toolchain installer
USAGE:
${SCRIPT} [FLAGS] <SUBCOMMAND>
FLAGS:
-v, --verbose Enable verbose output
-h, --help Prints help information
-V, --version Prints version information
-w, --wget Use wget instead of curl
-c, --cache Use \"${CACHE_LOCATION}\" for caching tarballs
(these will not be removed by ghcup)$(${VERBOSE} && printf "\n -o, --os Overwrite OS detection with the given string (must be a correct OS alias, e.g. 'alpine')")
SUBCOMMANDS:
install Install GHC$(${VERBOSE} && printf "\n compile Compile and install GHC from source (UNSTABLE!!!)")
set Set currently active GHC version
list Show available GHCs and other tools
upgrade Upgrade this script in-place
rm Remove an already installed GHC
install-cabal Install cabal-install
debug-info Print debug info (e.g. detected system/distro)
changelog Show the changelog of a GHC release (online)
print-system-reqs Print an approximation of system requirements
DISCUSSION:
ghcup installs the Glasgow Haskell Compiler from the official
release channels, enabling you to easily switch between different
versions.
")
exit 1
}
# @FUNCTION: install_usage
# @DESCRIPTION:
# Print the help message for 'ghcup install' to STDERR
# and exit the script with status code 1.
install_usage() {
(>&2 echo "ghcup-install
Install GHC from binary tarball
USAGE:
${SCRIPT} install [FLAGS] [VERSION|TAG]
FLAGS:
-h, --help Prints help information
-f, --force Overwrite already existing installation
ARGS:
[VERSION|TAG] E.g. \"8.4.3\" or \"8.6.1\" or
a tag like \"recommended\" or \"latest\"
(default: discovers recommended version)
DISCUSSION:
Installs the specified GHC version (or a recommended default one) into
a self-contained \"~/.ghcup/ghc/<ghcver>\" directory
and symlinks the ghc binaries to \"~/.ghcup/bin/<binary>-<ghcver>\".
")
exit 1
}
# @FUNCTION: set_usage
# @DESCRIPTION:
# Print the help message for 'ghcup set' to STDERR
# and exit the script with status code 1.
set_usage() {
(>&2 echo "ghcup-set
Set the currently active GHC to the specified version
USAGE:
${SCRIPT} set [FLAGS] [VERSION|TAG]
FLAGS:
-h, --help Prints help information
ARGS:
[VERSION|TAG] E.g. \"8.4.3\" or \"8.6.3\" or
a tag like \"recommended\" or \"latest\"
(default: discovers recommended version)
DISCUSSION:
Sets the the current GHC version by creating non-versioned
symlinks for all ghc binaries of the specified version in
\"~/.ghcup/bin/<binary>\".
")
exit 1
}
# @FUNCTION: upgrade_usage
# @DESCRIPTION:
# Print the help message for 'ghcup upgrade' to STDERR
# and exit the script with status code 1.
upgrade_usage() {
(>&2 echo "ghcup-upgrade
Update the ghcup script in-place
USAGE:
${SCRIPT} upgrade [FLAGS] [TARGET-LOCATION]
FLAGS:
-i, --inplace Update this script in-place (wherever it's at)
-h, --help Prints help information
ARGS:
[TARGET-LOCATION] Where to place the updated script (defaults to ${BIN_LOCATION}).
This is ignored if --inplace is issued as well.
")
exit 1
}
# @FUNCTION: rm_usage
# @DESCRIPTION:
# Print the help message for 'ghcup rm' to STDERR
# and exit the script with status code 1.
rm_usage() {
(>&2 echo "ghcup-rm
Remove the given GHC version installed by ghcup
USAGE:
${SCRIPT} rm [FLAGS] <VERSION>
FLAGS:
-h, --help Prints help information
-f, --force Don't prompt user
ARGS:
<VERSION> E.g. \"8.4.3\" or \"8.6.1\"
")
exit 1
}
# @FUNCTION: install_cabal_usage
# @DESCRIPTION:
# Print the help message for 'ghcup install-cabal' to STDERR
# and exit the script with status code 1.
install_cabal_usage() {
(>&2 echo "ghcup-install-cabal
Install the specified or a default cabal version
USAGE:
${SCRIPT} install-cabal [FLAGS] [VERSION|TAG]
FLAGS:
-h, --help Prints help information
ARGS:
[VERSION|TAG] E.g. \"2.4.0.0\" or a tag
like \"recommended\" or \"latest\"
DISCUSSION:
Installs the specified cabal-install version (or the default recommended)
into \"${BIN_LOCATION}\", so it can be overwritten
by later \"cabal new-install cabal-install\", which installs into
\"~/.cabal/bin\". Make sure to set up your PATH appropriately, so
the cabal installation takes precedence.
")
exit 1
}
# @FUNCTION: compile_usage
# @DESCRIPTION:
# Print the help message for 'ghcup compile' to STDERR
# and exit the script with status code 1.
compile_usage() {
(>&2 echo "ghcup-compile
Compile and install the specified GHC version
USAGE:
${SCRIPT} compile [FLAGS] <VERSION> <BOOTSTRAP-GHC>
FLAGS:
-h, --help Prints help information
-f, --force Overwrite already existing installation
-j, --jobs <n> How many jobs for compilation
-c, --build-config <filepath> Use the given config file as build config
ARGS:
<VERSION> E.g. \"8.4.3\" or \"8.6.1\"
<BOOTSTRAP-GHC> E.g. \"ghc-8.2.2\" or a full path
DISCUSSION:
Compiles and installs the specified GHC version into
a self-contained \"~/.ghcup/ghc/<ghcver>\" directory
and symlinks the ghc binaries to \"~/.ghcup/bin/<binary>-<ghcver>\".
EXAMPLE:
ghcup -v compile -f -j 4 8.4.2 ghc-8.2.2
")
exit 1
}
# @FUNCTION: debug_info_usage
# @DESCRIPTION:
# Print the help message for 'ghcup debug-info' to STDERR
# and exit the script with status code 1.
debug_info_usage() {
(>&2 echo "ghcup-debug-info
Print debug info (e.g. detected system/distro)
USAGE:
${SCRIPT} debug-info
FLAGS:
-h, --help Prints help information
DISCUSSION:
Prints debug information, e.g. detected system architecture,
distribution, version, as well as script variables. This
is mainly useful for debugging purposes.
")
exit 1
}
# @FUNCTION: list_usage
# @DESCRIPTION:
# Print the help message for 'ghcup list' to STDERR
# and exit the script with status code 1.
list_usage() {
(>&2 echo "ghcup-list
Show available GHCs and other tools
USAGE:
${SCRIPT} list
FLAGS:
-h, --help Prints help information
-t, --tool <all|ghc|cabal-install> Tool to list versions for. Default is ghc only.
-c, --show-criteria <installed|set> Show only installed or set tool versions
-r, --raw-format Raw format, for machine parsing
DISCUSSION:
Prints tools (e.g. GHC and cabal-install) and their
available/installed/set versions.
")
exit 1
}
# @FUNCTION: changelog_usage
# @DESCRIPTION:
# Print the help message for 'ghcup changelog' to STDERR
# and exit the script with status code 1.
changelog_usage() {
(>&2 echo "ghcup-changelog
View the online changelog for the given GHC version
USAGE:
${SCRIPT} changelog [FLAGS] [VERSION|TAG]
FLAGS:
-h, --help Prints help information
ARGS:
[VERSION|TAG] E.g. \"8.4.3\" or \"8.6.3\" or
a tag like \"recommended\" or \"latest\"
(default: discovers latest version)
DISCUSSION:
Opens the online changelog for the given GHC version via
xdg-open.
")
exit 1
}
# @FUNCTION: print_system_reqs_usage
# @DESCRIPTION:
# Print the help message for 'ghcup print-system-reqs' to STDERR
# and exit the script with status code 1.
print_system_reqs_usage() {
(>&2 echo "ghcup-print-system-reqs
Print an approximation of system requirements
USAGE:
${SCRIPT} print-system-reqs
FLAGS:
-h, --help Prints help information
DISCUSSION:
Just prints an approximation of the system requirements
for the 'recommended' GHC version and the 'latest' distro version
you are on.
Review this output carefully!
")
exit 1
}
###########################
#--[ Utility functions ]--#
###########################
# @FUNCTION: die
# @USAGE: [msg]
# @DESCRIPTION:
# Exits the shell script with status code 2
# and prints the given message in red to STDERR, if any.
die() {
(>&2 red_message "$1")
exit 2
}
# @FUNCTION: edo
# @USAGE: <command>
# @DESCRIPTION:
# Executes the given command. Also prints what
# command that is (in blue) if verbosity is enabled.
# Exits with status code 2 if the command failed.
edo()
{
if ${VERBOSE} ; then
printf "\\033[0;34m%s\\033[0m\\n" "$*" 1>&2
fi
"$@" || exit 2
}
# @FUNCTION: emake
# @USAGE: [arguments]
# @DESCRIPTION:
# Wrapper around 'make', may call 'gmake' if it exists.
emake() { # avoid re-checking for gmake
if [ -n "${MAKE}" ] ; then
# shellcheck disable=SC2086
edo ${MAKE} "$@"
else
if command_exists gmake ; then
MAKE="gmake"
# shellcheck disable=SC2086
edo ${MAKE} "$@"
else
MAKE="make"
# shellcheck disable=SC2086
edo ${MAKE} "$@"
fi
fi
}
# @FUNCTION: debug_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a blue debug message if verbosity is enabled.
debug_message() {
if ${VERBOSE} ; then
(>&2 printf "\\033[0;34m%s\\033[0m\\n" "$1")
fi
}
# @FUNCTION: optionv
# @USAGE: <arg1> [arg2]
# @DESCRIPTION:
# If verbosity is enabled, echo the first argument, otherwise
# the second (if any).
# @STDOUT: first or second argument
optionv() {
if ${VERBOSE} ; then
echo "$1"
else
if [ -n "$2" ] ; then
echo "$2"
fi
fi
}
# @FUNCTION: status_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a green status message.
status_message() {
printf "\\033[0;32m%s\\033[0m\\n" "$1"
}
# @FUNCTION: warning_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a yellow warning message.
warning_message() {
printf "\\033[1;33m%s\\033[0m\\n" "$1"
}
# @FUNCTION: red_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a red message.
red_message() {
printf "\\033[0;31m%s\\033[0m\\n" "$1"
}
# @FUNCTION: command_exists
# @USAGE: <command>
# @DESCRIPTION:
# Check if a command exists (no arguments).
# @RETURNS: 0 if the command exists, non-zero otherwise
command_exists() {
[ -z "$1" ] && die "Internal error: no argument given to command_exists"
command -V "$1" >/dev/null 2>&1
return $?
}
# @FUNCTION: check_required_commands
# @USAGE: [additional-commands]
# @DESCRIPTION:
# Check that all required commands for this script exist.
# @STDOUT: The commands that do not exist
# @RETURNS: 0 if all command exists, non-zero otherwise
check_required_commands() {
_missing_commands=
mydistro=$(get_distro_alias "$(get_distro_name)")
for com in "$@" awk uname basename tar gzip mktemp dirname ; do
command_exists "${com}" || {
_missing_commands="${_missing_commands} ${com}"
}
done
unset com
# darwin uses tar's built-in xz decompression
if test "${mydistro}" != "darwin"; then
command_exists xz || {
_missing_commands="${_missing_commands} xz"
}
fi
if [ -n "${_missing_commands}" ] ; then
printf "%s" "${_missing_commands}"
unset _missing_commands mydistro
return 1
else
unset _missing_commands mydistro
return 0
fi
}
# @FUNCTION: get_distro_name
# @DESCRIPTION:
# Gets the current distro identifier following
# https://unix.stackexchange.com/a/6348
# (see also http://linuxmafia.com/faq/Admin/release-files.html)
# @STDOUT: current distro identifier
get_distro_name() {
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
# shellcheck disable=SC1091
. /etc/os-release
printf "%s" "$NAME"
elif command_exists lsb_release ; then
# linuxbase.org
printf "%s" "$(lsb_release -si)"
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
# shellcheck disable=SC1091
. /etc/lsb-release
printf "%s" "$DISTRIB_ID"
elif [ -f /etc/redhat-release ]; then
case "$(cat /etc/redhat-release)" in
# Older CentOS releases didn't have a /etc/centos-release file
"CentOS release "*)
printf "CentOS"
;;
"CentOS Linux release "*)
printf "CentOS Linux"
;;
"Fedora release "*)
printf "Fedora"
;;
# Fallback to uname
*)
printf "%s" "$(uname -s)"
;;
esac
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
printf "Debian"
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
printf "%s" "$(uname -s)"
fi
}
# @FUNCTION: get_distro_ver
# @DESCRIPTION:
# Gets the current distro version (if any) following
# https://unix.stackexchange.com/a/6348
# @STDOUT: current distro version, if any
get_distro_ver() {
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
# shellcheck disable=SC1091
. /etc/os-release
printf "%s" "$VERSION_ID"
elif command_exists lsb_release ; then
# linuxbase.org
printf "%s" "$(lsb_release -sr)"
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
# shellcheck disable=SC1091
. /etc/lsb-release
printf "%s" "$DISTRIB_RELEASE"
elif [ -f /etc/redhat-release ]; then
case "$(cat /etc/redhat-release)" in
# NB: Older CentOS releases didn't have a /etc/centos-release file
"CentOS release "*|"Fedora release "*)
printf "%s" "$(awk 'NR==1 { split($3, a, "."); print a[1] }' /etc/redhat-release)"
;;
"CentOS Linux release "*)
printf "%s" "$(awk 'NR==1 { split($4, a, "."); print a[1] }' /etc/redhat-release)"
;;
# Fallback to uname
*)
printf "%s" "$(uname -r)"
;;
esac
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
printf "%s" "$(cat /etc/debian_version)"
else
case "$(uname -s)" in
AIX)
printf "%s" "$(uname -v)"
;;
FreeBSD)
# we only care about the major numeric version part left of
# the '.' in "11.2-RELEASE".
printf "%s" "$(uname -r | cut -d . -f 1)"
;;
*)
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
printf "%s" "$(uname -r)"
esac
fi
}
# @FUNCTION: get_arch
# @DESCRIPTION:
# Gets the architecture following
# https://unix.stackexchange.com/a/6348
# Fails for any architecture that we don't know a GHC version for.
# @STDOUT: current architecture
get_arch() {
myarch=$(uname -m)
case "${myarch}" in
x86_64|amd64)
printf "x86_64" # or AMD64 or Intel64 or whatever
;;
i*86)
printf "i386" # or IA32 or Intel32 or whatever
;;
*)
case "$(uname -s)" in
AIX)
case "$(uname -p)" in
powerpc)
printf "powerpc"
;;
*)
die "Cannot figure out architecture on AIX (was: ${myarch})"
;;
esac
;;
*)
die "Cannot figure out architecture (was: ${myarch})"
;;
esac
esac
unset myarch
}
# @FUNCTION: try_download_url
# @USAGE: <tool> <ver> <arch> <distro-ident> <file>
# @DESCRIPTION:
# Tries to get the download url of a tool with our
# specified format for download urls (see ${META_DOWNLOAD_URL}").
# STDOUT: the download url, if an appropriate was found
try_download_url() {
[ "$#" -lt 5 ] && die "Internal error: not enough arguments to try_download_url"
tool=$1
ver=$2
arch=$3
distro_ident=$4
filename=$5
awk "
NF {
split(\$4,a,\",\")
if (\$1 == \"${tool}\" && \$2 == \"${ver}\" && \$3 == \"${arch}\") {
for (i in a) if (a[i] == \"${distro_ident}\") {
print \$5
exit
}
}
}" "${filename}" || die "awk failed!"
unset tool ver arch distro_ident filename
}
# @FUNCTION: check_meta_file_version
# @USAGE: <file> <metaver>
# @DESCRIPTION:
# Check that the given meta file has the same format version
# as specified, otherwise die.
check_meta_file_version() {
{ [ -z "$1" ] || [ -z "$2" ] ;} && die "Internal error: not enough arguments given to check_meta_file_version"
mymetavar=$(awk "
NR==1 {
if (\$2 ~ \"fmt-version\") {
{
split(\$2,a,\"=\")
print a[2]
exit
}
}
}" "$1")
if [ "${mymetavar}" != "$2" ] ; then
die "Unsupported meta file format, run: ${SCRIPT} upgrade"
fi
unset mymetavar
}
# @FUNCTION: get_download_url
# @USAGE: <tool> <version> [os-overwrite]
# @DESCRIPTION:
# Gets the download url for the given tool and version
# and the current distro and architecture (which it tries to discover).
# This uses "${META_DOWNLOAD_URL}" for url discovery.
# @STDOUT: download url or nothing if no appropriate was found
get_download_url() {
{ [ -z "$1" ] || [ -z "$2" ] ;} && die "Internal error: not enough arguments given to get_download_url"
mytool=$1
myver=$2
myarch=$(get_arch)
[ -z "${myarch}" ] && die "failed to get architecture"
if [ -n "$3" ] ; then
mydistro=$(get_distro_alias "$3")
else
mydistro=$(get_distro_alias "$(get_distro_name)")
fi
mydistrover=$(get_distro_ver)
meta_file="$(get_meta_download_file)"
[ -z "${meta_file}" ] && die "failed to get meta file"
# 1st try with full distro=ver
url=$(try_download_url "${mytool}" "${myver}" "${myarch}" "${mydistro}=${mydistrover}" "${meta_file}")
if [ -n "${url}" ] ; then
printf "%s" "${url}"
exit 0
fi
# 2nd try with just distro
url=$(try_download_url "${mytool}" "${myver}" "${myarch}" "${mydistro}" "${meta_file}")
if [ -n "${url}" ] ; then
printf "%s" "${url}"
exit 0
fi
# 3rd try with unknown
url=$(try_download_url "${mytool}" "${myver}" "${myarch}" "unknown" "${meta_file}")
if [ -n "${url}" ] ; then
printf "%s" "${url}"
exit 0
fi
unset mytool myver myarch mydistro mydistrover meta_file
}
# @FUNCTION: get_tool_ver_from_tag
# @USAGE: <tool> <tag>
# @DESCRIPTION:
# Gets the tool version with the given tag (first match) from
# "${META_VERSION_URL}".
# STDOUT: the version, if any, or nothing
get_tool_ver_from_tag() {
{ [ -z "$1" ] || [ -z "$2" ] ;} && die "Internal error: not enough arguments given to get_tool_ver_from_tag"
mytool=$1
mytag=$2
meta_file="$(get_meta_version_file)"
[ -z "${meta_file}" ] && die "failed to get meta file"
awk "
NF {
if (\$1 == \"${mytool}\") {
split(\$3,a,\",\");
for (i in a) if (a[i] == \"${mytag}\") {
print \$2
exit
}
}
}" "${meta_file}" || die "awk failed!"
unset mytool mytag meta_file
}
# @FUNCTION: ghc_already_installed
# @USAGE: <ghcversion>
# @DESCRIPTION:
# Checks whether the specified GHC version
# has been installed by ghcup already.
# @RETURN: 0 if GHC is already installed, 1 otherwise
ghc_already_installed() {
[ -z "$1" ] && die "Internal error: no argument given to ghc_already_installed"
if [ -e "$(get_ghc_location "$1")" ] ; then
return 0
else
return 1
fi
}
# @FUNCTION: cabal_already_installed
# @USAGE: <cabalversion>
# @DESCRIPTION:
# Checks whether the specified cabal version
# has been installed by ghcup already.
# @RETURN: 0 if cabal is already installed, 1 otherwise
cabal_already_installed() {
[ -z "$1" ] && die "Internal error: no argument given to cabal_already_installed"
if [ -x "${BIN_LOCATION}/cabal" ] ; then
if [ "$("${BIN_LOCATION}/cabal" --numeric-version)" = "$1" ] ; then
return 0
else
return 1
fi
else
return 1
fi
}
# @FUNCTION: tool_already_installed
# @USAGE: <tool> <cabalversion>
# @DESCRIPTION:
# Checks whether the specified tool and version
# has been installed by ghcup already.
# @RETURN: 0 if tool is already installed, 1 otherwise
tool_already_installed() {
if [ "$1" = "ghc" ] ; then
ghc_already_installed "$2"
return $?
elif [ "$1" = "cabal-install" ] ; then
cabal_already_installed "$2"
return $?
else
return 1
fi
}
# @FUNCTION: get_ghc_location
# @USAGE: <ghcversion>
# @DESCRIPTION:
# Gets/prints the location where the specified GHC is or would be installed.
# Doesn't check whether that directory actually exist. Use
# 'ghc_already_installed' for that.
# @STDOUT: ghc location
get_ghc_location() {
[ -z "$1" ] && die "Internal error: no argument given to get_ghc_location"
myghcver=$1
inst_location=${GHC_LOCATION}/${myghcver}
printf "%s" "${inst_location}"
unset myghcver inst_location
}
# @FUNCTION: download
# @USAGE: <url>
# @DESCRIPTION:
# Downloads the given url as a file into the current directory.
download() {
[ -z "$1" ] && die "Internal error: no argument given to download"
# shellcheck disable=SC2086
edo ${DOWNLOADER} ${DOWNLOADER_OPTS} "$1"
}
# @FUNCTION: download_to_cache
# @USAGE: <url>
# @DESCRIPTION:
# Downloads the given url as a file into the cache directory
# and makes sure the file is deleted on failed/interrupted download.
download_to_cache() {
[ -z "$1" ] && die "Internal error: no argument given to download_to_cache"
_dtc_download_url="$1"
_dtc_download_tarball_name=$(basename "${_dtc_download_url}")
rm_tarball() {
if [ -f "${CACHE_LOCATION}/${_dtc_download_tarball_name}" ] ; then
rm "${CACHE_LOCATION}/${_dtc_download_tarball_name}"
fi
}
(
trap 'rm_tarball' 2
edo cd "${CACHE_LOCATION}"
# shellcheck disable=SC2086
edo ${DOWNLOADER} ${DOWNLOADER_OPTS} "${_dtc_download_url}"
trap - 2
) || {
rm_tarball
die "Failed to download"
}
unset _dtc_download_tarball_name _dtc_download_url
}
# @FUNCTION: download_silent