PackageIndex.hs 25.1 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
-- 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.
--
-- 'PackageIndex' is parametric over what it actually records, and it
-- is used in two ways:
--
--      * 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.)
--
--      * The 'PlanIndex' (defined in 'Distribution.Client.InstallPlan'),
--        contains a graph of 'GenericPlanPackage'.  Ignoring its type
--        parameters for a moment, a 'PlanIndex' is an extension of the
--        'InstalledPackageIndex' to also record nodes for packages
--        which are *planned* to be installed, but not actually
--        installed yet.  A 'PlanIndex' containing only 'PreExisting'
--        packages is essentially a 'PackageIndex'.
--
--        'PlanIndex'es actually require some auxiliary information, so
--        most users interact with a 'GenericInstallPlan'.  This type is
--        specialized as an 'ElaboratedInstallPlan' (for @cabal
--        new-build@) or an 'InstallPlan' (for @cabal install@).
--
-- 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
57
--
58 59
module Distribution.Simple.PackageIndex (
  -- * Package index data type
60
  InstalledPackageIndex,
61
  PackageIndex,
62

63
  -- * Creating an index
64 65
  fromList,

66
  -- * Updates
67
  merge,
Duncan Coutts's avatar
Duncan Coutts committed
68

69
  insert,
Duncan Coutts's avatar
Duncan Coutts committed
70

71
  deleteUnitId,
Duncan Coutts's avatar
Duncan Coutts committed
72
  deleteSourcePackageId,
73
  deletePackageName,
Duncan Coutts's avatar
Duncan Coutts committed
74
--  deleteDependency,
75 76 77 78

  -- * Queries

  -- ** Precise lookups
79
  lookupUnitId,
80
  lookupComponentId,
Duncan Coutts's avatar
Duncan Coutts committed
81
  lookupSourcePackageId,
82
  lookupPackageId,
83
  lookupPackageName,
84 85 86 87 88 89 90 91 92 93
  lookupDependency,

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

  -- ** Bulk queries
  allPackages,
  allPackagesByName,
94
  allPackagesBySourcePackageId,
95 96 97 98 99 100 101 102 103 104

  -- ** Special queries
  brokenPackages,
  dependencyClosure,
  reverseDependencyClosure,
  topologicalOrder,
  reverseTopologicalOrder,
  dependencyInconsistencies,
  dependencyCycles,
  dependencyGraph,
Duncan Coutts's avatar
Duncan Coutts committed
105
  moduleNameIndex,
106 107 108 109

  -- * Backwards compatibility
  deleteInstalledPackageId,
  lookupInstalledPackageId,
110 111
  ) where

112 113 114
import Prelude ()
import Distribution.Compat.Prelude hiding (lookup)

115
import Distribution.Package
116
import Distribution.Backpack
117 118 119 120
import Distribution.ModuleName
import qualified Distribution.InstalledPackageInfo as IPI
import Distribution.Version
import Distribution.Simple.Utils
121
import Distribution.Types.Dependency
122

123
import Control.Exception (assert)
124
import Data.Array ((!))
ttuegel's avatar
ttuegel committed
125 126
import qualified Data.Array as Array
import qualified Data.Graph as Graph
127
import Data.List as List ( groupBy,  deleteBy, deleteFirstsBy )
ttuegel's avatar
ttuegel committed
128 129
import qualified Data.Map as Map
import qualified Data.Tree  as Tree
130 131

