ghc-wrapper 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/bash -e
  2. # ghc wrapper script (for managing installed GHC versions)
  3. # this is a small script I use that allows multiple simultaneous ghc
  4. # installations. This makes the following assumptions about how
  5. # you want to set up your system:
  6. # - GHC version {X} is installed with prefix ~/install/ghc-${X}
  7. # - A file naming the current selected GHC version is placed
  8. # at ~/.current-ghc
  9. # - cabal is configured to point to this script instead of ghc
  10. # - If using fetch, then mktemp must result in a directory to which
  11. # this script can write, as well
  12. # This script is being run as a user that can read and modify things
  13. # in your $HOME directory.
  14. if [[ "$1" = "list" ]]; then
  15. for f in $(ls ~/install/ | grep ghc); do
  16. # It's possible (for various reasons) for /none/ of the
  17. # current versions to be selected, in which case all of
  18. # them will be prefixed by a -; otherwise, the one that
  19. # is selected will be prefixed by a +.
  20. if [[ "$f" = "$(cat ~/.current-ghc)" ]]; then
  21. echo "+" $f
  22. else
  23. echo "-" $f
  24. fi
  25. done
  26. elif [[ "$1" = "set" ]]; then
  27. MATCHES=($(ls ~/install | grep ghc | grep -e "$2"))
  28. if [[ -z $MATCHES ]]; then
  29. # There is nothing in ~/install that matches that version
  30. echo "Unknown GHC version: $2"
  31. elif [ "${#MATCHES[@]}" -eq "1" ]; then
  32. # There is exactly one relevant match---we can use that one!
  33. echo "Setting GHC version to $MATCHES"
  34. echo $MATCHES >~/.current-ghc
  35. else
  36. # The regex given matches too many versions.
  37. echo "Ambiguous GHC version: argument matches"
  38. for f in "${MATCHES[@]}"; do
  39. echo " " $f
  40. done
  41. fi
  42. elif [[ "$1" = "fetch" ]]; then
  43. VERSION="$2"
  44. URL="https://www.haskell.org/ghc/dist/$VERSION/ghc-$VERSION-src.tar.xz"
  45. LOC=$(mktemp -d)
  46. (
  47. echo "Attempting to fetch $URL..." &&
  48. curl -L "$URL" >"$LOC/ghc.tar.xz" &&
  49. cd "$LOC" &&
  50. echo "...tarball fetched; unpacking..." &&
  51. tar -xf "ghc.tar.xz" &&
  52. cd "$LOC/ghc-$VERSION" &&
  53. echo "Creating $HOME/install/ghc-$VERSION" &&
  54. mkdir -p "$HOME/install/ghc-$VERSION" &&
  55. ./configure --prefix="$HOME/install/ghc-$VERSION" &&
  56. echo "... starting build process..." &&
  57. make &&
  58. make install &&
  59. echo "...finished!"
  60. )
  61. elif [[ "$1" = "--wrapper-help" ]]; then
  62. cat <<EOF
  63. $0 [wrapper script]
  64. Usage:
  65. $0 list List installed versions of ghc
  66. $0 set [regex] Set current GHC to the version that uniquely
  67. matches the supplied regex
  68. $0 fetch [version] Fetch, build, and install the specified version
  69. of GHC. (Does not change the current version.)
  70. $0 --wrapper-help See this help menu
  71. $0 [anything else] Use GHC normally
  72. This wrapper script assumes that you'll always install your GHC versions
  73. to ~/install/ghc-{VERSION}, and will store the currently selected GHC version
  74. in ~/.current-ghc.
  75. WARNING: This script is quite brittle and makes a lot of assumptions about
  76. how you lay things out! While I suspect it shouldn't actively break anything,
  77. be careful about using it!
  78. EOF
  79. else
  80. exec /home/gdritter/install/$(cat ~/.current-ghc)/bin/$(basename $0) "$@"
  81. fi