Defining CompilerOptions.

This commit is contained in:
Kazu Yamamoto 2013-09-19 15:58:50 +09:00
parent 76795b31f2
commit 97da4e9be1
5 changed files with 38 additions and 30 deletions

View File

@ -36,19 +36,16 @@ import System.FilePath
-- | Parsing a cabal file in 'Cradle' and returns
-- options for GHC, include directories for modules and
-- package names of dependency.
fromCabalFile :: [GHCOption]
-> Cradle
-> IO ([GHCOption],[IncludeDir],[Package])
fromCabalFile ghcOptions cradle =
parseCabalFile cfile >>= cookInfo ghcOptions cradle
fromCabalFile :: [GHCOption] -> Cradle -> IO CompilerOptions
fromCabalFile ghcopts cradle =
parseCabalFile cfile >>= cookInfo ghcopts cradle
where
Just cfile = cradleCabalFile cradle
cookInfo :: [GHCOption] -> Cradle -> PackageDescription
-> IO ([GHCOption],[IncludeDir],[Package])
cookInfo ghcOptions cradle cabal = do
gopts <- getGHCOptions ghcOptions cdir $ head buildInfos
return (gopts,idirs,depPkgs)
cookInfo :: [GHCOption] -> Cradle -> PackageDescription -> IO CompilerOptions
cookInfo ghcopts cradle cabal = do
gopts <- getGHCOptions ghcopts cdir $ head buildInfos
return $ CompilerOptions gopts idirs depPkgs
where
wdir = cradleCurrentDir cradle
Just cdir = cradleCabalDir cradle
@ -106,10 +103,10 @@ parseCabalFile file = do
----------------------------------------------------------------
getGHCOptions :: [GHCOption] -> FilePath -> BuildInfo -> IO [GHCOption]
getGHCOptions ghcOptions cdir binfo = do
getGHCOptions ghcopts cdir binfo = do
cabalCpp <- cabalCppOptions cdir
let cpps = map ("-optP" ++) $ cppOptions binfo ++ cabalCpp
return $ ghcOptions ++ exts ++ [lang] ++ libs ++ libDirs ++ cpps
return $ ghcopts ++ exts ++ [lang] ++ libs ++ libDirs ++ cpps
where
lang = maybe "-XHaskell98" (("-X" ++) . display) $ defaultLanguage binfo
libDirs = map ("-L" ++) $ extraLibDirs binfo

View File

@ -29,11 +29,11 @@ debug :: Options
-> FilePath -- ^ A target file.
-> Ghc [String]
debug opt cradle ver fileName = do
(gopts, incDir, pkgs) <-
CompilerOptions gopts incDir pkgs <-
if cabal then
liftIO $ fromCabalFile (ghcOpts opt) cradle ||> return (ghcOpts opt, [], [])
liftIO $ fromCabalFile (ghcOpts opt) cradle ||> return (CompilerOptions (ghcOpts opt) [] [])
else
return (ghcOpts opt, [], [])
return (CompilerOptions (ghcOpts opt) [] [])
[fast] <- do
void $ initializeFlagsWithCradle opt cradle gopts True
setTargetFiles [fileName]

View File

@ -61,36 +61,39 @@ data Build = CabalPkg | SingleFile deriving Eq
-- file or GHC session according to the 'Cradle' and 'Options'
-- provided.
initializeFlagsWithCradle :: GhcMonad m => Options -> Cradle -> [GHCOption] -> Bool -> m LogReader
initializeFlagsWithCradle opt cradle ghcOptions logging
initializeFlagsWithCradle opt cradle ghcopts logging
| cabal = withCabal |||> withoutCabal
| otherwise = withoutCabal
where
cabal = isJust $ cradleCabalFile cradle
withCabal = do
(gopts,idirs,depPkgs) <- liftIO $ fromCabalFile ghcOptions cradle
initSession CabalPkg opt gopts idirs (Just depPkgs) logging
compOpts <- liftIO $ fromCabalFile ghcopts cradle
initSession CabalPkg opt compOpts logging
withoutCabal =
initSession SingleFile opt ghcOptions importDirs Nothing logging
initSession SingleFile opt compOpts logging
where
compOpts = CompilerOptions ghcopts importDirs []
----------------------------------------------------------------
initSession :: GhcMonad m => Build
-> Options
-> [GHCOption]
-> [IncludeDir]
-> Maybe [Package]
-> CompilerOptions
-> Bool
-> m LogReader
initSession build opt cmdOpts idirs mDepPkgs logging = do
initSession build opt compOpts logging = do
dflags0 <- getSessionDynFlags
(dflags1,readLog) <- setupDynamicFlags dflags0
_ <- setSessionDynFlags dflags1
return readLog
where
cmdOpts = ghcOptions compOpts
idirs = includeDirs compOpts
depPkgs = depPackages compOpts
ls = lineSeparator opt
setupDynamicFlags df0 = do
df1 <- modifyFlagsWithOpts df0 cmdOpts
let df2 = modifyFlags df1 idirs mDepPkgs (expandSplice opt) build
let df2 = modifyFlags df1 idirs depPkgs (expandSplice opt) build
df3 <- modifyFlagsWithOpts df2 $ ghcOpts opt
liftIO $ setLogger logging df3 ls
@ -107,14 +110,14 @@ initializeFlags opt = do
----------------------------------------------------------------
-- FIXME removing Options
modifyFlags :: DynFlags -> [IncludeDir] -> Maybe [Package] -> Bool -> Build -> DynFlags
modifyFlags d0 idirs mDepPkgs splice build
modifyFlags :: DynFlags -> [IncludeDir] -> [Package] -> Bool -> Build -> DynFlags
modifyFlags d0 idirs pepPkgs splice build
| splice = setSplice d4
| otherwise = d4
where
d1 = d0 { importPaths = idirs }
d2 = setFastOrNot d1 Fast
d3 = maybe d2 (Gap.addDevPkgs d2) mDepPkgs
d3 = Gap.addDevPkgs d2 pepPkgs
d4 | build == CabalPkg = Gap.setCabalPkg d3
| otherwise = d3

View File

@ -6,6 +6,7 @@ module Language.Haskell.GhcMod.Internal (
, GHCOption
, Package
, IncludeDir
, CompilerOptions(..)
-- * Cabal API
, fromCabalFile
, parseCabalFile

View File

@ -91,13 +91,13 @@ data Cradle = Cradle {
----------------------------------------------------------------
-- | A single GHC option, as it would appear on the command line.
-- | A single GHC command line option.
type GHCOption = String
-- | Include directories for modules
-- | An include directory for modules.
type IncludeDir = FilePath
-- | Package names
-- | A package name.
type Package = String
-- | GHC version in 'String'.
@ -110,3 +110,10 @@ type Expression = String
type ModuleString = String
data CheckSpeed = Slow | Fast
-- | Option information for GHC
data CompilerOptions = CompilerOptions {
ghcOptions :: [GHCOption] -- ^ Command line options
, includeDirs :: [IncludeDir] -- ^ Include directories for modules
, depPackages :: [Package] -- ^ Dependent package names
}