2014-06-29 08:28:28 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2014-08-11 21:45:33 +00:00
|
|
|
-- | This module facilitates extracting information from Cabal's on-disk
|
|
|
|
-- 'LocalBuildInfo' (@dist/setup-config@).
|
2014-05-09 18:35:13 +00:00
|
|
|
module Language.Haskell.GhcMod.CabalConfig (
|
|
|
|
CabalConfig
|
|
|
|
, cabalConfigDependencies
|
2014-09-10 12:23:36 +00:00
|
|
|
, cabalConfigFlags
|
2014-09-23 05:49:13 +00:00
|
|
|
, setupConfigFile
|
2014-05-09 18:35:13 +00:00
|
|
|
) where
|
|
|
|
|
2014-08-28 09:54:01 +00:00
|
|
|
import Language.Haskell.GhcMod.Error
|
2014-05-09 18:35:13 +00:00
|
|
|
import Language.Haskell.GhcMod.GhcPkg
|
|
|
|
import Language.Haskell.GhcMod.Utils
|
2014-05-10 11:36:54 +00:00
|
|
|
import Language.Haskell.GhcMod.Read
|
2014-05-09 18:35:13 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
|
|
|
|
|
|
|
import qualified Language.Haskell.GhcMod.Cabal16 as C16
|
|
|
|
import qualified Language.Haskell.GhcMod.Cabal18 as C18
|
2014-09-12 02:26:34 +00:00
|
|
|
import qualified Language.Haskell.GhcMod.Cabal21 as C21
|
2014-05-09 18:35:13 +00:00
|
|
|
|
2014-07-17 08:16:44 +00:00
|
|
|
#ifndef MIN_VERSION_mtl
|
|
|
|
#define MIN_VERSION_mtl(x,y,z) 1
|
|
|
|
#endif
|
|
|
|
|
2014-05-09 18:35:13 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2014-08-28 09:54:01 +00:00
|
|
|
import Control.Monad (mplus)
|
2014-06-29 08:28:28 +00:00
|
|
|
#if MIN_VERSION_mtl(2,2,1)
|
|
|
|
import Control.Monad.Except ()
|
|
|
|
#else
|
2014-05-09 18:35:13 +00:00
|
|
|
import Control.Monad.Error ()
|
2014-06-29 08:28:28 +00:00
|
|
|
#endif
|
2014-05-09 18:35:13 +00:00
|
|
|
import Data.Maybe ()
|
|
|
|
import Data.Set ()
|
|
|
|
import Data.List (find,tails,isPrefixOf,isInfixOf,nub,stripPrefix)
|
2014-05-09 18:38:35 +00:00
|
|
|
import Distribution.Package (InstalledPackageId(..)
|
2014-09-12 02:26:34 +00:00
|
|
|
, PackageIdentifier(..)
|
|
|
|
, PackageName(..))
|
2014-09-10 12:23:36 +00:00
|
|
|
import Distribution.PackageDescription (FlagAssignment)
|
2014-05-09 18:35:13 +00:00
|
|
|
import Distribution.Simple.BuildPaths (defaultDistPref)
|
|
|
|
import Distribution.Simple.Configure (localBuildInfoFile)
|
|
|
|
import Distribution.Simple.LocalBuildInfo (ComponentName)
|
2014-08-28 09:54:01 +00:00
|
|
|
import MonadUtils (liftIO)
|
2014-05-09 18:35:13 +00:00
|
|
|
import System.FilePath ((</>))
|
2014-08-18 06:24:38 +00:00
|
|
|
|
2014-05-09 18:35:13 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-08-11 21:45:33 +00:00
|
|
|
-- | 'Show'ed cabal 'LocalBuildInfo' string
|
2014-05-09 18:35:13 +00:00
|
|
|
type CabalConfig = String
|
|
|
|
|
2014-08-11 21:45:33 +00:00
|
|
|
-- | Get contents of the file containing 'LocalBuildInfo' data. If it doesn't
|
|
|
|
-- exist run @cabal configure@ i.e. configure with default options like @cabal
|
|
|
|
-- build@ would do.
|
2014-08-28 09:54:01 +00:00
|
|
|
getConfig :: (IOish m, MonadError GhcModError m)
|
2014-08-18 06:06:36 +00:00
|
|
|
=> Cradle
|
|
|
|
-> m CabalConfig
|
2014-09-23 05:49:13 +00:00
|
|
|
getConfig cradle = liftIO (readFile file) `tryFix` \_ ->
|
2014-08-28 09:54:01 +00:00
|
|
|
configure `modifyError'` GMECabalConfigure
|
2014-05-09 18:35:13 +00:00
|
|
|
where
|
2014-09-23 05:49:13 +00:00
|
|
|
file = setupConfigFile cradle
|
2014-05-09 18:35:13 +00:00
|
|
|
prjDir = cradleRootDir cradle
|
2014-08-28 09:54:01 +00:00
|
|
|
|
|
|
|
configure :: (IOish m, MonadError GhcModError m) => m ()
|
|
|
|
configure =
|
|
|
|
withDirectory_ prjDir $ readProcess' "cabal" ["configure"] >> return ()
|
2014-05-09 18:35:13 +00:00
|
|
|
|
|
|
|
|
2014-09-23 05:49:13 +00:00
|
|
|
setupConfigFile :: Cradle -> FilePath
|
|
|
|
setupConfigFile crdl = cradleRootDir crdl </> setupConfigPath
|
|
|
|
|
2014-05-09 18:35:13 +00:00
|
|
|
-- | Path to 'LocalBuildInfo' file, usually @dist/setup-config@
|
2014-09-23 05:49:13 +00:00
|
|
|
setupConfigPath :: FilePath
|
|
|
|
setupConfigPath = localBuildInfoFile defaultDistPref
|
2014-05-09 18:35:13 +00:00
|
|
|
|
2014-08-11 21:45:33 +00:00
|
|
|
-- | Get list of 'Package's needed by all components of the current package
|
2014-08-28 09:54:01 +00:00
|
|
|
cabalConfigDependencies :: (IOish m, MonadError GhcModError m)
|
2014-08-18 06:06:36 +00:00
|
|
|
=> Cradle
|
|
|
|
-> PackageIdentifier
|
|
|
|
-> m [Package]
|
2014-05-09 18:35:13 +00:00
|
|
|
cabalConfigDependencies cradle thisPkg =
|
|
|
|
configDependencies thisPkg <$> getConfig cradle
|
|
|
|
|
2014-08-11 21:45:33 +00:00
|
|
|
-- | Extract list of depencenies for all components from 'CabalConfig'
|
2014-05-09 18:35:13 +00:00
|
|
|
configDependencies :: PackageIdentifier -> CabalConfig -> [Package]
|
|
|
|
configDependencies thisPkg config = map fromInstalledPackageId deps
|
|
|
|
where
|
|
|
|
deps :: [InstalledPackageId]
|
2014-09-12 02:26:34 +00:00
|
|
|
deps = case deps21 `mplus` deps18 `mplus` deps16 of
|
2014-05-09 18:35:13 +00:00
|
|
|
Right ps -> ps
|
|
|
|
Left msg -> error msg
|
|
|
|
|
2014-05-09 19:12:52 +00:00
|
|
|
-- True if this dependency is an internal one (depends on the library
|
|
|
|
-- defined in the same package).
|
|
|
|
internal pkgid = pkgid == thisPkg
|
2014-05-09 18:35:13 +00:00
|
|
|
|
2014-09-12 02:26:34 +00:00
|
|
|
-- Cabal >= 1.21
|
|
|
|
deps21 :: Either String [InstalledPackageId]
|
|
|
|
deps21 =
|
|
|
|
map fst
|
|
|
|
<$> filterInternal21
|
|
|
|
<$> (readEither =<< extractField config "componentsConfigs")
|
|
|
|
|
|
|
|
filterInternal21
|
|
|
|
:: [(ComponentName, C21.ComponentLocalBuildInfo, [ComponentName])]
|
|
|
|
-> [(InstalledPackageId, C21.PackageIdentifier)]
|
|
|
|
|
|
|
|
filterInternal21 ccfg = [ (ipkgid, pkgid)
|
|
|
|
| (_,clbi,_) <- ccfg
|
|
|
|
, (ipkgid, pkgid) <- C21.componentPackageDeps clbi
|
|
|
|
, not (internal . packageIdentifierFrom21 $ pkgid) ]
|
|
|
|
|
|
|
|
packageIdentifierFrom21 :: C21.PackageIdentifier -> PackageIdentifier
|
|
|
|
packageIdentifierFrom21 (C21.PackageIdentifier (C21.PackageName myName) myVersion) =
|
|
|
|
PackageIdentifier (PackageName myName) myVersion
|
|
|
|
|
|
|
|
-- Cabal >= 1.18 && < 1.21
|
2014-05-09 18:35:13 +00:00
|
|
|
deps18 :: Either String [InstalledPackageId]
|
|
|
|
deps18 =
|
2014-05-09 19:12:52 +00:00
|
|
|
map fst
|
|
|
|
<$> filterInternal
|
2014-05-09 18:35:13 +00:00
|
|
|
<$> (readEither =<< extractField config "componentsConfigs")
|
|
|
|
|
2014-05-09 19:12:52 +00:00
|
|
|
filterInternal
|
|
|
|
:: [(ComponentName, C18.ComponentLocalBuildInfo, [ComponentName])]
|
|
|
|
-> [(InstalledPackageId, PackageIdentifier)]
|
|
|
|
|
|
|
|
filterInternal ccfg = [ (ipkgid, pkgid)
|
|
|
|
| (_,clbi,_) <- ccfg
|
|
|
|
, (ipkgid, pkgid) <- C18.componentPackageDeps clbi
|
|
|
|
, not (internal pkgid) ]
|
2014-05-09 18:35:13 +00:00
|
|
|
|
|
|
|
-- Cabal 1.16 and below
|
|
|
|
deps16 :: Either String [InstalledPackageId]
|
|
|
|
deps16 = map fst <$> filter (not . internal . snd) . nub <$> do
|
|
|
|
cbi <- concat <$> sequence [ extract "executableConfigs"
|
|
|
|
, extract "testSuiteConfigs"
|
|
|
|
, extract "benchmarkConfigs" ]
|
|
|
|
:: Either String [(String, C16.ComponentLocalBuildInfo)]
|
|
|
|
|
|
|
|
return $ maybe [] C16.componentPackageDeps libraryConfig
|
|
|
|
++ concatMap (C16.componentPackageDeps . snd) cbi
|
|
|
|
where
|
|
|
|
libraryConfig :: Maybe C16.ComponentLocalBuildInfo
|
|
|
|
libraryConfig = do
|
|
|
|
field <- find ("libraryConfig" `isPrefixOf`) (tails config)
|
|
|
|
clbi <- stripPrefix " = " field
|
|
|
|
if "Nothing" `isPrefixOf` clbi
|
|
|
|
then Nothing
|
2014-05-10 11:36:54 +00:00
|
|
|
else case readMaybe =<< stripPrefix "Just " clbi of
|
2014-05-09 18:35:13 +00:00
|
|
|
Just x -> x
|
|
|
|
Nothing -> error $ "reading libraryConfig failed\n" ++ show (stripPrefix "Just " clbi)
|
|
|
|
|
|
|
|
extract :: String -> Either String [(String, C16.ComponentLocalBuildInfo)]
|
|
|
|
extract field = readConfigs field <$> extractField config field
|
|
|
|
|
|
|
|
readConfigs :: String -> String -> [(String, C16.ComponentLocalBuildInfo)]
|
2014-05-10 11:36:54 +00:00
|
|
|
readConfigs f s = case readEither s of
|
|
|
|
Right x -> x
|
|
|
|
Left msg -> error $ "reading config " ++ f ++ " failed ("++msg++")"
|
2014-05-09 18:35:13 +00:00
|
|
|
|
2014-09-10 12:23:36 +00:00
|
|
|
-- | Get the flag assignment from the local build info of the given cradle
|
|
|
|
cabalConfigFlags :: (IOish m, MonadError GhcModError m)
|
|
|
|
=> Cradle
|
|
|
|
-> m FlagAssignment
|
|
|
|
cabalConfigFlags cradle = do
|
|
|
|
config <- getConfig cradle
|
|
|
|
case configFlags config of
|
|
|
|
Right x -> return x
|
|
|
|
Left msg -> throwError (GMECabalFlags (GMEString msg))
|
|
|
|
|
|
|
|
-- | Extract the cabal flags from the 'CabalConfig'
|
|
|
|
configFlags :: CabalConfig -> Either String FlagAssignment
|
|
|
|
configFlags config = readEither =<< flip extractField "configConfigurationsFlags" =<< extractField config "configFlags"
|
|
|
|
|
2014-08-11 21:45:33 +00:00
|
|
|
-- | Find @field@ in 'CabalConfig'. Returns 'Left' containing a user readable
|
|
|
|
-- error message with lots of context on failure.
|
2014-05-09 18:35:13 +00:00
|
|
|
extractField :: CabalConfig -> String -> Either String String
|
|
|
|
extractField config field =
|
|
|
|
case extractParens <$> find (field `isPrefixOf`) (tails config) of
|
|
|
|
Just f -> Right f
|
|
|
|
Nothing -> Left $ "extractField: failed extracting "++field++" from input, input contained `"++field++"'? " ++ show (field `isInfixOf` config)
|