Skip to content

Commit

Permalink
Clean up dependency selection logic ever so slightly
Browse files Browse the repository at this point in the history
No need for `ResolvedDependency` to duplicate field across variants, or
`selectDependency` to ferry argument into return value
  • Loading branch information
Ericson2314 committed Mar 2, 2017
1 parent 445aa1c commit a811630
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions Cabal/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -980,29 +980,27 @@ configureDependencies
-> IO [(PackageName, ComponentName, InstalledPackageInfo)]
configureDependencies verbosity use_external_internal_deps
installedPackageSet requiredDepsMap pkg_descr enableSpec = do
let selectDependencies :: [LibDependency] ->
([FailedDependency], [ResolvedDependency])
selectDependencies =
partitionEithers
. map (selectDependency (package pkg_descr)
installedPackageSet
requiredDepsMap use_external_internal_deps)

(failedDeps, allPkgDeps) =
selectDependencies (targetBuildDepends =<< enabledBuildInfos pkg_descr enableSpec)

internalPkgDeps =
[ dep | InternalDependency dep _ <- allPkgDeps ]
-- NB: we have to SAVE the package name, because this is the only
-- way we can be able to resolve package names in the package
-- description.
externalPkgDeps =
[ (pn, cn, pkgid)
| ExternalDependency (LibDependency pn mcn _) pkgid <- allPkgDeps
, let cn = case mcn of
Nothing -> CLibName
Just n -> CSubLibName n
]
let
failedDeps :: [FailedDependency]
allPkgDeps :: [ResolvedDependency]
(failedDeps, allPkgDeps) = partitionEithers
[ (\s -> (dep, s)) <$> status
| dep <- targetBuildDepends =<< enabledBuildInfos pkg_descr enableSpec
, let status = selectDependency
(package pkg_descr) installedPackageSet
requiredDepsMap use_external_internal_deps dep ]

internalPkgDeps =
[ dep | (dep, InternalDependency _) <- allPkgDeps ]

-- NB: we have to SAVE the package name, because this is the only way we
-- can be able to resolve package names in the package description.
externalPkgDeps =
[ (pn, cn, pkgid)
| (LibDependency pn mcn _, ExternalDependency pkgid) <- allPkgDeps
, let cn = case mcn of
Nothing -> CLibName
Just n -> CSubLibName n ]

when (not (null internalPkgDeps)
&& not (newPackageDepsBehaviour pkg_descr)) $
Expand Down Expand Up @@ -1134,16 +1132,18 @@ reportProgram verbosity prog (Just configuredProg)
hackageUrl :: String
hackageUrl = "http://hackage.haskell.org/package/"

data ResolvedDependency
type ResolvedDependency = (LibDependency, DependencyResolution)

data DependencyResolution
-- | An external dependency from the package database, OR an
-- internal dependency which we are getting from the package
-- database.
= ExternalDependency LibDependency InstalledPackageInfo
= ExternalDependency InstalledPackageInfo
-- | An internal dependency ('PackageId' should be a library name)
-- which we are going to have to build. (The
-- 'PackageId' here is a hack to get a modest amount of
-- polymorphism out of the 'Package' typeclass.)
| InternalDependency LibDependency PackageId
| InternalDependency PackageId

data FailedDependency = DependencyNotExists PackageName
| DependencyMissingInternal PackageName PackageName
Expand All @@ -1158,10 +1158,10 @@ selectDependency :: PackageId -- ^ Package id of current package
-> UseExternalInternalDeps -- ^ Are we configuring a
-- single component?
-> LibDependency
-> Either FailedDependency ResolvedDependency
-> Either FailedDependency DependencyResolution
selectDependency pkgid installedIndex requiredDepsMap
use_external_internal_deps
dep@(LibDependency dep_pkgname dep_mb_compname vr) =
(LibDependency dep_pkgname dep_mb_compname vr) =
-- If external sublibs can someday we be used, we can simplify this
-- case. For now, we do the error as a basic sanity
-- check. PackageDescription.Check should give the user a nicer
Expand All @@ -1172,15 +1172,15 @@ selectDependency pkgid installedIndex requiredDepsMap
"Should have already checked that external sub-libs are not depended on"
(_, _, _) -> do_external
where
do_internal = Right (InternalDependency dep
do_internal = Right (InternalDependency
(PackageIdentifier dep_pkgname (packageVersion pkgid)))
do_external = case Map.lookup (dep_pkgname, compName) requiredDepsMap of
-- If we know the exact pkg to use, then use it.
Just pkginstance -> Right (ExternalDependency dep pkginstance)
Just pkginstance -> Right (ExternalDependency pkginstance)
-- Otherwise we just pick an arbitrary instance of the latest version.
Nothing -> case PackageIndex.lookupDependency installedIndex legacyDep of
[] -> Left errVal
pkgs -> Right $ ExternalDependency dep $ case last pkgs of
pkgs -> Right $ ExternalDependency $ case last pkgs of
(_ver, pkginstances) -> head pkginstances
where
(legacyDep, compName, errVal) = case dep_mb_compname of
Expand All @@ -1203,10 +1203,10 @@ reportSelectedDependencies verbosity deps =
info verbosity $ unlines
[ "Dependency " ++ display (simplifyLibDependency dep)
++ ": using " ++ display pkgid
| resolved <- deps
, let (dep, pkgid) = case resolved of
ExternalDependency dep' pkg' -> (dep', packageId pkg')
InternalDependency dep' pkgid' -> (dep', pkgid') ]
| (dep, res) <- deps
, let pkgid = case res of
ExternalDependency pkg' -> packageId pkg'
InternalDependency pkgid' -> pkgid' ]

reportFailedDependencies :: Verbosity -> [FailedDependency] -> IO ()
reportFailedDependencies _ [] = return ()
Expand Down

0 comments on commit a811630

Please sign in to comment.