PackageIndex.hs 24 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 14 15 16 17 18
-- An index of packages whose primary key is 'UnitId'.  Public libraries
-- are additionally indexed by 'PackageName' and 'Version'.
-- Technically, these are an index of *units* (so we should eventually
-- rename it to 'UnitIndex'); but in the absence of internal libraries
-- or Backpack each unit is equivalent to a package.
--
Edward Z. Yang's avatar
Edward Z. Yang committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
-- While 'PackageIndex' is parametric over what it actually records,
-- it is in fact only ever instantiated with a single element:
-- The 'InstalledPackageIndex' (defined here) contains a graph of
-- 'InstalledPackageInfo's representing the packages in a
-- package database stack.  It is used in a variety of ways:
--
--   * The primary use to let Cabal access the same installed
--     package database which is used by GHC during compilation.
--     For example, this data structure is used by 'ghc-pkg'
--     and 'Cabal' to do consistency checks on the database
--     (are the references closed).
--
--   * Given a set of dependencies, we can compute the transitive
--     closure of dependencies.  This is to check if the versions
--     of packages are consistent, and also needed by multiple
--     tools (Haddock must be explicitly told about the every
--     transitive package to do cross-package linking;
--     preprocessors must know about the include paths of all
--     transitive dependencies.)
38 39 40 41 42
--
-- This 'PackageIndex' is NOT to be confused with
-- 'Distribution.Client.PackageIndex', which indexes packages only by
-- 'PackageName' (this makes it suitable for indexing source packages,
-- for which we don't know 'UnitId's.)
Duncan Coutts's avatar
Duncan Coutts committed
43
--
44 45
module Distribution.Simple.PackageIndex (
  -- * Package index data type
46
  InstalledPackageIndex,
47
  PackageIndex,
48

49
  -- * Creating an index
50 51
  fromList,

52
  -- * Updates
53
  merge,
Duncan Coutts's avatar
Duncan Coutts committed
54

55
  insert,
Duncan Coutts's avatar
Duncan Coutts committed
56

57
  deleteUnitId,
Duncan Coutts's avatar
Duncan Coutts committed
58
  deleteSourcePackageId,
59
  deletePackageName,
Duncan Coutts's avatar
Duncan Coutts committed
60
--  deleteDependency,
61 62 63 64

  -- * Queries

  -- ** Precise lookups
65
  lookupUnitId,
66
  lookupComponentId,
Duncan Coutts's avatar
Duncan Coutts committed
67
  lookupSourcePackageId,
68
  lookupPackageId,
69
  lookupPackageName,
70 71 72 73 74 75 76 77 78 79
  lookupDependency,

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

  -- ** Bulk queries
  allPackages,
  allPackagesByName,
80
  allPackagesBySourcePackageId,
81 82 83 84 85 86 87 88 89 90

  -- ** Special queries
  brokenPackages,
  dependencyClosure,
  reverseDependencyClosure,
  topologicalOrder,
  reverseTopologicalOrder,
  dependencyInconsistencies,
  dependencyCycles,
  dependencyGraph,
Duncan Coutts's avatar
Duncan Coutts committed
91
  moduleNameIndex,
92 93 94 95

  -- * Backwards compatibility
  deleteInstalledPackageId,
  lookupInstalledPackageId,
96 97
  ) where

98 99
import Prelude ()
import Distribution.Compat.Prelude hiding (lookup)
100
import qualified Distribution.Compat.Map.Strict as Map
101

102
import Distribution.Package
103
import Distribution.Backpack
104 105 106 107
import Distribution.ModuleName
import qualified Distribution.InstalledPackageInfo as IPI
import Distribution.Version
import Distribution.Simple.Utils
108
import Distribution.Types.Dependency
109

110
import Control.Exception (assert)
111
import Data.Array ((!))
ttuegel's avatar
ttuegel committed
112 113
import qualified Data.Array as Array
import qualified Data.Graph as Graph
114
import Data.List as List ( groupBy,  deleteBy, deleteFirstsBy )
ttuegel's avatar
ttuegel committed
115
import qualified Data.Tree  as Tree
116
import Control.Monad
117 118

-- | The collection of information about packages from one or more 'PackageDB's.
119
-- These packages generally should have an instance of 'PackageInstalled'
120
--
121
-- Packages are uniquely identified in by their 'UnitId', they can
Ian D. Bollinger's avatar
Ian D. Bollinger committed
122
-- also be efficiently looked up by package name or by name and version.
123
--
124
data PackageIndex a = PackageIndex {
Duncan Coutts's avatar
Duncan Coutts committed
125
  -- The primary index. Each InstalledPackageInfo record is uniquely identified
126
  -- by its UnitId.
Duncan Coutts's avatar
Duncan Coutts committed
127
  --
128
  unitIdIndex :: !(Map UnitId a),
Duncan Coutts's avatar
Duncan Coutts committed
129

Ian D. Bollinger's avatar
Ian D. Bollinger committed
130
  -- This auxiliary index maps package names (case-sensitively) to all the
Duncan Coutts's avatar
Duncan Coutts committed
131 132
  -- versions and instances of that package. This allows us to find all
  -- versions satisfying a dependency.
133
  --
Duncan Coutts's avatar
Duncan Coutts committed
134 135
  -- 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
136
  -- of the same package version. These are unique by UnitId
Duncan Coutts's avatar
Duncan Coutts committed
137
  -- and are kept in preference order.
138
  --
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
139 140
  -- FIXME: Clarify what "preference order" means. Check that this invariant is
  -- preserved. See #1463 for discussion.
141
  packageIdIndex :: !(Map PackageName (Map Version [a]))
142

143
  } deriving (Eq, Generic, Show, Read)
ttuegel's avatar
ttuegel committed
144 145

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

147 148
-- | The default package index which contains 'InstalledPackageInfo'.  Normally
-- use this.
149
type InstalledPackageIndex = PackageIndex IPI.InstalledPackageInfo
150

151
instance HasUnitId a => Monoid (PackageIndex a) where
Duncan Coutts's avatar
Duncan Coutts committed
152
  mempty  = PackageIndex Map.empty Map.empty
153
  mappend = (<>)
154
  --save one mappend with empty in the common case:
155
  mconcat [] = mempty
156 157
  mconcat xs = foldr1 mappend xs

158
instance HasUnitId a => Semigroup (PackageIndex a) where
159 160
  (<>) = merge

161
invariant :: HasUnitId a => PackageIndex a -> Bool
Duncan Coutts's avatar
Duncan Coutts committed
162
invariant (PackageIndex pids pnames) =
163
     map installedUnitId (Map.elems pids)
164
  == sort
165
     [ assert pinstOk (installedUnitId pinst)
Duncan Coutts's avatar
Duncan Coutts committed
166 167 168
     | (pname, pvers)  <- Map.toList pnames
     , let pversOk = not (Map.null pvers)
     , (pver,  pinsts) <- assert pversOk $ Map.toList pvers
169
     , let pinsts'  = sortBy (comparing installedUnitId) pinsts
Duncan Coutts's avatar
Duncan Coutts committed
170
           pinstsOk = all (\g -> length g == 1)
171
                          (groupBy (equating installedUnitId) pinsts')
Duncan Coutts's avatar
Duncan Coutts committed
172 173 174 175
     , pinst           <- assert pinstsOk $ pinsts'
     , let pinstOk = packageName    pinst == pname
                  && packageVersion pinst == pver
     ]
176 177 178 179 180
  -- If you see this invariant failing (ie the assert in mkPackageIndex below)
  -- then one thing to check is if it is happening in fromList. Check if the
  -- second list above (the sort [...] bit) is ending up with duplicates. This
  -- has been observed in practice once due to a messed up ghc-pkg db. How/why
  -- it became messed up was not discovered.
Duncan Coutts's avatar
Duncan Coutts committed
181

182

183 184 185 186
--
-- * Internal helpers
--

187 188
mkPackageIndex :: HasUnitId a
               => Map UnitId a
189 190
               -> Map PackageName (Map Version [a])
               -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
191 192
mkPackageIndex pids pnames = assert (invariant index) index
  where index = PackageIndex pids pnames
193

194 195 196

--
-- * Construction
197 198
--

199
-- | Build an index out of a bunch of packages.
200
--
201
-- If there are duplicates by 'UnitId' then later ones mask earlier
Duncan Coutts's avatar
Duncan Coutts committed
202
-- ones.
203
--
204
fromList :: HasUnitId a => [a] -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
205
fromList pkgs = mkPackageIndex pids pnames
206
  where
207
    pids      = Map.fromList [ (installedUnitId pkg, pkg) | pkg <- pkgs ]
Duncan Coutts's avatar
Duncan Coutts committed
208 209 210 211 212 213 214 215 216
    pnames    =
      Map.fromList
        [ (packageName (head pkgsN), pvers)
        | pkgsN <- groupBy (equating  packageName)
                 . sortBy  (comparing packageId)
                 $ pkgs
        , let pvers =
                Map.fromList
                [ (packageVersion (head pkgsNV),
217
                   nubBy (equating installedUnitId) (reverse pkgsNV))
Duncan Coutts's avatar
Duncan Coutts committed
218 219 220
                | pkgsNV <- groupBy (equating packageVersion) pkgsN
                ]
        ]
221

222 223 224 225
--
-- * Updates
--

226 227
-- | Merge two indexes.
--
Duncan Coutts's avatar
Duncan Coutts committed
228
-- Packages from the second mask packages from the first if they have the exact
229
-- same 'UnitId'.
230
--
Duncan Coutts's avatar
Duncan Coutts committed
231 232 233 234 235
-- 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.
--
236
merge :: HasUnitId a => PackageIndex a -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
237
      -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
238
merge (PackageIndex pids1 pnames1) (PackageIndex pids2 pnames2) =
239
  mkPackageIndex (Map.unionWith (\_ y -> y) pids1 pids2)
Duncan Coutts's avatar
Duncan Coutts committed
240 241 242 243 244
                 (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)
245
    (\\) = deleteFirstsBy (equating installedUnitId)
246

247

Duncan Coutts's avatar
Duncan Coutts committed
248
-- | Inserts a single package into the index.
249 250 251 252
--
-- This is equivalent to (but slightly quicker than) using 'mappend' or
-- 'merge' with a singleton index.
--
253
insert :: HasUnitId a => a -> PackageIndex a -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
254 255 256
insert pkg (PackageIndex pids pnames) =
    mkPackageIndex pids' pnames'

257
  where
258
    pids'   = Map.insert (installedUnitId pkg) pkg pids
Duncan Coutts's avatar
Duncan Coutts committed
259 260
    pnames' = insertPackageName pnames
    insertPackageName =
261
      Map.insertWith (\_ -> insertPackageVersion)
Duncan Coutts's avatar
Duncan Coutts committed
262 263 264 265
                     (packageName pkg)
                     (Map.singleton (packageVersion pkg) [pkg])

    insertPackageVersion =
266
      Map.insertWith (\_ -> insertPackageInstance)
Duncan Coutts's avatar
Duncan Coutts committed
267 268 269
                     (packageVersion pkg) [pkg]

    insertPackageInstance pkgs =
270
      pkg : deleteBy (equating installedUnitId) pkg pkgs
Duncan Coutts's avatar
Duncan Coutts committed
271 272 273 274


-- | Removes a single installed package from the index.
--
275
deleteUnitId :: HasUnitId a
276 277
             => UnitId -> PackageIndex a
             -> PackageIndex a
278
deleteUnitId ipkgid original@(PackageIndex pids pnames) =
Duncan Coutts's avatar
Duncan Coutts committed
279 280 281 282 283
  case Map.updateLookupWithKey (\_ _ -> Nothing) ipkgid pids of
    (Nothing,     _)     -> original
    (Just spkgid, pids') -> mkPackageIndex pids'
                                          (deletePkgName spkgid pnames)

Duncan Coutts's avatar
Duncan Coutts committed
284
  where
Duncan Coutts's avatar
Duncan Coutts committed
285 286 287 288 289 290
    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
291

Duncan Coutts's avatar
Duncan Coutts committed
292
    deletePkgInstance =
293
        (\xs -> if null xs then Nothing else Just xs)
294
      . List.deleteBy (\_ pkg -> installedUnitId pkg == ipkgid) undefined
Duncan Coutts's avatar
Duncan Coutts committed
295

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
296
-- | Backwards compatibility wrapper for Cabal pre-1.24.
297 298 299 300 301
{-# DEPRECATED deleteInstalledPackageId "Use deleteUnitId instead" #-}
deleteInstalledPackageId :: HasUnitId a
                         => UnitId -> PackageIndex a
                         -> PackageIndex a
deleteInstalledPackageId = deleteUnitId
Duncan Coutts's avatar
Duncan Coutts committed
302 303

-- | Removes all packages with this source 'PackageId' from the index.
304
--
305
deleteSourcePackageId :: HasUnitId a => PackageId -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
306
                      -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
307 308 309 310 311 312
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
313
                     (foldl' (flip (Map.delete . installedUnitId)) pids pkgs)
Duncan Coutts's avatar
Duncan Coutts committed
314 315 316 317 318 319 320 321 322
                     (deletePkgName pnames)
  where
    deletePkgName =
      Map.update deletePkgVersion (packageName pkgid)

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

323 324 325

-- | Removes all packages with this (case-sensitive) name from the index.
--
326
deletePackageName :: HasUnitId a => PackageName -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
327
                  -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
328 329 330 331
deletePackageName name original@(PackageIndex pids pnames) =
  case Map.lookup name pnames of
    Nothing     -> original
    Just pvers  -> mkPackageIndex
332
                     (foldl' (flip (Map.delete . installedUnitId)) pids
Duncan Coutts's avatar
Duncan Coutts committed
333 334
                             (concat (Map.elems pvers)))
                     (Map.delete name pnames)
335

Duncan Coutts's avatar
Duncan Coutts committed
336
{-
337 338
-- | Removes all packages satisfying this dependency from the index.
--
Duncan Coutts's avatar
Duncan Coutts committed
339
deleteDependency :: Dependency -> PackageIndex -> PackageIndex
340
deleteDependency (Dependency name verstionRange) =
Duncan Coutts's avatar
Duncan Coutts committed
341 342
  delete' name (\pkg -> packageVersion pkg `withinRange` verstionRange)
-}
343

344 345 346 347
--
-- * Bulk queries
--

348 349
-- | Get all the packages from the index.
--
350
allPackages :: PackageIndex a -> [a]
351
allPackages = Map.elems . unitIdIndex
352 353 354

-- | Get all the packages from the index.
--
355
-- They are grouped by package name (case-sensitively).
356
--
357
allPackagesByName :: PackageIndex a -> [(PackageName, [a])]
358
allPackagesByName index =
359
  [ (pkgname, concat (Map.elems pvers))
360
  | (pkgname, pvers) <- Map.toList (packageIdIndex index) ]
361 362 363 364 365

-- | Get all the packages from the index.
--
-- They are grouped by source package id (package name and version).
--
366
allPackagesBySourcePackageId :: HasUnitId a => PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
367
                             -> [(PackageId, [a])]
368
allPackagesBySourcePackageId index =
369
  [ (packageId ipkg, ipkgs)
370
  | pvers <- Map.elems (packageIdIndex index)
371
  , ipkgs@(ipkg:_) <- Map.elems pvers ]
372 373 374 375 376

--
-- * Lookups
--

377
-- | Does a lookup by unit identifier.
378
--
379
-- Since multiple package DBs mask each other by 'UnitId',
380 381
-- then we get back at most one package.
--
382
lookupUnitId :: PackageIndex a -> UnitId
383
             -> Maybe a
384
lookupUnitId index uid = Map.lookup uid (unitIdIndex index)
385 386 387 388 389 390

-- | Does a lookup by component identifier.  In the absence
-- of Backpack, this is just a 'lookupUnitId'.
--
lookupComponentId :: PackageIndex a -> ComponentId
                  -> Maybe a
391 392
lookupComponentId index cid =
    Map.lookup (newSimpleUnitId cid) (unitIdIndex index)
Duncan Coutts's avatar
Duncan Coutts committed
393

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
394
-- | Backwards compatibility for Cabal pre-1.24.
395 396 397 398 399
{-# DEPRECATED lookupInstalledPackageId "Use lookupUnitId instead" #-}
lookupInstalledPackageId :: PackageIndex a -> UnitId
                         -> Maybe a
lookupInstalledPackageId = lookupUnitId

400

Duncan Coutts's avatar
Duncan Coutts committed
401 402 403
-- | Does a lookup by source package id (name & version).
--
-- There can be multiple installed packages with the same source 'PackageId'
404
-- but different 'UnitId'. They are returned in order of
Duncan Coutts's avatar
Duncan Coutts committed
405
-- preference, with the most preferred first.
406
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
407
lookupSourcePackageId :: PackageIndex a -> PackageId -> [a]
408 409
lookupSourcePackageId index pkgid =
  case Map.lookup (packageName pkgid) (packageIdIndex index) of
Duncan Coutts's avatar
Duncan Coutts committed
410 411 412 413
    Nothing     -> []
    Just pvers  -> case Map.lookup (packageVersion pkgid) pvers of
      Nothing   -> []
      Just pkgs -> pkgs -- in preference order
414

415 416
-- | Convenient alias of 'lookupSourcePackageId', but assuming only
-- one package per package ID.
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
417
lookupPackageId :: PackageIndex a -> PackageId -> Maybe a
418 419 420 421
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
422 423 424

-- | Does a lookup by source package name.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
425
lookupPackageName :: PackageIndex a -> PackageName
426
                  -> [(Version, [a])]
427 428
lookupPackageName index name =
  case Map.lookup name (packageIdIndex index) of
Duncan Coutts's avatar
Duncan Coutts committed
429 430 431 432 433
    Nothing     -> []
    Just pvers  -> Map.toList pvers


-- | Does a lookup by source package name and a range of versions.
434 435 436 437
--
-- We get back any number of versions of the specified package name, all
-- satisfying the version range constraint.
--
438 439 440 441
-- INVARIANT: List of eligible 'IPI.InstalledPackageInfo' is non-empty.
--
lookupDependency :: InstalledPackageIndex -> Dependency
                 -> [(Version, [IPI.InstalledPackageInfo])]
442 443
lookupDependency index (Dependency name versionRange) =
  case Map.lookup name (packageIdIndex index) of
Duncan Coutts's avatar
Duncan Coutts committed
444
    Nothing    -> []
445 446 447 448 449 450 451 452 453 454 455 456 457
    Just pvers -> [ (ver, pkgs')
                  | (ver, pkgs) <- Map.toList pvers
                  , ver `withinRange` versionRange
                  , let pkgs' = filter eligible pkgs
                  -- Enforce the invariant
                  , not (null pkgs')
                  ]
 where
  -- When we select for dependencies, we ONLY want to pick up indefinite
  -- packages, or packages with no instantiations.  We'll do mix-in
  -- linking to improve any such package into an instantiated one
  -- later.
  eligible pkg = IPI.indefinite pkg || null (IPI.instantiatedWith pkg)
458 459 460 461

--
-- * Case insensitive name lookups
--
462 463 464

-- | Does a case-insensitive search by package name.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
465
-- If there is only one package that compares case-insensitively to this name
466
-- then the search is unambiguous and we get back all versions of that package.
Ian D. Bollinger's avatar
Ian D. Bollinger committed
467
-- If several match case-insensitively but one matches exactly then it is also
468 469
-- unambiguous.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
470
-- If however several match case-insensitively and none match exactly then we
471 472 473 474
-- 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
475
searchByName :: PackageIndex a -> String -> SearchResult [a]
476
searchByName index name =
477 478
  case [ pkgs | pkgs@(pname,_) <- Map.toList (packageIdIndex index)
              , lowercase (unPackageName pname) == lname ] of
Duncan Coutts's avatar
Duncan Coutts committed
479 480
    []               -> None
    [(_,pvers)]      -> Unambiguous (concat (Map.elems pvers))
481
    pkgss            -> case find ((mkPackageName name ==) . fst) pkgss of
Duncan Coutts's avatar
Duncan Coutts committed
482 483
      Just (_,pvers) -> Unambiguous (concat (Map.elems pvers))
      Nothing        -> Ambiguous (map (concat . Map.elems . snd) pkgss)
484
  where lname = lowercase name
485 486 487 488 489 490 491

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
492
searchByNameSubstring :: PackageIndex a -> String -> [a]
493
searchByNameSubstring index searchterm =
494
  [ pkg
495 496
  | (pname, pvers) <- Map.toList (packageIdIndex index)
  , lsearchterm `isInfixOf` lowercase (unPackageName pname)
Duncan Coutts's avatar
Duncan Coutts committed
497
  , pkgs <- Map.elems pvers
498
  , pkg <- pkgs ]
499
  where lsearchterm = lowercase searchterm
500

Duncan Coutts's avatar
Duncan Coutts committed
501 502 503 504 505 506 507 508

--
-- * Special queries
--

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

509 510 511 512 513 514 515
-- | 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.
--
516
dependencyCycles :: PackageInstalled a => PackageIndex a -> [[a]]
517
dependencyCycles index =
518 519
  [ vs | Graph.CyclicSCC vs <- Graph.stronglyConnComp adjacencyList ]
  where
520
    adjacencyList = [ (pkg, installedUnitId pkg, installedDepends pkg)
521 522 523
                    | pkg <- allPackages index ]


Duncan Coutts's avatar
Duncan Coutts committed
524
-- | All packages that have immediate dependencies that are not in the index.
525
--
526 527
-- Returns such packages along with the dependencies that they're missing.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
528
brokenPackages :: PackageInstalled a => PackageIndex a
529
               -> [(a, [UnitId])]
530
brokenPackages index =
531
  [ (pkg, missing)
Duncan Coutts's avatar
Duncan Coutts committed
532
  | pkg  <- allPackages index
533
  , let missing = [ pkg' | pkg' <- installedDepends pkg
534
                         , isNothing (lookupUnitId index pkg') ]
535 536
  , not (null missing) ]

537
-- | Tries to take the transitive closure of the package dependencies.
538
--
539
-- If the transitive closure is complete then it returns that subset of the
540 541 542
-- 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
543
-- the original given 'PackageId's do not occur in the index.
544
--
545
dependencyClosure :: PackageInstalled a => PackageIndex a
546
                  -> [UnitId]
547
                  -> Either (PackageIndex a)
548
                            [(a, [UnitId])]
549
dependencyClosure index pkgids0 = case closure mempty [] pkgids0 of
550 551
  (completed, []) -> Left completed
  (completed, _)  -> Right (brokenPackages completed)
552
 where
553
    closure completed failed []             = (completed, failed)
554
    closure completed failed (pkgid:pkgids) = case lookupUnitId index pkgid of
555
      Nothing   -> closure completed (pkgid:failed) pkgids
556
      Just pkg  -> case lookupUnitId completed (installedUnitId pkg) of
557 558
        Just _  -> closure completed  failed pkgids
        Nothing -> closure completed' failed pkgids'
Duncan Coutts's avatar
Duncan Coutts committed
559
          where completed' = insert pkg completed
560
                pkgids'    = installedDepends pkg ++ pkgids
561

562
-- | Takes the transitive closure of the packages reverse dependencies.
563
--
Duncan Coutts's avatar
Duncan Coutts committed
564
-- * The given 'PackageId's must be in the index.
565
--
566
reverseDependencyClosure :: PackageInstalled a => PackageIndex a
567
                         -> [UnitId]
568
                         -> [a]
569
reverseDependencyClosure index =
570 571 572 573
    map vertexToPkg
  . concatMap Tree.flatten
  . Graph.dfs reverseDepGraph
  . map (fromMaybe noSuchPkgId . pkgIdToVertex)
574

575
  where
576
    (depGraph, vertexToPkg, pkgIdToVertex) = dependencyGraph index
577 578 579
    reverseDepGraph = Graph.transposeG depGraph
    noSuchPkgId = error "reverseDependencyClosure: package is not in the graph"

580
topologicalOrder :: PackageInstalled a => PackageIndex a -> [a]
581 582 583 584 585
topologicalOrder index = map toPkgId
                       . Graph.topSort
                       $ graph
  where (graph, toPkgId, _) = dependencyGraph index

586
reverseTopologicalOrder :: PackageInstalled a => PackageIndex a -> [a]
587 588 589 590 591 592 593 594 595 596 597
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'.
--
598
dependencyGraph :: PackageInstalled a => PackageIndex a
599
                -> (Graph.Graph,
600
                    Graph.Vertex -> a,
601
                    UnitId -> Maybe Graph.Vertex)
602
dependencyGraph index = (graph, vertex_to_pkg, id_to_vertex)
603 604
  where
    graph = Array.listArray bounds
605
              [ [ v | Just v <- map id_to_vertex (installedDepends pkg) ]
606
              | pkg <- pkgs ]
607

Duncan Coutts's avatar
Duncan Coutts committed
608
    pkgs             = sortBy (comparing packageId) (allPackages index)
609
    vertices         = zip (map installedUnitId pkgs) [0..]
610
    vertex_map       = Map.fromList vertices
611
    id_to_vertex pid = Map.lookup pid vertex_map
612 613

    vertex_to_pkg vertex = pkgTable ! vertex
614 615 616 617 618

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

619 620 621 622
-- | We maintain the invariant that, for any 'DepUniqueKey', there
-- is only one instance of the package in our database.
type DepUniqueKey = (PackageName, Map ModuleName OpenModule)

623 624 625 626 627 628 629 630 631 632
-- | 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.
--
633
dependencyInconsistencies :: InstalledPackageIndex
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
                             -- At DepUniqueKey...
                          -> [(DepUniqueKey,
                               -- There were multiple packages (BAD!)
                               [(UnitId,
                                 -- And here are the packages which
                                 -- immediately depended on it
                                 [IPI.InstalledPackageInfo])])]
dependencyInconsistencies index = do
    (dep_key, insts_map) <- Map.toList inverseIndex
    let insts = Map.toList insts_map
    guard (length insts >= 2)
    return (dep_key, insts)
  where
    inverseIndex :: Map DepUniqueKey (Map UnitId [IPI.InstalledPackageInfo])
    inverseIndex = Map.fromListWith (Map.unionWith (++)) $ do
        pkg <- allPackages index
        dep_ipid <- installedDepends pkg
        Just dep <- [lookupUnitId index dep_ipid]
        let dep_key = (packageName dep, Map.fromList (IPI.instantiatedWith dep))
        return (dep_key, Map.singleton dep_ipid [pkg])
Duncan Coutts's avatar
Duncan Coutts committed
654

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
655 656 657 658
-- | 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@.
659
moduleNameIndex :: InstalledPackageIndex -> Map ModuleName [IPI.InstalledPackageInfo]
Duncan Coutts's avatar
Duncan Coutts committed
660
moduleNameIndex index =
661 662
  Map.fromListWith (++) $ do
    pkg <- allPackages index
663
    IPI.ExposedModule m reexport <- IPI.exposedModules pkg
664 665
    case reexport of
        Nothing -> return (m, [pkg])
666 667
        Just (OpenModuleVar _) -> []
        Just (OpenModule _ m') | m == m'   -> []
668
                                | otherwise -> return (m', [pkg])
669 670 671 672 673
        -- 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!