ghc-mod/GHCApi.hs

113 lines
3.6 KiB
Haskell
Raw Normal View History

2012-02-14 07:09:53 +00:00
module GHCApi where
2013-03-03 06:47:03 +00:00
import CabalApi
2012-02-15 05:52:48 +00:00
import Control.Applicative
import Control.Exception
2012-02-14 07:09:53 +00:00
import CoreMonad
2013-03-02 07:14:55 +00:00
import Data.Maybe (isJust)
2012-02-14 07:09:53 +00:00
import DynFlags
import ErrMsg
import Exception
import GHC
import GHC.Paths (libdir)
import HeaderInfo
import System.Exit
import System.IO
2012-02-14 07:09:53 +00:00
import Types
----------------------------------------------------------------
2012-02-15 05:52:48 +00:00
withGHC :: Alternative m => Ghc (m a) -> IO (m a)
2012-12-07 05:27:02 +00:00
withGHC = withGHC' "Dummy"
withGHC' :: Alternative m => FilePath -> Ghc (m a) -> IO (m a)
withGHC' file body = ghandle ignore $ runGhc (Just libdir) $ do
2012-02-14 10:14:21 +00:00
dflags <- getSessionDynFlags
defaultCleanupHandler dflags body
2012-02-14 07:09:53 +00:00
where
2012-02-15 05:52:48 +00:00
ignore :: Alternative m => SomeException -> IO (m a)
ignore e = do
2012-12-07 05:27:02 +00:00
hPutStr stderr $ file ++ ":0:0:Error:"
hPrint stderr e
2012-06-04 06:40:26 +00:00
exitSuccess
2012-02-14 07:09:53 +00:00
----------------------------------------------------------------
initSession0 :: Options -> Ghc [PackageId]
initSession0 opt = getSessionDynFlags >>=
(>>= setSessionDynFlags) . setGhcFlags opt
----------------------------------------------------------------
importDirs :: [IncludeDir]
importDirs = [".","..","../..","../../..","../../../..","../../../../.."]
2013-03-02 07:14:55 +00:00
initializeGHC :: Options -> Cradle -> FilePath -> [GHCOption] -> Bool -> Ghc LogReader
initializeGHC opt cradle fileName ghcOptions logging
| cabal =
initSession opt ghcOptions importDirs Nothing Nothing logging fileName
| otherwise = do
2013-03-03 06:50:09 +00:00
(gopts,idirs,depPkgs,hdrExts) <- liftIO $ fromCabalFile ghcOptions cradle
2013-03-02 07:14:55 +00:00
initSession opt gopts idirs (Just depPkgs) (Just hdrExts) logging fileName
where
2013-03-02 07:14:55 +00:00
cabal = isJust $ cradleCabalFile cradle
initSession :: Options
-> [GHCOption]
-> [IncludeDir]
-> Maybe [Package]
-> Maybe [LangExt]
-> Bool
-> FilePath
-> Ghc LogReader
initSession opt cmdOpts idirs mDepPkgs mLangExts logging file = do
2012-02-14 07:09:53 +00:00
dflags <- getSessionDynFlags
hdrExts <- liftIO $ map unLoc <$> getOptionsFromFile dflags file
let th = useTemplateHaskell mLangExts hdrExts
opts = map noLoc cmdOpts
2012-02-14 07:09:53 +00:00
(dflags',_,_) <- parseDynamicFlags dflags opts
(dflags'',readLog) <- liftIO . (>>= setLogger logging)
. setGhcFlags opt . setFlags opt dflags' idirs mDepPkgs $ th
2012-08-06 00:43:47 +00:00
_ <- setSessionDynFlags dflags''
2012-02-14 07:09:53 +00:00
return readLog
useTemplateHaskell :: Maybe [LangExt] -> [HeaderExt] -> Bool
useTemplateHaskell mLangExts hdrExts = th1 || th2
where
th1 = "-XTemplateHaskell" `elem` hdrExts
th2 = maybe False ("TemplateHaskell" `elem`) mLangExts
2012-02-14 07:09:53 +00:00
----------------------------------------------------------------
setFlags :: Options -> DynFlags -> [IncludeDir] -> Maybe [Package] -> Bool -> DynFlags
setFlags opt d idirs mDepPkgs th
2012-02-27 03:37:11 +00:00
| expandSplice opt = dopt_set d' Opt_D_dump_splices
| otherwise = d'
2012-02-14 07:09:53 +00:00
where
d' = addDevPkgs mDepPkgs $ d {
importPaths = idirs
, ghcLink = if th then LinkInMemory else NoLink
, hscTarget = if th then HscInterpreted else HscNothing
, flags = flags d
2012-02-14 07:09:53 +00:00
}
addDevPkgs :: Maybe [Package] -> DynFlags -> DynFlags
addDevPkgs Nothing df = df
addDevPkgs (Just pkgs) df = df' {
packageFlags = map ExposePackage pkgs ++ packageFlags df
}
where
df' = dopt_set df Opt_HideAllPackages
2012-02-14 07:09:53 +00:00
setGhcFlags :: Monad m => Options -> DynFlags -> m DynFlags
setGhcFlags opt flagset =
do (flagset',_,_) <- parseDynamicFlags flagset (map noLoc (ghcOpts opt))
return flagset'
----------------------------------------------------------------
setTargetFile :: (GhcMonad m) => String -> m ()
setTargetFile file = do
target <- guessTarget file Nothing
setTargets [target]