Skip to content

Commit

Permalink
Allow to set ghcup msys2 environment
Browse files Browse the repository at this point in the history
Fixes #982
  • Loading branch information
hasufell committed Jan 29, 2024
1 parent 318ac21 commit f0694c1
Show file tree
Hide file tree
Showing 7 changed files with 802 additions and 715 deletions.
3 changes: 2 additions & 1 deletion docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ This is the complete list of env variables that change GHCup behavior:
On windows, there's additionally:

* `GHCUP_MSYS2`: Has to point to the root of an existing MSYS2 installation (when installed by GHCup, that's e.g. `C:\ghcup\msys64`). GHCup bootstrap takes care of this usually.
* `GHCUP_MSYS2_ENV`: The [MSYS2 environment](https://www.msys2.org/docs/environments/) to use when executing e.g. `ghcup run --mingw-path`. Possible values are `MSYS`, `UCRT64`, `CLANG64`, `CLANGARM64`, `CLANG32`, `MINGW64`, `MINGW32`. Defauts to MINGW64, MINGW32 or CLANGARM64, depending on the architecture.

### XDG support

Expand Down Expand Up @@ -508,7 +509,7 @@ See `ghcup compile ghc --help` for further information.

Since ghcup version 0.1.20.0, we provide cross bindists for GHC JS and WASM. These can be installed conveniently.
However, these are intended as a developer preview only. By using these GHC variants, you are implicitly signing up to participate in GHC development!
If you run into bugs or missing behavior, join the dev chat at https://matrix.to/#/#GHC:matrix.org.
If you run into bugs or missing behavior, join the dev chat at https://matrix.to/#/#GHC:matrix.org.

First, add the cross release channel:

Expand Down
14 changes: 2 additions & 12 deletions lib/GHCup/Prelude/Process/Windows.hs
Original file line number Diff line number Diff line change
Expand Up @@ -263,24 +263,14 @@ createProcessWithMingwPath :: MonadIO m
=> CreateProcess
-> m CreateProcess
createProcessWithMingwPath cp = do
msys2Dir <- liftIO ghcupMsys2Dir
cEnv <- Map.fromList <$> maybe (liftIO getEnvironment) pure (env cp)
let mingWPaths = [msys2Dir </> "mingw64" </> "bin"
,msys2Dir </> "usr" </> "bin"
]
paths = ["PATH", "Path"]
mingWPaths <- liftIO ghcupMsys2BinDirs'
let paths = ["PATH", "Path"]
curPaths = (\x -> maybe [] splitSearchPath (Map.lookup x cEnv)) =<< paths
newPath = intercalate [searchPathSeparator] (mingWPaths ++ curPaths)
envWithoutPath = foldr (\x y -> Map.delete x y) cEnv paths
envWithNewPath = Map.insert "Path" newPath envWithoutPath
liftIO $ setEnv "Path" newPath
pure $ cp { env = Just $ Map.toList envWithNewPath }

ghcupMsys2Dir :: IO FilePath
ghcupMsys2Dir =
lookupEnv "GHCUP_MSYS2" >>= \case
Just fp -> pure fp
Nothing -> do
baseDir <- liftIO ghcupBaseDir
pure (fromGHCupPath baseDir </> "msys64")

12 changes: 12 additions & 0 deletions lib/GHCup/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,23 @@ data Dirs = Dirs
, dbDir :: GHCupPath
, recycleDir :: GHCupPath -- mainly used on windows
, tmpDir :: GHCupPath
, msys2Dir :: FilePath
}
deriving (Show, GHC.Generic)

instance NFData Dirs

data MSYS2Env = MSYS
| UCRT64
| CLANG64
| CLANGARM64
| CLANG32
| MINGW64
| MINGW32
deriving (Eq, Show, Ord, GHC.Generic, Read)

instance NFData MSYS2Env

data KeepDirs = Always
| Errors
| Never
Expand Down
2 changes: 1 addition & 1 deletion lib/GHCup/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ ensureShimGen

-- | Ensure ghcup directory structure exists.
ensureDirectories :: Dirs -> IO ()
ensureDirectories (Dirs baseDir binDir cacheDir logsDir confDir trashDir dbDir tmpDir) = do
ensureDirectories (Dirs baseDir binDir cacheDir logsDir confDir trashDir dbDir tmpDir _) = do
createDirRecursive' (fromGHCupPath baseDir)
createDirRecursive' (fromGHCupPath baseDir </> "ghc")
createDirRecursive' (fromGHCupPath baseDir </> "hls")
Expand Down
46 changes: 46 additions & 0 deletions lib/GHCup/Utils/Dirs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module GHCup.Utils.Dirs
, getConfigFilePath'
, useXDG
, cleanupTrash
, ghcupMsys2BinDirs
, ghcupMsys2BinDirs'

, GHCupPath
, appendGHCupPath
Expand Down Expand Up @@ -136,6 +138,7 @@ import GHC.IO.Exception ( IOErrorType(NoSuchThing) )
import Haskus.Utils.Variant.Excepts
import Optics hiding ( uncons )
import Safe
import System.Info
import System.Directory hiding ( removeDirectory
, removeDirectoryRecursive
, removePathForcibly
Expand Down Expand Up @@ -338,6 +341,48 @@ ghcupTMPDir
else ghcupBaseDir <&> (\(GHCupPath gp) -> GHCupPath (gp </> "tmp"))


ghcupMsys2Dir :: IO FilePath
ghcupMsys2Dir =
lookupEnv "GHCUP_MSYS2" >>= \case
Just fp -> pure fp
Nothing -> do
baseDir <- liftIO ghcupBaseDir
pure (fromGHCupPath baseDir </> "msys64")

ghcupMsys2BinDirs :: (MonadFail m, MonadIO m, MonadReader env m, HasDirs env) => m [FilePath]
ghcupMsys2BinDirs = do
Dirs{..} <- getDirs
liftIO $ ghcupMsys2BinDirs_ msys2Dir

ghcupMsys2BinDirs' :: IO [FilePath]
ghcupMsys2BinDirs' = do
msys2Dir <- ghcupMsys2Dir
ghcupMsys2BinDirs_ msys2Dir

ghcupMsys2BinDirs_ :: FilePath -> IO [FilePath]
ghcupMsys2BinDirs_ msys2Dir' = do
env <- liftIO (lookupEnv "GHCUP_MSYS2_ENV") >>= \case
Just env -> maybe (fail parseFailMsg) pure $ readMay @MSYS2Env env
Nothing
| "x86_64" <- arch -> pure MINGW64
| "i386" <- arch -> pure MINGW32
| "aarch64" <- arch -> pure CLANGARM64
| otherwise -> fail "No compatible architecture for msys2"
pure [msys2Dir' </> toEnvDir env, msys2Dir' </> toEnvDir MSYS]
where
-- https://www.msys2.org/docs/environments/
toEnvDir :: MSYS2Env -> FilePath
toEnvDir MSYS = "usr"
toEnvDir UCRT64 = "ucrt64"
toEnvDir CLANG64 = "clang64"
toEnvDir CLANGARM64 = "clangarm64"
toEnvDir CLANG32 = "clang32"
toEnvDir MINGW64 = "mingw64"
toEnvDir MINGW32 = "mingw32"

parseFailMsg = "Invalid value for GHCUP_MSYS2_ENV. Valid values are: MSYS, UCRT64, CLANG64, CLANGARM64, CLANG32, MINGW64, MINGW32"


getAllDirs :: IO Dirs
getAllDirs = do
baseDir <- ghcupBaseDir
Expand All @@ -348,6 +393,7 @@ getAllDirs = do
recycleDir <- ghcupRecycleDir
tmpDir <- ghcupTMPDir
dbDir <- ghcupDbDir
msys2Dir <- ghcupMsys2Dir
pure Dirs { .. }


Expand Down
49 changes: 40 additions & 9 deletions scripts/bootstrap/bootstrap-haskell
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@
# * BOOTSTRAP_HASKELL_ADJUST_CABAL_CONFIG - whether to adjust mingw paths in cabal.config on windows
# * BOOTSTRAP_HASKELL_DOWNLOADER - which downloader to use (default: curl)
# * GHCUP_BASE_URL - the base url for ghcup binary download (use this to overwrite https://downloads.haskell.org/~ghcup with a mirror)
# * GHCUP_MSYS2_ENV - the msys2 environment to use on windows, see https://www.msys2.org/docs/environments/ (defauts to MINGW64, MINGW32 or CLANGARM64, depending on the architecture)

# License: LGPL-3.0


# safety subshell to avoid executing anything in case this script is not downloaded properly
(

die() {
if [ -n "${NO_COLOR}" ] ; then
(>&2 printf "%s\\n" "$1")
else
(>&2 printf "\\033[0;31m%s\\033[0m\\n" "$1")
fi
exit 2
}

plat="$(uname -s)"
arch=$(uname -m)
ghver="0.1.20.0"
Expand Down Expand Up @@ -55,18 +65,39 @@ case "${plat}" in
;;
esac

case "${GHCUP_MSYS2_ENV}" in
"")
case "${arch}" in
x86_64|amd64)
GHCUP_MSYS2_ENV_DIR="mingw64" ;;
i*86)
GHCUP_MSYS2_ENV_DIR="mingw32" ;;
aarch64|arm64)
GHCUP_MSYS2_ENV_DIR="clangarm64" ;;
*) die "Unknown architecture: ${arch}" ;;
esac
MSYS)
GHCUP_MSYS2_ENV_DIR="usr" ;;
UCRT64)
GHCUP_MSYS2_ENV_DIR="ucrt64" ;;
CLANG64)
GHCUP_MSYS2_ENV_DIR="clang64" ;;
CLANGARM64)
GHCUP_MSYS2_ENV_DIR="clangarm64" ;;
CLANG32)
GHCUP_MSYS2_ENV_DIR="clang32" ;;
MINGW64)
GHCUP_MSYS2_ENV_DIR="mingw64" ;;
MINGW32)
GHCUP_MSYS2_ENV_DIR="mingw32" ;;
*)
die "Invalid value for GHCUP_MSYS2_ENV. Valid values are: MSYS, UCRT64, CLANG64, CLANGARM64, CLANG32, MINGW64, MINGW32" ;;
esac

