{-# LANGUAGE ScopedTypeVariables, CPP #-}
module HIE.Bios.Ghc.Api (
initializeFlagsWithCradle
, initializeFlagsWithCradleWithMessage
, G.SuccessFlag(..)
, withGHC
, withGHC'
, withGhcT
, getSystemLibDir
, withDynFlags
) where
import CoreMonad (liftIO)
import Exception (ghandle, SomeException(..), ExceptionMonad(..))
import GHC (Ghc, LoadHowMuch(..), GhcMonad, GhcT)
import DynFlags
import qualified GHC as G
import qualified MonadUtils as G
import qualified HscMain as G
import qualified GhcMake as G
import Control.Monad (void)
import System.Exit (exitSuccess)
import HIE.Bios.Types
import qualified HIE.Bios.Internal.Log as Log
import HIE.Bios.Environment
import HIE.Bios.Flags
withGHC :: FilePath
-> Ghc a
-> IO a
withGHC file body = ghandle ignore $ withGHC' body
where
ignore :: SomeException -> IO a
ignore e = do
Log.logm $ file ++ ":0:0:Error:"
Log.logm (show e)
exitSuccess
withGHC' :: Ghc a -> IO a
withGHC' body = do
mlibdir <- getSystemLibDir
G.runGhc mlibdir body
withGhcT :: (Exception.ExceptionMonad m, G.MonadIO m, Monad m) => GhcT m a -> m a
withGhcT body = do
mlibdir <- G.liftIO $ getSystemLibDir
G.runGhcT mlibdir body
initializeFlagsWithCradle ::
GhcMonad m
=> FilePath
-> Cradle
-> m (CradleLoadResult (m G.SuccessFlag))
initializeFlagsWithCradle = initializeFlagsWithCradleWithMessage (Just G.batchMsg)
initializeFlagsWithCradleWithMessage ::
GhcMonad m
=> Maybe G.Messager
-> FilePath
-> Cradle
-> m (CradleLoadResult (m G.SuccessFlag))
initializeFlagsWithCradleWithMessage msg fp cradle =
fmap (initSessionWithMessage msg) <$> (liftIO $ getCompilerOptions fp cradle)
initSessionWithMessage :: (GhcMonad m)
=> Maybe G.Messager
-> ComponentOptions
-> m G.SuccessFlag
initSessionWithMessage msg compOpts = do
targets <- initSession compOpts
G.setTargets targets
mod_graph <- G.depanal [] True
G.load' LoadAllTargets msg mod_graph
withDynFlags ::
(GhcMonad m)
=> (DynFlags -> DynFlags) -> m a -> m a
withDynFlags setFlag body = G.gbracket setup teardown (\_ -> body)
where
setup = do
dflag <- G.getSessionDynFlags
void $ G.setSessionDynFlags (setFlag dflag)
return dflag
teardown = void . G.setSessionDynFlags