PackageIndex.hs 21.4 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
  deleteComponentId,
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
  lookupComponentId,
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
  ) where

import Control.Exception (assert)
65
import Data.Array ((!))
ttuegel's avatar
ttuegel committed
66
import qualified Data.Array as Array
67
import Distribution.Compat.Binary (Binary)
ttuegel's avatar
ttuegel committed
68
import qualified Data.Graph as Graph
Duncan Coutts's avatar
Duncan Coutts committed
69
import Data.List as List
70
         ( null, foldl', sort
Duncan Coutts's avatar
Duncan Coutts committed
71
         , groupBy, sortBy, find, isInfixOf, nubBy, deleteBy, deleteFirstsBy )
72
import Data.Monoid as Mon (Monoid(..))
ttuegel's avatar
ttuegel committed
73 74
import Data.Map (Map)
import qualified Data.Map as Map
75
import Data.Maybe (isNothing, fromMaybe)
ttuegel's avatar
ttuegel committed
76 77 78
import qualified Data.Tree  as Tree
import GHC.Generics (Generic)
import Prelude hiding (lookup)
79

80
import Distribution.Package
Duncan Coutts's avatar
Duncan Coutts committed
81
         ( PackageName(..), PackageId
82
         , Package(..), packageName, packageVersion
Duncan Coutts's avatar
Duncan Coutts committed
83
         , Dependency(Dependency)--, --PackageFixedDeps(..)
84 85
         , HasComponentId(..), PackageInstalled(..)
         , ComponentId )
Duncan Coutts's avatar
Duncan Coutts committed
86 87
import Distribution.ModuleName
         ( ModuleName )
88
import Distribution.InstalledPackageInfo
89
         ( InstalledPackageInfo )
90
import qualified Distribution.InstalledPackageInfo as IPI
91
import Distribution.Version
92
         ( Version, withinRange )
Duncan Coutts's avatar
Duncan Coutts committed
93
import Distribution.Simple.Utils (lowercase, comparing, equating)
94 95

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

Ian D. Bollinger's avatar
Ian D. Bollinger committed
107
  -- This auxiliary index maps package names (case-sensitively) to all the
Duncan Coutts's avatar
Duncan Coutts committed
108 109
  -- versions and instances of that package. This allows us to find all
  -- versions satisfying a dependency.
110
  --
Duncan Coutts's avatar
Duncan Coutts committed
111 112
  -- 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
113
  -- of the same package version. These are unique by ComponentId
Duncan Coutts's avatar
Duncan Coutts committed
114
  -- and are kept in preference order.
115
  --
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
116 117
  -- FIXME: Clarify what "preference order" means. Check that this invariant is
  -- preserved. See #1463 for discussion.
118
  !(Map PackageName (Map Version [a]))
119

ttuegel's avatar
ttuegel committed
120 121 122
  deriving (Generic, Show, Read)

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

124 125 126 127
-- | The default package index which contains 'InstalledPackageInfo'.  Normally
-- use this.
type InstalledPackageIndex = PackageIndex InstalledPackageInfo

128
instance HasComponentId a => Monoid (PackageIndex a) where
Duncan Coutts's avatar
Duncan Coutts committed
129
  mempty  = PackageIndex Map.empty Map.empty
130 131
  mappend = merge
  --save one mappend with empty in the common case:
132
  mconcat [] = Mon.mempty
133 134
  mconcat xs = foldr1 mappend xs

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

151

152 153 154 155
--
-- * Internal helpers
--

156 157
mkPackageIndex :: HasComponentId a
               => Map ComponentId a
158 159
               -> Map PackageName (Map Version [a])
               -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
160 161
mkPackageIndex pids pnames = assert (invariant index) index
  where index = PackageIndex pids pnames
162

163 164 165

--
-- * Construction
166 167
--

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

191 192 193 194
--
-- * Updates
--

195 196
-- | Merge two indexes.
--
Duncan Coutts's avatar
Duncan Coutts committed
197
-- Packages from the second mask packages from the first if they have the exact
198
-- same 'ComponentId'.
199
--
Duncan Coutts's avatar
Duncan Coutts committed
200 201 202 203 204
-- 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.
--
205
merge :: HasComponentId a => PackageIndex a -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
206
      -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
