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

View File

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

View File

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

View File

@ -6,6 +6,7 @@ module Language.Haskell.GhcMod.Internal (
, GHCOption , GHCOption
, Package , Package
, IncludeDir , IncludeDir
, CompilerOptions(..)
-- * Cabal API -- * Cabal API
, fromCabalFile , fromCabalFile
, parseCabalFile , 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 type GHCOption = String
-- | Include directories for modules -- | An include directory for modules.
type IncludeDir = FilePath type IncludeDir = FilePath
-- | Package names -- | A package name.
type Package = String type Package = String
-- | GHC version in 'String'. -- | GHC version in 'String'.
@ -110,3 +110,10 @@ type Expression = String
type ModuleString = String type ModuleString = String
data CheckSpeed = Slow | Fast 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
}