ghcup/ghcup

1093 lines
30 KiB
Plaintext
Raw Normal View History

2018-09-29 07:50:26 +00:00
#!/bin/sh
2018-09-29 15:31:07 +00:00
#
# 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.
2018-09-30 11:39:40 +00:00
# TODO:
# - make removal more robust
2018-09-29 07:50:26 +00:00
2018-09-30 06:26:59 +00:00
##########################
#--[ Global Variables ]--#
##########################
2018-09-29 07:50:26 +00:00
2018-09-30 06:20:32 +00:00
# @VARIABLE: VERSION
# @DESCRIPTION:
# Version of this script.
2018-10-08 16:34:17 +00:00
VERSION=0.0.4
2018-09-30 06:20:32 +00:00
# @VARIABLE: SCRIPT
# @DESCRIPTION:
# Name of this script.
2018-09-29 13:46:39 +00:00
SCRIPT="$(basename "$0")"
2018-09-30 06:20:32 +00:00
# @VARIABLE: VERBOSE
# @DESCRIPTION:
# Whether to print verbose messages in this script.
2018-09-29 07:50:26 +00:00
VERBOSE=false
2018-09-30 06:20:32 +00:00
# @VARIABLE: FORCE
# @DESCRIPTION:
# Whether to force installation and overwrite files.
FORCE=false
2018-09-30 06:20:32 +00:00
# @VARIABLE: INSTALL_BASE
# @DESCRIPTION:
# The main install directory where all ghcup stuff happens.
INSTALL_BASE="$HOME/.ghcup"
2018-09-30 06:20:32 +00:00
# @VARIABLE: GHC_LOCATION
# @DESCRIPTION:
# The location where ghcup will install different ghc versions.
2018-10-05 02:44:23 +00:00
# This is expected to be a subdirectory of INSTALL_BASE.
2018-09-29 18:57:03 +00:00
GHC_LOCATION="$INSTALL_BASE/ghc"
2018-09-30 06:20:32 +00:00
# @VARIABLE: BIN_LOCATION
# @DESCRIPTION:
# The location where ghcup will create symlinks for GHC binaries.
2018-10-05 02:44:23 +00:00
# This is expected to be a subdirectory of INSTALL_BASE.
2018-09-29 18:57:03 +00:00
BIN_LOCATION="$INSTALL_BASE/bin"
2018-09-30 06:20:32 +00:00
# @VARIABLE: DOWNLOADER
# @DESCRIPTION:
# What program to use for downloading files.
2018-09-30 05:25:17 +00:00
DOWNLOADER="curl"
2018-09-30 06:20:32 +00:00
# @VARIABLE: DOWNLOADER_OPTS
# @DESCRIPTION:
# Options passed to the download program.
2018-09-30 05:25:17 +00:00
DOWNLOADER_OPTS="--fail -O"
2018-09-30 06:20:32 +00:00
# @VARIABLE: SCRIPT_UPDATE_URL
# @DESCRIPTION:
# Location to update this script from.
2018-09-30 05:33:04 +00:00
SCRIPT_UPDATE_URL="https://raw.githubusercontent.com/hasufell/ghcup/master/ghcup"
2018-09-30 06:20:32 +00:00
# @VARIABLE: GHC_DOWNLOAD_BASEURL
# @DESCRIPTION:
# Base URL for all GHC tarballs.
2018-09-30 05:34:54 +00:00
GHC_DOWNLOAD_BASEURL="https://downloads.haskell.org/~ghc"
2018-09-29 07:50:26 +00:00
2018-09-30 08:46:50 +00:00
# @VARIABLE: KNOWN_GOOD_CABAL
# @DESCRIPTION:
# The latests known good cabal-install version for
# which a pre-built binary exists.
KNOWN_GOOD_CABAL="2.2.0.0"
2018-09-29 07:50:26 +00:00
2018-09-30 11:39:40 +00:00
# @VARIABLE: JOBS
# @DESCRIPTION:
# How many jobs to use for compiling GHC.
JOBS="1"
2018-10-15 09:07:00 +00:00
# @VARIABLE: SOURCE
# @DESCRIPTION:
# The $0 argument, which contains
# the script name.
SOURCE="$0"
2018-09-30 06:26:59 +00:00
####################
#--[ Print Help ]--#
####################
2018-09-29 07:50:26 +00:00
2018-09-30 06:20:32 +00:00
# @FUNCTION: usage
# @DESCRIPTION:
# Print the help message for 'ghcup' to STDERR
# and exit the script with status code 1.
2018-09-29 07:50:26 +00:00
usage() {
2018-09-29 13:03:24 +00:00
(>&2 echo "ghcup ${VERSION}
GHC up toolchain installer
USAGE:
${SCRIPT} [FLAGS] <SUBCOMMAND>
2018-09-29 07:50:26 +00:00
FLAGS:
-v, --verbose Enable verbose output
-h, --help Prints help information
-V, --version Prints version information
SUBCOMMANDS:
install Install GHC
2018-10-05 02:34:26 +00:00
compile Compile and install GHC from source
2018-09-29 15:01:39 +00:00
show Show current/installed GHC
2018-09-29 13:07:57 +00:00
set Set currently active GHC version
self-update Update this script in-place
2018-09-29 18:58:55 +00:00
rm Remove an already installed GHC
2018-09-30 08:46:50 +00:00
install-cabal Install cabal-install
2018-09-29 17:46:21 +00:00
DISCUSSION:
ghcup installs the Glasgow Haskell Compiler from the official
release channels, enabling you to easily switch between different
versions.
2018-09-29 07:50:26 +00:00
")
exit 1
}
2018-09-30 06:20:32 +00:00
# @FUNCTION: install_usage
# @DESCRIPTION:
# Print the help message for 'ghcup install' to STDERR
# and exit the script with status code 1.
2018-09-29 07:50:26 +00:00
install_usage() {
2018-09-29 13:03:24 +00:00
(>&2 echo "ghcup-install
Install the specified GHC version
USAGE:
${SCRIPT} install [FLAGS] <VERSION>
2018-09-29 07:50:26 +00:00
FLAGS:
-h, --help Prints help information
-f, --force Overwrite already existing installation
2018-09-29 07:50:26 +00:00
ARGS:
<VERSION> E.g. \"8.4.3\" or \"8.6.1\"
2018-09-29 17:46:21 +00:00
DISCUSSION:
Installs the specified GHC version into
a self-contained \"~/.ghcup/ghc/<ghcver>\" directory
and symlinks the ghc binaries to \"~/.ghcup/bin/<binary>-<ghcver>\".
2018-09-29 07:50:26 +00:00
")
exit 1
}
2018-09-30 06:20:32 +00:00
# @FUNCTION: set_usage
# @DESCRIPTION:
# Print the help message for 'ghcup set' to STDERR
# and exit the script with status code 1.
2018-09-29 13:07:57 +00:00
set_usage() {
(>&2 echo "ghcup-set
2018-09-29 13:03:24 +00:00
Set the currently active GHC to the specified version
USAGE:
2018-09-29 13:07:57 +00:00
${SCRIPT} set [FLAGS] <VERSION>
FLAGS:
-h, --help Prints help information
ARGS:
<VERSION> E.g. \"8.4.3\" or \"8.6.1\"
2018-09-29 17:46:21 +00:00
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
}
2018-09-30 06:20:32 +00:00
# @FUNCTION: self_update_usage
# @DESCRIPTION:
# Print the help message for 'ghcup self-update' to STDERR
# and exit the script with status code 1.
self_update_usage() {
2018-09-29 13:03:24 +00:00
(>&2 echo "ghcup-self-update
2018-09-29 17:17:14 +00:00
Update the ghcup script in-place
2018-09-29 13:03:24 +00:00
USAGE:
${SCRIPT} self-update [FLAGS] [TARGET-LOCATION]
FLAGS:
-h, --help Prints help information
ARGS:
2018-10-15 14:16:40 +00:00
[TARGET-LOCATION] Where to place the updated script (defaults to directory of the script).
")
exit 1
}
2018-09-30 06:20:32 +00:00
# @FUNCTION: show_usage
# @DESCRIPTION:
# Print the help message for 'ghcup show' to STDERR
# and exit the script with status code 1.
2018-09-29 15:01:39 +00:00
show_usage() {
(>&2 echo "ghcup-show
Show the installed/current GHC versions
USAGE:
${SCRIPT} show [FLAGS]
FLAGS:
-h, --help Prints help information
-i, --installed Show installed GHC version only
")
exit 1
}
2018-09-30 06:20:32 +00:00
# @FUNCTION: rm_usage
# @DESCRIPTION:
# Print the help message for 'ghcup rm' to STDERR
# and exit the script with status code 1.
2018-09-29 18:58:55 +00:00
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
ARGS:
<VERSION> E.g. \"8.4.3\" or \"8.6.1\"
")
exit 1
}
2018-09-30 08:46:50 +00:00
# @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]
FLAGS:
-h, --help Prints help information
ARGS:
<VERSION> E.g. \"2.4.0.0\"
DISCUSSION:
Installs the specified cabal-install version (or the default ${KNOWN_GOOD_CABAL})
2018-10-05 02:44:23 +00:00
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.
2018-09-30 08:46:50 +00:00
")
exit 1
}
2018-09-30 11:39:40 +00:00
# @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
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
}
2018-09-30 08:46:50 +00:00
2018-09-29 15:01:39 +00:00
2018-09-30 06:26:59 +00:00
###########################
#--[ Utility functions ]--#
###########################
2018-09-29 07:50:26 +00:00
2018-09-30 06:20:32 +00:00
# @FUNCTION: die
# @USAGE: [msg]
# @DESCRIPTION:
# Exits the shell script with status code 2
# and prints the given message in red to STDERR, if any.
2018-09-29 07:50:26 +00:00
die() {
2018-09-29 19:14:56 +00:00
(>&2 red_message "$1")
2018-09-29 07:50:26 +00:00
exit 2
}
2018-09-30 06:20:32 +00:00
# @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.
2018-09-29 14:31:08 +00:00
edo()
{
if ${VERBOSE} ; then
2018-09-29 17:33:08 +00:00
printf "\\033[0;34m%s\\033[0m\\n" "$*" 1>&2
2018-09-29 14:31:08 +00:00
fi
"$@" || exit 2
}
2018-09-30 06:20:32 +00:00
# @FUNCTION: debug_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a blue debug message if verbosity is enabled.
2018-09-29 18:57:03 +00:00
debug_message() {
2018-09-29 07:50:26 +00:00
if ${VERBOSE} ; then
2018-09-29 17:33:08 +00:00
printf "\\033[0;34m%s\\033[0m\\n" "$1"
2018-09-29 07:50:26 +00:00
fi
}
2018-09-30 06:20:32 +00:00
# @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
}
2018-09-30 06:20:32 +00:00
# @FUNCTION: status_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a green status message.
2018-09-29 18:57:03 +00:00
status_message() {
2018-09-29 13:46:39 +00:00
printf "\\033[0;32m%s\\033[0m\\n" "$1"
2018-09-29 07:50:26 +00:00
}
2018-09-30 06:20:32 +00:00
# @FUNCTION: warning_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a yellow warning message.
2018-09-29 18:57:03 +00:00
warning_message() {
printf "\\033[1;33m%s\\033[0m\\n" "$1"
}
2018-09-30 06:20:32 +00:00
# @FUNCTION: red_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a red message.
2018-09-29 19:14:56 +00:00
red_message() {
printf "\\033[0;31m%s\\033[0m\\n" "$1"
}
2018-09-30 06:20:32 +00:00
# @FUNCTION: get_distro_name
# @DESCRIPTION:
# Gets the current distro identifier following
# https://unix.stackexchange.com/a/6348
# @STDOUT: current distro identifier
2018-09-29 07:50:26 +00:00
get_distro_name() {
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
2018-09-29 14:22:54 +00:00
# shellcheck disable=SC1091
2018-09-29 07:50:26 +00:00
. /etc/os-release
2018-09-29 13:46:39 +00:00
printf "%s" "$NAME"
2018-09-29 09:53:14 +00:00
elif command -V lsb_release >/dev/null 2>&1; then
2018-09-29 07:50:26 +00:00
# linuxbase.org
2018-09-29 13:46:39 +00:00
printf "%s" "$(lsb_release -si)"
2018-09-29 07:50:26 +00:00
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
2018-09-29 14:22:54 +00:00
# shellcheck disable=SC1091
2018-09-29 07:50:26 +00:00
. /etc/lsb-release
2018-09-29 13:46:39 +00:00
printf "%s" "$DISTRIB_ID"
2018-09-29 07:50:26 +00:00
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.
2018-09-29 13:46:39 +00:00
printf "%s" "$(uname -s)"
2018-09-29 07:50:26 +00:00
fi
}
2018-09-30 06:20:32 +00:00
# @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
2018-09-29 07:50:26 +00:00
get_distro_ver() {
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
2018-09-29 14:22:54 +00:00
# shellcheck disable=SC1091
2018-09-29 07:50:26 +00:00
. /etc/os-release
2018-09-29 13:46:39 +00:00
printf "%s" "$VERSION_ID"
2018-09-29 09:53:14 +00:00
elif command -V lsb_release >/dev/null 2>&1; then
2018-09-29 07:50:26 +00:00
# linuxbase.org
2018-09-29 13:46:39 +00:00
printf "%s" "$(lsb_release -sr)"
2018-09-29 07:50:26 +00:00
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
2018-09-29 14:22:54 +00:00
# shellcheck disable=SC1091
2018-09-29 07:50:26 +00:00
. /etc/lsb-release
2018-09-29 13:46:39 +00:00
printf "%s" "$DISTRIB_RELEASE"
2018-09-29 07:50:26 +00:00
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
2018-09-29 13:46:39 +00:00
printf "%s" "$(cat /etc/debian_version)"
2018-09-29 07:50:26 +00:00
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
2018-09-29 13:46:39 +00:00
printf "%s" "$(uname -r)"
2018-09-29 07:50:26 +00:00
fi
}
2018-09-30 06:20:32 +00:00
# @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
2018-09-29 07:50:26 +00:00
get_arch() {
myarch=$(uname -m)
case "${myarch}" in
x86_64)
printf "x86_64" # or AMD64 or Intel64 or whatever
;;
i*86)
printf "i386" # or IA32 or Intel32 or whatever
;;
*)
die "Cannot figure out architecture (was: ${myarch})"
;;
esac
unset myarch
}
2018-09-30 05:52:01 +00:00
# @FUNCTION: get_download_url
# @USAGE: <ghcversion>
# @DESCRIPTION:
# Gets the right (hopefully) download url for the given ghc version
# and the current distro and architecture (which it tries to discover).
# @STDOUT: ghc download url
2018-09-29 07:50:26 +00:00
get_download_url() {
2018-09-30 05:52:40 +00:00
[ -z "$1" ] && die "Internal error: no argument given to get_download_url"
2018-09-29 07:50:26 +00:00
myghcver=$1
myarch=$(get_arch)
mydistro=$(get_distro_name)
mydistrover=$(get_distro_ver)
# TODO: awkward, restructure
2018-09-29 07:50:26 +00:00
case "${mydistro},${mydistrover},${myarch},${myghcver}" in
Debian,7,i386,8.2.2)
2018-09-30 05:34:54 +00:00
printf "%s" "${GHC_DOWNLOAD_BASEURL}/${myghcver}/ghc-${myghcver}-${myarch}-deb${mydistrover}-linux.tar.xz"
2018-09-29 13:46:39 +00:00
;;
2018-09-29 07:50:26 +00:00
*,*,i386,*)
2018-09-30 05:34:54 +00:00
printf "%s" "${GHC_DOWNLOAD_BASEURL}/${myghcver}/ghc-${myghcver}-${myarch}-deb8-linux.tar.xz"
2018-09-29 13:46:39 +00:00
;;
Debian,*,*,8.2.2)
2018-09-30 05:34:54 +00:00
printf "%s" "${GHC_DOWNLOAD_BASEURL}/${myghcver}/ghc-${myghcver}-${myarch}-deb8-linux.tar.xz"
2018-09-29 13:46:39 +00:00
;;
2018-09-29 07:50:26 +00:00
Debian,8,*,*)
2018-09-30 05:34:54 +00:00
printf "%s" "${GHC_DOWNLOAD_BASEURL}/${myghcver}/ghc-${myghcver}-${myarch}-deb8-linux.tar.xz"
2018-09-29 13:46:39 +00:00
;;
2018-09-29 07:50:26 +00:00
Debian,*,*,*)
2018-09-30 05:34:54 +00:00
printf "%s" "${GHC_DOWNLOAD_BASEURL}/${myghcver}/ghc-${myghcver}-${myarch}-deb9-linux.tar.xz"
2018-09-29 13:46:39 +00:00
;;
Ubuntu,*,*,8.2.2)
2018-09-30 05:34:54 +00:00
printf "%s" "${GHC_DOWNLOAD_BASEURL}/${myghcver}/ghc-${myghcver}-${myarch}-deb8-linux.tar.xz"
2018-09-29 13:46:39 +00:00
;;
2018-09-29 07:50:26 +00:00
Ubuntu,*,*,*)
2018-09-30 05:34:54 +00:00
printf "%s" "${GHC_DOWNLOAD_BASEURL}/${myghcver}/ghc-${myghcver}-${myarch}-deb9-linux.tar.xz"
2018-09-29 13:46:39 +00:00
;;
*,*,*,8.2.2)
2018-09-30 05:34:54 +00:00
printf "%s" "${GHC_DOWNLOAD_BASEURL}/${myghcver}/ghc-${myghcver}-${myarch}-deb8-linux.tar.xz"
2018-09-29 13:46:39 +00:00
;;
2018-09-29 07:50:26 +00:00
*,*,*,*) # this is our best guess
2018-09-30 05:34:54 +00:00
printf "%s" "${GHC_DOWNLOAD_BASEURL}/${myghcver}/ghc-${myghcver}-${myarch}-fedora27-linux.tar.xz"
2018-09-29 13:46:39 +00:00
;;
2018-09-29 07:50:26 +00:00
esac
2018-09-30 05:34:54 +00:00
unset myghcver myarch mydistro mydistrover
2018-09-29 07:50:26 +00:00
}
2018-09-29 18:57:03 +00:00
# @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() {
2018-09-30 05:52:40 +00:00
[ -z "$1" ] && die "Internal error: no argument given to ghc_already_installed"
2018-09-29 18:57:03 +00:00
if [ -e "$(get_ghc_location "$1")" ] ; then
return 0
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() {
2018-09-30 05:52:40 +00:00
[ -z "$1" ] && die "Internal error: no argument given to get_ghc_location"
2018-09-29 18:57:03 +00:00
myghcver=$1
inst_location=${GHC_LOCATION}/${myghcver}
printf "%s" "${inst_location}"
2018-09-30 05:21:09 +00:00
unset myghcver inst_location
2018-09-29 18:57:03 +00:00
}
2018-09-30 05:25:17 +00:00
# @FUNCTION: download
# @USAGE: <url>
# @DESCRIPTION:
# Downloads the given url as a file into the current directory.
# @RETURN: status code from the downloader
download() {
2018-09-30 05:52:40 +00:00
[ -z "$1" ] && die "Internal error: no argument given to download"
2018-09-30 05:25:17 +00:00
# shellcheck disable=SC2086
${DOWNLOADER} ${DOWNLOADER_OPTS} "$1"
return $?
}
2018-09-29 07:50:26 +00:00
2018-09-30 06:26:59 +00:00
############################
#--[ Subcommand install ]--#
############################
2018-09-29 07:50:26 +00:00
2018-09-30 05:52:01 +00:00
# @FUNCTION: install_ghc
# @USAGE: <ghcversion>
# @DESCRIPTION:
# Installs the given ghc version with a lot of side effects.
2018-09-29 07:50:26 +00:00
install_ghc() {
2018-09-30 05:52:40 +00:00
[ -z "$1" ] && die "Internal error: no argument given to install_ghc"
2018-09-29 07:50:26 +00:00
myghcver=$1
2018-09-29 18:57:03 +00:00
inst_location=$(get_ghc_location "$1")
2018-09-29 15:12:13 +00:00
download_url=$(get_download_url "${myghcver}")
download_tarball_name=$(basename "${download_url}")
2018-09-29 09:27:01 +00:00
2018-09-29 18:57:03 +00:00
if ghc_already_installed "${myghcver}" ; then
if ${FORCE} ; then
echo "GHC already installed in ${inst_location}, overwriting!"
else
die "GHC already installed in ${inst_location}, use --force to overwrite"
fi
fi
2018-09-29 07:50:26 +00:00
2018-09-29 18:57:03 +00:00
status_message "Installing GHC for $(get_distro_name) on architecture $(get_arch)"
2018-09-29 14:31:08 +00:00
tmp_dir=$(mktemp -d)
[ -z "${tmp_dir}" ] && die "Failed to create temporary directory"
(
2018-09-29 14:31:08 +00:00
edo cd "${tmp_dir}"
2018-09-29 07:50:26 +00:00
2018-09-30 05:25:17 +00:00
edo download "${download_url}"
2018-09-29 07:50:26 +00:00
2018-09-29 14:31:08 +00:00
edo tar -xf ghc-*-linux.tar.xz
edo cd "ghc-${myghcver}"
2018-09-29 07:50:26 +00:00
2018-09-29 18:57:03 +00:00
debug_message "Installing GHC into ${inst_location}"
2018-09-29 14:31:08 +00:00
edo ./configure --prefix="${inst_location}"
edo make install
2018-09-29 09:35:39 +00:00
# clean up
2018-09-29 14:31:08 +00:00
edo cd ..
2018-09-29 15:12:13 +00:00
[ -e "${tmp_dir}/${download_tarball_name}" ] && rm "${tmp_dir}/${download_tarball_name}"
[ -e "${tmp_dir}/ghc-${myghcver}" ] && rm -r "${tmp_dir}/ghc-${myghcver}"
2018-09-29 14:31:08 +00:00
) || {
2018-09-29 15:12:13 +00:00
[ -e "${tmp_dir}/${download_tarball_name}" ] && rm "${tmp_dir}/${download_tarball_name}"
[ -e "${tmp_dir}/ghc-${myghcver}" ] && rm -r "${tmp_dir}/ghc-${myghcver}"
2018-09-29 20:06:53 +00:00
die "Failed to install, consider updating this script via: ${SCRIPT} self-update"
2018-09-29 14:31:08 +00:00
}
2018-09-29 18:57:03 +00:00
[ -e "${BIN_LOCATION}" ] || mkdir "${BIN_LOCATION}"
2018-09-29 15:55:33 +00:00
2018-09-29 13:46:39 +00:00
for f in "${inst_location}"/bin/*-"${myghcver}" ; do
2018-09-29 15:02:00 +00:00
[ -e "${f}" ] || die "Something went wrong, ${f} does not exist!"
2018-09-29 13:46:39 +00:00
fn=$(basename "${f}")
2018-09-29 14:22:54 +00:00
# shellcheck disable=SC2046
2018-09-29 18:57:03 +00:00
edo ln $(optionv "-v") -sf ../ghc/"${myghcver}/bin/${fn}" "${BIN_LOCATION}/${fn}"
2018-09-29 09:27:01 +00:00
unset fn
done
2018-09-29 14:22:54 +00:00
# shellcheck disable=SC2046
2018-09-29 18:57:03 +00:00
edo ln $(optionv "-v") -sf ../ghc/"${myghcver}"/bin/runhaskell "${BIN_LOCATION}/runhaskell-${myghcver}"
2018-09-29 09:27:01 +00:00
2018-09-30 06:33:42 +00:00
status_message "Done installing, run \"ghci-${myghcver}\" or set up your current GHC via: ${SCRIPT} set ${myghcver}"
2018-09-30 05:25:17 +00:00
unset myghcver inst_location f download_url download_tarball_name
}
2018-09-30 06:26:59 +00:00
2018-09-30 06:33:42 +00:00
########################
#--[ Subcommand set ]--#
########################
2018-09-30 06:26:59 +00:00
2018-09-30 05:52:01 +00:00
# @FUNCTION: set_ghc
# @USAGE: <ghcversion>
# @DESCRIPTION:
# Sets the current ghc version by creating symlinks.
set_ghc() {
2018-09-30 05:52:40 +00:00
[ -z "$1" ] && die "Internal error: no argument given to set_ghc"
myghcver=$1
2018-09-29 18:57:03 +00:00
inst_location=$(get_ghc_location "$1")
[ -e "${inst_location}" ] || die "GHC ${myghcver} not installed yet, use: ${SCRIPT} install ${myghcver}"
2018-09-29 18:57:03 +00:00
[ -e "${BIN_LOCATION}" ] || edo mkdir "${BIN_LOCATION}"
2018-09-29 18:57:03 +00:00
status_message "Setting GHC to ${myghcver}"
2018-09-29 13:46:39 +00:00
for f in "${inst_location}"/bin/*-"${myghcver}" ; do
2018-09-29 15:02:00 +00:00
[ -e "${f}" ] || die "Something went wrong, ${f} does not exist!"
2018-09-29 13:46:39 +00:00
source_fn=$(basename "${f}")
target_fn=$(echo "${source_fn}" | sed "s#-${myghcver}##")
2018-09-29 14:22:54 +00:00
# shellcheck disable=SC2046
2018-09-29 18:57:03 +00:00
edo ln $(optionv "-v") -sf ../ghc/"${myghcver}/bin/${source_fn}" "${BIN_LOCATION}/${target_fn}"
unset source_fn target_fn
done
2018-09-29 14:22:54 +00:00
# shellcheck disable=SC2046
2018-09-29 18:57:03 +00:00
edo ln $(optionv "-v") -sf runghc "${BIN_LOCATION}"/runhaskell
2018-09-29 07:50:26 +00:00
2018-09-29 18:57:03 +00:00
status_message "Done, make sure \"${BIN_LOCATION}\" is in your PATH!"
2018-09-29 07:50:26 +00:00
2018-09-29 18:57:03 +00:00
unset myghcver inst_location f
2018-09-29 07:50:26 +00:00
}
2018-09-30 06:26:59 +00:00
################################
#--[ Subcommand self-update ]--#
################################
2018-10-15 09:07:00 +00:00
# @FUNCTION: script_dir
# @DESCRIPTION:
# Portably gets the full directory of where
# this script resides in and prints it to stdout.
# @STDOUT: script directory
script_dir() {
mysource=${SOURCE}
while [ -h "${mysource}" ]; do
mydir="$( cd -P "$( dirname "${mysource}" )" > /dev/null && pwd )"
mysource="$(readlink "${mysource}")"
[ "${mysource%${mysource#?}}"x != '/x' ] && mysource="${mydir}/${mysource}"
done
mydir="$( cd -P "$( dirname "${mysource}" )" > /dev/null && pwd )"
echo "${mydir}"
unset mysource mydir
}
2018-09-30 05:52:01 +00:00
# @FUNCTION: self_update
# @USAGE: <install-location>
# @DESCRIPTION:
# Downloads the latest version of this script and places it into
# the given directory.
self_update() {
target_location=$1
[ -e "${target_location}" ] || die "Destination \"${target_location}\" does not exist, cannot update script"
2018-09-29 18:57:03 +00:00
status_message "Updating ${SCRIPT}"
(
edo cd "$(mktemp -d)"
2018-09-30 05:33:04 +00:00
edo download "${SCRIPT_UPDATE_URL}"
edo chmod +x ghcup
edo mv -f ghcup "${target_location}"/ghcup
2018-09-29 17:33:08 +00:00
) || die "failed to install"
2018-09-29 18:57:03 +00:00
status_message "Done, make sure \"${target_location}\" is in your PATH!"
2018-09-30 05:33:04 +00:00
unset target_location
}
2018-09-30 06:26:59 +00:00
#########################
#--[ Subcommand show ]--#
#########################
2018-09-29 15:01:39 +00:00
2018-09-30 05:52:01 +00:00
# @FUNCTION: show_ghc
# @DESCRIPTION:
# Prints the currently installed and selected GHC, in human-friendly
# format.
2018-09-29 15:01:39 +00:00
show_ghc() {
current_ghc=$(show_ghc_installed)
echo "Installed GHCs:"
2018-09-29 18:57:03 +00:00
for i in "${GHC_LOCATION}"/* ; do
if [ -e "${i}" ] ; then
echo " $(basename "${i}")"
else # directory is empty
echo " None"
exit 0
fi
2018-09-29 15:01:39 +00:00
done
if [ -n "${current_ghc}" ] ; then
echo
echo "Current GHC"
echo " ${current_ghc}"
fi
2018-09-29 18:57:03 +00:00
unset current_ghc i
2018-09-29 15:01:39 +00:00
}
2018-09-30 05:52:01 +00:00
# @FUNCTION: show_ghc_installed
# @DESCRIPTION:
# Prints the currently selected GHC only as version string.
# @STDOUT: current GHC version
2018-09-29 15:01:39 +00:00
show_ghc_installed() {
2018-09-29 19:14:56 +00:00
current_ghc="${BIN_LOCATION}/ghc"
real_ghc=$(realpath "${current_ghc}" 2>/dev/null)
if [ -L "${current_ghc}" ] ; then # is symlink
if [ -e "${real_ghc}" ] ; then # exists (realpath was called)
real_ghc="$(basename "${real_ghc}" | sed 's#ghc-##')"
printf "%s" "${real_ghc}"
else # is a broken symlink
red_message "broken symlink"
fi
2018-09-29 15:01:39 +00:00
fi
2018-09-29 19:14:56 +00:00
unset real_ghc current_ghc
2018-09-29 15:01:39 +00:00
}
2018-09-30 06:26:59 +00:00
#######################
#--[ Subcommand rm ]--#
#######################
2018-09-29 18:58:55 +00:00
2018-09-30 05:52:01 +00:00
# @FUNCTION: rm_ghc
# @USAGE: <ghcversion>
# @DESCRIPTION:
# Removes the given GHC version installed by ghcup.
2018-09-29 18:58:55 +00:00
rm_ghc() {
2018-09-30 05:52:40 +00:00
[ -z "$1" ] && die "Internal error: no argument given to rm_ghc"
2018-09-29 18:58:55 +00:00
myghcver=$1
inst_location=$(get_ghc_location "${myghcver}")
[ -z "${myghcver}" ] && die "We are paranoid, ghcver not set"
if ghc_already_installed "${myghcver}" ; then
for f in "${BIN_LOCATION}"/*-"${myghcver}" ; do
# https://tanguy.ortolo.eu/blog/article113/test-symlink
[ ! -e "${f}" ] && [ ! -h "${f}" ] && die "Something went wrong, ${f} does not exist!"
edo rm "${f}"
done
2018-10-02 17:39:48 +00:00
[ -z "${inst_location}" ] && die "internal error: inst_location empty!"
2018-09-29 18:58:55 +00:00
edo rm -r "${inst_location}"
2018-09-30 08:11:56 +00:00
status_message "Successfully removed GHC ${myghcver}."
if [ ! -e "${BIN_LOCATION}"/ghc ] ; then
warning_message "Currently active GHC is a dangling symlink, run:"
warning_message " ghcup set <ghcver>"
fi
2018-09-29 18:58:55 +00:00
else
warning_message "${myghcver} doesn't appear to be installed, skipping"
fi
unset myghcver inst_location f
}
2018-09-30 08:46:50 +00:00
############################
#--[ Subcommand install ]--#
############################
# @FUNCTION: install_cabal
# @USAGE: <cabalversion>
# @DESCRIPTION:
# Installs the given cabal version.
install_cabal() {
[ -z "$1" ] && die "Internal error: no argument given to install_cabal"
mycabalver=$1
myarch=$(get_arch)
2018-10-05 02:44:23 +00:00
inst_location=$BIN_LOCATION
2018-09-30 08:46:50 +00:00
[ -e "${inst_location}" ] || {
2018-10-05 02:44:23 +00:00
# TODO: this is a bit shaky because we don't use -p
edo mkdir "${INSTALL_BASE}"
edo mkdir "${BIN_LOCATION}"
2018-09-30 08:46:50 +00:00
}
(
edo cd "$(mktemp -d)"
2018-09-30 11:39:40 +00:00
edo download "https://downloads.haskell.org/~cabal/cabal-install-${mycabalver}/cabal-install-${mycabalver}-${myarch}-unknown-linux.tar.gz"
2018-09-30 08:46:50 +00:00
edo tar -xzf "cabal-install-${mycabalver}-${myarch}-unknown-linux.tar.gz"
2018-10-05 02:44:23 +00:00
edo mv -f cabal "${inst_location}"/cabal
2018-09-30 08:46:50 +00:00
) || die "Failed to install cabal-install"
2018-10-05 02:44:23 +00:00
status_message "Successfully installed cabal-install into"
status_message " ${BIN_LOCATION}"
status_message ""
status_message "You may want to run the following to get the really latest version:"
2018-09-30 08:46:50 +00:00
status_message " cabal new-install cabal-install"
2018-10-05 02:44:23 +00:00
status_message ""
status_message "And make sure that \"~/.cabal/bin\" comes *before* \"${BIN_LOCATION}\""
status_message "in your PATH!"
2018-09-30 08:46:50 +00:00
unset mycabalver myarch inst_location
}
2018-09-30 11:39:40 +00:00
# @FUNCTION: compile_ghc
# @USAGE: <ghcversion> <bootstrap-ghc>
# @DESCRIPTION:
# Compile and installs the given GHC version with the
# specified GHC bootstrap version.
compile_ghc() {
{ [ -z "$1" ] || [ -z "$2" ] ;} && die "Internal error: not enough arguments given to compile_ghc"
myghcver=$1
bootstrap_ghc=$2
inst_location=$(get_ghc_location "$1")
download_url="https://downloads.haskell.org/~ghc/${myghcver}/ghc-${myghcver}-src.tar.xz"
download_tarball_name=$(basename "${download_url}")
if ghc_already_installed "${myghcver}" ; then
if ${FORCE} ; then
echo "GHC already installed in ${inst_location}, overwriting!"
else
die "GHC already installed in ${inst_location}, use --force to overwrite"
fi
fi
status_message "Compiling GHC for version ${myghcver} from source"
tmp_dir=$(mktemp -d)
[ -z "${tmp_dir}" ] && die "Failed to create temporary directory"
(
edo cd "${tmp_dir}"
edo download "${download_url}"
edo tar -xf ghc-*-src.tar.xz
edo cd "ghc-${myghcver}"
cat <<-EOF > mk/build.mk || die
BuildFlavour = quick
V=0
BUILD_MAN = NO
BUILD_SPHINX_HTML = NO
BUILD_SPHINX_PDF = NO
HADDOCK_DOCS = YES
GhcWithLlvmCodeGen = YES
EOF
edo ./boot
edo ./configure --prefix="${inst_location}" --with-ghc="${bootstrap_ghc}"
edo make -j${JOBS}
edo make install
# clean up
edo cd ..
[ -e "${tmp_dir}/${download_tarball_name}" ] && rm "${tmp_dir}/${download_tarball_name}"
[ -e "${tmp_dir}/ghc-${myghcver}" ] && rm -r "${tmp_dir}/ghc-${myghcver}"
) || {
[ -e "${tmp_dir}/${download_tarball_name}" ] && rm "${tmp_dir}/${download_tarball_name}"
[ -e "${tmp_dir}/ghc-${myghcver}" ] && rm -r "${tmp_dir}/ghc-${myghcver}"
die "Failed to install, consider updating this script via:
${SCRIPT} self-update
Also check https://ghc.haskell.org/trac/ghc/wiki/Building/Preparation/Linux for build requirements and follow the instructions."
}
[ -e "${BIN_LOCATION}" ] || mkdir "${BIN_LOCATION}"
for f in "${inst_location}"/bin/*-"${myghcver}" ; do
[ -e "${f}" ] || die "Something went wrong, ${f} does not exist!"
fn=$(basename "${f}")
# shellcheck disable=SC2046
edo ln $(optionv "-v") -sf ../ghc/"${myghcver}/bin/${fn}" "${BIN_LOCATION}/${fn}"
unset fn
done
# shellcheck disable=SC2046
edo ln $(optionv "-v") -sf ../ghc/"${myghcver}"/bin/runhaskell "${BIN_LOCATION}/runhaskell-${myghcver}"
status_message "Done installing, run \"ghci-${myghcver}\" or set up your current GHC via: ${SCRIPT} set ${myghcver}"
unset myghcver bootstrap_ghc inst_location f download_url download_tarball_name
}
2018-09-30 08:46:50 +00:00
2018-09-29 18:58:55 +00:00
2018-09-29 07:50:26 +00:00
2018-09-30 06:26:59 +00:00
#######################
#--[ Sanity checks ]--#
#######################
2018-09-29 07:50:26 +00:00
if [ -z "$HOME" ] ; then
die "HOME env not set, cannot operate"
fi
2018-09-30 06:26:59 +00:00
##############################################
#--[ Command line parsing and entry point ]--#
##############################################
[ $# -lt 1 ] && usage
2018-09-29 07:50:26 +00:00
while [ $# -gt 0 ] ; do
case $1 in
-v|--verbose)
VERBOSE=true
2018-09-30 06:36:50 +00:00
shift 1
if [ $# -lt 1 ] ; then
usage
fi
;;
2018-09-29 07:50:26 +00:00
-V|--version)
2018-09-29 13:46:39 +00:00
printf "%s" "${VERSION}"
2018-09-29 07:50:26 +00:00
exit 0;;
-h|--help)
usage;;
*) case $1 in
install)
shift 1
while [ $# -gt 0 ] ; do
case $1 in
-h|--help) install_usage;;
-f|--force) FORCE=true
shift 1;;
2018-09-29 07:50:26 +00:00
*) GHC_VER=$1
break;;
esac
done
[ "${GHC_VER}" ] || install_usage
2018-09-29 13:46:39 +00:00
install_ghc "${GHC_VER}"
2018-09-29 07:50:26 +00:00
break;;
2018-09-29 13:07:57 +00:00
set)
shift 1
while [ $# -gt 0 ] ; do
case $1 in
2018-09-29 13:07:57 +00:00
-h|--help) set_usage;;
*) GHC_VER=$1
break;;
esac
done
2018-09-29 13:07:57 +00:00
[ "${GHC_VER}" ] || set_usage
2018-09-29 13:46:39 +00:00
set_ghc "${GHC_VER}"
break;;
self-update)
shift 1
while [ $# -gt 0 ] ; do
case $1 in
-h|--help) self_update_usage;;
*) TARGET_LOCATION=$1
break;;
esac
done
if [ "${TARGET_LOCATION}" ] ; then
self_update "${TARGET_LOCATION}"
else
2018-10-15 09:07:00 +00:00
self_update "$(script_dir)"
fi
break;;
2018-09-29 15:01:39 +00:00
show)
SHOW_INSTALLED=false
shift 1
while [ $# -gt 0 ] ; do
case $1 in
-h|--help) show_usage;;
-i|--installed) SHOW_INSTALLED=true
break;;
*) show_usage;;
esac
done
if ${SHOW_INSTALLED} ; then
show_ghc_installed
else
show_ghc
fi
break;;
2018-09-29 18:58:55 +00:00
rm)
shift 1
while [ $# -gt 0 ] ; do
case $1 in
-h|--help) rm_usage;;
*) GHC_VER=$1
break;;
esac
done
[ "${GHC_VER}" ] || rm_usage
rm_ghc "${GHC_VER}"
break;;
2018-09-30 08:46:50 +00:00
install-cabal)
shift 1
while [ $# -gt 0 ] ; do
case $1 in
-h|--help) install_cabal_usage;;
-f|--force) FORCE=true
shift 1;;
*) CABAL_VER=$1
break;;
esac
done
if [ "${CABAL_VER}" ] ; then
install_cabal "${CABAL_VER}"
else
install_cabal "${KNOWN_GOOD_CABAL}"
fi
break;;
2018-09-30 11:39:40 +00:00
compile)
shift 1
while [ $# -gt 0 ] ; do
case $1 in
-h|--help) compile_usage;;
-f|--force) FORCE=true
shift 1;;
-j|--jobs) JOBS=$2
shift 2;;
*) GHC_VER=$1
BOOTSTRAP_GHC=$2
break;;
esac
done
[ "${GHC_VER}" ] || compile_usage
[ "${BOOTSTRAP_GHC}" ] || compile_usage
compile_ghc "${GHC_VER}" "${BOOTSTRAP_GHC}"
break;;
2018-09-29 07:50:26 +00:00
*) usage;;
esac
break;;
esac
done
2018-10-02 17:41:24 +00:00
# vim: tabstop=4 shiftwidth=4 expandtab