module GHC.Check.Executable where
import Data.Version
import GHC.Check.Util
import System.FilePath
import System.Process
import Text.ParserCombinators.ReadP
import Data.Char (isSpace)
import Data.List (dropWhileEnd)
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NonEmpty
getGhcVersion :: FilePath -> IO Version
getGhcVersion fp = do
out <- readProcess fp ["--numeric-version"] ""
case readP_to_S (parseVersion <* eof) (trim out) of
[(v, "")] -> return v
_ -> error $ "Failed to parse GHC version: " <> out
trim :: String -> String
trim = dropWhileEnd isSpace . dropWhile isSpace
guessExecutablePathFromLibdir :: FilePath -> NonEmpty FilePath
guessExecutablePathFromLibdir fp = NonEmpty.fromList
[ fp </> "bin" </> "ghc"
, fp </> ".." </> "bin" </> "ghc.exe"
]