module Stack.Package
(readPackageDir
,readPackageUnresolvedDir
,readPackageUnresolvedIndex
,readPackageDescriptionDir
,readDotBuildinfo
,resolvePackage
,packageFromPackageDescription
,Package(..)
,PackageDescriptionPair(..)
,GetPackageFiles(..)
,GetPackageOpts(..)
,PackageConfig(..)
,buildLogPath
,PackageException (..)
,resolvePackageDescription
,packageDescTools
,packageDependencies
,autogenDir
,cabalFilePackageId
,gpdPackageIdentifier
,gpdPackageName
,gpdVersion)
where
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as C8
import Data.List (isSuffixOf, partition, isPrefixOf)
import Data.List.Extra (nubOrd)
import qualified Data.Map.Strict as M
import qualified Data.Set as S
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8, decodeUtf8With)
import Data.Text.Encoding.Error (lenientDecode)
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.Parse
import qualified Distribution.PackageDescription.Parse as D
import Distribution.ParseUtils
import Distribution.Simple.Utils
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 qualified Distribution.Types.UnqualComponentName as Cabal
import qualified Distribution.Verbosity as D
import Distribution.Version (showVersion)
import Lens.Micro (lens)
import qualified Hpack
import qualified Hpack.Config as Hpack
import Path as FL
import Path.Extra
import Path.Find
import Path.IO hiding (findFiles)
import Stack.Build.Installed
import Stack.Constants
import Stack.Constants.Config
import Stack.Prelude
import Stack.PrettyPrint
import Stack.Types.Build
import Stack.Types.BuildPlan (ExeName (..))
import Stack.Types.Compiler
import Stack.Types.Config
import Stack.Types.FlagName
import Stack.Types.GhcPkgId
import Stack.Types.Package
import Stack.Types.PackageIdentifier
import Stack.Types.PackageName
import Stack.Types.Runner
import Stack.Types.Version
import qualified System.Directory as D
import System.FilePath (splitExtensions, replaceExtension)
import qualified System.FilePath as FilePath
import System.IO.Error
import System.Process.Run (runCmd, Cmd(..))
data Ctx = Ctx { ctxFile :: !(Path Abs File)
, ctxDir :: !(Path Abs Dir)
, ctxEnvConfig :: !EnvConfig
}
instance HasPlatform Ctx
instance HasGHCVariant Ctx
instance HasLogFunc Ctx where
logFuncL = configL.logFuncL
instance HasRunner Ctx where
runnerL = configL.runnerL
instance HasConfig Ctx
instance HasBuildConfig Ctx
instance HasEnvConfig Ctx where
envConfigL = lens ctxEnvConfig (\x y -> x { ctxEnvConfig = y })
rawParseGPD
:: MonadThrow m
=> Either PackageIdentifierRevision (Path Abs File)
-> BS.ByteString
-> m ([PWarning], GenericPackageDescription)
rawParseGPD key bs =
case parseGenericPackageDescription chars of
ParseFailed e -> throwM $ PackageInvalidCabalFile key e
ParseOk warnings gpkg -> return (warnings,gpkg)
where
chars = T.unpack (dropBOM (decodeUtf8With lenientDecode bs))
dropBOM t = fromMaybe t $ T.stripPrefix "\xFEFF" t
readPackageUnresolvedDir
:: forall env. HasConfig env
=> Path Abs Dir
-> Bool
-> RIO env (GenericPackageDescription, Path Abs File)
readPackageUnresolvedDir dir printWarnings = do
ref <- view $ runnerL.to runnerParsedCabalFiles
(_, m) <- readIORef ref
case M.lookup dir m of
Just x -> return x
Nothing -> do
cabalfp <- findOrGenerateCabalFile dir
bs <- liftIO $ BS.readFile $ toFilePath cabalfp
(warnings, gpd) <- rawParseGPD (Right cabalfp) bs
when printWarnings
$ mapM_ (prettyWarnL . toPretty (toFilePath cabalfp)) warnings
checkCabalFileName (gpdPackageName gpd) cabalfp
let ret = (gpd, cabalfp)
atomicModifyIORef' ref $ \(m1, m2) ->
((m1, M.insert dir ret m2), ret)
where
toPretty :: String -> PWarning -> [Doc AnsiAnn]
toPretty src (PWarning x) =
[ flow "Cabal file warning in"
, fromString src <> ":"
, flow x
]
toPretty src (UTFWarning ln msg) =
[ flow "Cabal file warning in"
, fromString src <> ":" <> fromString (show ln) <> ":"
, flow msg
]
checkCabalFileName :: MonadThrow m => PackageName -> Path Abs File -> m ()
checkCabalFileName name cabalfp = do
let expected = packageNameString name ++ ".cabal"
when (expected /= toFilePath (filename cabalfp))
$ throwM $ MismatchedCabalName cabalfp name
gpdPackageIdentifier :: GenericPackageDescription -> PackageIdentifier
gpdPackageIdentifier = fromCabalPackageIdentifier . D.package . D.packageDescription
gpdPackageName :: GenericPackageDescription -> PackageName
gpdPackageName = packageIdentifierName . gpdPackageIdentifier
gpdVersion :: GenericPackageDescription -> Version
gpdVersion = packageIdentifierVersion . gpdPackageIdentifier
readPackageUnresolvedIndex
:: forall env. HasRunner env
=> (PackageIdentifierRevision -> IO ByteString)
-> PackageIdentifierRevision
-> RIO env GenericPackageDescription
readPackageUnresolvedIndex loadFromIndex pir@(PackageIdentifierRevision pi' _) = do
ref <- view $ runnerL.to runnerParsedCabalFiles
(m, _) <- readIORef ref
case M.lookup pir m of
Just gpd -> return gpd
Nothing -> do
bs <- liftIO $ loadFromIndex pir
(_warnings, gpd) <- rawParseGPD (Left pir) bs
let foundPI =
fromCabalPackageIdentifier
$ D.package
$ D.packageDescription gpd
unless (pi' == foundPI) $ throwM $ MismatchedCabalIdentifier pir foundPI
atomicModifyIORef' ref $ \(m1, m2) ->
((M.insert pir gpd m1, m2), gpd)
readPackageDir
:: forall env. HasConfig env
=> PackageConfig
-> Path Abs Dir
-> Bool
-> RIO env (Package, Path Abs File)
readPackageDir packageConfig dir printWarnings =
first (resolvePackage packageConfig) <$> readPackageUnresolvedDir dir printWarnings
readPackageDescriptionDir
:: forall env. HasConfig env
=> PackageConfig
-> Path Abs Dir
-> Bool
-> RIO env (GenericPackageDescription, PackageDescriptionPair)
readPackageDescriptionDir config pkgDir printWarnings = do
(gdesc, _) <- readPackageUnresolvedDir pkgDir printWarnings
return (gdesc, resolvePackageDescription config gdesc)
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 = fromCabalVersion (pkgVersion pkgId)
, packageLicense = license pkg
, packageDeps = deps
, packageFiles = pkgFiles
, packageTools = packageDescTools pkg
, packageGhcOptions = packageConfigGhcOptions packageConfig
, packageFlags = packageConfigFlags packageConfig
, packageDefaultFlags = M.fromList
[(fromCabalFlagName (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
| null extraLibNames -> NoLibraries
| otherwise -> error "Package has buildable sublibraries but no buildable libraries, I'm giving up"
Just _ -> HasLibraries foreignLibNames
, 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 $
\sourceMap installedMap omitPkgs addPkgs cabalfp ->
do (componentsModules,componentFiles,_,_) <- getPackageFiles pkgFiles cabalfp
componentsOpts <-
generatePkgDescOpts sourceMap installedMap omitPkgs addPkgs cabalfp pkg componentFiles
return (componentsModules,componentFiles,componentsOpts)
, packageHasExposedModules = maybe
False
(not . null . exposedModules)
(library pkg)
, packageBuildType = buildType pkg
, packageSetupDeps = msetupDeps
}
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
pkgFiles = GetPackageFiles $
\cabalfp -> debugBracket ("getPackageFiles" <+> display cabalfp) $ do
let pkgDir = parent cabalfp
distDir <- distDirFromDir pkgDir
env <- view envConfigL
(componentModules,componentFiles,dataFiles',warnings) <-
runReaderT
(packageDescModulesAndFiles pkg)
(Ctx cabalfp (buildDir distDir) env)
setupFiles <-
if buildType pkg `elem` [Nothing, Just Custom]
then do
let setupHsPath = pkgDir </> $(mkRelFile "Setup.hs")
setupLhsPath = pkgDir </> $(mkRelFile "Setup.lhs")
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 </> $(mkRelFile Hpack.packageConfig)
hpackExists <- doesFileExist hpackPath
return $ if hpackExists then S.singleton hpackPath else S.empty
return (componentModules, componentFiles, buildFiles <> dataFiles', warnings)
pkgId = package pkg
name = fromCabalPackageName (pkgName pkgId)
deps = M.filterWithKey (const . not . isMe) (M.union
(packageDependencies pkg)
(fromMaybe M.empty msetupDeps))
msetupDeps = fmap
(M.fromList . map (depName &&& depRange) . setupDepends)
(setupBuildInfo pkg)
isMe name' = name' == name || packageNameText name' `S.member` extraLibNames
generatePkgDescOpts
:: (HasEnvConfig env, MonadThrow m, MonadReader env m, MonadIO m)
=> SourceMap
-> InstalledMap
-> [PackageName]
-> [PackageName]
-> Path Abs File
-> PackageDescription
-> Map NamedComponent (Set DotCabalPath)
-> m (Map NamedComponent BuildInfoOpts)
generatePkgDescOpts sourceMap installedMap omitPkgs addPkgs cabalfp pkg componentPaths = do
config <- view configL
distDir <- distDirFromDir cabalDir
let cabalMacros = autogenDir distDir </> $(mkRelFile "cabal_macros.h")
exists <- doesFileExist cabalMacros
let mcabalMacros =
if exists
then Just cabalMacros
else Nothing
let generate namedComponent binfo =
( namedComponent
, generateBuildInfoOpts BioInput
{ biSourceMap = sourceMap
, biInstalledMap = installedMap
, biCabalMacros = mcabalMacros
, biCabalDir = cabalDir
, biDistDir = distDir
, biOmitPackages = omitPkgs
, biAddPackages = addPkgs
, biBuildInfo = binfo
, biDotCabalPaths = fromMaybe mempty (M.lookup namedComponent componentPaths)
, biConfigLibDirs = configExtraLibDirs config
, biConfigIncludeDirs = configExtraIncludeDirs config
, biComponentName = namedComponent
}
)
return
( M.fromList
(concat
[ maybe
[]
(return . generate CLib . libBuildInfo)
(library 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
{ biSourceMap :: !SourceMap
, biInstalledMap :: !InstalledMap
, biCabalMacros :: !(Maybe (Path Abs File))
, biCabalDir :: !(Path Abs Dir)
, biDistDir :: !(Path Abs Dir)
, biOmitPackages :: ![PackageName]
, biAddPackages :: ![PackageName]
, biBuildInfo :: !BuildInfo
, biDotCabalPaths :: !(Set DotCabalPath)
, biConfigLibDirs :: !(Set FilePath)
, biConfigIncludeDirs :: !(Set FilePath)
, biComponentName :: !NamedComponent
}
generateBuildInfoOpts :: BioInput -> BuildInfoOpts
generateBuildInfoOpts BioInput {..} =
BuildInfoOpts
{ bioOpts = ghcOpts ++ cppOptions biBuildInfo
, bioOneWordOpts = nubOrd $ concat
[extOpts, srcOpts, includeOpts, libOpts, fworks, cObjectFiles]
, bioPackageFlags = deps
, bioCabalMacros = biCabalMacros
}
where
cObjectFiles =
mapMaybe (fmap toFilePath .
makeObjectFilePathFromC biCabalDir biComponentName biDistDir)
cfiles
cfiles = mapMaybe dotCabalCFilePath (S.toList biDotCabalPaths)
deps =
concat
[ case M.lookup name biInstalledMap of
Just (_, Stack.Types.Package.Library _ident ipid _) -> ["-package-id=" <> ghcPkgIdString ipid]
_ -> ["-package=" <> packageNameString name <>
maybe ""
((("-" <>) . versionString) . piiVersion)
(M.lookup name biSourceMap)]
| name <- pkgs]
pkgs =
biAddPackages ++
[ name
| Dependency cname _ <- targetBuildDepends biBuildInfo
, let name = fromCabalPackageName cname
, 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)
([biCabalDir | null (hsSourceDirs biBuildInfo)] <>
mapMaybe toIncludeDir (hsSourceDirs biBuildInfo) <>
[autogenDir biDistDir,buildDir biDistDir] <>
[makeGenDir (buildDir biDistDir)
| Just makeGenDir <- [fileGenDirFromComponentName biComponentName]]) ++
["-stubdir=" ++ toFilePathNoTrailingSep (buildDir biDistDir)]
toIncludeDir "." = Just biCabalDir
toIncludeDir relDir = concatAndColapseAbsDir biCabalDir relDir
includeOpts =
map ("-I" <>) (configExtraIncludeDirs <> pkgIncludeOpts)
configExtraIncludeDirs = S.toList biConfigIncludeDirs
pkgIncludeOpts =
[ toFilePathNoTrailingSep absDir
| dir <- includeDirs biBuildInfo
, absDir <- handleDir dir
]
libOpts =
map ("-l" <>) (extraLibs biBuildInfo) <>
map ("-L" <>) (configExtraLibDirs <> pkgLibDirs)
configExtraLibDirs = S.toList biConfigLibDirs
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")
addComponentPrefix <- fileGenDirFromComponentName namedComponent
return (addComponentPrefix (buildDir distDir) </> relOFilePath)
fileGenDirFromComponentName
:: MonadThrow m
=> NamedComponent -> m (Path b Dir -> Path b Dir)
fileGenDirFromComponentName namedComponent =
case namedComponent of
CLib -> return id
CExe name -> makeTmp name
CTest name -> makeTmp name
CBench name -> makeTmp name
where makeTmp name = do
prefix <- parseRelDir (T.unpack name <> "/" <> T.unpack name <> "-tmp")
return (</> prefix)
autogenDir :: Path Abs Dir -> Path Abs Dir
autogenDir distDir = buildDir distDir </> $(mkRelDir "autogen")
buildDir :: Path Abs Dir -> Path Abs Dir
buildDir distDir = distDir </> $(mkRelDir "build")
getBuildComponentDir :: Maybe String -> Maybe (Path Rel Dir)
getBuildComponentDir Nothing = Nothing
getBuildComponentDir (Just name) = parseRelDir (name FilePath.</> (name ++ "-tmp"))
packageDependencies :: PackageDescription -> Map PackageName VersionRange
packageDependencies pkg =
M.fromListWith intersectVersionRanges $
map (depName &&& depRange) $
concatMap targetBuildDepends (allBuildInfo' pkg) ++
maybe [] setupDepends (setupBuildInfo pkg)
packageDescTools :: PackageDescription -> Map ExeName VersionRange
packageDescTools =
M.fromList . concatMap tools . allBuildInfo'
where
tools bi = map go1 (buildTools bi) ++ map go2 (buildToolDepends bi)
go1 :: Cabal.LegacyExeDependency -> (ExeName, VersionRange)
go1 (Cabal.LegacyExeDependency name range) = (ExeName $ T.pack name, range)
go2 :: Cabal.ExeDependency -> (ExeName, VersionRange)
go2 (Cabal.ExeDependency _pkg name range) = (ExeName $ T.pack $ Cabal.unUnqualComponentName name, range)
allBuildInfo' :: PackageDescription -> [BuildInfo]
allBuildInfo' pkg = allBuildInfo pkg ++
[ bi | flib <- foreignLibs pkg
, let bi = foreignLibBuildInfo flib
, buildable bi
]
packageDescModulesAndFiles
:: (MonadLogger m, MonadUnliftIO m, MonadReader Ctx m, MonadThrow m)
=> PackageDescription
-> m (Map NamedComponent (Set ModuleName), Map NamedComponent (Set DotCabalPath), Set (Path Abs File), [PackageWarning])
packageDescModulesAndFiles pkg = do
(libraryMods,libDotCabalFiles,libWarnings) <-
maybe
(return (M.empty, M.empty, []))
(asModuleAndFileMap libComponent libraryFiles)
(library 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
(extraSrcFiles pkg
++ map (dataDir pkg FilePath.</>) (dataFiles pkg))
let modules = libraryMods <> executableMods <> testMods <> benchModules
files =
libDotCabalFiles <> exeDotCabalFiles <> testDotCabalFiles <>
benchDotCabalPaths
warnings = libWarnings <> exeWarnings <> testWarnings <> benchWarnings
return (modules, files, dfiles, warnings)
where
libComponent = const CLib
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 lib
return (M.singleton (label lib) a, M.singleton (label lib) b, c)
foldTuples = foldl' (<>) (M.empty, M.empty, [])
resolveGlobFiles :: (MonadLogger m,MonadUnliftIO m,MonadReader Ctx m)
=> [String] -> m (Set (Path Abs File))
resolveGlobFiles =
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
(matchDirFileGlob_ dir glob)
(\(e :: IOException) ->
if isUserError e
then do
prettyWarnL
[ flow "Wildcard does not match any files:"
, styleFile $ fromString glob
, line <> flow "in directory:"
, styleDir $ fromString dir
]
return []
else throwIO e)
matchDirFileGlob_ :: (MonadLogger m, MonadIO m, HasRunner env, MonadReader env m) => String -> String -> m [String]
matchDirFileGlob_ dir filepath = case parseFileGlob filepath of
Nothing -> liftIO $ throwString $
"invalid file glob '" ++ filepath
++ "'. Wildcards '*' are only allowed in place of the file"
++ " name, not in the directory name or file extension."
++ " If a wildcard is used it must be with an file extension."
Just (NoGlob filepath') -> return [filepath']
Just (FileGlob dir' ext) -> do
efiles <- liftIO $ try $ D.getDirectoryContents (dir FilePath.</> dir')
let matches =
case efiles of
Left (_ :: IOException) -> []
Right files ->
[ dir' FilePath.</> file
| file <- files
, let (name, ext') = splitExtensions file
, not (null name) && isSuffixOf ext ext'
]
when (null matches) $
prettyWarnL
[ flow "filepath wildcard"
, "'" <> styleFile (fromString filepath) <> "'"
, flow "does not match any files."
]
return matches
benchmarkFiles
:: (MonadLogger m, MonadIO m, MonadReader Ctx m, MonadThrow m)
=> Benchmark -> m (Set ModuleName, Set DotCabalPath, [PackageWarning])
benchmarkFiles bench = do
dirs <- mapMaybeM resolveDirOrWarn (hsSourceDirs build)
dir <- asks (parent . ctxFile)
(modules,files,warnings) <-
resolveFilesAndDeps
(Just $ Cabal.unUnqualComponentName $ benchmarkName bench)
(dirs ++ [dir])
(bnames <> exposed)
haskellModuleExts
cfiles <- buildOtherSources build
return (modules, files <> cfiles, warnings)
where
exposed =
case benchmarkInterface bench of
BenchmarkExeV10 _ fp -> [DotCabalMain fp]
BenchmarkUnsupported _ -> []
bnames = map DotCabalModule (otherModules build)
build = benchmarkBuildInfo bench
testFiles
:: (MonadLogger m, MonadIO m, MonadReader Ctx m, MonadThrow m)
=> TestSuite
-> m (Set ModuleName, Set DotCabalPath, [PackageWarning])
testFiles test = do
dirs <- mapMaybeM resolveDirOrWarn (hsSourceDirs build)
dir <- asks (parent . ctxFile)
(modules,files,warnings) <-
resolveFilesAndDeps
(Just $ Cabal.unUnqualComponentName $ testName test)
(dirs ++ [dir])
(bnames <> exposed)
haskellModuleExts
cfiles <- buildOtherSources build
return (modules, files <> cfiles, warnings)
where
exposed =
case testInterface test of
TestSuiteExeV10 _ fp -> [DotCabalMain fp]
TestSuiteLibV09 _ mn -> [DotCabalModule mn]
TestSuiteUnsupported _ -> []
bnames = map DotCabalModule (otherModules build)
build = testBuildInfo test
executableFiles
:: (MonadLogger m, MonadIO m, MonadReader Ctx m, MonadThrow m)
=> Executable
-> m (Set ModuleName, Set DotCabalPath, [PackageWarning])
executableFiles exe = do
dirs <- mapMaybeM resolveDirOrWarn (hsSourceDirs build)
dir <- asks (parent . ctxFile)
(modules,files,warnings) <-
resolveFilesAndDeps
(Just $ Cabal.unUnqualComponentName $ exeName exe)
(dirs ++ [dir])
(map DotCabalModule (otherModules build) ++
[DotCabalMain (modulePath exe)])
haskellModuleExts
cfiles <- buildOtherSources build
return (modules, files <> cfiles, warnings)
where
build = buildInfo exe
libraryFiles
:: (MonadLogger m, MonadIO m, MonadReader Ctx m, MonadThrow m)
=> Library -> m (Set ModuleName, Set DotCabalPath, [PackageWarning])
libraryFiles lib = do
dirs <- mapMaybeM resolveDirOrWarn (hsSourceDirs build)
dir <- asks (parent . ctxFile)
(modules,files,warnings) <-
resolveFilesAndDeps
Nothing
(dirs ++ [dir])
names
haskellModuleExts
cfiles <- buildOtherSources build
return (modules, files <> cfiles, warnings)
where
names = bnames ++ exposed
exposed = map DotCabalModule (exposedModules lib)
bnames = map DotCabalModule (otherModules build)
build = libBuildInfo lib
buildOtherSources :: (MonadLogger m,MonadIO m,MonadReader Ctx m)
=> BuildInfo -> m (Set DotCabalPath)
buildOtherSources build =
do csources <- liftM
(S.map DotCabalCFilePath . S.fromList)
(mapMaybeM resolveFileOrWarn (cSources build))
jsources <- liftM
(S.map DotCabalFilePath . S.fromList)
(mapMaybeM resolveFileOrWarn (targetJsSources build))
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 (MkFlag (fromCabalFlagName -> name) _desc def _manual) = (name,def)
data ResolveConditions = ResolveConditions
{ rcFlags :: Map FlagName Bool
, rcCompilerVersion :: CompilerVersion 'CVActual
, rcOS :: OS
, rcArch :: Arch
}
mkResolveConditions :: CompilerVersion 'CVActual
-> Platform
-> Map FlagName Bool
-> ResolveConditions
mkResolveConditions compilerVersion (Platform arch os) flags = ResolveConditions
{ rcFlags = flags
, rcCompilerVersion = compilerVersion
, rcOS = os
, rcArch = arch
}
resolveConditions :: (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 (fromCabalFlagName flag) (rcFlags rc)
Impl flavor range ->
case (flavor, rcCompilerVersion rc) of
(GHC, GhcVersion vghc) -> vghc `withinRange` range
(GHC, GhcjsVersion _ vghc) -> vghc `withinRange` range
(GHCJS, GhcjsVersion vghcjs _) ->
vghcjs `withinRange` range
_ -> False
depName :: Dependency -> PackageName
depName (Dependency n _) = fromCabalPackageName n
depRange :: Dependency -> VersionRange
depRange (Dependency _ r) = r
resolveFilesAndDeps
:: (MonadIO m, MonadLogger m, MonadReader Ctx m, MonadThrow m)
=> Maybe String
-> [Path Abs Dir]
-> [DotCabalDescriptor]
-> [Text]
-> m (Set ModuleName,Set DotCabalPath,[PackageWarning])
resolveFilesAndDeps component dirs names0 exts = do
(dotCabalPaths, foundModules, missingModules) <- loop names0 S.empty
warnings <- liftM2 (++) (warnUnlisted foundModules) (warnMissing missingModules)
return (foundModules, dotCabalPaths, warnings)
where
loop [] _ = return (S.empty, S.empty, [])
loop names doneModules0 = do
resolved <- resolveFiles dirs names exts
let foundFiles = mapMaybe snd resolved
(foundModules', missingModules') = partition (isJust . snd) resolved
foundModules = mapMaybe (dotCabalModule . fst) foundModules'
missingModules = mapMaybe (dotCabalModule . fst) missingModules'
pairs <- mapM (getDependencies component) 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
( S.union
(S.fromList
(foundFiles <> map DotCabalFilePath thDepFiles))
resolvedFiles
, S.union
(S.fromList foundModules)
resolvedModules
, missingModules)
warnUnlisted foundModules = do
let unlistedModules =
foundModules `S.difference`
S.fromList (mapMaybe dotCabalModule names0)
return $
if S.null unlistedModules
then []
else [ UnlistedModulesWarning
component
(S.toList unlistedModules)]
warnMissing _missingModules = do
return []
getDependencies
:: (MonadReader Ctx m, MonadIO m, MonadLogger m)
=> Maybe String -> DotCabalPath -> m (Set ModuleName, [Path Abs File])
getDependencies component 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 <- getDumpHIDir
dir <- asks (parent . ctxFile)
case stripProperPrefix dir resolvedFile of
Nothing -> return (S.empty, [])
Just fileRel -> do
let dumpHIPath =
FilePath.replaceExtension
(toFilePath (dumpHIDir </> fileRel))
".dump-hi"
dumpHIExists <- liftIO $ D.doesFileExist dumpHIPath
if dumpHIExists
then parseDumpHI dumpHIPath
else return (S.empty, [])
getDumpHIDir = do
bld <- asks ctxDir
return $ maybe bld (bld </>) (getBuildComponentDir component)
parseDumpHI
:: (MonadReader Ctx m, MonadIO m, MonadLogger m)
=> FilePath -> m (Set ModuleName, [Path Abs File])
parseDumpHI dumpHIPath = do
dir <- asks (parent . ctxFile)
dumpHI <- liftIO $ fmap C8.lines (C8.readFile dumpHIPath)
let startModuleDeps =
dropWhile (not . ("module dependencies:" `C8.isPrefixOf`)) dumpHI
moduleDeps =
S.fromList $
mapMaybe (D.simpleParse . T.unpack . decodeUtf8) $
C8.words $
C8.concat $
C8.dropWhile (/= ' ') (fromMaybe "" $ listToMaybe startModuleDeps) :
takeWhile (" " `C8.isPrefixOf`) (drop 1 startModuleDeps)
thDeps =
mapMaybe
(fmap T.unpack .
(T.stripSuffix "\"" <=< T.stripPrefix "\"") .
T.dropWhileEnd (== '\r') . decodeUtf8 . C8.dropWhile (/= '"')) $
filter ("addDependentFile \"" `C8.isPrefixOf`) dumpHI
thDepsResolved <- liftM catMaybes $ forM thDeps $ \x -> do
mresolved <- liftIO (forgivingAbsence (resolveFile dir x)) >>= rejectMissingFile
when (isNothing mresolved) $
prettyWarnL
[ flow "addDependentFile path (Template Haskell) listed in"
, styleFile $ fromString dumpHIPath
, flow "does not exist:"
, styleFile $ fromString x
]
return mresolved
return (moduleDeps, thDepsResolved)
resolveFiles
:: (MonadIO m, MonadLogger m, MonadThrow m, MonadReader Ctx m)
=> [Path Abs Dir]
-> [DotCabalDescriptor]
-> [Text]
-> m [(DotCabalDescriptor, Maybe DotCabalPath)]
resolveFiles dirs names exts =
forM names (\name -> liftM (name, ) (findCandidate dirs exts name))
findCandidate
:: (MonadIO m, MonadLogger m, MonadThrow m, MonadReader Ctx m)
=> [Path Abs Dir]
-> [Text]
-> DotCabalDescriptor
-> m (Maybe DotCabalPath)
findCandidate dirs exts 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 ->
liftM concat
$ mapM
((\ ext ->
resolveCandidate dir (Cabal.toFilePath mn ++ "." ++ ext))
. T.unpack)
exts
resolveCandidate
:: (MonadIO m, MonadThrow m)
=> Path Abs Dir -> FilePath.FilePath -> m [Path Abs File]
resolveCandidate x y = do
p <- parseCollapsedAbsFile (toFilePath x FilePath.</> y)
exists <- doesFileExist p
return $ if exists then [p] else []
warnMultiple
:: (MonadLogger m, HasRunner env, MonadReader env m)
=> DotCabalDescriptor -> Path b t -> [Path b t] -> m ()
warnMultiple name candidate rest =
prettyWarnL
[ flow "There were multiple candidates for the Cabal entry \""
, fromString . showName $ name
, line <> bulletedList (map dispOne 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
:: (MonadIO m, MonadThrow m, MonadLogger m, HasRunner env,
MonadReader env m)
=> [Path Abs Dir] -> ModuleName -> m ()
logPossibilities dirs mn = do
possibilities <- liftM concat (makePossibilities mn)
unless (null possibilities) $ prettyWarnL
[ flow "Unable to find a known candidate for the Cabal entry"
, (styleModule . fromString $ D.display mn) <> ","
, flow "but did find:"
, line <> bulletedList (map display 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
findOrGenerateCabalFile
:: forall m env.
(MonadIO m, MonadUnliftIO m, MonadLogger m, HasRunner env, HasConfig env, MonadReader env m)
=> Path Abs Dir
-> m (Path Abs File)
findOrGenerateCabalFile pkgDir = do
hpack pkgDir
findCabalFile
where
findCabalFile :: m (Path Abs File)
findCabalFile = findCabalFile' >>= either throwIO return
findCabalFile' :: m (Either PackageException (Path Abs File))
findCabalFile' = do
files <- liftIO $ findFiles
pkgDir
(flip hasExtension "cabal" . FL.toFilePath)
(const False)
return $ case files of
[] -> Left $ PackageNoCabalFileFound pkgDir
[x] -> Right x
(filter (not . ("." `isPrefixOf`) . toFilePath . filename) -> [x]) -> Right x
_:_ -> Left $ PackageMultipleCabalFilesFound pkgDir files
where hasExtension fp x = FilePath.takeExtension fp == "." ++ x
hpack :: (MonadIO m, MonadUnliftIO m, MonadLogger m, HasRunner env, HasConfig env, MonadReader env m)
=> Path Abs Dir -> m ()
hpack pkgDir = do
let hpackFile = pkgDir </> $(mkRelFile Hpack.packageConfig)
exists <- liftIO $ doesFileExist hpackFile
when exists $ do
prettyDebugL [flow "Running hpack on", display hpackFile]
config <- view configL
case configOverrideHpack config of
HpackBundled -> do
r <- liftIO $ Hpack.hpackResult (Just $ toFilePath pkgDir) Hpack.NoForce
forM_ (Hpack.resultWarnings r) prettyWarnS
let cabalFile = styleFile . fromString . Hpack.resultCabalFile $ r
case Hpack.resultStatus r of
Hpack.Generated -> prettyDebugL
[flow "hpack generated a modified version of", cabalFile]
Hpack.OutputUnchanged -> prettyDebugL
[flow "hpack output unchanged in", cabalFile]
Hpack.AlreadyGeneratedByNewerHpack -> prettyWarnL
[ cabalFile
, flow "was generated with a newer version of hpack,"
, flow "please upgrade and try again."
]
Hpack.ExistingCabalFileWasModifiedManually -> prettyWarnL
[ flow "WARNING: "
, cabalFile
, flow " was modified manually. Ignoring package.yaml in favor of cabal file."
, flow "If you want to use package.yaml instead of the cabal file, "
, flow "then please delete the cabal file."
]
HpackCommand command -> do
envOverride <- getMinimalEnvOverride
let cmd = Cmd (Just pkgDir) command envOverride []
runCmd cmd Nothing
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 </> $(mkRelDir "logs") </> fp
resolveOrWarn :: (MonadLogger m, MonadIO m, MonadReader Ctx m)
=> Text
-> (Path Abs Dir -> String -> m (Maybe a))
-> FilePath.FilePath
-> m (Maybe a)
resolveOrWarn subject resolver path =
do cwd <- liftIO getCurrentDir
file <- asks ctxFile
dir <- asks (parent . ctxFile)
result <- resolver dir path
when (isNothing result) $
prettyWarnL
[ fromString . T.unpack $ subject
, flow "listed in"
, maybe (display file) display (stripProperPrefix cwd file)
, flow "file does not exist:"
, styleDir . fromString $ path
]
return result
resolveFileOrWarn :: (MonadIO m,MonadLogger m,MonadReader Ctx m)
=> FilePath.FilePath
-> m (Maybe (Path Abs File))
resolveFileOrWarn = resolveOrWarn "File" f
where f p x = liftIO (forgivingAbsence (resolveFile p x)) >>= rejectMissingFile
resolveDirOrWarn :: (MonadIO m,MonadLogger m,MonadReader Ctx m)
=> FilePath.FilePath
-> m (Maybe (Path Abs Dir))
resolveDirOrWarn = resolveOrWarn "Directory" f
where f p x = liftIO (forgivingAbsence (resolveDir p x)) >>= rejectMissingDir
cabalFilePackageId
:: (MonadIO m, MonadThrow m)
=> Path Abs File -> m PackageIdentifier
cabalFilePackageId fp = do
pkgDescr <- liftIO (D.readGenericPackageDescription D.silent $ toFilePath fp)
(toStackPI . D.package . D.packageDescription) pkgDescr
where
toStackPI (D.PackageIdentifier (D.unPackageName -> name) ver) = do
name' <- parsePackageNameFromString name
ver' <- parseVersionFromString (showVersion ver)
return (PackageIdentifier name' ver')