getCompilerOptions handles package-db options.

This commit is contained in:
Kazu Yamamoto
2013-09-20 17:15:41 +09:00
parent 46245fb694
commit 5f0fcd0442
8 changed files with 76 additions and 62 deletions

View File

@@ -36,7 +36,7 @@ import System.FilePath
-- | Getting necessary 'CompilerOptions' from three information sources.
getCompilerOptions :: [GHCOption] -> Cradle -> PackageDescription -> IO CompilerOptions
getCompilerOptions ghcopts cradle pkgDesc = do
gopts <- getGHCOptions ghcopts cdir $ head buildInfos
gopts <- getGHCOptions ghcopts cradle cdir $ head buildInfos
return $ CompilerOptions gopts idirs depPkgs
where
wdir = cradleCurrentDir cradle
@@ -96,12 +96,13 @@ parseCabalFile file = do
----------------------------------------------------------------
getGHCOptions :: [GHCOption] -> FilePath -> BuildInfo -> IO [GHCOption]
getGHCOptions ghcopts cdir binfo = do
getGHCOptions :: [GHCOption] -> Cradle -> FilePath -> BuildInfo -> IO [GHCOption]
getGHCOptions ghcopts cradle cdir binfo = do
cabalCpp <- cabalCppOptions cdir
let cpps = map ("-optP" ++) $ cppOptions binfo ++ cabalCpp
return $ ghcopts ++ exts ++ [lang] ++ libs ++ libDirs ++ cpps
return $ ghcopts ++ pkgDb ++ exts ++ [lang] ++ libs ++ libDirs ++ cpps
where
pkgDb = cradlePackageDbOpts cradle
lang = maybe "-XHaskell98" (("-X" ++) . display) $ defaultLanguage binfo
libDirs = map ("-L" ++) $ extraLibDirs binfo
exts = map (("-X" ++) . display) $ usedExtensions binfo

View File