-- | The collection of information about packages from one or more 'PackageDB's.
132
-- These packages generally should have an instance of 'PackageInstalled'
133
--
134
-- Packages are uniquely identified in by their 'UnitId', they can
Ian D. Bollinger's avatar
Ian D. Bollinger committed
135
-- also be efficiently looked up by package name or by name and version.
136
--
137
data PackageIndex a = PackageIndex {
Duncan Coutts's avatar
Duncan Coutts committed
138
  -- The primary index. Each InstalledPackageInfo record is uniquely identified
139
  -- by its UnitId.
Duncan Coutts's avatar
Duncan Coutts committed
140
  --
141
  unitIdIndex :: !(Map UnitId a),
Duncan Coutts's avatar
Duncan Coutts committed
142

Ian D. Bollinger's avatar
Ian D. Bollinger committed
143
  -- This auxiliary index maps package names (case-sensitively) to all the
Duncan Coutts's avatar
Duncan Coutts committed
144 145
  -- versions and instances of that package. This allows us to find all
  -- versions satisfying a dependency.
146
  --
Duncan Coutts's avatar
Duncan Coutts committed
147 148
  -- 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
149
  -- of the same package version. These are unique by UnitId
Duncan Coutts's avatar
Duncan Coutts committed
150
  -- and are kept in preference order.
151
  --
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
152 153
  -- FIXME: Clarify what "preference order" means. Check that this invariant is
  -- preserved. See #1463 for discussion.
154
  packageIdIndex :: !(Map PackageName (Map Version [a]))
155

156
  } deriving (Eq, Generic, Show, Read)
ttuegel's avatar
ttuegel committed
157 158

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

160 161
-- | The default package index which contains 'InstalledPackageInfo'.  Normally
-- use this.
162
type InstalledPackageIndex = PackageIndex IPI.InstalledPackageInfo
163

164
instance HasUnitId a => Monoid (PackageIndex a) where
Duncan Coutts's avatar
Duncan Coutts committed
165
  mempty  = PackageIndex Map.empty Map.empty
166
  mappend = (<>)
167
  --save one mappend with empty in the common case:
168
  mconcat [] = mempty
169 170
  mconcat xs = foldr1 mappend xs

171
instance HasUnitId a => Semigroup (PackageIndex a) where
172 173
  (<>) = merge

174
invariant :: HasUnitId a => PackageIndex a -> Bool
Duncan Coutts's avatar
Duncan Coutts committed
175
invariant (PackageIndex pids pnames) =
176
     map installedUnitId (Map.elems pids)
177
  == sort
