{-# LANGUAGE CPP #-}
module Test.DocTest.Internal.GhcUtil (withGhc) where
import GHC.Paths (libdir)
import GHC
#if __GLASGOW_HASKELL__ < 900
import DynFlags (gopt_set)
#else
import GHC.Driver.Session (gopt_set)
#endif
#if __GLASGOW_HASKELL__ < 900
import Panic (throwGhcException)
#else
import GHC.Utils.Panic (throwGhcException)
#endif
#if __GLASGOW_HASKELL__ < 900
import MonadUtils (liftIO)
#else
import GHC.Utils.Monad (liftIO)
#endif
import System.Exit (exitFailure)
handleSrcErrors :: Ghc a -> Ghc a
handleSrcErrors :: Ghc a -> Ghc a
handleSrcErrors Ghc a
action' = ((SourceError -> Ghc a) -> Ghc a -> Ghc a)
-> Ghc a -> (SourceError -> Ghc a) -> Ghc a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (SourceError -> Ghc a) -> Ghc a -> Ghc a
forall (m :: * -> *) a.
ExceptionMonad m =>
(SourceError -> m a) -> m a -> m a
handleSourceError Ghc a
action' ((SourceError -> Ghc a) -> Ghc a)
-> (SourceError -> Ghc a) -> Ghc a
forall a b. (a -> b) -> a -> b
$ \SourceError
err -> do
SourceError -> Ghc ()
forall (m :: * -> *). GhcMonad m => SourceError -> m ()
printException SourceError
err
IO a -> Ghc a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
forall a. IO a
exitFailure
withGhc :: [String] -> ([String] -> Ghc a) -> IO a
withGhc :: [String] -> ([String] -> Ghc a) -> IO a
withGhc [String]
flags [String] -> Ghc a
action = do
[Located String]
flags_ <- [String] -> IO [Located String]
handleStaticFlags [String]
flags
Maybe String -> Ghc a -> IO a
forall a. Maybe String -> Ghc a -> IO a
runGhc (String -> Maybe String
forall a. a -> Maybe a
Just String
libdir) (Ghc a -> IO a) -> Ghc a -> IO a
forall a b. (a -> b) -> a -> b
$ do
[Located String] -> Ghc [String]
forall (m :: * -> *). GhcMonad m => [Located String] -> m [String]
handleDynamicFlags [Located String]
flags_ Ghc [String] -> ([String] -> Ghc a) -> Ghc a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ghc a -> Ghc a
forall a. Ghc a -> Ghc a
handleSrcErrors (Ghc a -> Ghc a) -> ([String] -> Ghc a) -> [String] -> Ghc a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> Ghc a
action
handleStaticFlags :: [String] -> IO [Located String]
handleStaticFlags :: [String] -> IO [Located String]
handleStaticFlags [String]
flags = [Located String] -> IO [Located String]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Located String] -> IO [Located String])
-> [Located String] -> IO [Located String]
forall a b. (a -> b) -> a -> b
$ (String -> Located String) -> [String] -> [Located String]
forall a b. (a -> b) -> [a] -> [b]
map String -> Located String
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc ([String] -> [Located String]) -> [String] -> [Located String]
forall a b. (a -> b) -> a -> b
$ [String]
flags
handleDynamicFlags :: GhcMonad m => [Located String] -> m [String]
handleDynamicFlags :: [Located String] -> m [String]
handleDynamicFlags [Located String]
flags = do
#if __GLASGOW_HASKELL__ >= 901
logger <- getLogger
let parseDynamicFlags' = parseDynamicFlags logger
#else
let parseDynamicFlags' :: DynFlags
-> [Located String] -> m (DynFlags, [Located String], [Warn])
parseDynamicFlags' = DynFlags
-> [Located String] -> m (DynFlags, [Located String], [Warn])
forall (m :: * -> *).
MonadIO m =>
DynFlags
-> [Located String] -> m (DynFlags, [Located String], [Warn])
parseDynamicFlags
#endif
DynFlags
dynflags0 <- DynFlags -> DynFlags
setHaddockMode (DynFlags -> DynFlags) -> m DynFlags -> m DynFlags
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m DynFlags
forall (m :: * -> *). GhcMonad m => m DynFlags
getSessionDynFlags
(DynFlags
dynflags1, [Located String]
locSrcs, [Warn]
_) <- DynFlags
-> [Located String] -> m (DynFlags, [Located String], [Warn])
parseDynamicFlags' DynFlags
dynflags0 [Located String]
flags
[InstalledUnitId]
_ <- DynFlags -> m [InstalledUnitId]
forall (m :: * -> *). GhcMonad m => DynFlags -> m [InstalledUnitId]
setSessionDynFlags DynFlags
dynflags1
let srcs :: [String]
srcs = (Located String -> String) -> [Located String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Located String -> String
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc [Located String]
locSrcs
unknown_opts :: [String]
unknown_opts = [ String
f | f :: String
f@(Char
'-':String
_) <- [String]
srcs ]
case [String]
unknown_opts of
String
opt : [String]
_ -> GhcException -> m [String]
forall a. GhcException -> a
throwGhcException (String -> GhcException
UsageError (String
"unrecognized option `"String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
opt String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'"))
[String]
_ -> [String] -> m [String]
forall (m :: * -> *) a. Monad m => a -> m a
return [String]
srcs
setHaddockMode :: DynFlags -> DynFlags
setHaddockMode :: DynFlags -> DynFlags
setHaddockMode DynFlags
dynflags = (DynFlags -> GeneralFlag -> DynFlags
gopt_set DynFlags
dynflags GeneralFlag
Opt_Haddock) {
#if __GLASGOW_HASKELL__ >= 901
backend = NoBackend
#else
hscTarget :: HscTarget
hscTarget = HscTarget
HscNothing
#endif
, ghcMode :: GhcMode
ghcMode = GhcMode
CompManager
, ghcLink :: GhcLink
ghcLink = GhcLink
NoLink
}