@@ -4,10 +4,10 @@ import Data.Char (isSpace)
import Control.Applicative ((<$>))
import Control.Exception as E (catch, throwIO, SomeException)
import Control.Monad (filterM)
import Data.List (isPrefixOf, isSuffixOf)
import Data.List (isPrefixOf, isSuffixOf, tails)
import Language.Haskell.GhcMod.Types
import System.Directory (getCurrentDirectory, getDirectoryContents, doesFileExist)
import System.FilePath ((</>),takeDirectory)
import System.FilePath ((</>), takeDirectory, takeFileName)
----------------------------------------------------------------
@@ -22,21 +22,21 @@ findCradle = do
where
handler :: FilePath -> SomeException -> IO Cradle
handler wdir _ = return Cradle {
cradleCurrentDir = wdir
, cradleCabalDir = Nothing
, cradleCabalFile = Nothing
, cradlePackageConf = Nothing
cradleCurrentDir = wdir
, cradleCabalDir = Nothing
, cradleCabalFile = Nothing
, cradlePackageDbOpts = []
}
findCradle' :: FilePath -> IO Cradle
findCradle' wdir = do
(cdir,cfile) <- cabalDir wdir
mPkgConf <- getPackageDbDir cdir
pkgDbOpts <- getPackageDbOpts cdir
return Cradle {
cradleCurrentDir = wdir
, cradleCabalDir = Just cdir
, cradleCabalFile = Just cfile
, cradlePackageConf = mPkgConf
cradleCurrentDir = wdir
, cradleCabalDir = Just cdir
, cradleCabalFile = Just cfile
, cradlePackageDbOpts = pkgDbOpts
}
----------------------------------------------------------------
@@ -81,11 +81,40 @@ pkgDbKeyLen :: Int
pkgDbKeyLen = length pkgDbKey
-- | Extract a package db directory from the sandbox config file.
getPackageDbDir :: FilePath -> IO (Maybe FilePath)
getPackageDbDir cdir = (Just <$> getPkgDb) `E.catch` handler
getPackageDbOpts :: FilePath -> IO [GHCOption]
getPackageDbOpts cdir = (sandboxArguments <$> getPkgDb ) `E.catch` handler
where
getPkgDb = extractValue . parse <$> readFile (cdir </> configFile)
parse = head . filter ("package-db:" `isPrefixOf`) . lines
extractValue = fst . break isSpace . dropWhile isSpace . drop pkgDbKeyLen
handler :: SomeException -> IO (Maybe FilePath)
handler _ = return Nothing
handler :: SomeException -> IO [GHCOption]
handler _ = return []
-- | Adding necessary GHC options to the package db.
-- Exception is thrown if the string argument is incorrect.
--
-- >>> sandboxArguments "/foo/bar/i386-osx-ghc-7.6.3-packages.conf.d"
-- ["-no-user-package-db","-package-db","/foo/bar/i386-osx-ghc-7.6.3-packages.conf.d"]
-- >>> sandboxArguments "/foo/bar/i386-osx-ghc-7.4.1-packages.conf.d"
-- ["-no-user-package-conf","-package-conf","/foo/bar/i386-osx-ghc-7.4.1-packages.conf.d"]
sandboxArguments :: FilePath -> [String]
sandboxArguments pkgDb = [noUserPkgDbOpt, pkgDbOpt, pkgDb]
where
ver = extractGhcVer pkgDb
(pkgDbOpt,noUserPkgDbOpt)
| ver < 706 = ("-package-conf","-no-user-package-conf")
| otherwise = ("-package-db", "-no-user-package-db")
-- | Extracting GHC version from the path of package db.
-- Exception is thrown if the string argument is incorrect.
--
-- >>> extractGhcVer "/foo/bar/i386-osx-ghc-7.6.3-packages.conf.d"
-- 706
extractGhcVer :: String -> Int
extractGhcVer dir = ver
where
file = takeFileName dir
findVer = drop 4 . head . filter ("ghc-" `isPrefixOf`) . tails
(verStr1,_:left) = break (== '.') $ findVer file
(verStr2,_) = break (== '.') left
ver = read verStr1 * 100 + read verStr2

View File

@@ -17,18 +17,16 @@ import Prelude
-- | Obtaining debug information.
debugInfo :: Options
-> Cradle
-> GHCVersion
-> FilePath -- ^ A target file.
-> IO String
debugInfo opt cradle ver fileName = unlines <$> withGHC fileName (debug opt cradle ver fileName)
debugInfo opt cradle fileName = unlines <$> withGHC fileName (debug opt cradle fileName)
-- | Obtaining debug information.
debug :: Options
-> Cradle
-> GHCVersion
-> FilePath -- ^ A target file.
-> Ghc [String]
debug opt cradle ver fileName = do
debug opt cradle fileName = do
CompilerOptions gopts incDir pkgs <-
if cabal then
liftIO (fromCabalFile ||> return simpleCompilerOption)
@@ -39,8 +37,7 @@ debug opt cradle ver fileName = do
setTargetFiles [fileName]
pure . canCheckFast <$> depanal [] False
return [
"GHC version: " ++ ver
, "Current directory: " ++ currentDir
"Current directory: " ++ currentDir
, "Cabal file: " ++ cabalFile
, "GHC options: " ++ unwords gopts
, "Include directories: " ++ unwords incDir

View File

@@ -82,8 +82,8 @@ data Cradle = Cradle {
, cradleCabalDir :: Maybe FilePath
-- | The file name of the found cabal file.
, cradleCabalFile :: Maybe FilePath
-- | The sandbox directory. (e.g. \"\/foo\/bar\/packages-\<ver\>.conf/\")
, cradlePackageConf :: Maybe FilePath
-- | The package db options. ([\"-no-user-package-db\",\"-package-db\",\"\/foo\/bar\/i386-osx-ghc-7.6.3-packages.conf.d\"])
, cradlePackageDbOpts :: [GHCOption]
} deriving (Eq, Show)
----------------------------------------------------------------