2010-04-30 09:36:31 +00:00
|
|
|
module Types where
|
|
|
|
|
2010-11-12 07:27:50 +00:00
|
|
|
import Control.Monad
|
|
|
|
import DynFlags
|
2010-04-30 09:36:31 +00:00
|
|
|
import Exception
|
|
|
|
import GHC
|
|
|
|
import GHC.Paths (libdir)
|
|
|
|
|
2010-11-12 07:27:50 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2010-04-30 09:36:31 +00:00
|
|
|
data Options = Options {
|
2010-06-14 02:56:35 +00:00
|
|
|
convert :: [String] -> String
|
|
|
|
, hlintOpts :: [String]
|
2011-08-02 18:18:07 +00:00
|
|
|
, checkIncludes :: [String]
|
2011-01-27 05:29:39 +00:00
|
|
|
, operators :: Bool
|
2011-05-27 20:43:52 +00:00
|
|
|
, packageConfs :: [FilePath]
|
|
|
|
, useUserPackageConf :: Bool
|
2010-04-30 09:36:31 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 07:27:50 +00:00
|
|
|
withGHC :: (MonadPlus m) => Ghc (m a) -> IO (m a)
|
2010-04-30 09:36:31 +00:00
|
|
|
withGHC body = ghandle ignore $ runGhc (Just libdir) body
|
|
|
|
where
|
2010-11-12 07:27:50 +00:00
|
|
|
ignore :: (MonadPlus m) => SomeException -> IO (m a)
|
|
|
|
ignore _ = return mzero
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
2010-04-30 09:36:31 +00:00
|
|
|
|
2011-05-27 20:43:52 +00:00
|
|
|
initSession0 :: Options -> Ghc [PackageId]
|
|
|
|
initSession0 opt = getSessionDynFlags >>=
|
|
|
|
setSessionDynFlags . setPackageConfFlags opt
|
2010-11-12 07:27:50 +00:00
|
|
|
|
2011-05-27 20:43:52 +00:00
|
|
|
initSession :: Options -> [String] -> Maybe [FilePath] -> Ghc [PackageId]
|
|
|
|
initSession opt cmdOpts midirs = do
|
2010-11-12 07:27:50 +00:00
|
|
|
dflags <- getSessionDynFlags
|
|
|
|
let opts = map noLoc cmdOpts
|
|
|
|
(dflags',_,_) <- parseDynamicFlags dflags opts
|
2011-05-27 20:43:52 +00:00
|
|
|
setSessionDynFlags $ setPackageConfFlags opt $ setFlags dflags' midirs
|
2010-11-12 07:27:50 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2011-05-24 07:00:47 +00:00
|
|
|
setFlags :: DynFlags -> Maybe [FilePath] -> DynFlags
|
|
|
|
setFlags d midirs = maybe d' (\x -> d' { importPaths = x }) midirs
|
|
|
|
where
|
|
|
|
d' = d {
|
|
|
|
packageFlags = ghcPackage : packageFlags d
|
|
|
|
, ghcLink = NoLink
|
|
|
|
, hscTarget = HscInterpreted
|
|
|
|
}
|
2010-11-12 07:27:50 +00:00
|
|
|
|
|
|
|
ghcPackage :: PackageFlag
|
|
|
|
ghcPackage = ExposePackage "ghc"
|
|
|
|
|
2011-05-27 20:43:52 +00:00
|
|
|
setPackageConfFlags :: Options -> DynFlags -> DynFlags
|
|
|
|
setPackageConfFlags
|
|
|
|
Options { packageConfs = confs, useUserPackageConf = useUser }
|
|
|
|
flagset@DynFlags { extraPkgConfs = extra, flags = origFlags }
|
|
|
|
= flagset { extraPkgConfs = extra', flags = flags' }
|
|
|
|
where
|
|
|
|
extra' = confs ++ extra
|
|
|
|
flags' = if useUser
|
|
|
|
then origFlags
|
|
|
|
else filter (/=Opt_ReadUserPackageConf) origFlags
|
|
|
|
|
2010-11-12 07:27:50 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
setTargetFile :: (GhcMonad m) => String -> m ()
|
|
|
|
setTargetFile file = do
|
|
|
|
target <- guessTarget file Nothing
|
|
|
|
setTargets [target]
|