{-# LANGUAGE CPP #-}
module SimpleCabal (
findCabalFile,
readFinalPackageDescription,
finalPackageDescription,
#if MIN_VERSION_Cabal(2,2,0)
parseFinalPackageDescription,
#endif
makeFinalPackageDescription,
getPackageId,
#if defined(MIN_VERSION_Cabal) && MIN_VERSION_Cabal(2,4,0)
buildDepends,
#endif
buildDependencies,
setupDependencies,
testsuiteDependencies,
allBuildInfo,
allLibraries,
BuildInfo (..),
Library(..),
depPkgName, exeDepName, pkgcfgDepName,
FlagName, mkFlagName,
hasExes, hasLibs,
#if defined(MIN_VERSION_Cabal) && MIN_VERSION_Cabal(1,20,0)
#else
licenseFiles,
#endif
PackageDescription (..),
PackageIdentifier (..),
PackageName, mkPackageName, unPackageName,
packageName, packageVersion,
readGenericPackageDescription,
showPkgId,
showVersion,
simpleParse,
tryFindPackageDesc
) where
#if (defined(MIN_VERSION_base) && MIN_VERSION_base(4,8,0))
#else
import Control.Applicative ((<$>))
#endif
#if MIN_VERSION_Cabal(2,2,0)
import qualified Data.ByteString.Char8 as B
#endif
#if !MIN_VERSION_Cabal(2,0,0)
import Data.Maybe (maybeToList)
#endif
import Data.List (delete, nub)
import Distribution.Compiler
import Distribution.Package (
packageName,
#if defined(MIN_VERSION_Cabal) && MIN_VERSION_Cabal(1,22,0)
#if defined(MIN_VERSION_Cabal) && MIN_VERSION_Cabal(2,0,0)
depPkgName,
mkPackageName,
unPackageName,
unPkgconfigName,
#if defined(MIN_VERSION_Cabal) && MIN_VERSION_Cabal(2,4,0)
Dependency,
#endif
#else
#endif
#endif
#if defined(MIN_VERSION_Cabal) && MIN_VERSION_Cabal(2,0,0)
#else
Dependency (..),
#endif
#if defined(MIN_VERSION_Cabal) && MIN_VERSION_Cabal(2,0,0)
PackageName,
#else
PackageName (..),
#endif
PackageIdentifier (..),
)
import Distribution.PackageDescription (
PackageDescription (..),
allBuildInfo,
#if MIN_VERSION_Cabal(2,0,0)
allLibraries,
#endif
BuildInfo (..),
#if MIN_VERSION_Cabal(2,4,0)
enabledBuildDepends,
#endif
extraLibs,
#if MIN_VERSION_Cabal(2,0,0)
FlagName,
mkFlagName,
#else
FlagName (..),
#endif
GenericPackageDescription(packageDescription),
hasExes, hasLibs,
Library(..),
#if MIN_VERSION_Cabal(2,2,0)
mkFlagAssignment,
#endif
pkgconfigDepends,
#if MIN_VERSION_Cabal(1,24,0)
setupDepends,
#endif
targetBuildDepends,
TestSuite (..)
)
#if MIN_VERSION_Cabal(2,0,0)
import Distribution.PackageDescription.Configuration (finalizePD)
import Distribution.Types.ComponentRequestedSpec (defaultComponentRequestedSpec)
import Distribution.Types.LegacyExeDependency (LegacyExeDependency (..))
import Distribution.Types.PkgconfigDependency (PkgconfigDependency (..))
#else
import Distribution.PackageDescription.Configuration (finalizePackageDescription)
#endif
#if MIN_VERSION_Cabal(2,2,0)
import Distribution.PackageDescription.Parsec
(readGenericPackageDescription, parseGenericPackageDescriptionMaybe)
#elif MIN_VERSION_Cabal(2,0,0)
import Distribution.PackageDescription.Parse (readGenericPackageDescription)
#else
import Distribution.PackageDescription.Parse (readPackageDescription)
#endif
import Distribution.Simple.Compiler (
#if MIN_VERSION_Cabal(1,22,0)
compilerInfo
#else
Compiler (..)
#endif
)
import Distribution.Simple.Configure (
#if MIN_VERSION_Cabal(1,18,0)
configCompilerEx
#else
configCompiler
#endif
)
#if MIN_VERSION_Cabal(2,0,0)
import Distribution.Simple.Program (defaultProgramDb)
#else
import Distribution.Simple.Program (defaultProgramConfiguration)
#endif
import qualified Distribution.Simple.Utils as DSU (
#if MIN_VERSION_Cabal(1,20,0)
tryFindPackageDesc
#else
findPackageDesc
#endif
)
import Distribution.System (Platform (..), buildArch, buildOS)
import Distribution.Text (simpleParse)
import Distribution.Verbosity (normal,
#if !MIN_VERSION_Cabal(2,0,0)
Verbosity
#endif
)
#if MIN_VERSION_Cabal(2,2,0)
import Distribution.Pretty (prettyShow)
#else
#if MIN_VERSION_Cabal(2,0,0)
import Distribution.Version (showVersion)
#else
import Data.Version (showVersion)
#endif
#endif
#if MIN_VERSION_Cabal(2,2,0)
import qualified Distribution.Version (Version)
#endif
import System.Directory (getDirectoryContents)
import System.FilePath (takeExtension)
findCabalFile :: IO FilePath
findCabalFile :: IO FilePath
findCabalFile = do
[FilePath]
allCabals <- FilePath -> FilePath -> IO [FilePath]
filesWithExtension "." ".cabal"
case [FilePath]
allCabals of
[file :: FilePath
file] -> FilePath -> IO FilePath
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
file
[] -> FilePath -> IO FilePath
forall a. HasCallStack => FilePath -> a
error "No .cabal file found"
_ -> FilePath -> IO FilePath
forall a. HasCallStack => FilePath -> a
error "More than one .cabal file found!"
where
filesWithExtension :: FilePath -> String -> IO [FilePath]
filesWithExtension :: FilePath -> FilePath -> IO [FilePath]
filesWithExtension dir :: FilePath
dir ext :: FilePath
ext =
(FilePath -> Bool) -> [FilePath] -> [FilePath]
forall a. (a -> Bool) -> [a] -> [a]
filter (\ f :: FilePath
f -> FilePath -> FilePath
takeExtension FilePath
f FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath
ext Bool -> Bool -> Bool
&& FilePath -> Char
forall a. [a] -> a
head FilePath
f Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= '.')
([FilePath] -> [FilePath]) -> IO [FilePath] -> IO [FilePath]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO [FilePath]
getDirectoryContents FilePath
dir
getPackageId :: IO PackageIdentifier
getPackageId :: IO PackageIdentifier
getPackageId = do
GenericPackageDescription
gpd <- IO FilePath
findCabalFile IO FilePath
-> (FilePath -> IO GenericPackageDescription)
-> IO GenericPackageDescription
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Verbosity -> FilePath -> IO GenericPackageDescription
readGenericPackageDescription Verbosity
normal
PackageIdentifier -> IO PackageIdentifier
forall (m :: * -> *) a. Monad m => a -> m a
return (PackageIdentifier -> IO PackageIdentifier)
-> PackageIdentifier -> IO PackageIdentifier
forall a b. (a -> b) -> a -> b
$ PackageDescription -> PackageIdentifier
package (PackageDescription -> PackageIdentifier)
-> PackageDescription -> PackageIdentifier
forall a b. (a -> b) -> a -> b
$ GenericPackageDescription -> PackageDescription
packageDescription GenericPackageDescription
gpd
#if !MIN_VERSION_Cabal(2,0,0)
readGenericPackageDescription :: Verbosity
-> FilePath -> IO GenericPackageDescription
readGenericPackageDescription = readPackageDescription
#endif
#if MIN_VERSION_Cabal(2,2,0)
parseFinalPackageDescription :: [(FlagName, Bool)] -> B.ByteString
-> IO (Maybe PackageDescription)
parseFinalPackageDescription :: [(FlagName, Bool)] -> ByteString -> IO (Maybe PackageDescription)
parseFinalPackageDescription flags :: [(FlagName, Bool)]
flags cabalfile :: ByteString
cabalfile = do
let mgenPkgDesc :: Maybe GenericPackageDescription
mgenPkgDesc = ByteString -> Maybe GenericPackageDescription
parseGenericPackageDescriptionMaybe ByteString
cabalfile
case Maybe GenericPackageDescription
mgenPkgDesc of
Nothing -> Maybe PackageDescription -> IO (Maybe PackageDescription)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe PackageDescription
forall a. Maybe a
Nothing
Just genPkgDesc :: GenericPackageDescription
genPkgDesc -> PackageDescription -> Maybe PackageDescription
forall a. a -> Maybe a
Just (PackageDescription -> Maybe PackageDescription)
-> IO PackageDescription -> IO (Maybe PackageDescription)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(FlagName, Bool)]
-> GenericPackageDescription -> IO PackageDescription
makeFinalPackageDescription [(FlagName, Bool)]
flags GenericPackageDescription
genPkgDesc
#endif
finalPackageDescription :: [(FlagName, Bool)] -> FilePath
-> IO PackageDescription
finalPackageDescription :: [(FlagName, Bool)] -> FilePath -> IO PackageDescription
finalPackageDescription = [(FlagName, Bool)] -> FilePath -> IO PackageDescription
readFinalPackageDescription
readFinalPackageDescription :: [(FlagName, Bool)] -> FilePath
-> IO PackageDescription
readFinalPackageDescription :: [(FlagName, Bool)] -> FilePath -> IO PackageDescription
readFinalPackageDescription flags :: [(FlagName, Bool)]
flags cabalfile :: FilePath
cabalfile =
Verbosity -> FilePath -> IO GenericPackageDescription
readGenericPackageDescription Verbosity
normal FilePath
cabalfile IO GenericPackageDescription
-> (GenericPackageDescription -> IO PackageDescription)
-> IO PackageDescription
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
[(FlagName, Bool)]
-> GenericPackageDescription -> IO PackageDescription
makeFinalPackageDescription [(FlagName, Bool)]
flags
makeFinalPackageDescription :: [(FlagName, Bool)] -> GenericPackageDescription
-> IO PackageDescription
makeFinalPackageDescription :: [(FlagName, Bool)]
-> GenericPackageDescription -> IO PackageDescription
makeFinalPackageDescription flags :: [(FlagName, Bool)]
flags genPkgDesc :: GenericPackageDescription
genPkgDesc = do
#if !MIN_VERSION_Cabal(2,0,0)
let defaultProgramDb = defaultProgramConfiguration
#endif
CompilerInfo
compiler <- do
#if MIN_VERSION_Cabal(1,18,0)
(compiler :: Compiler
compiler, _, _) <- Maybe CompilerFlavor
-> Maybe FilePath
-> Maybe FilePath
-> ProgramDb
-> Verbosity
-> IO (Compiler, Platform, ProgramDb)
configCompilerEx
#else
(compiler, _) <- configCompiler
#endif
(CompilerFlavor -> Maybe CompilerFlavor
forall a. a -> Maybe a
Just CompilerFlavor
GHC) Maybe FilePath
forall a. Maybe a
Nothing Maybe FilePath
forall a. Maybe a
Nothing ProgramDb
defaultProgramDb Verbosity
normal
#if MIN_VERSION_Cabal(1,22,0)
CompilerInfo -> IO CompilerInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (Compiler -> CompilerInfo
compilerInfo Compiler
compiler)
#else
return (compilerId compiler)
#endif
#if !MIN_VERSION_Cabal(2,2,0)
let mkFlagAssignment = id
#endif
#if MIN_VERSION_Cabal(2,0,0)
let finalizePackageDescription :: FlagAssignment
-> (Dependency -> Bool)
-> Platform
-> CompilerInfo
-> [Dependency]
-> GenericPackageDescription
-> Either [Dependency] (PackageDescription, FlagAssignment)
finalizePackageDescription flags' :: FlagAssignment
flags' = FlagAssignment
-> ComponentRequestedSpec
-> (Dependency -> Bool)
-> Platform
-> CompilerInfo
-> [Dependency]
-> GenericPackageDescription
-> Either [Dependency] (PackageDescription, FlagAssignment)
finalizePD FlagAssignment
flags' ComponentRequestedSpec
defaultComponentRequestedSpec
#endif
let final :: Either [Dependency] (PackageDescription, FlagAssignment)
final =
FlagAssignment
-> (Dependency -> Bool)
-> Platform
-> CompilerInfo
-> [Dependency]
-> GenericPackageDescription
-> Either [Dependency] (PackageDescription, FlagAssignment)
finalizePackageDescription ([(FlagName, Bool)] -> FlagAssignment
mkFlagAssignment [(FlagName, Bool)]
flags)
(Bool -> Dependency -> Bool
forall a b. a -> b -> a
const Bool
True) (Arch -> OS -> Platform
Platform Arch
buildArch OS
buildOS)
CompilerInfo
compiler
[] GenericPackageDescription
genPkgDesc
case Either [Dependency] (PackageDescription, FlagAssignment)
final of
Left e :: [Dependency]
e -> FilePath -> IO PackageDescription
forall a. HasCallStack => FilePath -> a
error (FilePath -> IO PackageDescription)
-> FilePath -> IO PackageDescription
forall a b. (a -> b) -> a -> b
$ "finalize failed: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ [Dependency] -> FilePath
forall a. Show a => a -> FilePath
show [Dependency]
e
Right res :: (PackageDescription, FlagAssignment)
res -> PackageDescription -> IO PackageDescription
forall (m :: * -> *) a. Monad m => a -> m a
return (PackageDescription -> IO PackageDescription)
-> PackageDescription -> IO PackageDescription
forall a b. (a -> b) -> a -> b
$ (PackageDescription, FlagAssignment) -> PackageDescription
forall a b. (a, b) -> a
fst (PackageDescription, FlagAssignment)
res
buildDependencies :: PackageDescription -> [PackageName]
buildDependencies :: PackageDescription -> [PackageName]
buildDependencies pkgDesc :: PackageDescription
pkgDesc =
let deps :: [PackageName]
deps = [PackageName] -> [PackageName]
forall a. Eq a => [a] -> [a]
nub ([PackageName] -> [PackageName]) -> [PackageName] -> [PackageName]
forall a b. (a -> b) -> a -> b
$ (Dependency -> PackageName) -> [Dependency] -> [PackageName]
forall a b. (a -> b) -> [a] -> [b]
map Dependency -> PackageName
depPkgName (PackageDescription -> [Dependency]
buildDepends PackageDescription
pkgDesc)
self :: PackageName
self = PackageIdentifier -> PackageName
pkgName (PackageIdentifier -> PackageName)
-> PackageIdentifier -> PackageName
forall a b. (a -> b) -> a -> b
$ PackageDescription -> PackageIdentifier
package PackageDescription
pkgDesc
in PackageName -> [PackageName] -> [PackageName]
forall a. Eq a => a -> [a] -> [a]
delete PackageName
self [PackageName]
deps
testsuiteDependencies :: PackageDescription -> [PackageName]
testsuiteDependencies :: PackageDescription -> [PackageName]
testsuiteDependencies pkgDesc :: PackageDescription
pkgDesc =
let self :: PackageName
self = PackageIdentifier -> PackageName
pkgName (PackageIdentifier -> PackageName)
-> PackageIdentifier -> PackageName
forall a b. (a -> b) -> a -> b
$ PackageDescription -> PackageIdentifier
package PackageDescription
pkgDesc in
PackageName -> [PackageName] -> [PackageName]
forall a. Eq a => a -> [a] -> [a]
delete PackageName
self ([PackageName] -> [PackageName])
-> ([Dependency] -> [PackageName]) -> [Dependency] -> [PackageName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PackageName] -> [PackageName]
forall a. Eq a => [a] -> [a]
nub ([PackageName] -> [PackageName])
-> ([Dependency] -> [PackageName]) -> [Dependency] -> [PackageName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Dependency -> PackageName) -> [Dependency] -> [PackageName]
forall a b. (a -> b) -> [a] -> [b]
map Dependency -> PackageName
depPkgName ([Dependency] -> [PackageName]) -> [Dependency] -> [PackageName]
forall a b. (a -> b) -> a -> b
$ (TestSuite -> [Dependency]) -> [TestSuite] -> [Dependency]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (BuildInfo -> [Dependency]
targetBuildDepends (BuildInfo -> [Dependency])
-> (TestSuite -> BuildInfo) -> TestSuite -> [Dependency]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TestSuite -> BuildInfo
testBuildInfo) (PackageDescription -> [TestSuite]
testSuites PackageDescription
pkgDesc)
packageVersion :: PackageIdentifier -> String
packageVersion :: PackageIdentifier -> FilePath
packageVersion =
#if MIN_VERSION_Cabal(2,2,0)
Version -> FilePath
forall a. Pretty a => a -> FilePath
prettyShow (Version -> FilePath)
-> (PackageIdentifier -> Version) -> PackageIdentifier -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageIdentifier -> Version
pkgVersion
#else
showVersion . pkgVersion
#endif
showPkgId :: PackageIdentifier -> String
showPkgId :: PackageIdentifier -> FilePath
showPkgId pkgid :: PackageIdentifier
pkgid =
#if MIN_VERSION_Cabal(2,2,0)
PackageIdentifier -> FilePath
forall a. Pretty a => a -> FilePath
prettyShow PackageIdentifier
pkgid
#else
unPackageName (packageName pkgid) ++ "-" ++ packageVersion pkgid
#endif
#if !MIN_VERSION_Cabal(1,22,0)
unPackageName :: PackageName -> String
unPackageName (PackageName n) = n
#endif
#if MIN_VERSION_Cabal(2,2,0)
showVersion :: Distribution.Version.Version -> String
showVersion :: Version -> FilePath
showVersion = Version -> FilePath
forall a. Pretty a => a -> FilePath
prettyShow
#endif
#if !MIN_VERSION_Cabal(2,0,0)
mkFlagName :: String -> FlagName
mkFlagName = FlagName
#endif
tryFindPackageDesc :: FilePath -> IO FilePath
tryFindPackageDesc :: FilePath -> IO FilePath
tryFindPackageDesc =
#if MIN_VERSION_Cabal(1,20,0)
Verbosity -> FilePath -> IO FilePath
DSU.tryFindPackageDesc
#if MIN_VERSION_Cabal(3,0,0)
Verbosity
normal
#endif
#else
DSU.findPackageDesc
#endif
#if !MIN_VERSION_Cabal(1,20,0)
licenseFiles :: PackageDescription -> [FilePath]
licenseFiles pkgDesc =
[licenseFile pkgDesc | licenseFile pkgDesc /= ""]
#endif
#if MIN_VERSION_Cabal(2,4,0)
buildDepends :: PackageDescription -> [Dependency]
buildDepends :: PackageDescription -> [Dependency]
buildDepends = (PackageDescription -> ComponentRequestedSpec -> [Dependency])
-> ComponentRequestedSpec -> PackageDescription -> [Dependency]
forall a b c. (a -> b -> c) -> b -> a -> c
flip PackageDescription -> ComponentRequestedSpec -> [Dependency]
enabledBuildDepends ComponentRequestedSpec
defaultComponentRequestedSpec
#endif
#if MIN_VERSION_Cabal(2,0,0)
exeDepName :: LegacyExeDependency -> String
exeDepName :: LegacyExeDependency -> FilePath
exeDepName (LegacyExeDependency n :: FilePath
n _) = FilePath
n
pkgcfgDepName :: PkgconfigDependency -> String
pkgcfgDepName :: PkgconfigDependency -> FilePath
pkgcfgDepName (PkgconfigDependency n :: PkgconfigName
n _) = PkgconfigName -> FilePath
unPkgconfigName PkgconfigName
n
#else
depPkgName :: Dependency -> PackageName
depPkgName (Dependency pn _) = pn
exeDepName :: Dependency -> String
exeDepName = unPackageName . depPkgName
pkgcfgDepName :: Dependency -> String
pkgcfgDepName = unPackageName . depPkgName
#endif
#if !MIN_VERSION_Cabal(2,0,0)
mkPackageName :: String -> PackageName
mkPackageName = PackageName
#endif
setupDependencies :: PackageDescription
-> [PackageName]
#if MIN_VERSION_Cabal(1,24,0)
setupDependencies :: PackageDescription -> [PackageName]
setupDependencies pkgDesc :: PackageDescription
pkgDesc =
[PackageName]
-> (SetupBuildInfo -> [PackageName])
-> Maybe SetupBuildInfo
-> [PackageName]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] ((Dependency -> PackageName) -> [Dependency] -> [PackageName]
forall a b. (a -> b) -> [a] -> [b]
map Dependency -> PackageName
depPkgName ([Dependency] -> [PackageName])
-> (SetupBuildInfo -> [Dependency])
-> SetupBuildInfo
-> [PackageName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SetupBuildInfo -> [Dependency]
setupDepends) (PackageDescription -> Maybe SetupBuildInfo
setupBuildInfo PackageDescription
pkgDesc)
#else
setupDependencies _pkgDesc = []
#endif
#if !MIN_VERSION_Cabal(2,0,0)
allLibraries :: PackageDescription -> [Library]
allLibraries = maybeToList . library
#endif