2014-07-16 17:01:43 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2014-07-12 00:53:59 +00:00
|
|
|
module Language.Haskell.GhcMod.DynFlags where
|
|
|
|
|
|
|
|
import Control.Applicative ((<$>))
|
2014-07-18 05:05:20 +00:00
|
|
|
import Control.Monad (void)
|
|
|
|
import GHC (DynFlags(..), GhcMode(..), GhcLink(..), HscTarget(..))
|
2014-07-12 00:53:59 +00:00
|
|
|
import qualified GHC as G
|
|
|
|
import GHC.Paths (libdir)
|
2014-07-18 01:52:57 +00:00
|
|
|
import GhcMonad
|
|
|
|
import qualified Language.Haskell.GhcMod.Gap as Gap
|
|
|
|
import Language.Haskell.GhcMod.Types
|
2014-07-12 00:53:59 +00:00
|
|
|
import System.IO.Unsafe (unsafePerformIO)
|
|
|
|
|
|
|
|
data Build = CabalPkg | SingleFile deriving Eq
|
|
|
|
|
|
|
|
setEmptyLogger :: DynFlags -> DynFlags
|
|
|
|
setEmptyLogger df = Gap.setLogAction df $ \_ _ _ _ _ -> return ()
|
|
|
|
|
2014-08-29 15:21:38 +00:00
|
|
|
-- * Fast
|
|
|
|
-- * Friendly to foreign export
|
|
|
|
-- * Not friendly to -XTemplateHaskell and -XPatternSynonyms
|
|
|
|
-- * Uses little memory
|
2014-07-15 08:20:35 +00:00
|
|
|
setModeSimple :: DynFlags -> DynFlags
|
|
|
|
setModeSimple df = df {
|
|
|
|
ghcMode = CompManager
|
|
|
|
, ghcLink = NoLink
|
|
|
|
, hscTarget = HscNothing
|
2014-07-15 12:34:05 +00:00
|
|
|
, optLevel = 0
|
2014-07-15 08:20:35 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 15:21:38 +00:00
|
|
|
-- * Slow
|
|
|
|
-- * Not friendly to foreign export
|
|
|
|
-- * Friendly to -XTemplateHaskell and -XPatternSynonyms
|
|
|
|
-- * Uses lots of memory
|
2014-07-15 08:20:35 +00:00
|
|
|
setModeIntelligent :: DynFlags -> DynFlags
|
|
|
|
setModeIntelligent df = df {
|
2014-07-15 03:06:07 +00:00
|
|
|
ghcMode = CompManager
|
|
|
|
, ghcLink = LinkInMemory
|
2014-07-12 00:53:59 +00:00
|
|
|
, hscTarget = HscInterpreted
|
2014-07-15 12:34:05 +00:00
|
|
|
, optLevel = 0
|
2014-07-12 00:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setIncludeDirs :: [IncludeDir] -> DynFlags -> DynFlags
|
|
|
|
setIncludeDirs idirs df = df { importPaths = idirs }
|
|
|
|
|
|
|
|
setBuildEnv :: Build -> DynFlags -> DynFlags
|
|
|
|
setBuildEnv build = setHideAllPackages build . setCabalPackage build
|
|
|
|
|
2014-08-29 15:21:38 +00:00
|
|
|
-- | With ghc-7.8 this option simply makes GHC print a message suggesting users
|
|
|
|
-- add hiddend packages to the build-depends field in their cabal file when the
|
|
|
|
-- user tries to import a module form a hidden package.
|
2014-07-12 00:53:59 +00:00
|
|
|
setCabalPackage :: Build -> DynFlags -> DynFlags
|
|
|
|
setCabalPackage CabalPkg df = Gap.setCabalPkg df
|
|
|
|
setCabalPackage _ df = df
|
|
|
|
|
|
|
|
-- | Enable hiding of all package not explicitly exposed (like Cabal does)
|
|
|
|
setHideAllPackages :: Build -> DynFlags -> DynFlags
|
|
|
|
setHideAllPackages CabalPkg df = Gap.setHideAllPackages df
|
|
|
|
setHideAllPackages _ df = df
|
|
|
|
|
|
|
|
-- | Parse command line ghc options and add them to the 'DynFlags' passed
|
|
|
|
addCmdOpts :: GhcMonad m => [GHCOption] -> DynFlags -> m DynFlags
|
|
|
|
addCmdOpts cmdOpts df =
|
|
|
|
tfst <$> G.parseDynamicFlags df (map G.noLoc cmdOpts)
|
|
|
|
where
|
|
|
|
tfst (a,_,_) = a
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Return the 'DynFlags' currently in use in the GHC session.
|
|
|
|
getDynamicFlags :: IO DynFlags
|
2014-07-17 08:16:44 +00:00
|
|
|
getDynamicFlags = G.runGhc (Just libdir) G.getSessionDynFlags
|
2014-07-12 00:53:59 +00:00
|
|
|
|
|
|
|
withDynFlags :: GhcMonad m
|
|
|
|
=> (DynFlags -> DynFlags)
|
|
|
|
-> m a
|
|
|
|
-> m a
|
|
|
|
withDynFlags setFlags body = G.gbracket setup teardown (\_ -> body)
|
|
|
|
where
|
|
|
|
setup = do
|
|
|
|
dflags <- G.getSessionDynFlags
|
|
|
|
void $ G.setSessionDynFlags (setFlags dflags)
|
|
|
|
return dflags
|
|
|
|
teardown = void . G.setSessionDynFlags
|
|
|
|
|
|
|
|
withCmdFlags :: GhcMonad m => [GHCOption] -> m a -> m a
|
|
|
|
withCmdFlags flags body = G.gbracket setup teardown (\_ -> body)
|
|
|
|
where
|
|
|
|
setup = do
|
2014-08-13 16:04:37 +00:00
|
|
|
dflags <- G.getSessionDynFlags
|
|
|
|
void $ G.setSessionDynFlags =<< addCmdOpts flags dflags
|
2014-07-12 00:53:59 +00:00
|
|
|
return dflags
|
|
|
|
teardown = void . G.setSessionDynFlags
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Set 'DynFlags' equivalent to "-w:".
|
2014-08-19 08:18:36 +00:00
|
|
|
setNoWarningFlags :: DynFlags -> DynFlags
|
|
|
|
setNoWarningFlags df = df { warningFlags = Gap.emptyWarnFlags}
|
2014-07-12 00:53:59 +00:00
|
|
|
|
|
|
|
-- | Set 'DynFlags' equivalent to "-Wall".
|
2014-08-19 08:18:36 +00:00
|
|
|
setAllWarningFlags :: DynFlags -> DynFlags
|
|
|
|
setAllWarningFlags df = df { warningFlags = allWarningFlags }
|
2014-07-12 00:53:59 +00:00
|
|
|
|
|
|
|
allWarningFlags :: Gap.WarnFlags
|
2014-07-17 08:16:44 +00:00
|
|
|
allWarningFlags = unsafePerformIO $
|
2014-07-12 00:53:59 +00:00
|
|
|
G.runGhc (Just libdir) $ do
|
|
|
|
df <- G.getSessionDynFlags
|
|
|
|
df' <- addCmdOpts ["-Wall"] df
|
|
|
|
return $ G.warningFlags df'
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
2014-07-16 17:01:43 +00:00
|
|
|
|
|
|
|
-- | Set 'DynFlags' equivalent to "-fno-max-relevant-bindings".
|
|
|
|
setNoMaxRelevantBindings :: DynFlags -> DynFlags
|
|
|
|
#if __GLASGOW_HASKELL__ >= 708
|
|
|
|
setNoMaxRelevantBindings df = df { maxRelevantBinds = Nothing }
|
|
|
|
#else
|
|
|
|
setNoMaxRelevantBindings = id
|
|
|
|
#endif
|