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

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

21
  -- * Creating an index
22 23
  fromList,

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

27
  insert,
Duncan Coutts's avatar
Duncan Coutts committed
28 29 30

  deleteInstalledPackageId,
  deleteSourcePackageId,
31
  deletePackageName,
Duncan Coutts's avatar
Duncan Coutts committed
32
--  deleteDependency,
33 34 35 36

  -- * Queries

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

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

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

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

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

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

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

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

ttuegel's avatar
ttuegel committed
123 124 125
  deriving (Generic, Show, Read)

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

127 128 129 130
-- | The default package index which contains 'InstalledPackageInfo'.  Normally
-- use this.
type InstalledPackageIndex = PackageIndex InstalledPackageInfo

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

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

154

155 156 157 158
--
-- * Internal helpers
--

159
mkPackageIndex :: HasInstalledPackageId a
160 161 162
               => Map InstalledPackageId a
               -> Map PackageName (Map Version [a])
               -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
163 164
mkPackageIndex pids pnames = assert (invariant index) index
  where index = PackageIndex pids pnames
165

166 167 168

--
-- * Construction
169 170
--

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

194 195 196 197
--
-- * Updates
--

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

218

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

228
  where
Duncan Coutts's avatar
Duncan Coutts committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    pids'   = Map.insert (installedPackageId pkg) pkg pids
    pnames' = insertPackageName pnames
    insertPackageName =
      Map.insertWith' (\_ -> insertPackageVersion)
                     (packageName pkg)
                     (Map.singleton (packageVersion pkg) [pkg])

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

    insertPackageInstance pkgs =
      pkg : deleteBy (equating installedPackageId) pkg pkgs


-- | Removes a single installed package from the index.
--
246
deleteInstalledPackageId :: HasInstalledPackageId a => InstalledPackageId -> PackageIndex a -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
247 248 249 250 251 252
deleteInstalledPackageId ipkgid original@(PackageIndex pids pnames) =
  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 263 264 265 266
    deletePkgInstance =
        (\xs -> if List.null xs then Nothing else Just xs)
      . List.deleteBy (\_ pkg -> installedPackageId pkg == ipkgid) undefined


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

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

285 286 287

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

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

305 306 307 308
--
-- * Bulk queries
--

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

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

--
-- * Lookups
--

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

346

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

361 362
-- | Convenient alias of 'lookupSourcePackageId', but assuming only
-- one package per package ID.
363
lookupPackageId :: HasInstalledPackageId a => PackageIndex a -> PackageId -> Maybe a
364 365 366 367
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
368 369 370

-- | Does a lookup by source package name.
--
371
lookupPackageName :: HasInstalledPackageId a => PackageIndex a -> PackageName
372
                  -> [(Version, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
373 374 375 376 377 378 379
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.
380 381 382 383
--
-- We get back any number of versions of the specified package name, all
-- satisfying the version range constraint.
--
384
lookupDependency :: HasInstalledPackageId a => PackageIndex a -> Dependency
385
                 -> [(Version, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
386 387 388 389 390 391
lookupDependency (PackageIndex _ pnames) (Dependency name versionRange) =
  case Map.lookup name pnames of
    Nothing    -> []
    Just pvers -> [ entry
                  | entry@(ver, _) <- Map.toList pvers
                  , ver `withinRange` versionRange ]
392 393 394 395

--
-- * Case insensitive name lookups
--
396 397 398

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

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

Duncan Coutts's avatar
Duncan Coutts committed
435 436 437 438 439 440 441 442

--
-- * Special queries
--

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

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


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

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

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

508
  where
509
    (depGraph, vertexToPkg, pkgIdToVertex) = dependencyGraph index
510 511 512
    reverseDepGraph = Graph.transposeG depGraph
    noSuchPkgId = error "reverseDependencyClosure: package is not in the graph"

513
topologicalOrder :: PackageInstalled a => PackageIndex a -> [a]
514 515 516 517 518
topologicalOrder index = map toPkgId
                       . Graph.topSort
                       $ graph
  where (graph, toPkgId, _) = dependencyGraph index

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

Duncan Coutts's avatar
Duncan Coutts committed
541
    pkgs             = sortBy (comparing packageId) (allPackages index)
542 543
    vertices         = zip (map installedPackageId pkgs) [0..]
    vertex_map       = Map.fromList vertices
544
    id_to_vertex pid = Map.lookup pid vertex_map
545 546

    vertex_to_pkg vertex = pkgTable ! vertex
547 548 549 550 551

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

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

582
        reallyIsInconsistent :: PackageInstalled a => [a] -> Bool
583 584 585
        reallyIsInconsistent []       = False
        reallyIsInconsistent [_p]     = False
        reallyIsInconsistent [p1, p2] =
586 587
          let pid1 = installedPackageId p1
              pid2 = installedPackageId p2
588 589
          in pid1 `notElem` installedDepends p2
          && pid2 `notElem` installedDepends p1
590
        reallyIsInconsistent _ = True
Duncan Coutts's avatar
Duncan Coutts committed
591

592
-- | A rough approximation of GHC's module finder, takes a 'InstalledPackageIndex' and
593 594
-- turns it into a map from module names to their source packages.  It's used to
-- initialize the @build-deps@ field in @cabal init@.
595
moduleNameIndex :: InstalledPackageIndex -> Map ModuleName [InstalledPackageInfo]
Duncan Coutts's avatar
Duncan Coutts committed
596
moduleNameIndex index =
597 598 599 600 601 602 603
  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])
604 605 606 607 608
        -- 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!