{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
module Stack.Package
(readDotBuildinfo
,resolvePackage
,packageFromPackageDescription
,Package(..)
,PackageDescriptionPair(..)
,GetPackageFiles(..)
,GetPackageOpts(..)
,PackageConfig(..)
,buildLogPath
,PackageException (..)
,resolvePackageDescription
,packageDependencies
,applyForceCustomBuild
) where
import Data.List (find, isPrefixOf, unzip)
import Data.Maybe (maybe)
import qualified Data.Map.Strict as M
import qualified Data.Set as S
import qualified Data.Text as T
import Distribution.Compiler
import Distribution.ModuleName (ModuleName)
import qualified Distribution.ModuleName as Cabal
import qualified Distribution.Package as D
import Distribution.Package hiding (Package,PackageName,packageName,packageVersion,PackageIdentifier)
import qualified Distribution.PackageDescription as D
import Distribution.PackageDescription hiding (FlagName)
import Distribution.PackageDescription.Parsec
import Distribution.Simple.Glob (matchDirFileGlob)
import Distribution.System (OS (..), Arch, Platform (..))
import qualified Distribution.Text as D
import qualified Distribution.Types.CondTree as Cabal
import qualified Distribution.Types.ExeDependency as Cabal
import Distribution.Types.ForeignLib
import qualified Distribution.Types.LegacyExeDependency as Cabal
import Distribution.Types.MungedPackageName
import qualified Distribution.Types.UnqualComponentName as Cabal
import qualified Distribution.Verbosity as D
import Distribution.Version (mkVersion, orLaterVersion, anyVersion)
import qualified HiFileParser as Iface
import Path as FL
import Path.Extra
import Path.IO hiding (findFiles)
import Stack.Build.Installed
import Stack.Constants
import Stack.Constants.Config
import Stack.Prelude hiding (Display (..))
import Stack.Types.Compiler
import Stack.Types.Config
import Stack.Types.GhcPkgId
import Stack.Types.NamedComponent
import Stack.Types.Package
import Stack.Types.Version
import qualified System.Directory as D
import System.FilePath (replaceExtension)
import qualified System.FilePath as FilePath
import System.IO.Error
import RIO.Process
import RIO.PrettyPrint
import qualified RIO.PrettyPrint as PP (Style (Module))
data Ctx = Ctx { ctxFile :: !(Path Abs File)
, ctxDistDir :: !(Path Abs Dir)
, ctxBuildConfig :: !BuildConfig
}
instance HasPlatform Ctx
instance HasGHCVariant Ctx
instance HasLogFunc Ctx where
logFuncL = configL.logFuncL
instance HasRunner Ctx where
runnerL = configL.runnerL
instance HasStylesUpdate Ctx where
stylesUpdateL = runnerL.stylesUpdateL
instance HasTerm Ctx where
useColorL = runnerL.useColorL
termWidthL = runnerL.termWidthL
instance HasConfig Ctx
instance HasPantryConfig Ctx where
pantryConfigL = configL.pantryConfigL
instance HasProcessContext Ctx where
processContextL = configL.processContextL
instance HasBuildConfig Ctx where
buildConfigL = lens ctxBuildConfig (\x y -> x { ctxBuildConfig = y })
readDotBuildinfo :: MonadIO m
=> Path Abs File
-> m HookedBuildInfo
readDotBuildinfo buildinfofp =
liftIO $ readHookedBuildInfo D.silent (toFilePath buildinfofp)
resolvePackage :: PackageConfig
-> GenericPackageDescription
-> Package
resolvePackage packageConfig gpkg =
packageFromPackageDescription
packageConfig
(genPackageFlags gpkg)
(resolvePackageDescription packageConfig gpkg)
packageFromPackageDescription :: PackageConfig
-> [D.Flag]
-> PackageDescriptionPair
-> Package
packageFromPackageDescription packageConfig pkgFlags (PackageDescriptionPair pkgNoMod pkg) =
Package
{ packageName = name
, packageVersion = pkgVersion pkgId
, packageLicense = licenseRaw pkg
, packageDeps = deps
, packageFiles = pkgFiles
, packageUnknownTools = unknownTools
, packageGhcOptions = packageConfigGhcOptions packageConfig
, packageCabalConfigOpts = packageConfigCabalConfigOpts packageConfig
, packageFlags = packageConfigFlags packageConfig
, packageDefaultFlags = M.fromList
[(flagName flag, flagDefault flag) | flag <- pkgFlags]
, packageAllDeps = S.fromList (M.keys deps)
, packageLibraries =
let mlib = do
lib <- library pkg
guard $ buildable $ libBuildInfo lib
Just lib
in
case mlib of
Nothing -> NoLibraries
Just _ -> HasLibraries foreignLibNames
, packageInternalLibraries = subLibNames
, packageTests = M.fromList
[(T.pack (Cabal.unUnqualComponentName $ testName t), testInterface t)
| t <- testSuites pkgNoMod
, buildable (testBuildInfo t)
]
, packageBenchmarks = S.fromList
[T.pack (Cabal.unUnqualComponentName $ benchmarkName b)
| b <- benchmarks pkgNoMod
, buildable (benchmarkBuildInfo b)
]
, packageExes = S.fromList
[T.pack (Cabal.unUnqualComponentName $ exeName biBuildInfo)
| biBuildInfo <- executables pkg
, buildable (buildInfo biBuildInfo)]
, packageOpts = GetPackageOpts $
\installMap installedMap omitPkgs addPkgs cabalfp ->
do (componentsModules,componentFiles,_,_) <- getPackageFiles pkgFiles cabalfp
let internals = S.toList $ internalLibComponents $ M.keysSet componentsModules
excludedInternals <- mapM (parsePackageNameThrowing . T.unpack) internals
mungedInternals <- mapM (parsePackageNameThrowing . T.unpack .
toInternalPackageMungedName) internals
componentsOpts <-
generatePkgDescOpts installMap installedMap
(excludedInternals ++ omitPkgs) (mungedInternals ++ addPkgs)
cabalfp pkg componentFiles
return (componentsModules,componentFiles,componentsOpts)
, packageHasExposedModules = maybe
False
(not . null . exposedModules)
(library pkg)
, packageBuildType = buildType pkg
, packageSetupDeps = msetupDeps
, packageCabalSpec = either orLaterVersion id $ specVersionRaw pkg
}
where
extraLibNames = S.union subLibNames foreignLibNames
subLibNames
= S.fromList
$ map (T.pack . Cabal.unUnqualComponentName)
$ mapMaybe libName
$ filter (buildable . libBuildInfo)
$ subLibraries pkg
foreignLibNames
= S.fromList
$ map (T.pack . Cabal.unUnqualComponentName . foreignLibName)
$ filter (buildable . foreignLibBuildInfo)
$ foreignLibs pkg
toInternalPackageMungedName
= T.pack . unMungedPackageName . computeCompatPackageName (pkgName pkgId)
. Just . Cabal.mkUnqualComponentName . T.unpack
pkgFiles = GetPackageFiles $
\cabalfp -> debugBracket ("getPackageFiles" <+> pretty cabalfp) $ do
let pkgDir = parent cabalfp
distDir <- distDirFromDir pkgDir
bc <- view buildConfigL
(componentModules,componentFiles,dataFiles',warnings) <-
runRIO
(Ctx cabalfp distDir bc)
(packageDescModulesAndFiles pkg)
setupFiles <-
if buildType pkg == Custom
then do
let setupHsPath = pkgDir </> relFileSetupHs
setupLhsPath = pkgDir </> relFileSetupLhs
setupHsExists <- doesFileExist setupHsPath
if setupHsExists then return (S.singleton setupHsPath) else do
setupLhsExists <- doesFileExist setupLhsPath
if setupLhsExists then return (S.singleton setupLhsPath) else return S.empty
else return S.empty
buildFiles <- liftM (S.insert cabalfp . S.union setupFiles) $ do
let hpackPath = pkgDir </> relFileHpackPackageConfig
hpackExists <- doesFileExist hpackPath
return $ if hpackExists then S.singleton hpackPath else S.empty
return (componentModules, componentFiles, buildFiles <> dataFiles', warnings)
pkgId = package pkg
name = pkgName pkgId
(unknownTools, knownTools) = packageDescTools pkg
deps = M.filterWithKey (const . not . isMe) (M.unionsWith (<>)
[ asLibrary <$> packageDependencies packageConfig pkg
, asLibrary <$> fromMaybe M.empty msetupDeps
, knownTools
])
msetupDeps = fmap
(M.fromList . map (depName &&& depRange) . setupDepends)
(setupBuildInfo pkg)
asLibrary range = DepValue
{ dvVersionRange = range
, dvType = AsLibrary
}
isMe name' = name' == name || fromString (packageNameString name') `S.member` extraLibNames
generatePkgDescOpts
:: (HasEnvConfig env, MonadThrow m, MonadReader env m, MonadIO m)
=> InstallMap
-> InstalledMap
-> [PackageName]
-> [PackageName]
-> Path Abs File
-> PackageDescription
-> Map NamedComponent [DotCabalPath]
-> m (Map NamedComponent BuildInfoOpts)
generatePkgDescOpts installMap installedMap omitPkgs addPkgs cabalfp pkg componentPaths = do
config <- view configL
cabalVer <- view cabalVersionL
distDir <- distDirFromDir cabalDir
let generate namedComponent binfo =
( namedComponent
, generateBuildInfoOpts BioInput
{ biInstallMap = installMap
, biInstalledMap = installedMap
, biCabalDir = cabalDir
, biDistDir = distDir
, biOmitPackages = omitPkgs
, biAddPackages = addPkgs
, biBuildInfo = binfo
, biDotCabalPaths = fromMaybe [] (M.lookup namedComponent componentPaths)
, biConfigLibDirs = configExtraLibDirs config
, biConfigIncludeDirs = configExtraIncludeDirs config
, biComponentName = namedComponent
, biCabalVersion = cabalVer
}
)
return
( M.fromList
(concat
[ maybe
[]
(return . generate CLib . libBuildInfo)
(library pkg)
, mapMaybe
(\sublib -> do
let maybeLib = CInternalLib . T.pack . Cabal.unUnqualComponentName <$> libName sublib
flip generate (libBuildInfo sublib) <$> maybeLib
)
(subLibraries pkg)
, fmap
(\exe ->
generate
(CExe (T.pack (Cabal.unUnqualComponentName (exeName exe))))
(buildInfo exe))
(executables pkg)
, fmap
(\bench ->
generate
(CBench (T.pack (Cabal.unUnqualComponentName (benchmarkName bench))))
(benchmarkBuildInfo bench))
(benchmarks pkg)
, fmap
(\test ->
generate
(CTest (T.pack (Cabal.unUnqualComponentName (testName test))))
(testBuildInfo test))
(testSuites pkg)]))
where
cabalDir = parent cabalfp
data BioInput = BioInput
{ biInstallMap :: !InstallMap
, biInstalledMap :: !InstalledMap
, biCabalDir :: !(Path Abs Dir)
, biDistDir :: !(Path Abs Dir)
, biOmitPackages :: ![PackageName]
, biAddPackages :: ![PackageName]
, biBuildInfo :: !BuildInfo
, biDotCabalPaths :: ![DotCabalPath]
, biConfigLibDirs :: ![FilePath]
, biConfigIncludeDirs :: ![FilePath]
, biComponentName :: !NamedComponent
, biCabalVersion :: !Version
}
generateBuildInfoOpts :: BioInput -> BuildInfoOpts
generateBuildInfoOpts BioInput {..} =
BuildInfoOpts
{ bioOpts = ghcOpts ++ cppOptions biBuildInfo
, bioOneWordOpts = nubOrd $ concat
[extOpts, srcOpts, includeOpts, libOpts, fworks, cObjectFiles]
, bioPackageFlags = deps
, bioCabalMacros = componentAutogen </> relFileCabalMacrosH
}
where
cObjectFiles =
mapMaybe (fmap toFilePath .
makeObjectFilePathFromC biCabalDir biComponentName biDistDir)
cfiles
cfiles = mapMaybe dotCabalCFilePath biDotCabalPaths
installVersion = snd
deps =
concat
[ case M.lookup name biInstalledMap of
Just (_, Stack.Types.Package.Library _ident ipid _) -> ["-package-id=" <> ghcPkgIdString ipid]
_ -> ["-package=" <> packageNameString name <>
maybe ""
((("-" <>) . versionString) . installVersion)
(M.lookup name biInstallMap)]
| name <- pkgs]
pkgs =
biAddPackages ++
[ name
| Dependency name _ <- targetBuildDepends biBuildInfo
, name `notElem` biOmitPackages]
ghcOpts = concatMap snd . filter (isGhc . fst) $ options biBuildInfo
where
isGhc GHC = True
isGhc _ = False
extOpts = map (("-X" ++) . D.display) (usedExtensions biBuildInfo)
srcOpts =
map (("-i" <>) . toFilePathNoTrailingSep)
(concat
[ [ componentBuildDir biCabalVersion biComponentName biDistDir ]
, [ biCabalDir
| null (hsSourceDirs biBuildInfo)
]
, mapMaybe toIncludeDir (hsSourceDirs biBuildInfo)
, [ componentAutogen ]
, maybeToList (packageAutogenDir biCabalVersion biDistDir)
, [ componentOutputDir biComponentName biDistDir ]
]) ++
[ "-stubdir=" ++ toFilePathNoTrailingSep (buildDir biDistDir) ]
componentAutogen = componentAutogenDir biCabalVersion biComponentName biDistDir
toIncludeDir "." = Just biCabalDir
toIncludeDir relDir = concatAndColapseAbsDir biCabalDir relDir
includeOpts =
map ("-I" <>) (biConfigIncludeDirs <> pkgIncludeOpts)
pkgIncludeOpts =
[ toFilePathNoTrailingSep absDir
| dir <- includeDirs biBuildInfo
, absDir <- handleDir dir
]
libOpts =
map ("-l" <>) (extraLibs biBuildInfo) <>
map ("-L" <>) (biConfigLibDirs <> pkgLibDirs)
pkgLibDirs =
[ toFilePathNoTrailingSep absDir
| dir <- extraLibDirs biBuildInfo
, absDir <- handleDir dir
]
handleDir dir = case (parseAbsDir dir, parseRelDir dir) of
(Just ab, _ ) -> [ab]
(_ , Just rel) -> [biCabalDir </> rel]
(Nothing, Nothing ) -> []
fworks = map (\fwk -> "-framework=" <> fwk) (frameworks biBuildInfo)
makeObjectFilePathFromC
:: MonadThrow m
=> Path Abs Dir
-> NamedComponent
-> Path Abs Dir
-> Path Abs File
-> m (Path Abs File)
makeObjectFilePathFromC cabalDir namedComponent distDir cFilePath = do
relCFilePath <- stripProperPrefix cabalDir cFilePath
relOFilePath <-
parseRelFile (replaceExtension (toFilePath relCFilePath) "o")
return (componentOutputDir namedComponent distDir </> relOFilePath)
packageAutogenDir :: Version -> Path Abs Dir -> Maybe (Path Abs Dir)
packageAutogenDir cabalVer distDir
| cabalVer < mkVersion [2, 0] = Nothing
| otherwise = Just $ buildDir distDir </> relDirGlobalAutogen
componentAutogenDir :: Version -> NamedComponent -> Path Abs Dir -> Path Abs Dir
componentAutogenDir cabalVer component distDir =
componentBuildDir cabalVer component distDir </> relDirAutogen
componentBuildDir :: Version -> NamedComponent -> Path Abs Dir -> Path Abs Dir
componentBuildDir cabalVer component distDir
| cabalVer < mkVersion [2, 0] = buildDir distDir
| otherwise =
case component of
CLib -> buildDir distDir
CInternalLib name -> buildDir distDir </> componentNameToDir name
CExe name -> buildDir distDir </> componentNameToDir name
CTest name -> buildDir distDir </> componentNameToDir name
CBench name -> buildDir distDir </> componentNameToDir name
componentOutputDir :: NamedComponent -> Path Abs Dir -> Path Abs Dir
componentOutputDir namedComponent distDir =
case namedComponent of
CLib -> buildDir distDir
CInternalLib name -> makeTmp name
CExe name -> makeTmp name
CTest name -> makeTmp name
CBench name -> makeTmp name
where
makeTmp name =
buildDir distDir </> componentNameToDir (name <> "/" <> name <> "-tmp")
buildDir :: Path Abs Dir -> Path Abs Dir
buildDir distDir = distDir </> relDirBuild
componentNameToDir :: Text -> Path Rel Dir
componentNameToDir name =
fromMaybe (error "Invariant violated: component names should always parse as directory names")
(parseRelDir (T.unpack name))
packageDependencies
:: PackageConfig
-> PackageDescription
-> Map PackageName VersionRange
packageDependencies pkgConfig pkg' =
M.fromListWith intersectVersionRanges $
map (depName &&& depRange) $
concatMap targetBuildDepends (allBuildInfo' pkg) ++
maybe [] setupDepends (setupBuildInfo pkg)
where
pkg
| getGhcVersion (packageConfigCompilerVersion pkgConfig) >= mkVersion [8, 0] = pkg'
| otherwise = pkg'
{ library = (\c -> c { libBuildInfo = go (libBuildInfo c) }) <$> library pkg'
, executables = (\c -> c { buildInfo = go (buildInfo c) }) <$> executables pkg'
, testSuites =
if packageConfigEnableTests pkgConfig
then (\c -> c { testBuildInfo = go (testBuildInfo c) }) <$> testSuites pkg'
else testSuites pkg'
, benchmarks =
if packageConfigEnableBenchmarks pkgConfig
then (\c -> c { benchmarkBuildInfo = go (benchmarkBuildInfo c) }) <$> benchmarks pkg'
else benchmarks pkg'
}
go bi = bi { buildable = True }
packageDescTools
:: PackageDescription
-> (Set ExeName, Map PackageName DepValue)
packageDescTools pd =
(S.fromList $ concat unknowns, M.fromListWith (<>) $ concat knowns)
where
(unknowns, knowns) = unzip $ map perBI $ allBuildInfo' pd
perBI :: BuildInfo -> ([ExeName], [(PackageName, DepValue)])
perBI bi =
(unknownTools, tools)
where
(unknownTools, knownTools) = partitionEithers $ map go1 (buildTools bi)
tools = mapMaybe go2 (knownTools ++ buildToolDepends bi)
go1 :: Cabal.LegacyExeDependency -> Either ExeName Cabal.ExeDependency
go1 (Cabal.LegacyExeDependency name range) =
case M.lookup name hardCodedMap of
Just pkgName -> Right $ Cabal.ExeDependency pkgName (Cabal.mkUnqualComponentName name) range
Nothing -> Left $ ExeName $ T.pack name
go2 :: Cabal.ExeDependency -> Maybe (PackageName, DepValue)
go2 (Cabal.ExeDependency pkg _name range)
| pkg `S.member` preInstalledPackages = Nothing
| otherwise = Just
( pkg
, DepValue
{ dvVersionRange = range
, dvType = AsBuildTool
}
)
hardCodedMap :: Map String D.PackageName
hardCodedMap = M.fromList
[ ("alex", Distribution.Package.mkPackageName "alex")
, ("happy", Distribution.Package.mkPackageName "happy")
, ("cpphs", Distribution.Package.mkPackageName "cpphs")
, ("greencard", Distribution.Package.mkPackageName "greencard")
, ("c2hs", Distribution.Package.mkPackageName "c2hs")
, ("hscolour", Distribution.Package.mkPackageName "hscolour")
, ("hspec-discover", Distribution.Package.mkPackageName "hspec-discover")
, ("hsx2hs", Distribution.Package.mkPackageName "hsx2hs")
, ("gtk2hsC2hs", Distribution.Package.mkPackageName "gtk2hs-buildtools")
, ("gtk2hsHookGenerator", Distribution.Package.mkPackageName "gtk2hs-buildtools")
, ("gtk2hsTypeGen", Distribution.Package.mkPackageName "gtk2hs-buildtools")
]
preInstalledPackages :: Set D.PackageName
preInstalledPackages = S.fromList
[ D.mkPackageName "hsc2hs"
, D.mkPackageName "haddock"
]
allBuildInfo' :: PackageDescription -> [BuildInfo]
allBuildInfo' pkg_descr = [ bi | lib <- allLibraries pkg_descr
, let bi = libBuildInfo lib
, buildable bi ]
++ [ bi | flib <- foreignLibs pkg_descr
, let bi = foreignLibBuildInfo flib
, buildable bi ]
++ [ bi | exe <- executables pkg_descr
, let bi = buildInfo exe
, buildable bi ]
++ [ bi | tst <- testSuites pkg_descr
, let bi = testBuildInfo tst
, buildable bi ]
++ [ bi | tst <- benchmarks pkg_descr
, let bi = benchmarkBuildInfo tst
, buildable bi ]
packageDescModulesAndFiles
:: PackageDescription
-> RIO Ctx (Map NamedComponent (Map ModuleName (Path Abs File)), Map NamedComponent [DotCabalPath], Set (Path Abs File), [PackageWarning])
packageDescModulesAndFiles pkg = do
(libraryMods,libDotCabalFiles,libWarnings) <-
maybe
(return (M.empty, M.empty, []))
(asModuleAndFileMap libComponent libraryFiles)
(library pkg)
(subLibrariesMods,subLibDotCabalFiles,subLibWarnings) <-
liftM
foldTuples
(mapM
(asModuleAndFileMap internalLibComponent libraryFiles)
(subLibraries pkg))
(executableMods,exeDotCabalFiles,exeWarnings) <-
liftM
foldTuples
(mapM
(asModuleAndFileMap exeComponent executableFiles)
(executables pkg))
(testMods,testDotCabalFiles,testWarnings) <-
liftM
foldTuples
(mapM (asModuleAndFileMap testComponent testFiles) (testSuites pkg))
(benchModules,benchDotCabalPaths,benchWarnings) <-
liftM
foldTuples
(mapM
(asModuleAndFileMap benchComponent benchmarkFiles)
(benchmarks pkg))
dfiles <- resolveGlobFiles (specVersion pkg)
(extraSrcFiles pkg
++ map (dataDir pkg FilePath.</>) (dataFiles pkg))
let modules = libraryMods <> subLibrariesMods <> executableMods <> testMods <> benchModules
files =
libDotCabalFiles <> subLibDotCabalFiles <> exeDotCabalFiles <> testDotCabalFiles <>
benchDotCabalPaths
warnings = libWarnings <> subLibWarnings <> exeWarnings <> testWarnings <> benchWarnings
return (modules, files, dfiles, warnings)
where
libComponent = const CLib
internalLibComponent = CInternalLib . T.pack . maybe "" Cabal.unUnqualComponentName . libName
exeComponent = CExe . T.pack . Cabal.unUnqualComponentName . exeName
testComponent = CTest . T.pack . Cabal.unUnqualComponentName . testName
benchComponent = CBench . T.pack . Cabal.unUnqualComponentName . benchmarkName
asModuleAndFileMap label f lib = do
(a,b,c) <- f (label lib) lib
return (M.singleton (label lib) a, M.singleton (label lib) b, c)
foldTuples = foldl' (<>) (M.empty, M.empty, [])
resolveGlobFiles
:: Version
-> [String]
-> RIO Ctx (Set (Path Abs File))
resolveGlobFiles cabalFileVersion =
liftM (S.fromList . catMaybes . concat) .
mapM resolve
where
resolve name =
if '*' `elem` name
then explode name
else liftM return (resolveFileOrWarn name)
explode name = do
dir <- asks (parent . ctxFile)
names <-
matchDirFileGlob'
(FL.toFilePath dir)
name
mapM resolveFileOrWarn names
matchDirFileGlob' dir glob =
catch
(liftIO (matchDirFileGlob minBound cabalFileVersion dir glob))
(\(e :: IOException) ->
if isUserError e
then do
prettyWarnL
[ flow "Wildcard does not match any files:"
, style File $ fromString glob
, line <> flow "in directory:"
, style Dir $ fromString dir
]
return []
else throwIO e)
benchmarkFiles
:: NamedComponent
-> Benchmark
-> RIO Ctx (Map ModuleName (Path Abs File), [DotCabalPath], [PackageWarning])
benchmarkFiles component bench = do
resolveComponentFiles component build names
where
names = bnames <> exposed
exposed =
case benchmarkInterface bench of
BenchmarkExeV10 _ fp -> [DotCabalMain fp]
BenchmarkUnsupported _ -> []
bnames = map DotCabalModule (otherModules build)
build = benchmarkBuildInfo bench
testFiles
:: NamedComponent
-> TestSuite
-> RIO Ctx (Map ModuleName (Path Abs File), [DotCabalPath], [PackageWarning])
testFiles component test = do
resolveComponentFiles component build names
where
names = bnames <> exposed
exposed =
case testInterface test of
TestSuiteExeV10 _ fp -> [DotCabalMain fp]
TestSuiteLibV09 _ mn -> [DotCabalModule mn]
TestSuiteUnsupported _ -> []
bnames = map DotCabalModule (otherModules build)
build = testBuildInfo test
executableFiles
:: NamedComponent
-> Executable
-> RIO Ctx (Map ModuleName (Path Abs File), [DotCabalPath], [PackageWarning])
executableFiles component exe = do
resolveComponentFiles component build names
where
build = buildInfo exe
names =
map DotCabalModule (otherModules build) ++
[DotCabalMain (modulePath exe)]
libraryFiles
:: NamedComponent
-> Library
-> RIO Ctx (Map ModuleName (Path Abs File), [DotCabalPath], [PackageWarning])
libraryFiles component lib = do
resolveComponentFiles component build names
where
build = libBuildInfo lib
names = bnames ++ exposed
exposed = map DotCabalModule (exposedModules lib)
bnames = map DotCabalModule (otherModules build)
resolveComponentFiles
:: NamedComponent
-> BuildInfo
-> [DotCabalDescriptor]
-> RIO Ctx (Map ModuleName (Path Abs File), [DotCabalPath], [PackageWarning])
resolveComponentFiles component build names = do
dirs <- mapMaybeM resolveDirOrWarn (hsSourceDirs build)
dir <- asks (parent . ctxFile)
(modules,files,warnings) <-
resolveFilesAndDeps
component
(if null dirs then [dir] else dirs)
names
cfiles <- buildOtherSources build
return (modules, files <> cfiles, warnings)
buildOtherSources :: BuildInfo -> RIO Ctx [DotCabalPath]
buildOtherSources build = do
cwd <- liftIO getCurrentDir
dir <- asks (parent . ctxFile)
file <- asks ctxFile
let resolveDirFiles files toCabalPath =
forMaybeM files $ \fp -> do
result <- resolveDirFile dir fp
case result of
Nothing -> do
warnMissingFile "File" cwd fp file
return Nothing
Just p -> return $ Just (toCabalPath p)
csources <- resolveDirFiles (cSources build) DotCabalCFilePath
jsources <- resolveDirFiles (targetJsSources build) DotCabalFilePath
return (csources <> jsources)
targetJsSources :: BuildInfo -> [FilePath]
targetJsSources = jsSources
data PackageDescriptionPair = PackageDescriptionPair
{ pdpOrigBuildable :: PackageDescription
, pdpModifiedBuildable :: PackageDescription
}
resolvePackageDescription :: PackageConfig
-> GenericPackageDescription
-> PackageDescriptionPair
resolvePackageDescription packageConfig (GenericPackageDescription desc defaultFlags mlib subLibs foreignLibs' exes tests benches) =
PackageDescriptionPair
{ pdpOrigBuildable = go False
, pdpModifiedBuildable = go True
}
where
go modBuildable =
desc {library =
fmap (resolveConditions rc updateLibDeps) mlib
,subLibraries =
map (\(n, v) -> (resolveConditions rc updateLibDeps v){libName=Just n})
subLibs
,foreignLibs =
map (\(n, v) -> (resolveConditions rc updateForeignLibDeps v){foreignLibName=n})
foreignLibs'
,executables =
map (\(n, v) -> (resolveConditions rc updateExeDeps v){exeName=n})
exes
,testSuites =
map (\(n,v) -> (resolveConditions rc (updateTestDeps modBuildable) v){testName=n})
tests
,benchmarks =
map (\(n,v) -> (resolveConditions rc (updateBenchmarkDeps modBuildable) v){benchmarkName=n})
benches}
flags =
M.union (packageConfigFlags packageConfig)
(flagMap defaultFlags)
rc = mkResolveConditions
(packageConfigCompilerVersion packageConfig)
(packageConfigPlatform packageConfig)
flags
updateLibDeps lib deps =
lib {libBuildInfo =
(libBuildInfo lib) {targetBuildDepends = deps}}
updateForeignLibDeps lib deps =
lib {foreignLibBuildInfo =
(foreignLibBuildInfo lib) {targetBuildDepends = deps}}
updateExeDeps exe deps =
exe {buildInfo =
(buildInfo exe) {targetBuildDepends = deps}}
updateTestDeps modBuildable test deps =
let bi = testBuildInfo test
bi' = bi
{ targetBuildDepends = deps
, buildable = buildable bi && (if modBuildable then packageConfigEnableTests packageConfig else True)
}
in test { testBuildInfo = bi' }
updateBenchmarkDeps modBuildable benchmark deps =
let bi = benchmarkBuildInfo benchmark
bi' = bi
{ targetBuildDepends = deps
, buildable = buildable bi && (if modBuildable then packageConfigEnableBenchmarks packageConfig else True)
}
in benchmark { benchmarkBuildInfo = bi' }
flagMap :: [Flag] -> Map FlagName Bool
flagMap = M.fromList . map pair
where pair :: Flag -> (FlagName, Bool)
pair = flagName &&& flagDefault
data ResolveConditions = ResolveConditions
{ rcFlags :: Map FlagName Bool
, rcCompilerVersion :: ActualCompiler
, rcOS :: OS
, rcArch :: Arch
}
mkResolveConditions :: ActualCompiler
-> Platform
-> Map FlagName Bool
-> ResolveConditions
mkResolveConditions compilerVersion (Platform arch os) flags = ResolveConditions
{ rcFlags = flags
, rcCompilerVersion = compilerVersion
, rcOS = os
, rcArch = arch
}
resolveConditions :: (Semigroup target,Monoid target,Show target)
=> ResolveConditions
-> (target -> cs -> target)
-> CondTree ConfVar cs target
-> target
resolveConditions rc addDeps (CondNode lib deps cs) = basic <> children
where basic = addDeps lib deps
children = mconcat (map apply cs)
where apply (Cabal.CondBranch cond node mcs) =
if condSatisfied cond
then resolveConditions rc addDeps node
else maybe mempty (resolveConditions rc addDeps) mcs
condSatisfied c =
case c of
Var v -> varSatisifed v
Lit b -> b
CNot c' ->
not (condSatisfied c')
COr cx cy ->
condSatisfied cx || condSatisfied cy
CAnd cx cy ->
condSatisfied cx && condSatisfied cy
varSatisifed v =
case v of
OS os -> os == rcOS rc
Arch arch -> arch == rcArch rc
Flag flag ->
fromMaybe False $ M.lookup flag (rcFlags rc)
Impl flavor range ->
case (flavor, rcCompilerVersion rc) of
(GHC, ACGhc vghc) -> vghc `withinRange` range
(GHC, ACGhcjs _ vghc) -> vghc `withinRange` range
(GHCJS, ACGhcjs vghcjs _) ->
vghcjs `withinRange` range
_ -> False
depName :: Dependency -> PackageName
depName (Dependency n _) = n
depRange :: Dependency -> VersionRange
depRange (Dependency _ r) = r
resolveFilesAndDeps
:: NamedComponent
-> [Path Abs Dir]
-> [DotCabalDescriptor]
-> RIO Ctx (Map ModuleName (Path Abs File),[DotCabalPath],[PackageWarning])
resolveFilesAndDeps component dirs names0 = do
(dotCabalPaths, foundModules, missingModules) <- loop names0 S.empty
warnings <- liftM2 (++) (warnUnlisted foundModules) (warnMissing missingModules)
return (foundModules, dotCabalPaths, warnings)
where
loop [] _ = return ([], M.empty, [])
loop names doneModules0 = do
resolved <- resolveFiles dirs names
let foundFiles = mapMaybe snd resolved
foundModules = mapMaybe toResolvedModule resolved
missingModules = mapMaybe toMissingModule resolved
pairs <- mapM (getDependencies component dirs) foundFiles
let doneModules =
S.union
doneModules0
(S.fromList (mapMaybe dotCabalModule names))
moduleDeps = S.unions (map fst pairs)
thDepFiles = concatMap snd pairs
modulesRemaining = S.difference moduleDeps doneModules
(resolvedFiles, resolvedModules, _) <-
loop (map DotCabalModule (S.toList modulesRemaining)) doneModules
return
( nubOrd $ foundFiles <> map DotCabalFilePath thDepFiles <> resolvedFiles
, M.union
(M.fromList foundModules)
resolvedModules
, missingModules)
warnUnlisted foundModules = do
let unlistedModules =
foundModules `M.difference`
M.fromList (mapMaybe (fmap (, ()) . dotCabalModule) names0)
return $
if M.null unlistedModules
then []
else [ UnlistedModulesWarning
component
(map fst (M.toList unlistedModules))]
warnMissing _missingModules = do
return []
toResolvedModule
:: (DotCabalDescriptor, Maybe DotCabalPath)
-> Maybe (ModuleName, Path Abs File)
toResolvedModule (DotCabalModule mn, Just (DotCabalModulePath fp)) =
Just (mn, fp)
toResolvedModule _ =
Nothing
toMissingModule
:: (DotCabalDescriptor, Maybe DotCabalPath)
-> Maybe ModuleName
toMissingModule (DotCabalModule mn, Nothing) =
Just mn
toMissingModule _ =
Nothing
getDependencies
:: NamedComponent -> [Path Abs Dir] -> DotCabalPath -> RIO Ctx (Set ModuleName, [Path Abs File])
getDependencies component dirs dotCabalPath =
case dotCabalPath of
DotCabalModulePath resolvedFile -> readResolvedHi resolvedFile
DotCabalMainPath resolvedFile -> readResolvedHi resolvedFile
DotCabalFilePath{} -> return (S.empty, [])
DotCabalCFilePath{} -> return (S.empty, [])
where
readResolvedHi resolvedFile = do
dumpHIDir <- componentOutputDir component <$> asks ctxDistDir
dir <- asks (parent . ctxFile)
let sourceDir = fromMaybe dir $ find (`isProperPrefixOf` resolvedFile) dirs
stripSourceDir d = stripProperPrefix d resolvedFile
case stripSourceDir sourceDir of
Nothing -> return (S.empty, [])
Just fileRel -> do
let hiPath =
FilePath.replaceExtension
(toFilePath (dumpHIDir </> fileRel))
".hi"
dumpHIExists <- liftIO $ D.doesFileExist hiPath
if dumpHIExists
then parseHI hiPath
else return (S.empty, [])
parseHI
:: FilePath -> RIO Ctx (Set ModuleName, [Path Abs File])
parseHI hiPath = do
dir <- asks (parent . ctxFile)
result <- liftIO $ Iface.fromFile hiPath
case result of
Left msg -> do
prettyWarnL
[ flow "Failed to decode module interface:"
, style File $ fromString hiPath
, flow "Decoding failure:"
, style Error $ fromString msg
]
pure (S.empty, [])
Right iface -> do
let moduleNames = fmap (fromString . T.unpack . decodeUtf8Lenient . fst) .
Iface.unList . Iface.dmods . Iface.deps
resolveFileDependency file = do
resolved <- liftIO (forgivingAbsence (resolveFile dir file)) >>= rejectMissingFile
when (isNothing resolved) $
prettyWarnL
[ flow "Dependent file listed in:"
, style File $ fromString hiPath
, flow "does not exist:"
, style File $ fromString file
]
pure resolved
resolveUsages = traverse (resolveFileDependency . Iface.unUsage) . Iface.unList . Iface.usage
resolvedUsages <- catMaybes <$> resolveUsages iface
pure (S.fromList $ moduleNames iface, resolvedUsages)
resolveFiles
:: [Path Abs Dir]
-> [DotCabalDescriptor]
-> RIO Ctx [(DotCabalDescriptor, Maybe DotCabalPath)]
resolveFiles dirs names =
forM names (\name -> liftM (name, ) (findCandidate dirs name))
data CabalFileNameParseFail
= CabalFileNameParseFail FilePath
| CabalFileNameInvalidPackageName FilePath
deriving (Typeable)
instance Exception CabalFileNameParseFail
instance Show CabalFileNameParseFail where
show (CabalFileNameParseFail fp) = "Invalid file path for cabal file, must have a .cabal extension: " ++ fp
show (CabalFileNameInvalidPackageName fp) = "cabal file names must use valid package names followed by a .cabal extension, the following is invalid: " ++ fp
parsePackageNameFromFilePath :: MonadThrow m => Path a File -> m PackageName
parsePackageNameFromFilePath fp = do
base <- clean $ toFilePath $ filename fp
case parsePackageName base of
Nothing -> throwM $ CabalFileNameInvalidPackageName $ toFilePath fp
Just x -> return x
where clean = liftM reverse . strip . reverse
strip ('l':'a':'b':'a':'c':'.':xs) = return xs
strip _ = throwM (CabalFileNameParseFail (toFilePath fp))
findCandidate
:: [Path Abs Dir]
-> DotCabalDescriptor
-> RIO Ctx (Maybe DotCabalPath)
findCandidate dirs name = do
pkg <- asks ctxFile >>= parsePackageNameFromFilePath
candidates <- liftIO makeNameCandidates
case candidates of
[candidate] -> return (Just (cons candidate))
[] -> do
case name of
DotCabalModule mn
| D.display mn /= paths_pkg pkg -> logPossibilities dirs mn
_ -> return ()
return Nothing
(candidate:rest) -> do
warnMultiple name candidate rest
return (Just (cons candidate))
where
cons =
case name of
DotCabalModule{} -> DotCabalModulePath
DotCabalMain{} -> DotCabalMainPath
DotCabalFile{} -> DotCabalFilePath
DotCabalCFile{} -> DotCabalCFilePath
paths_pkg pkg = "Paths_" ++ packageNameString pkg
makeNameCandidates =
liftM (nubOrd . concat) (mapM makeDirCandidates dirs)
makeDirCandidates :: Path Abs Dir
-> IO [Path Abs File]
makeDirCandidates dir =
case name of
DotCabalMain fp -> resolveCandidate dir fp
DotCabalFile fp -> resolveCandidate dir fp
DotCabalCFile fp -> resolveCandidate dir fp
DotCabalModule mn -> do
let perExt ext =
resolveCandidate dir (Cabal.toFilePath mn ++ "." ++ T.unpack ext)
withHaskellExts <- mapM perExt haskellFileExts
withPPExts <- mapM perExt haskellPreprocessorExts
pure $
case (concat withHaskellExts, concat withPPExts) of
([_], [y]) -> [y]
(xs, ys) -> xs ++ ys
resolveCandidate dir = fmap maybeToList . resolveDirFile dir
resolveDirFile
:: (MonadIO m, MonadThrow m)
=> Path Abs Dir -> FilePath.FilePath -> m (Maybe (Path Abs File))
resolveDirFile x y = do
p <- parseCollapsedAbsFile (toFilePath x FilePath.</> y)
exists <- doesFileExist p
return $ if exists then Just p else Nothing
warnMultiple
:: DotCabalDescriptor -> Path b t -> [Path b t] -> RIO Ctx ()
warnMultiple name candidate rest =
prettyWarnL
[ flow "There were multiple candidates for the Cabal entry"
, fromString . showName $ name
, line <> bulletedList (map dispOne (candidate:rest))
, line <> flow "picking:"
, dispOne candidate
]
where showName (DotCabalModule name') = D.display name'
showName (DotCabalMain fp) = fp
showName (DotCabalFile fp) = fp
showName (DotCabalCFile fp) = fp
dispOne = fromString . toFilePath
logPossibilities
:: HasTerm env
=> [Path Abs Dir] -> ModuleName -> RIO env ()
logPossibilities dirs mn = do
possibilities <- liftM concat (makePossibilities mn)
unless (null possibilities) $ prettyWarnL
[ flow "Unable to find a known candidate for the Cabal entry"
, (style PP.Module . fromString $ D.display mn) <> ","
, flow "but did find:"
, line <> bulletedList (map pretty possibilities)
, flow "If you are using a custom preprocessor for this module"
, flow "with its own file extension, consider adding the file(s)"
, flow "to your .cabal under extra-source-files."
]
where
makePossibilities name =
mapM
(\dir ->
do (_,files) <- listDir dir
return
(map
filename
(filter
(isPrefixOf (D.display name) .
toFilePath . filename)
files)))
dirs
buildLogPath :: (MonadReader env m, HasBuildConfig env, MonadThrow m)
=> Package -> Maybe String -> m (Path Abs File)
buildLogPath package' msuffix = do
env <- ask
let stack = getProjectWorkDir env
fp <- parseRelFile $ concat $
packageIdentifierString (packageIdentifier package') :
maybe id (\suffix -> ("-" :) . (suffix :)) msuffix [".log"]
return $ stack </> relDirLogs </> fp
resolveOrWarn :: Text
-> (Path Abs Dir -> String -> RIO Ctx (Maybe a))
-> FilePath.FilePath
-> RIO Ctx (Maybe a)
resolveOrWarn subject resolver path =
do cwd <- liftIO getCurrentDir
file <- asks ctxFile
dir <- asks (parent . ctxFile)
result <- resolver dir path
when (isNothing result) $ warnMissingFile subject cwd path file
return result
warnMissingFile :: Text -> Path Abs Dir -> FilePath -> Path Abs File -> RIO Ctx ()
warnMissingFile subject cwd path fromFile =
prettyWarnL
[ fromString . T.unpack $ subject
, flow "listed in"
, maybe (pretty fromFile) pretty (stripProperPrefix cwd fromFile)
, flow "file does not exist:"
, style Dir . fromString $ path
]
resolveFileOrWarn :: FilePath.FilePath
-> RIO Ctx (Maybe (Path Abs File))
resolveFileOrWarn = resolveOrWarn "File" f
where f p x = liftIO (forgivingAbsence (resolveFile p x)) >>= rejectMissingFile
resolveDirOrWarn :: FilePath.FilePath
-> RIO Ctx (Maybe (Path Abs Dir))
resolveDirOrWarn = resolveOrWarn "Directory" f
where f p x = liftIO (forgivingAbsence (resolveDir p x)) >>= rejectMissingDir
applyForceCustomBuild
:: Version
-> Package
-> Package
applyForceCustomBuild cabalVersion package
| forceCustomBuild =
package
{ packageBuildType = Custom
, packageDeps = M.insertWith (<>) "Cabal" (DepValue cabalVersionRange AsLibrary)
$ packageDeps package
, packageSetupDeps = Just $ M.fromList
[ ("Cabal", cabalVersionRange)
, ("base", anyVersion)
]
}
| otherwise = package
where
cabalVersionRange = packageCabalSpec package
forceCustomBuild =
packageBuildType package == Simple &&
not (cabalVersion `withinRange` cabalVersionRange)