207
merge (PackageIndex pids1 pnames1) (PackageIndex pids2 pnames2) =
208
  mkPackageIndex (Map.unionWith (\_ y -> y) pids1 pids2)
Duncan Coutts's avatar
Duncan Coutts committed
209 210 211 212 213
                 (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)
214
    (\\) = deleteFirstsBy (equating installedComponentId)
215

216

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

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

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

    insertPackageInstance pkgs =
239
      pkg : deleteBy (equating installedComponentId) pkg pkgs
Duncan Coutts's avatar
Duncan Coutts committed
240 241 242 243


-- | Removes a single installed package from the index.
--
244 245
deleteComponentId :: HasComponentId a
                         => ComponentId -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
246
                         -> PackageIndex a
247
deleteComponentId ipkgid original@(PackageIndex pids pnames) =
Duncan Coutts's avatar
Duncan Coutts committed
248 249 250 251 252
  case Map.updateLookupWithKey (\_ _ -> Nothing) ipkgid pids of
    (Nothing,     _)     -> original
    (Just spkgid, pids') -> mkPackageIndex pids'
                                          (deletePkgName spkgid pnames)

Duncan Coutts's avatar
Duncan Coutts committed
253
  where
Duncan Coutts's avatar
Duncan Coutts committed
254 255 256 257 258 259
    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
260

Duncan Coutts's avatar
Duncan Coutts committed
261 262
    deletePkgInstance =
        (\xs -> if List.null xs then Nothing else Just xs)
263
      . List.deleteBy (\_ pkg -> installedComponentId pkg == ipkgid) undefined
Duncan Coutts's avatar
Duncan Coutts committed
264 265 266


-- | Removes all packages with this source 'PackageId' from the index.
267
--
268
deleteSourcePackageId :: HasComponentId a => PackageId -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
269
                      -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
270 271 272 273 274 275
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
276
                     (foldl' (flip (Map.delete . installedComponentId)) pids pkgs)
Duncan Coutts's avatar
Duncan Coutts committed
277 278 279 280 281 282 283 284 285
                     (deletePkgName pnames)
  where
    deletePkgName =
      Map.update deletePkgVersion (packageName pkgid)

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

286 287 288

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

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

307 308 309 310
--
-- * Bulk queries
--

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

-- | Get all the packages from the index.
--
318
-- They are grouped by package name (case-sensitively).
319
--
320
allPackagesByName :: PackageIndex a -> [(PackageName, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
321
allPackagesByName (PackageIndex _ pnames) =
322 323 324 325 326 327 328
  [ (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).
--
329
allPackagesBySourcePackageId :: HasComponentId a => PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
330
                             -> [(PackageId, [a])]
331 332 333 334
allPackagesBySourcePackageId (PackageIndex _ pnames) =
  [ (packageId ipkg, ipkgs)
  | pvers <- Map.elems pnames
  , ipkgs@(ipkg:_) <- Map.elems pvers ]
335 336 337 338 339

--
-- * Lookups
--

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

349

Duncan Coutts's avatar
Duncan Coutts committed
350 351 352
-- | Does a lookup by source package id (name & version).
--
-- There can be multiple installed packages with the same source 'PackageId'
353
-- but different 'ComponentId'. They are returned in order of
Duncan Coutts's avatar
Duncan Coutts committed
354
-- preference, with the most preferred first.
355
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
356
lookupSourcePackageId :: PackageIndex a -> PackageId -> [a]
Duncan Coutts's avatar
Duncan Coutts committed
357 358 359 360 361 362
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
363

364 365
-- | Convenient alias of 'lookupSourcePackageId', but assuming only
-- one package per package ID.
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
366
lookupPackageId :: PackageIndex a -> PackageId -> Maybe a
367 368 369 370
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
371 372 373

-- | Does a lookup by source package name.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
374
lookupPackageName :: PackageIndex a -> PackageName
375
                  -> [(Version, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
376 377 378 379 380 381 382
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.
383 384 385 386
--
-- 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
387
lookupDependency :: PackageIndex a -> Dependency
388
                 -> [(Version, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
389 390 391 392 393 394
lookupDependency (PackageIndex _ pnames) (Dependency name versionRange) =
  case Map.lookup name pnames of
    Nothing    -> []
    Just pvers -> [ entry
                  | entry@(ver, _) <- Map.toList pvers
                  , ver `withinRange` versionRange ]
395 396 397 398

--
-- * Case insensitive name lookups
--
399 400 401

-- | Does a case-insensitive search by package name.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
402
-- If there is only one package that compares case-insensitively to this name
403
-- then the search is unambiguous and we get back all versions of that package.
Ian D. Bollinger's avatar
Ian D. Bollinger committed
404
-- If several match case-insensitively but one matches exactly then it is also
405 406
-- unambiguous.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
407
-- If however several match case-insensitively and none match exactly then we
408 409 410 411
-- 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
412
searchByName :: PackageIndex a -> String -> SearchResult [a]
Duncan Coutts's avatar
Duncan Coutts committed
413 414
searchByName (PackageIndex _ pnames) name =
  case [ pkgs | pkgs@(PackageName name',_) <- Map.toList pnames
415
              , lowercase name' == lname ] of
Duncan Coutts's avatar
Duncan Coutts committed
416 417 418 419 420
    []               -> 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)
421
  where lname = lowercase name
422 423 424 425 426 427 428

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
429
searchByNameSubstring :: PackageIndex a -> String -> [a]
Duncan Coutts's avatar
Duncan Coutts committed
430
searchByNameSubstring (PackageIndex _ pnames) searchterm =
431
  [ pkg
Duncan Coutts's avatar
Duncan Coutts committed
432
  | (PackageName name, pvers) <- Map.toList pnames
433
  , lsearchterm `isInfixOf` lowercase name
Duncan Coutts's avatar
Duncan Coutts committed
434
  , pkgs <- Map.elems pvers
435
  , pkg <- pkgs ]
436
  where lsearchterm = lowercase searchterm
437

Duncan Coutts's avatar
Duncan Coutts committed
438 439 440 441 442 443 444 445

--
-- * Special queries
--

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

446 447 448 449 450 451 452
-- | 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.
--
453
dependencyCycles :: PackageInstalled a => PackageIndex a -> [[a]]
454
dependencyCycles index =
455 456
  [ vs | Graph.CyclicSCC vs <- Graph.stronglyConnComp adjacencyList ]
  where
457
    adjacencyList = [ (pkg, installedComponentId pkg, installedDepends pkg)
458 459 460
                    | pkg <- allPackages index ]


Duncan Coutts's avatar
Duncan Coutts committed
461
-- | All packages that have immediate dependencies that are not in the index.
462
--
463 464
-- Returns such packages along with the dependencies that they're missing.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
465
brokenPackages :: PackageInstalled a => PackageIndex a
466
               -> [(a, [ComponentId])]
467
brokenPackages index =
468
  [ (pkg, missing)
Duncan Coutts's avatar
Duncan Coutts committed
469
  | pkg  <- allPackages index
470
  , let missing = [ pkg' | pkg' <- installedDepends pkg
471
                         , isNothing (lookupComponentId index pkg') ]
472 473
  , not (null missing) ]

474
-- | Tries to take the transitive closure of the package dependencies.
475
--
476
-- If the transitive closure is complete then it returns that subset of the
477 478 479
-- 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
480
-- the original given 'PackageId's do not occur in the index.
481
--
482
dependencyClosure :: PackageInstalled a => PackageIndex a
483
                  -> [ComponentId]
484
                  -> Either (PackageIndex a)
485
                            [(a, [ComponentId])]
486
dependencyClosure index pkgids0 = case closure mempty [] pkgids0 of
487 488
  (completed, []) -> Left completed
  (completed, _)  -> Right (brokenPackages completed)
489
 where
490
    closure completed failed []             = (completed, failed)
491
    closure completed failed (pkgid:pkgids) = case lookupComponentId index pkgid of
492
      Nothing   -> closure completed (pkgid:failed) pkgids
493
      Just pkg  -> case lookupComponentId completed (installedComponentId pkg) of
494 495
        Just _  -> closure completed  failed pkgids
        Nothing -> closure completed' failed pkgids'
Duncan Coutts's avatar
Duncan Coutts committed
496
          where completed' = insert pkg completed
497
                pkgids'    = installedDepends pkg ++ pkgids
498

499
-- | Takes the transitive closure of the packages reverse dependencies.
500
--
Duncan Coutts's avatar
Duncan Coutts committed
501
-- * The given 'PackageId's must be in the index.
502
--
503
reverseDependencyClosure :: PackageInstalled a => PackageIndex a
504
                         -> [ComponentId]
505
                         -> [a]
506
reverseDependencyClosure index =
507 508 509 510
    map vertexToPkg
  . concatMap Tree.flatten
  . Graph.dfs reverseDepGraph
  . map (fromMaybe noSuchPkgId . pkgIdToVertex)
511

512
  where
513
    (depGraph, vertexToPkg, pkgIdToVertex) = dependencyGraph index
514 515 516
    reverseDepGraph = Graph.transposeG depGraph
    noSuchPkgId = error "reverseDependencyClosure: package is not in the graph"

517
topologicalOrder :: PackageInstalled a => PackageIndex a -> [a]
518 519 520 521 522
topologicalOrder index = map toPkgId
                       . Graph.topSort
                       $ graph
  where (graph, toPkgId, _) = dependencyGraph index

523
reverseTopologicalOrder :: PackageInstalled a => PackageIndex a -> [a]
524 525 526 527 528 529 530 531 532 533 534
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'.
--
535
dependencyGraph :: PackageInstalled a => PackageIndex a
536
                -> (Graph.Graph,
537
                    Graph.Vertex -> a,
538
                    ComponentId -> Maybe Graph.Vertex)
539
dependencyGraph index = (graph, vertex_to_pkg, id_to_vertex)
540 541
  where
    graph = Array.listArray bounds
542
              [ [ v | Just v <- map id_to_vertex (installedDepends pkg) ]
543
              | pkg <- pkgs ]
544

Duncan Coutts's avatar
Duncan Coutts committed
545
    pkgs             = sortBy (comparing packageId) (allPackages index)
546
    vertices         = zip (map installedComponentId pkgs) [0..]
547
    vertex_map       = Map.fromList vertices
548
    id_to_vertex pid = Map.lookup pid vertex_map
549 550

    vertex_to_pkg vertex = pkgTable ! vertex
551 552 553 554 555

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

556 557 558 559 560 561 562 563 564 565
-- | 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.
--
566
dependencyInconsistencies :: PackageInstalled a => PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
567
                          -> [(PackageName, [(PackageId, Version)])]
568
dependencyInconsistencies index =
569 570 571 572 573 574 575 576 577 578 579 580
  [ (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
581
          | pkg <- allPackages index
582
          , ipid <- installedDepends pkg
583
          , Just dep <- [lookupComponentId index ipid]
584 585
          ]

586
        reallyIsInconsistent :: PackageInstalled a => [a] -> Bool
587 588 589
        reallyIsInconsistent []       = False
        reallyIsInconsistent [_p]     = False
        reallyIsInconsistent [p1, p2] =
590 591
          let pid1 = installedComponentId p1
              pid2 = installedComponentId p2
592 593
          in pid1 `notElem` installedDepends p2
          && pid2 `notElem` installedDepends p1
594
        reallyIsInconsistent _ = True
Duncan Coutts's avatar
Duncan Coutts committed
595

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
596 597 598 599
-- | 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@.
600
moduleNameIndex :: InstalledPackageIndex -> Map ModuleName [InstalledPackageInfo]
Duncan Coutts's avatar
Duncan Coutts committed
601
moduleNameIndex index =
602 603 604 605 606 607 608
  Map.fromListWith (++) $ do
    pkg <- allPackages index
    IPI.ExposedModule m reexport _ <- IPI.exposedModules pkg
    case reexport of
        Nothing -> return (m, [pkg])
        Just (IPI.OriginalModule _ m') | m == m'   -> []
                                       | otherwise -> return (m', [pkg])
609 610 611 612 613
        -- 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!