{-# LANGUAGE ScopedTypeVariables, CPP #-}
module HIE.Bios.Ghc.Api (
initializeFlagsWithCradle
, initializeFlagsWithCradleWithMessage
, G.SuccessFlag(..)
, withDynFlags
) where
import CoreMonad (liftIO)
import GHC (LoadHowMuch(..), GhcMonad)
import DynFlags
import qualified GHC as G
import qualified HscMain as G
import qualified GhcMake as G
import Control.Monad (void)
import HIE.Bios.Types
import HIE.Bios.Environment
import HIE.Bios.Flags
initializeFlagsWithCradle ::
GhcMonad m
=> FilePath
-> Cradle a
-> m (CradleLoadResult (m G.SuccessFlag, ComponentOptions))
initializeFlagsWithCradle :: FilePath
-> Cradle a
-> m (CradleLoadResult (m SuccessFlag, ComponentOptions))
initializeFlagsWithCradle = Maybe Messager
-> FilePath
-> Cradle a
-> m (CradleLoadResult (m SuccessFlag, ComponentOptions))
forall (m :: * -> *) a.
GhcMonad m =>
Maybe Messager
-> FilePath
-> Cradle a
-> m (CradleLoadResult (m SuccessFlag, ComponentOptions))
initializeFlagsWithCradleWithMessage (Messager -> Maybe Messager
forall a. a -> Maybe a
Just Messager
G.batchMsg)
initializeFlagsWithCradleWithMessage ::
GhcMonad m
=> Maybe G.Messager
-> FilePath
-> Cradle a
-> m (CradleLoadResult (m G.SuccessFlag, ComponentOptions))
initializeFlagsWithCradleWithMessage :: Maybe Messager
-> FilePath
-> Cradle a
-> m (CradleLoadResult (m SuccessFlag, ComponentOptions))
initializeFlagsWithCradleWithMessage msg :: Maybe Messager
msg fp :: FilePath
fp cradle :: Cradle a
cradle =
(ComponentOptions -> (m SuccessFlag, ComponentOptions))
-> CradleLoadResult ComponentOptions
-> CradleLoadResult (m SuccessFlag, ComponentOptions)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Messager
-> ComponentOptions -> (m SuccessFlag, ComponentOptions)
forall (m :: * -> *).
GhcMonad m =>
Maybe Messager
-> ComponentOptions -> (m SuccessFlag, ComponentOptions)
initSessionWithMessage Maybe Messager
msg) (CradleLoadResult ComponentOptions
-> CradleLoadResult (m SuccessFlag, ComponentOptions))
-> m (CradleLoadResult ComponentOptions)
-> m (CradleLoadResult (m SuccessFlag, ComponentOptions))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (CradleLoadResult ComponentOptions)
-> m (CradleLoadResult ComponentOptions)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> Cradle a -> IO (CradleLoadResult ComponentOptions)
forall a.
FilePath -> Cradle a -> IO (CradleLoadResult ComponentOptions)
getCompilerOptions FilePath
fp Cradle a
cradle)
initSessionWithMessage :: (GhcMonad m)
=> Maybe G.Messager
-> ComponentOptions
-> (m G.SuccessFlag, ComponentOptions)
initSessionWithMessage :: Maybe Messager
-> ComponentOptions -> (m SuccessFlag, ComponentOptions)
initSessionWithMessage msg :: Maybe Messager
msg compOpts :: ComponentOptions
compOpts = (do
[Target]
targets <- ComponentOptions -> m [Target]
forall (m :: * -> *). GhcMonad m => ComponentOptions -> m [Target]
initSession ComponentOptions
compOpts
[Target] -> m ()
forall (m :: * -> *). GhcMonad m => [Target] -> m ()
G.setTargets [Target]
targets
ModuleGraph
mod_graph <- [ModuleName] -> Bool -> m ModuleGraph
forall (m :: * -> *).
GhcMonad m =>
[ModuleName] -> Bool -> m ModuleGraph
G.depanal [] Bool
True
LoadHowMuch -> Maybe Messager -> ModuleGraph -> m SuccessFlag
forall (m :: * -> *).
GhcMonad m =>
LoadHowMuch -> Maybe Messager -> ModuleGraph -> m SuccessFlag
G.load' LoadHowMuch
LoadAllTargets Maybe Messager
msg ModuleGraph
mod_graph, ComponentOptions
compOpts)
withDynFlags ::
(GhcMonad m)
=> (DynFlags -> DynFlags) -> m a -> m a
withDynFlags :: (DynFlags -> DynFlags) -> m a -> m a
withDynFlags setFlag :: DynFlags -> DynFlags
setFlag body :: m a
body = m DynFlags -> (DynFlags -> m ()) -> (DynFlags -> m a) -> m a
forall (m :: * -> *) a b c.
ExceptionMonad m =>
m a -> (a -> m b) -> (a -> m c) -> m c
G.gbracket m DynFlags
setup DynFlags -> m ()
teardown (\_ -> m a
body)
where
setup :: m DynFlags
setup = do
DynFlags
dflag <- m DynFlags
forall (m :: * -> *). GhcMonad m => m DynFlags
G.getSessionDynFlags
m [InstalledUnitId] -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m [InstalledUnitId] -> m ()) -> m [InstalledUnitId] -> m ()
forall a b. (a -> b) -> a -> b
$ DynFlags -> m [InstalledUnitId]
forall (m :: * -> *). GhcMonad m => DynFlags -> m [InstalledUnitId]
G.setSessionDynFlags (DynFlags -> DynFlags
setFlag DynFlags
dflag)
DynFlags -> m DynFlags
forall (m :: * -> *) a. Monad m => a -> m a
return DynFlags
dflag
teardown :: DynFlags -> m ()
teardown = m [InstalledUnitId] -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m [InstalledUnitId] -> m ())
-> (DynFlags -> m [InstalledUnitId]) -> DynFlags -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> m [InstalledUnitId]
forall (m :: * -> *). GhcMonad m => DynFlags -> m [InstalledUnitId]
G.setSessionDynFlags