178
     [ assert pinstOk (installedUnitId pinst)
Duncan Coutts's avatar
Duncan Coutts committed
179 180 181
     | (pname, pvers)  <- Map.toList pnames
     , let pversOk = not (Map.null pvers)
     , (pver,  pinsts) <- assert pversOk $ Map.toList pvers
182
     , let pinsts'  = sortBy (comparing installedUnitId) pinsts
Duncan Coutts's avatar
Duncan Coutts committed
183
           pinstsOk = all (\g -> length g == 1)
184
                          (groupBy (equating installedUnitId) pinsts')
Duncan Coutts's avatar
Duncan Coutts committed
185 186 187 188
     , pinst           <- assert pinstsOk $ pinsts'
     , let pinstOk = packageName    pinst == pname
                  && packageVersion pinst == pver
     ]
189 190 191 192 193
  -- 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
194

195

196 197 198 199
--
-- * Internal helpers
--

200 201
mkPackageIndex :: HasUnitId a
               => Map UnitId a
202 203
               -> Map PackageName (Map Version [a])
               -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
204 205
mkPackageIndex pids pnames = assert (invariant index) index
  where index = PackageIndex pids pnames
206

207 208 209

--
-- * Construction
210 211
--

212
-- | Build an index out of a bunch of packages.
213
--
214
-- If there are duplicates by 'UnitId' then later ones mask earlier
Duncan Coutts's avatar
Duncan Coutts committed
215
-- ones.
216
--
217
fromList :: HasUnitId a => [a] -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
218
fromList pkgs = mkPackageIndex pids pnames
219
  where
220
    pids      = Map.fromList [ (installedUnitId pkg, pkg) | pkg <- pkgs ]
Duncan Coutts's avatar
Duncan Coutts committed
221 222 223 224 225 226 227 228 229
    pnames    =
      Map.fromList
        [ (packageName (head pkgsN), pvers)
        | pkgsN <- groupBy (equating  packageName)
                 . sortBy  (comparing packageId)
                 $ pkgs
        , let pvers =
                Map.fromList
                [ (packageVersion (head pkgsNV),
230
                   nubBy (equating installedUnitId) (reverse pkgsNV))
Duncan Coutts's avatar
Duncan Coutts committed
231 232 233
                | pkgsNV <- groupBy (equating packageVersion) pkgsN
                ]
        ]
234

235 236 237 238
--
-- * Updates
--

239 240
-- | Merge two indexes.
--
Duncan Coutts's avatar
Duncan Coutts committed
241
-- Packages from the second mask packages from the first if they have the exact
242
-- same 'UnitId'.
243
--
Duncan Coutts's avatar
Duncan Coutts committed
244 245 246 247 248
-- 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.
--
249
merge :: HasUnitId a => PackageIndex a -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
250
      -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
251
merge (PackageIndex pids1 pnames1) (PackageIndex pids2 pnames2) =
252
  mkPackageIndex (Map.unionWith (\_ y -> y) pids1 pids2)
Duncan Coutts's avatar
Duncan Coutts committed
253 254 255 256 257
                 (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)
258
    (\\) = deleteFirstsBy (equating installedUnitId)
259

260

Duncan Coutts's avatar
Duncan Coutts committed
261
-- | Inserts a single package into the index.
262 263 264 265
--
-- This is equivalent to (but slightly quicker than) using 'mappend' or
-- 'merge' with a singleton index.
--
266
insert :: HasUnitId a => a -> PackageIndex a -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
267 268 269
insert pkg (PackageIndex pids pnames) =
    mkPackageIndex pids' pnames'

270
  where
271
    pids'   = Map.insert (installedUnitId pkg) pkg pids
Duncan Coutts's avatar
Duncan Coutts committed
272 273 274 275 276 277 278 279 280 281 282
    pnames' = insertPackageName pnames
    insertPackageName =
      Map.insertWith' (\_ -> insertPackageVersion)
                     (packageName pkg)
                     (Map.singleton (packageVersion pkg) [pkg])

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

    insertPackageInstance pkgs =
283
      pkg : deleteBy (equating installedUnitId) pkg pkgs
Duncan Coutts's avatar
Duncan Coutts committed
284 285 286 287


-- | Removes a single installed package from the index.
--
288
deleteUnitId :: HasUnitId a
289 290
             => UnitId -> PackageIndex a
             -> PackageIndex a
291
deleteUnitId ipkgid original@(PackageIndex pids pnames) =
Duncan Coutts's avatar
Duncan Coutts committed
292 293 294 295 296
  case Map.updateLookupWithKey (\_ _ -> Nothing) ipkgid pids of
    (Nothing,     _)     -> original
    (Just spkgid, pids') -> mkPackageIndex pids'
                                          (deletePkgName spkgid pnames)

Duncan Coutts's avatar
Duncan Coutts committed
297
  where
Duncan Coutts's avatar
Duncan Coutts committed
298 299 300 301 302 303
    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
304

Duncan Coutts's avatar
Duncan Coutts committed
305
    deletePkgInstance =
306
        (\xs -> if null xs then Nothing else Just xs)
307
      . List.deleteBy (\_ pkg -> installedUnitId pkg == ipkgid) undefined
Duncan Coutts's avatar
Duncan Coutts committed
308

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
309
-- | Backwards compatibility wrapper for Cabal pre-1.24.
310 311 312 313 314
{-# DEPRECATED deleteInstalledPackageId "Use deleteUnitId instead" #-}
deleteInstalledPackageId :: HasUnitId a
                         => UnitId -> PackageIndex a
                         -> PackageIndex a
deleteInstalledPackageId = deleteUnitId
Duncan Coutts's avatar
Duncan Coutts committed
315 316

-- | Removes all packages with this source 'PackageId' from the index.
317
--
318
deleteSourcePackageId :: HasUnitId a => PackageId -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
319
                      -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
320 321 322 323 324 325
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
326
                     (foldl' (flip (Map.delete . installedUnitId)) pids pkgs)
Duncan Coutts's avatar
Duncan Coutts committed
327 328 329 330 331 332 333 334 335
                     (deletePkgName pnames)
  where
    deletePkgName =
      Map.update deletePkgVersion (packageName pkgid)

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

336 337 338

-- | Removes all packages with this (case-sensitive) name from the index.
--
339
deletePackageName :: HasUnitId a => PackageName -> PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
340
                  -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
341 342 343 344
deletePackageName name original@(PackageIndex pids pnames) =
  case Map.lookup name pnames of
    Nothing     -> original
    Just pvers  -> mkPackageIndex
345
                     (foldl' (flip (Map.delete . installedUnitId)) pids
Duncan Coutts's avatar
Duncan Coutts committed
346 347
                             (concat (Map.elems pvers)))
                     (Map.delete name pnames)
348

Duncan Coutts's avatar
Duncan Coutts committed
349
{-
350 351
-- | Removes all packages satisfying this dependency from the index.
--
Duncan Coutts's avatar
Duncan Coutts committed
352
deleteDependency :: Dependency -> PackageIndex -> PackageIndex
353
deleteDependency (Dependency name verstionRange) =
Duncan Coutts's avatar
Duncan Coutts committed
354 355
  delete' name (\pkg -> packageVersion pkg `withinRange` verstionRange)
-}
356

357 358 359 360
--
-- * Bulk queries
--

361 362
-- | Get all the packages from the index.
--
363
allPackages :: PackageIndex a -> [a]
364
allPackages = Map.elems . unitIdIndex
365 366 367

-- | Get all the packages from the index.
--
368
-- They are grouped by package name (case-sensitively).
369
--
370
allPackagesByName :: PackageIndex a -> [(PackageName, [a])]
371
allPackagesByName index =
372
  [ (pkgname, concat (Map.elems pvers))
373
  | (pkgname, pvers) <- Map.toList (packageIdIndex index) ]
374 375 376 377 378

-- | Get all the packages from the index.
--
-- They are grouped by source package id (package name and version).
--
379
allPackagesBySourcePackageId :: HasUnitId a => PackageIndex a
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
380
                             -> [(PackageId, [a])]
381
allPackagesBySourcePackageId index =
382
  [ (packageId ipkg, ipkgs)
383
  | pvers <- Map.elems (packageIdIndex index)
384
  , ipkgs@(ipkg:_) <- Map.elems pvers ]
385 386 387 388 389

--
-- * Lookups
--

390
-- | Does a lookup by unit identifier.
391
--
392
-- Since multiple package DBs mask each other by 'UnitId',
393 394
-- then we get back at most one package.
--
395
lookupUnitId :: PackageIndex a -> UnitId
396
             -> Maybe a
397
lookupUnitId index uid = Map.lookup uid (unitIdIndex index)
398 399 400 401 402 403

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

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
407
-- | Backwards compatibility for Cabal pre-1.24.
408 409 410 411 412
{-# DEPRECATED lookupInstalledPackageId "Use lookupUnitId instead" #-}
lookupInstalledPackageId :: PackageIndex a -> UnitId
                         -> Maybe a
lookupInstalledPackageId = lookupUnitId

413

Duncan Coutts's avatar
Duncan Coutts committed
414 415 416
-- | Does a lookup by source package id (name & version).
--
-- There can be multiple installed packages with the same source 'PackageId'
417
-- but different 'UnitId'. They are returned in order of
Duncan Coutts's avatar
Duncan Coutts committed
418
-- preference, with the most preferred first.
419
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
420
lookupSourcePackageId :: PackageIndex a -> PackageId -> [a]
421 422
lookupSourcePackageId index pkgid =
  case Map.lookup (packageName pkgid) (packageIdIndex index) of
Duncan Coutts's avatar
Duncan Coutts committed
423 424 425 426
    Nothing     -> []
    Just pvers  -> case Map.lookup (packageVersion pkgid) pvers of
      Nothing   -> []
      Just pkgs -> pkgs -- in preference order
427

428 429
-- | Convenient alias of 'lookupSourcePackageId', but assuming only
-- one package per package ID.
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
430
lookupPackageId :: PackageIndex a -> PackageId -> Maybe a
431 432 433 434
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
435 436 437

-- | Does a lookup by source package name.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
438
lookupPackageName :: PackageIndex a -> PackageName
439
                  -> [(Version, [a])]
440 441
lookupPackageName index name =
  case Map.lookup name (packageIdIndex index) of
Duncan Coutts's avatar
Duncan Coutts committed
442 443 444 445 446
    Nothing     -> []
    Just pvers  -> Map.toList pvers


-- | Does a lookup by source package name and a range of versions.
447 448 449 450
--
-- We get back any number of versions of the specified package name, all
-- satisfying the version range constraint.
--
451 452 453 454
-- INVARIANT: List of eligible 'IPI.InstalledPackageInfo' is non-empty.
--
lookupDependency :: InstalledPackageIndex -> Dependency
                 -> [(Version, [IPI.InstalledPackageInfo])]
455 456
lookupDependency index (Dependency name versionRange) =
  case Map.lookup name (packageIdIndex index) of
Duncan Coutts's avatar
Duncan Coutts committed
457
    Nothing    -> []
458 459 460 461 462 463 464 465 466 467 468 469 470
    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)
471 472 473 474

--
-- * Case insensitive name lookups
--
475 476 477

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

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
505
searchByNameSubstring :: PackageIndex a -> String -> [a]
506
searchByNameSubstring index searchterm =
507
  [ pkg
508 509
  | (pname, pvers) <- Map.toList (packageIdIndex index)
  , lsearchterm `isInfixOf` lowercase (unPackageName pname)
Duncan Coutts's avatar
Duncan Coutts committed
510
  , pkgs <- Map.elems pvers
511
  , pkg <- pkgs ]
512
  where lsearchterm = lowercase searchterm
513

Duncan Coutts's avatar
Duncan Coutts committed
514 515 516 517 518 519 520 521

--
-- * Special queries
--

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

522 523 524 525 526 527 528
-- | 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.
--
529
dependencyCycles :: PackageInstalled a => PackageIndex a -> [[a]]
530
dependencyCycles index =
531 532
  [ vs | Graph.CyclicSCC vs <- Graph.stronglyConnComp adjacencyList ]
  where
533
    adjacencyList = [ (pkg, installedUnitId pkg, installedDepends pkg)
534 535 536
                    | pkg <- allPackages index ]


Duncan Coutts's avatar
Duncan Coutts committed
537
-- | All packages that have immediate dependencies that are not in the index.
538
--
539 540
-- Returns such packages along with the dependencies that they're missing.
--
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
541
brokenPackages :: PackageInstalled a => PackageIndex a
542
               -> [(a, [UnitId])]
543
brokenPackages index =
544
  [ (pkg, missing)
Duncan Coutts's avatar
Duncan Coutts committed
545
  | pkg  <- allPackages index
546
  , let missing = [ pkg' | pkg' <- installedDepends pkg
547
                         , isNothing (lookupUnitId index pkg') ]
548 549
  , not (null missing) ]

550
-- | Tries to take the transitive closure of the package dependencies.
551
--
552
-- If the transitive closure is complete then it returns that subset of the
553 554 555
-- 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
556
-- the original given 'PackageId's do not occur in the index.
557
--
558
dependencyClosure :: PackageInstalled a => PackageIndex a
559
                  -> [UnitId]
560
                  -> Either (PackageIndex a)
561
                            [(a, [UnitId])]
562
dependencyClosure index pkgids0 = case closure mempty [] pkgids0 of
563 564
  (completed, []) -> Left completed
  (completed, _)  -> Right (brokenPackages completed)
565
 where
566
    closure completed failed []             = (completed, failed)
567
    closure completed failed (pkgid:pkgids) = case lookupUnitId index pkgid of
568
      Nothing   -> closure completed (pkgid:failed) pkgids
569
      Just pkg  -> case lookupUnitId completed (installedUnitId pkg) of
570 571
        Just _  -> closure completed  failed pkgids
        Nothing -> closure completed' failed pkgids'
Duncan Coutts's avatar
Duncan Coutts committed
572
          where completed' = insert pkg completed
573
                pkgids'    = installedDepends pkg ++ pkgids
574

575
-- | Takes the transitive closure of the packages reverse dependencies.
576
--
Duncan Coutts's avatar
Duncan Coutts committed
577
-- * The given 'PackageId's must be in the index.
578
--
579
reverseDependencyClosure :: PackageInstalled a => PackageIndex a
580
                         -> [UnitId]
581
                         -> [a]
582
reverseDependencyClosure index =
583 584 585 586
    map vertexToPkg
  . concatMap Tree.flatten
  . Graph.dfs reverseDepGraph
  . map (fromMaybe noSuchPkgId . pkgIdToVertex)
587

588
  where
589
    (depGraph, vertexToPkg, pkgIdToVertex) = dependencyGraph index
590 591 592
    reverseDepGraph = Graph.transposeG depGraph
    noSuchPkgId = error "reverseDependencyClosure: package is not in the graph"

593
topologicalOrder :: PackageInstalled a => PackageIndex a -> [a]
594 595 596 597 598
topologicalOrder index = map toPkgId
                       . Graph.topSort
                       $ graph
  where (graph, toPkgId, _) = dependencyGraph index

599
reverseTopologicalOrder :: PackageInstalled a => PackageIndex a -> [a]
600 601 602 603 604 605 606 607 608 609 610
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'.
--
611
dependencyGraph :: PackageInstalled a => PackageIndex a
612
                -> (Graph.Graph,
613
                    Graph.Vertex -> a,
614
                    UnitId -> Maybe Graph.Vertex)
615
dependencyGraph index = (graph, vertex_to_pkg, id_to_vertex)
616 617
  where
    graph = Array.listArray bounds
618
              [ [ v | Just v <- map id_to_vertex (installedDepends pkg) ]
619
              | pkg <- pkgs ]
620

Duncan Coutts's avatar
Duncan Coutts committed
621
    pkgs             = sortBy (comparing packageId) (allPackages index)
622
    vertices         = zip (map installedUnitId pkgs) [0..]
623
    vertex_map       = Map.fromList vertices
624
    id_to_vertex pid = Map.lookup pid vertex_map
625 626

    vertex_to_pkg vertex = pkgTable ! vertex
627 628 629 630 631

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

632 633 634 635 636 637 638 639 640 641
-- | 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.
--
642
dependencyInconsistencies :: PackageInstalled a => PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
643
                          -> [(PackageName, [(PackageId, Version)])]
644
dependencyInconsistencies index =
645 646 647 648 649 650 651 652 653 654 655 656
  [ (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
657
          | pkg <- allPackages index
658
          , ipid <- installedDepends pkg
659
          , Just dep <- [lookupUnitId index ipid]
660 661
          ]

662 663 664 665 666
        -- 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.
667
        reallyIsInconsistent :: PackageInstalled a => [a] -> Bool
668 669 670
        reallyIsInconsistent []       = False
        reallyIsInconsistent [_p]     = False
        reallyIsInconsistent [p1, p2] =
671 672
          let pid1 = installedUnitId p1
              pid2 = installedUnitId p2
673 674
          in pid1 `notElem` installedDepends p2
          && pid2 `notElem` installedDepends p1
675
        reallyIsInconsistent _ = True
Duncan Coutts's avatar
Duncan Coutts committed
676

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
677 678 679 680
-- | 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@.
681
moduleNameIndex :: InstalledPackageIndex -> Map ModuleName [IPI.InstalledPackageInfo]
Duncan Coutts's avatar
Duncan Coutts committed
682
moduleNameIndex index =
683 684
  Map.fromListWith (++) $ do
    pkg <- allPackages index
685
    IPI.ExposedModule m reexport <- IPI.exposedModules pkg
686 687
    case reexport of
        Nothing -> return (m, [pkg])
688 689
        Just (OpenModuleVar _) -> []
        Just (OpenModule _ m') | m == m'   -> []
690
                                | otherwise -> return (m', [pkg])
691 692 693 694 695
        -- 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!