Merge pull request #10546 from cabalism/fix/dedup-using-config-from
[cabal.git] / cabal-install-solver / src / Distribution / Solver / Modular / ConfiguredConversion.hs
blob0e2e8ad5baa89424ac8eb99f951d788af51cfc56
1 module Distribution.Solver.Modular.ConfiguredConversion
2 ( convCP
3 ) where
5 import Data.Maybe
6 import Prelude hiding (pi)
7 import Data.Either (partitionEithers)
9 import Distribution.Package (UnitId, packageId)
11 import qualified Distribution.Simple.PackageIndex as SI
13 import Distribution.Solver.Modular.Configured
14 import Distribution.Solver.Modular.Package
16 import Distribution.Solver.Types.ComponentDeps (ComponentDeps)
17 import qualified Distribution.Solver.Types.PackageIndex as CI
18 import Distribution.Solver.Types.PackagePath
19 import Distribution.Solver.Types.ResolverPackage
20 import Distribution.Solver.Types.SolverId
21 import Distribution.Solver.Types.SolverPackage
22 import Distribution.Solver.Types.InstSolverPackage
23 import Distribution.Solver.Types.SourcePackage
25 -- | Converts from the solver specific result @CP QPN@ into
26 -- a 'ResolverPackage', which can then be converted into
27 -- the install plan.
28 convCP :: SI.InstalledPackageIndex ->
29 CI.PackageIndex (SourcePackage loc) ->
30 CP QPN -> ResolverPackage loc
31 convCP iidx sidx (CP qpi fa es ds) =
32 case convPI qpi of
33 Left pi -> PreExisting $
34 InstSolverPackage {
35 instSolverPkgIPI = fromJust $ SI.lookupUnitId iidx pi,
36 instSolverPkgLibDeps = fmap fst ds',
37 instSolverPkgExeDeps = fmap snd ds'
39 Right pi -> Configured $
40 SolverPackage {
41 solverPkgSource = srcpkg,
42 solverPkgFlags = fa,
43 solverPkgStanzas = es,
44 solverPkgLibDeps = fmap fst ds',
45 solverPkgExeDeps = fmap snd ds'
47 where
48 srcpkg = fromMaybe (error "convCP: lookupPackageId failed") $ CI.lookupPackageId sidx pi
49 where
50 ds' :: ComponentDeps ([SolverId] {- lib -}, [SolverId] {- exe -})
51 ds' = fmap (partitionEithers . map convConfId) ds
53 convPI :: PI QPN -> Either UnitId PackageId
54 convPI (PI _ (I _ (Inst pi))) = Left pi
55 convPI pi = Right (packageId (either id id (convConfId pi)))
57 convConfId :: PI QPN -> Either SolverId {- is lib -} SolverId {- is exe -}
58 convConfId (PI (Q (PackagePath _ q) pn) (I v loc)) =
59 case loc of
60 Inst pi -> Left (PreExistingId sourceId pi)
61 _otherwise
62 | QualExe _ pn' <- q
63 -- NB: the dependencies of the executable are also
64 -- qualified. So the way to tell if this is an executable
65 -- dependency is to make sure the qualifier is pointing
66 -- at the actual thing. Fortunately for us, I was
67 -- silly and didn't allow arbitrarily nested build-tools
68 -- dependencies, so a shallow check works.
69 , pn == pn' -> Right (PlannedId sourceId)
70 | otherwise -> Left (PlannedId sourceId)
71 where
72 sourceId = PackageIdentifier pn v