: "${BOOTSTRAP_HASKELL_GHC_VERSION:=recommended}"
: "${BOOTSTRAP_HASKELL_CABAL_VERSION:=recommended}"


die() {
if [ -n "${NO_COLOR}" ] ; then
(>&2 printf "%s\\n" "$1")
else
(>&2 printf "\\033[0;31m%s\\033[0m\\n" "$1")
fi
exit 2
}

warn() {
if [ -n "${NO_COLOR}" ] ; then
Expand Down Expand Up @@ -591,7 +622,7 @@ adjust_cabal_config() {
else
cabal_bin="$HOME/AppData/Roaming/cabal/bin"
fi
ecabal user-config -a "extra-prog-path: $(cygpath -w "$GHCUP_BIN"), $(cygpath -w "$cabal_bin"), $(cygpath -w "$GHCUP_MSYS2"/mingw64/bin), $(cygpath -w "$GHCUP_MSYS2"/usr/bin)" -a "extra-include-dirs: $(cygpath -w "$GHCUP_MSYS2"/mingw64/include)" -a "extra-lib-dirs: $(cygpath -w "$GHCUP_MSYS2"/mingw64/lib)" -f init
ecabal user-config -a "extra-prog-path: $(cygpath -w "$GHCUP_BIN"), $(cygpath -w "$cabal_bin"), $(cygpath -w "$GHCUP_MSYS2"/${GHCUP_MSYS2_ENV_DIR}/bin), $(cygpath -w "$GHCUP_MSYS2"/usr/bin)" -a "extra-include-dirs: $(cygpath -w "$GHCUP_MSYS2"/${GHCUP_MSYS2_ENV_DIR}/include)" -a "extra-lib-dirs: $(cygpath -w "$GHCUP_MSYS2"/${GHCUP_MSYS2_ENV_DIR}/lib)" -f init
}

ask_cabal_config_init() {
Expand Down
Loading

0 comments on commit f0694c1

Please sign in to comment.