PackageIndex.hs 21.6 KB
Newer Older
ttuegel's avatar
ttuegel committed
1 2
{-# LANGUAGE DeriveGeneric #-}

3 4
-----------------------------------------------------------------------------
-- |
5
-- Module      :  Distribution.Simple.PackageIndex
6 7
-- Copyright   :  (c) David Himmelstrup 2005,
--                    Bjorn Bringert 2007,
Duncan Coutts's avatar
Duncan Coutts committed
8
--                    Duncan Coutts 2008-2009
9
--
Duncan Coutts's avatar
Duncan Coutts committed
10
-- Maintainer  :  cabal-devel@haskell.org
11 12
-- Portability :  portable
--
13
-- An index of packages.
Duncan Coutts's avatar
Duncan Coutts committed
14
--
15 16
module Distribution.Simple.PackageIndex (
  -- * Package index data type
17
  InstalledPackageIndex,
18
  PackageIndex,
19

20
  -- * Creating an index
21 22
  fromList,

23
  -- * Updates
24
  merge,
Duncan Coutts's avatar
Duncan Coutts committed
25

26
  insert,
Duncan Coutts's avatar
Duncan Coutts committed
27

28
  deleteUnitId,
Duncan Coutts's avatar
Duncan Coutts committed
29
  deleteSourcePackageId,
30
  deletePackageName,
Duncan Coutts's avatar
Duncan Coutts committed
31
--  deleteDependency,
32 33 34 35

  -- * Queries

  -- ** Precise lookups
36
  lookupUnitId,
Duncan Coutts's avatar
Duncan Coutts committed
37
  lookupSourcePackageId,
38
  lookupPackageId,
39
  lookupPackageName,
40 41 42 43 44 45 46 47 48 49
  lookupDependency,

  -- ** Case-insensitive searches
  searchByName,
  SearchResult(..),
  searchByNameSubstring,

  -- ** Bulk queries
  allPackages,
  allPackagesByName,
50
  allPackagesBySourcePackageId,
51 52 53 54 55 56 57 58 59 60

  -- ** Special queries
  brokenPackages,
  dependencyClosure,
  reverseDependencyClosure,
  topologicalOrder,
  reverseTopologicalOrder,
  dependencyInconsistencies,
  dependencyCycles,
  dependencyGraph,
Duncan Coutts's avatar
Duncan Coutts committed
61
  moduleNameIndex,
62 63 64 65

  -- * Backwards compatibility
  deleteInstalledPackageId,
  lookupInstalledPackageId,
66 67
  ) where

68 69 70 71 72 73 74 75
import Distribution.Compat.Binary
import Distribution.Compat.Semigroup as Semi
import Distribution.Package
import Distribution.ModuleName
import qualified Distribution.InstalledPackageInfo as IPI
import Distribution.Version
import Distribution.Simple.Utils

76
import Control.Exception (assert)
77
import Data.Array ((!))
ttuegel's avatar
ttuegel committed
78 79
import qualified Data.Array as Array
import qualified Data.Graph as Graph
Duncan Coutts's avatar
Duncan Coutts committed
80
import Data.List as List
81
         ( null, foldl', sort
82
         , groupBy, sortBy, find, nubBy, deleteBy, deleteFirstsBy )
ttuegel's avatar
ttuegel committed
83 84
import Data.Map (Map)
import qualified Data.Map as Map
85
import Data.Maybe (isNothing, fromMaybe)
ttuegel's avatar
ttuegel committed
86 87 88
import qualified Data.Tree  as Tree
import GHC.Generics (Generic)
import Prelude hiding (lookup)
89 90

-- | The collection of information about packages from one or more 'PackageDB's.
91
-- These packages generally should have an instance of 'PackageInstalled'
92
--
93
-- Packages are uniquely identified in by their 'UnitId', they can
Ian D. Bollinger's avatar
Ian D. Bollinger committed
94
-- also be efficiently looked up by package name or by name and version.
95
--
96
data PackageIndex a = PackageIndex
Duncan Coutts's avatar
Duncan Coutts committed
97
  -- The primary index. Each InstalledPackageInfo record is uniquely identified
98
  -- by its UnitId.
Duncan Coutts's avatar
Duncan Coutts committed
99
  --
100
  !(Map UnitId a)
Duncan Coutts's avatar
Duncan Coutts committed
101

Ian D. Bollinger's avatar
Ian D. Bollinger committed
102
  -- This auxiliary index maps package names (case-sensitively) to all the
Duncan Coutts's avatar
Duncan Coutts committed
103 104
  -- versions and instances of that package. This allows us to find all
  -- versions satisfying a dependency.
105
  --
Duncan Coutts's avatar
Duncan Coutts committed
106 107
  -- It is a three-level index. The first level is the package name,
  -- the second is the package version and the final level is instances
108
  -- of the same package version. These are unique by UnitId
Duncan Coutts's avatar
Duncan Coutts committed
109
  -- and are kept in preference order.
110
  --
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
111 112
  -- FIXME: Clarify what "preference order" means. Check that this invariant is
  -- preserved. See #1463 for discussion.
113
  !(Map PackageName (Map Version [a]))
114

115
  deriving (Eq, Generic, Show, Read)
ttuegel's avatar
ttuegel committed
116 117

instance Binary a => Binary (PackageIndex a)
Duncan Coutts's avatar
Duncan Coutts committed
118

119 120
-- | The default package index which contains 'InstalledPackageInfo'.  Normally
-- use this.
121
type InstalledPackageIndex = PackageIndex IPI.InstalledPackageInfo
122

123
instance HasUnitId a => Monoid (PackageIndex a) where
Duncan Coutts's avatar
Duncan Coutts committed
124
  mempty  = PackageIndex Map.empty Map.empty
125
  mappend = (Semi.<>)
126
  --save one mappend with empty in the common case:
127
  mconcat [] = mempty
128 129
  mconcat xs = foldr1 mappend xs

130
instance HasUnitId a => Semigroup (PackageIndex a) where
131 132
  (<>) = merge

133
invariant :: HasUnitId a => PackageIndex a -> Bool
Duncan Coutts's avatar
Duncan Coutts committed
134
invariant (PackageIndex pids pnames) =
135
     map installedUnitId (Map.elems pids)
136
  == sort
137
     [ assert pinstOk (installedUnitId pinst)
Duncan Coutts's avatar
Duncan Coutts committed
138 139 140
     | (pname, pvers)  <- Map.toList pnames
     , let pversOk = not (Map.null pvers)
     , (pver,  pinsts) <- assert pversOk $ Map.toList pvers
141
     , let pinsts'  = sortBy (comparing installedUnitId) pinsts
Duncan Coutts's avatar
Duncan Coutts committed
142
           pinstsOk = all (\g -> length g == 1)
143
                          (groupBy (equating installedUnitId) pinsts')
Duncan Coutts's avatar
Duncan Coutts committed
144 145 146 147 148
     , pinst           <- assert pinstsOk $ pinsts'
     , let pinstOk = packageName    pinst == pname
                  && packageVersion pinst == pver
     ]

149

150 151 152 153
--
-- * Internal helpers
--

154 155
mkPackageIndex :: HasUnitId a
               => Map UnitId a
156 157
               -> Map PackageName (Map Version [a])
               -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
158 159
mkPackageIndex pids pnames = assert (invariant index) index
  where index = PackageIndex pids pnames
160

161 162 163

--
-- * Construction
164 165
--

166
-- | Build an index out of a bunch of packages.
167
--
168
-- If there are duplicates by 'UnitId' then later ones mask earlier
Duncan Coutts's avatar
Duncan Coutts committed
169
-- ones.
170
--
171
fromList :: HasUnitId a => [a] -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
172
fromList pkgs = mkPackageIndex pids pnames
173
  where
174
    pids      = Map.fromList [ (installedUnitId pkg, pkg) | pkg <- pkgs ]
Duncan Coutts's avatar
Duncan Coutts committed
175 176 177 178 179 180 181 182 183
    pnames    =
      Map.fromList
        [ (packageName (head pkgsN), pvers)
        | pkgsN <- groupBy (equating  packageName)
                 . sortBy  (comparing packageId)
                 $ pkgs
        , let pvers =
                Map.fromList
                [ (packageVersion (head pkgsNV),
184
                   nubBy (equating installedUnitId) (reverse pkgsNV))
Duncan Coutts's avatar
Duncan Coutts committed
185 186 187
                | pkgsNV <- groupBy (equating packageVersion) pkgsN
                ]
        ]
188

189 190 191 192
--
-- * Updates
--

193 194
-- | Merge two indexes.
--
Duncan Coutts's avatar
Duncan Coutts committed
195
-- Packages from the second mask packages from the first if they have the exact
196
-- same 'UnitId'.
197
--
Duncan Coutts's avatar
Duncan Coutts committed
198 199 200 201 202
-- For packages with the same source 'PackageId', packages from the second are
-- \"preferred\" over those from the first. Being preferred means they are top
-- result when we do a lookup by source 'PackageId'. This is the mechanism we
-- use to prefer user packages over global packages.
--
203
merge :: HasUnitId a => PackageIndex a -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
204
      -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
205
merge (PackageIndex pids1 pnames1) (PackageIndex pids2 pnames2) =
206
  mkPackageIndex (Map.unionWith (\_ y -> y) pids1 pids2)
Duncan Coutts's avatar
Duncan Coutts committed
207 208 209 210 211
                 (Map.unionWith (Map.unionWith mergeBuckets) pnames1 pnames2)
  where
    -- Packages in the second list mask those in the first, however preferred
    -- packages go first in the list.
    mergeBuckets xs ys = ys ++ (xs \\ ys)
212
    (\\) = deleteFirstsBy (equating installedUnitId)
213

214

Duncan Coutts's avatar
Duncan Coutts committed
215
-- | Inserts a single package into the index.
216 217 218 219
--
-- This is equivalent to (but slightly quicker than) using 'mappend' or
-- 'merge' with a singleton index.
--
220
insert :: HasUnitId a => a -> PackageIndex a -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
221 222 223
insert pkg (PackageIndex pids pnames) =
    mkPackageIndex pids' pnames'

224
  where
225
    pids'   = Map.insert (installedUnitId pkg) pkg pids
Duncan Coutts's avatar
Duncan Coutts committed
226 227 228 229 230 231 232 233 234 235 236
    pnames' = insertPackageName pnames
    insertPackageName =
      Map.insertWith' (\_ -> insertPackageVersion)
                     (packageName pkg)
                     (Map.singleton (packageVersion pkg) [pkg])

    insertPackageVersion =
      Map.insertWith' (\_ -> insertPackageInstance)
                     (packageVersion pkg) [pkg]

    insertPackageInstance pkgs =
237
      pkg : deleteBy (equating installedUnitId) pkg pkgs
Duncan Coutts's avatar
Duncan Coutts committed
238 239 240 241


-- | Removes a single installed package from the index.
--
242
deleteUnitId :: HasUnitId a
243 244
             => UnitId -> PackageIndex a
             -> PackageIndex a
245
deleteUnitId ipkgid original@(PackageIndex pids pnames) =
Duncan Coutts's avatar
Duncan Coutts committed
246 247 248 249 250
  case Map.updateLookupWithKey (\_ _ -> Nothing) ipkgid pids of
    (Nothing,     _)     -> original
    (Just spkgid, pids') -> mkPackageIndex pids'
                                          (deletePkgName spkgid pnames)

Duncan Coutts's avatar
Duncan Coutts committed
251
  where
Duncan Coutts's avatar
Duncan Coutts committed
252 253 254 255 256 257
    deletePkgName spkgid =
      Map.update (deletePkgVersion spkgid) (packageName spkgid)

    deletePkgVersion spkgid =
        (\m -> if Map.null m then Nothing else Just m)
      . Map.update deletePkgInstance (packageVersion spkgid)
Duncan Coutts's avatar
Duncan Coutts committed
258

Duncan Coutts's avatar
Duncan Coutts committed
259 260
    deletePkgInstance =
        (\xs -> if List.null xs then Nothing else Just xs)
261
      . List.deleteBy (\_ pkg -> installedUnitId pkg == ipkgid) undefined
Duncan Coutts's avatar
Duncan Coutts committed
262

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
263
-- | Backwards compatibility wrapper for Cabal pre-1.24.
264 265 266 267 268
{-# DEPRECATED deleteInstalledPackageId "Use deleteUnitId instead" #-}
deleteInstalledPackageId :: HasUnitId a
                         => UnitId -> PackageIndex a
                         -> PackageIndex a
deleteInstalledPackageId = deleteUnitId
Duncan Coutts's avatar
Duncan Coutts committed
269 270

-- | Removes all packages with this source 'PackageId' from the index.
271
--
272
deleteSourcePackageId :: HasUnitId a => PackageId -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
273
                      -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
274 275 276 277 278 279
deleteSourcePackageId pkgid original@(PackageIndex pids pnames) =
  case Map.lookup (packageName pkgid) pnames of
    Nothing     -> original
    Just pvers  -> case Map.lookup (packageVersion pkgid) pvers of
      Nothing   -> original
      Just pkgs -> mkPackageIndex
280
                     (foldl' (flip (Map.delete . installedUnitId)) pids pkgs)
Duncan Coutts's avatar
Duncan Coutts committed
281 282 283 284 285 286 287 288 289
                     (deletePkgName pnames)
  where
    deletePkgName =
      Map.update deletePkgVersion (packageName pkgid)

    deletePkgVersion =
        (\m -> if Map.null m then Nothing else Just m)
      . Map.delete (packageVersion pkgid)

290 291 292

-- | Removes all packages with this (case-sensitive) name from the index.
--
293
deletePackageName :: HasUnitId a => PackageName -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
294
                  -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
295 296 297 298
deletePackageName name original@(PackageIndex pids pnames) =
  case Map.lookup name pnames of
    Nothing     -> original
    Just pvers  -> mkPackageIndex
299
                     (foldl' (flip (Map.delete . installedUnitId)) pids
Duncan Coutts's avatar
Duncan Coutts committed
300 301
                             (concat (Map.elems pvers)))
                     (Map.delete name pnames)
302

Duncan Coutts's avatar
Duncan Coutts committed
303
{-
304 305
-- | Removes all packages satisfying this dependency from the index.
--
Duncan Coutts's avatar
Duncan Coutts committed
306
deleteDependency :: Dependency -> PackageIndex -> PackageIndex
307
deleteDependency (Dependency name verstionRange) =
Duncan Coutts's avatar
Duncan Coutts committed
308 309
  delete' name (\pkg -> packageVersion pkg `withinRange` verstionRange)
-}
310

311 312 313 314
--
-- * Bulk queries
--

315 316
-- | Get all the packages from the index.
--
317
allPackages :: PackageIndex a -> [a]
Duncan Coutts's avatar
Duncan Coutts committed
318
allPackages (PackageIndex pids _) = Map.elems pids
319 320 321

-- | Get all the packages from the index.
--
322
-- They are grouped by package name (case-sensitively).
323
--
324
allPackagesByName :: PackageIndex a -> [(PackageName, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
325
allPackagesByName (PackageIndex _ pnames) =
326 327 328 329 330 331 332
  [ (pkgname, concat (Map.elems pvers))
  | (pkgname, pvers) <- Map.toList pnames ]

-- | Get all the packages from the index.
--
-- They are grouped by source package id (package name and version).
--
333
allPackagesBySourcePackageId :: HasUnitId a => PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
334
                             -> [(PackageId, [a])]
335 336 337 338
allPackagesBySourcePackageId (PackageIndex _ pnames) =
  [ (packageId ipkg, ipkgs)
  | pvers <- Map.elems pnames
  , ipkgs@(ipkg:_) <- Map.elems pvers ]
339 340 341 342 343

--
-- * Lookups
--

Duncan Coutts's avatar
Duncan Coutts committed
344
-- | Does a lookup by source package id (name & version).
345
--
346
-- Since multiple package DBs mask each other by 'UnitId',
347 348
-- then we get back at most one package.
--
349
lookupUnitId :: PackageIndex a -> UnitId
350
             -> Maybe a
351
lookupUnitId (PackageIndex pids _) pid = Map.lookup pid pids
Duncan Coutts's avatar
Duncan Coutts committed
352

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
353
-- | Backwards compatibility for Cabal pre-1.24.
354 355 356 357 358
{-# DEPRECATED lookupInstalledPackageId "Use lookupUnitId instead" #-}
lookupInstalledPackageId :: PackageIndex a -> UnitId
                         -> Maybe a
lookupInstalledPackageId = lookupUnitId

359

Duncan Coutts's avatar
Duncan Coutts committed
360 361 362
-- | Does a lookup by source package id (name & version).
--
-- There can be multiple installed packages with the same source 'PackageId'
363
-- but different 'UnitId'. They are returned in order of
Duncan Coutts's avatar
Duncan Coutts committed
364
-- preference, with the most preferred first.
365
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
366
lookupSourcePackageId :: PackageIndex a -> PackageId -> [a]
Duncan Coutts's avatar
Duncan Coutts committed
367 368 369 370 371 372
lookupSourcePackageId (PackageIndex _ pnames) pkgid =
  case Map.lookup (packageName pkgid) pnames of
    Nothing     -> []
    Just pvers  -> case Map.lookup (packageVersion pkgid) pvers of
      Nothing   -> []
      Just pkgs -> pkgs -- in preference order
373

374 375
-- | Convenient alias of 'lookupSourcePackageId', but assuming only
-- one package per package ID.
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
376
lookupPackageId :: PackageIndex a -> PackageId -> Maybe a
377 378 379 380
lookupPackageId index pkgid = case lookupSourcePackageId index pkgid  of
    []    -> Nothing
    [pkg] -> Just pkg
    _     -> error "Distribution.Simple.PackageIndex: multiple matches found"
Duncan Coutts's avatar
Duncan Coutts committed
381 382 383

-- | Does a lookup by source package name.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
384
lookupPackageName :: PackageIndex a -> PackageName
385
                  -> [(Version, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
386 387 388 389 390 391 392
lookupPackageName (PackageIndex _ pnames) name =
  case Map.lookup name pnames of
    Nothing     -> []
    Just pvers  -> Map.toList pvers


-- | Does a lookup by source package name and a range of versions.
393 394 395 396
--
-- We get back any number of versions of the specified package name, all
-- satisfying the version range constraint.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
397
lookupDependency :: PackageIndex a -> Dependency
398
                 -> [(Version, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
399 400 401 402 403 404
lookupDependency (PackageIndex _ pnames) (Dependency name versionRange) =
  case Map.lookup name pnames of
    Nothing    -> []
    Just pvers -> [ entry
                  | entry@(ver, _) <- Map.toList pvers
                  , ver `withinRange` versionRange ]
405 406 407 408

--
-- * Case insensitive name lookups
--
409 410 411

-- | Does a case-insensitive search by package name.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
412
-- If there is only one package that compares case-insensitively to this name
413
-- then the search is unambiguous and we get back all versions of that package.
Ian D. Bollinger's avatar
Ian D. Bollinger committed
414
-- If several match case-insensitively but one matches exactly then it is also
415 416
-- unambiguous.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
417
-- If however several match case-insensitively and none match exactly then we
418 419 420 421
-- have an ambiguous result, and we get back all the versions of all the
-- packages. The list of ambiguous results is split by exact package name. So
-- it is a non-empty list of non-empty lists.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
422
searchByName :: PackageIndex a -> String -> SearchResult [a]
Duncan Coutts's avatar
Duncan Coutts committed
423 424
searchByName (PackageIndex _ pnames) name =
  case [ pkgs | pkgs@(PackageName name',_) <- Map.toList pnames
425
              , lowercase name' == lname ] of
Duncan Coutts's avatar
Duncan Coutts committed
426 427 428 429 430
    []               -> None
    [(_,pvers)]      -> Unambiguous (concat (Map.elems pvers))
    pkgss            -> case find ((PackageName name==) . fst) pkgss of
      Just (_,pvers) -> Unambiguous (concat (Map.elems pvers))
      Nothing        -> Ambiguous (map (concat . Map.elems . snd) pkgss)
431
  where lname = lowercase name
432 433 434 435 436 437 438

data SearchResult a = None | Unambiguous a | Ambiguous [a]

-- | Does a case-insensitive substring search by package name.
--
-- That is, all packages that contain the given string in their name.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
439
searchByNameSubstring :: PackageIndex a -> String -> [a]
Duncan Coutts's avatar
Duncan Coutts committed
440
searchByNameSubstring (PackageIndex _ pnames) searchterm =
441
  [ pkg
Duncan Coutts's avatar
Duncan Coutts committed
442
  | (PackageName name, pvers) <- Map.toList pnames
443
  , lsearchterm `isInfixOf` lowercase name
Duncan Coutts's avatar
Duncan Coutts committed
444
  , pkgs <- Map.elems pvers
445
  , pkg <- pkgs ]
446
  where lsearchterm = lowercase searchterm
447

Duncan Coutts's avatar
Duncan Coutts committed
448 449 450 451 452 453 454 455

--
-- * Special queries
--

-- None of the stuff below depends on the internal representation of the index.
--

456 457 458 459 460 461 462
-- | Find if there are any cycles in the dependency graph. If there are no
-- cycles the result is @[]@.
--
-- This actually computes the strongly connected components. So it gives us a
-- list of groups of packages where within each group they all depend on each
-- other, directly or indirectly.
--
463
dependencyCycles :: PackageInstalled a => PackageIndex a -> [[a]]
464
dependencyCycles index =
465 466
  [ vs | Graph.CyclicSCC vs <- Graph.stronglyConnComp adjacencyList ]
  where
467
    adjacencyList = [ (pkg, installedUnitId pkg, installedDepends pkg)
468 469 470
                    | pkg <- allPackages index ]


Duncan Coutts's avatar
Duncan Coutts committed
471
-- | All packages that have immediate dependencies that are not in the index.
472
--
473 474
-- Returns such packages along with the dependencies that they're missing.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
475
brokenPackages :: PackageInstalled a => PackageIndex a
476
               -> [(a, [UnitId])]
477
brokenPackages index =
478
  [ (pkg, missing)
Duncan Coutts's avatar
Duncan Coutts committed
479
  | pkg  <- allPackages index
480
  , let missing = [ pkg' | pkg' <- installedDepends pkg
481
                         , isNothing (lookupUnitId index pkg') ]
482 483
  , not (null missing) ]

484
-- | Tries to take the transitive closure of the package dependencies.
485
--
486
-- If the transitive closure is complete then it returns that subset of the
487 488 489
-- index. Otherwise it returns the broken packages as in 'brokenPackages'.
--
-- * Note that if the result is @Right []@ it is because at least one of
Duncan Coutts's avatar
Duncan Coutts committed
490
-- the original given 'PackageId's do not occur in the index.
491
--
492
dependencyClosure :: PackageInstalled a => PackageIndex a
493
                  -> [UnitId]
494
                  -> Either (PackageIndex a)
495
                            [(a, [UnitId])]
496
dependencyClosure index pkgids0 = case closure mempty [] pkgids0 of
497 498
  (completed, []) -> Left completed
  (completed, _)  -> Right (brokenPackages completed)
499
 where
500
    closure completed failed []             = (completed, failed)
501
    closure completed failed (pkgid:pkgids) = case lookupUnitId index pkgid of
502
      Nothing   -> closure completed (pkgid:failed) pkgids
503
      Just pkg  -> case lookupUnitId completed (installedUnitId pkg) of
504 505
        Just _  -> closure completed  failed pkgids
        Nothing -> closure completed' failed pkgids'
Duncan Coutts's avatar
Duncan Coutts committed
506
          where completed' = insert pkg completed
507
                pkgids'    = installedDepends pkg ++ pkgids
508

509
-- | Takes the transitive closure of the packages reverse dependencies.
510
--
Duncan Coutts's avatar
Duncan Coutts committed
511
-- * The given 'PackageId's must be in the index.
512
--
513
reverseDependencyClosure :: PackageInstalled a => PackageIndex a
514
                         -> [UnitId]
515
                         -> [a]
516
reverseDependencyClosure index =
517 518 519 520
    map vertexToPkg
  . concatMap Tree.flatten
  . Graph.dfs reverseDepGraph
  . map (fromMaybe noSuchPkgId . pkgIdToVertex)
521

522
  where
523
    (depGraph, vertexToPkg, pkgIdToVertex) = dependencyGraph index
524 525 526
    reverseDepGraph = Graph.transposeG depGraph
    noSuchPkgId = error "reverseDependencyClosure: package is not in the graph"

527
topologicalOrder :: PackageInstalled a => PackageIndex a -> [a]
528 529 530 531 532
topologicalOrder index = map toPkgId
                       . Graph.topSort
                       $ graph
  where (graph, toPkgId, _) = dependencyGraph index

533
reverseTopologicalOrder :: PackageInstalled a => PackageIndex a -> [a]
534 535 536 537 538 539 540 541 542 543 544
reverseTopologicalOrder index = map toPkgId
                              . Graph.topSort
                              . Graph.transposeG
                              $ graph
  where (graph, toPkgId, _) = dependencyGraph index

-- | Builds a graph of the package dependencies.
--
-- Dependencies on other packages that are not in the index are discarded.
-- You can check if there are any such dependencies with 'brokenPackages'.
--
545
dependencyGraph :: PackageInstalled a => PackageIndex a
546
                -> (Graph.Graph,
547
                    Graph.Vertex -> a,
548
                    UnitId -> Maybe Graph.Vertex)
549
dependencyGraph index = (graph, vertex_to_pkg, id_to_vertex)
550 551
  where
    graph = Array.listArray bounds
552
              [ [ v | Just v <- map id_to_vertex (installedDepends pkg) ]
553
              | pkg <- pkgs ]
554

Duncan Coutts's avatar
Duncan Coutts committed
555
    pkgs             = sortBy (comparing packageId) (allPackages index)
556
    vertices         = zip (map installedUnitId pkgs) [0..]
557
    vertex_map       = Map.fromList vertices
558
    id_to_vertex pid = Map.lookup pid vertex_map
559 560

    vertex_to_pkg vertex = pkgTable ! vertex
561 562 563 564 565

    pkgTable   = Array.listArray bounds pkgs
    topBound = length pkgs - 1
    bounds = (0, topBound)

566 567 568 569 570 571 572 573 574 575
-- | Given a package index where we assume we want to use all the packages
-- (use 'dependencyClosure' if you need to get such a index subset) find out
-- if the dependencies within it use consistent versions of each package.
-- Return all cases where multiple packages depend on different versions of
-- some other package.
--
-- Each element in the result is a package name along with the packages that
-- depend on it and the versions they require. These are guaranteed to be
-- distinct.
--
576
dependencyInconsistencies :: PackageInstalled a => PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
577
                          -> [(PackageName, [(PackageId, Version)])]
578
dependencyInconsistencies index =
579 580 581 582 583 584 585 586 587 588 589 590
  [ (name, [ (pid,packageVersion dep) | (dep,pids) <- uses, pid <- pids])
  | (name, ipid_map) <- Map.toList inverseIndex
  , let uses = Map.elems ipid_map
  , reallyIsInconsistent (map fst uses) ]

  where -- for each PackageName,
        --   for each package with that name,
        --     the InstalledPackageInfo and the package Ids of packages
        --     that depend on it.
        inverseIndex = Map.fromListWith (Map.unionWith (\(a,b) (_,b') -> (a,b++b')))
          [ (packageName dep,
             Map.fromList [(ipid,(dep,[packageId pkg]))])
Duncan Coutts's avatar
Duncan Coutts committed
591
          | pkg <- allPackages index
592
          , ipid <- installedDepends pkg
593
          , Just dep <- [lookupUnitId index ipid]
594 595
          ]

596 597 598 599 600
        -- Added in 991e52a474e2b8280432257c1771dc474a320a30,
        -- this is a special case to handle the base 3 compatibility
        -- package which shipped with GHC 6.10 and GHC 6.12
        -- (it was removed in GHC 7.0).  Remove this when GHC 6.12
        -- goes out of our support window.
601
        reallyIsInconsistent :: PackageInstalled a => [a] -> Bool
602 603 604
        reallyIsInconsistent []       = False
        reallyIsInconsistent [_p]     = False
        reallyIsInconsistent [p1, p2] =
605 606
          let pid1 = installedUnitId p1
              pid2 = installedUnitId p2
607 608
          in pid1 `notElem` installedDepends p2
          && pid2 `notElem` installedDepends p1
609
        reallyIsInconsistent _ = True
Duncan Coutts's avatar
Duncan Coutts committed
610

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
611 612 613 614
-- | A rough approximation of GHC's module finder, takes a
-- 'InstalledPackageIndex' and turns it into a map from module names to their
-- source packages.  It's used to initialize the @build-deps@ field in @cabal
-- init@.
615
moduleNameIndex :: InstalledPackageIndex -> Map ModuleName [IPI.InstalledPackageInfo]
Duncan Coutts's avatar
Duncan Coutts committed
616
moduleNameIndex index =
617 618
  Map.fromListWith (++) $ do
    pkg <- allPackages index
619
    IPI.ExposedModule m reexport <- IPI.exposedModules pkg
620 621
    case reexport of
        Nothing -> return (m, [pkg])
Edward Z. Yang's avatar
Edward Z. Yang committed
622 623
        Just (Module _ m') | m == m'   -> []
                           | otherwise -> return (m', [pkg])
624 625 626 627 628
        -- The heuristic is this: we want to prefer the original package
        -- which originally exported a module.  However, if a reexport
        -- also *renamed* the module (m /= m'), then we have to use the
        -- downstream package, since the upstream package has the wrong
        -- module name!