PackageIndex.hs 26.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
-- An index of packages.
Duncan Coutts's avatar
Duncan Coutts committed
14
--
15 16
module Distribution.Simple.PackageIndex (
  -- * Package index data type
17
  InstalledPackageIndex,
18
  PackageIndex,
19
  FakeMap,
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 66 67 68 69 70 71

  -- ** Variants of special queries supporting fake map
  fakeLookupInstalledPackageId,
  brokenPackages',
  dependencyClosure',
  reverseDependencyClosure',
  dependencyInconsistencies',
  dependencyCycles',
  dependencyGraph',
72 73 74
  ) where

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

90
import Distribution.Package
Duncan Coutts's avatar
Duncan Coutts committed
91
         ( PackageName(..), PackageId
92
         , Package(..), packageName, packageVersion
Duncan Coutts's avatar
Duncan Coutts committed
93
         , Dependency(Dependency)--, --PackageFixedDeps(..)
94
         , InstalledPackageId(..), PackageInstalled(..) )
Duncan Coutts's avatar
Duncan Coutts committed
95 96
import Distribution.ModuleName
         ( ModuleName )
97
import Distribution.InstalledPackageInfo
98
         ( InstalledPackageInfo )
99
import qualified Distribution.InstalledPackageInfo as IPI
100
import Distribution.Version
101
         ( Version, withinRange )
Duncan Coutts's avatar
Duncan Coutts committed
102
import Distribution.Simple.Utils (lowercase, comparing, equating)
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
-- Note [FakeMap]
-----------------
-- We'd like to use the PackageIndex defined in this module for
-- cabal-install's InstallPlan.  However, at the moment, this
-- data structure is indexed by InstalledPackageId, which we don't
-- know until after we've compiled a package (whereas InstallPlan
-- needs to store not-compiled packages in the index.) Eventually,
-- an InstalledPackageId will be calculatable prior to actually
-- building the package (making it something of a misnomer), but
-- at the moment, the "fake installed package ID map" is a workaround
-- to solve this problem while reusing PackageIndex.  The basic idea
-- is that, since we don't know what an InstalledPackageId is
-- beforehand, we just fake up one based on the package ID (it only
-- needs to be unique for the particular install plan), and fill
-- it out with the actual generated InstalledPackageId after the
-- package is successfully compiled.
--
-- However, there is a problem: in the index there may be
-- references using the old package ID, which are now dangling if
-- we update the InstalledPackageId.  We could map over the entire
-- index to update these pointers as well (a costly operation), but
-- instead, we've chosen to parametrize a variety of important functions
-- by a FakeMap, which records what a fake installed package ID was
-- actually resolved to post-compilation.  If we do a lookup, we first
-- check and see if it's a fake ID in the FakeMap.
--
-- It's a bit grungy, but we expect this to only be temporary anyway.
-- (Another possible workaround would have been to *not* update
-- the installed package ID, but I decided this would be hard to
-- understand.)

-- | Map from fake installed package IDs to real ones.  See Note [FakeMap]
type FakeMap = Map InstalledPackageId InstalledPackageId

138
-- | The collection of information about packages from one or more 'PackageDB's.
139
-- These packages generally should have an instance of 'PackageInstalled'
140
--
Duncan Coutts's avatar
Duncan Coutts committed
141
-- Packages are uniquely identified in by their 'InstalledPackageId', they can
Ian D. Bollinger's avatar
Ian D. Bollinger committed
142
-- also be efficiently looked up by package name or by name and version.
143
--
144
data PackageIndex a = PackageIndex
Duncan Coutts's avatar
Duncan Coutts committed
145 146 147
  -- The primary index. Each InstalledPackageInfo record is uniquely identified
  -- by its InstalledPackageId.
  --
148
  !(Map InstalledPackageId a)
Duncan Coutts's avatar
Duncan Coutts committed
149

Ian D. Bollinger's avatar
Ian D. Bollinger committed
150
  -- This auxiliary index maps package names (case-sensitively) to all the
Duncan Coutts's avatar
Duncan Coutts committed
151 152
  -- versions and instances of that package. This allows us to find all
  -- versions satisfying a dependency.
153
  --
Duncan Coutts's avatar
Duncan Coutts committed
154 155 156 157
  -- 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.
158
  --
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
159 160
  -- FIXME: Clarify what "preference order" means. Check that this invariant is
  -- preserved. See #1463 for discussion.
161
  !(Map PackageName (Map Version [a]))
162

ttuegel's avatar
ttuegel committed
163 164 165
  deriving (Generic, Show, Read)

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

167 168 169 170 171
-- | The default package index which contains 'InstalledPackageInfo'.  Normally
-- use this.
type InstalledPackageIndex = PackageIndex InstalledPackageInfo

instance PackageInstalled a => Monoid (PackageIndex a) where
Duncan Coutts's avatar
Duncan Coutts committed
172
  mempty  = PackageIndex Map.empty Map.empty
173 174 175 176 177
  mappend = merge
  --save one mappend with empty in the common case:
  mconcat [] = mempty
  mconcat xs = foldr1 mappend xs

178
invariant :: PackageInstalled a => PackageIndex a -> Bool
Duncan Coutts's avatar
Duncan Coutts committed
179 180
invariant (PackageIndex pids pnames) =
     map installedPackageId (Map.elems pids)
181 182
  == sort
     [ assert pinstOk (installedPackageId pinst)
Duncan Coutts's avatar
Duncan Coutts committed
183 184 185 186 187 188 189 190 191 192 193
     | (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
     ]

194

195 196 197 198
--
-- * Internal helpers
--

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

206 207 208

--
-- * Construction
209 210
--

211
-- | Build an index out of a bunch of packages.
212
--
Duncan Coutts's avatar
Duncan Coutts committed
213 214
-- If there are duplicates by 'InstalledPackageId' then later ones mask earlier
-- ones.
215
--
216
fromList :: PackageInstalled a => [a] -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
217
fromList pkgs = mkPackageIndex pids pnames
218
  where
Duncan Coutts's avatar
Duncan Coutts committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232
    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
                ]
        ]
233

234 235 236 237
--
-- * Updates
--

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

258

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

268
  where
Duncan Coutts's avatar
Duncan Coutts committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    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.
--
286
deleteInstalledPackageId :: PackageInstalled a => InstalledPackageId -> PackageIndex a -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
287 288 289 290 291 292
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
293
  where
Duncan Coutts's avatar
Duncan Coutts committed
294 295 296 297 298 299
    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
300

Duncan Coutts's avatar
Duncan Coutts committed
301 302 303 304 305 306
    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.
307
--
308
deleteSourcePackageId :: PackageInstalled a => PackageId -> PackageIndex a -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
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)

325 326 327

-- | Removes all packages with this (case-sensitive) name from the index.
--
328
deletePackageName :: PackageInstalled a => PackageName -> PackageIndex a -> PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
329 330 331 332 333 334 335
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)
336

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

345 346 347 348
--
-- * Bulk queries
--

349 350
-- | Get all the packages from the index.
--
351
allPackages :: PackageIndex a -> [a]
Duncan Coutts's avatar
Duncan Coutts committed
352
allPackages (PackageIndex pids _) = Map.elems pids
353 354 355

-- | Get all the packages from the index.
--
356
-- They are grouped by package name (case-sensitively).
357
--
358
allPackagesByName :: PackageIndex a -> [(PackageName, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
359
allPackagesByName (PackageIndex _ pnames) =
360 361 362 363 364 365 366
  [ (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).
--
367
allPackagesBySourcePackageId :: PackageInstalled a => PackageIndex a -> [(PackageId, [a])]
368 369 370 371
allPackagesBySourcePackageId (PackageIndex _ pnames) =
  [ (packageId ipkg, ipkgs)
  | pvers <- Map.elems pnames
  , ipkgs@(ipkg:_) <- Map.elems pvers ]
372 373 374 375 376

--
-- * Lookups
--

Duncan Coutts's avatar
Duncan Coutts committed
377
-- | Does a lookup by source package id (name & version).
378
--
Duncan Coutts's avatar
Duncan Coutts committed
379
-- Since multiple package DBs mask each other by 'InstalledPackageId',
380 381
-- then we get back at most one package.
--
382 383
lookupInstalledPackageId :: PackageInstalled a => PackageIndex a -> InstalledPackageId
                         -> Maybe a
Duncan Coutts's avatar
Duncan Coutts committed
384 385
lookupInstalledPackageId (PackageIndex pids _) pid = Map.lookup pid pids

386

Duncan Coutts's avatar
Duncan Coutts committed
387 388 389 390 391
-- | 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.
392
--
393
lookupSourcePackageId :: PackageInstalled a => PackageIndex a -> PackageId -> [a]
Duncan Coutts's avatar
Duncan Coutts committed
394 395 396 397 398 399
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
400

401 402 403 404 405 406 407
-- | Convenient alias of 'lookupSourcePackageId', but assuming only
-- one package per package ID.
lookupPackageId :: PackageInstalled a => PackageIndex a -> PackageId -> Maybe a
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
408 409 410

-- | Does a lookup by source package name.
--
411 412
lookupPackageName :: PackageInstalled a => PackageIndex a -> PackageName
                  -> [(Version, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
413 414 415 416 417 418 419
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.
420 421 422 423
--
-- We get back any number of versions of the specified package name, all
-- satisfying the version range constraint.
--
424 425
lookupDependency :: PackageInstalled a => PackageIndex a -> Dependency
                 -> [(Version, [a])]
Duncan Coutts's avatar
Duncan Coutts committed
426 427 428 429 430 431
lookupDependency (PackageIndex _ pnames) (Dependency name versionRange) =
  case Map.lookup name pnames of
    Nothing    -> []
    Just pvers -> [ entry
                  | entry@(ver, _) <- Map.toList pvers
                  , ver `withinRange` versionRange ]
432 433 434 435

--
-- * Case insensitive name lookups
--
436 437 438

-- | Does a case-insensitive search by package name.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
439
-- If there is only one package that compares case-insensitively to this name
440
-- then the search is unambiguous and we get back all versions of that package.
Ian D. Bollinger's avatar
Ian D. Bollinger committed
441
-- If several match case-insensitively but one matches exactly then it is also
442 443
-- unambiguous.
--
Ian D. Bollinger's avatar
Ian D. Bollinger committed
444
-- If however several match case-insensitively and none match exactly then we
445 446 447 448
-- 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.
--
449
searchByName :: PackageInstalled a => PackageIndex a -> String -> SearchResult [a]
Duncan Coutts's avatar
Duncan Coutts committed
450 451
searchByName (PackageIndex _ pnames) name =
  case [ pkgs | pkgs@(PackageName name',_) <- Map.toList pnames
452
              , lowercase name' == lname ] of
Duncan Coutts's avatar
Duncan Coutts committed
453 454 455 456 457
    []               -> 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)
458
  where lname = lowercase name
459 460 461 462 463 464 465

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.
--
466
searchByNameSubstring :: PackageInstalled a => PackageIndex a -> String -> [a]
Duncan Coutts's avatar
Duncan Coutts committed
467
searchByNameSubstring (PackageIndex _ pnames) searchterm =
468
  [ pkg
Duncan Coutts's avatar
Duncan Coutts committed
469
  | (PackageName name, pvers) <- Map.toList pnames
470
  , lsearchterm `isInfixOf` lowercase name
Duncan Coutts's avatar
Duncan Coutts committed
471
  , pkgs <- Map.elems pvers
472
  , pkg <- pkgs ]
473
  where lsearchterm = lowercase searchterm
474

Duncan Coutts's avatar
Duncan Coutts committed
475 476 477 478 479 480 481 482

--
-- * Special queries
--

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

483 484 485 486 487 488 489
-- | 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.
--
490
dependencyCycles :: PackageInstalled a => PackageIndex a -> [[a]]
491 492 493 494 495
dependencyCycles = dependencyCycles' Map.empty

-- | Variant of 'dependencyCycles' which accepts a 'FakeMap'.  See Note [FakeMap].
dependencyCycles' :: PackageInstalled a => FakeMap -> PackageIndex a -> [[a]]
dependencyCycles' fakeMap index =
496 497
  [ vs | Graph.CyclicSCC vs <- Graph.stronglyConnComp adjacencyList ]
  where
498
    adjacencyList = [ (pkg, installedPackageId pkg, fakeInstalledDepends fakeMap pkg)
499 500 501
                    | pkg <- allPackages index ]


Duncan Coutts's avatar
Duncan Coutts committed
502
-- | All packages that have immediate dependencies that are not in the index.
503
--
504 505
-- Returns such packages along with the dependencies that they're missing.
--
506
brokenPackages :: PackageInstalled a => PackageIndex a -> [(a, [InstalledPackageId])]
507 508 509 510 511
brokenPackages = brokenPackages' Map.empty

-- | Variant of 'brokenPackages' which accepts a 'FakeMap'.  See Note [FakeMap].
brokenPackages' :: PackageInstalled a => FakeMap -> PackageIndex a -> [(a, [InstalledPackageId])]
brokenPackages' fakeMap index =
512
  [ (pkg, missing)
Duncan Coutts's avatar
Duncan Coutts committed
513
  | pkg  <- allPackages index
514
  , let missing = [ pkg' | pkg' <- installedDepends pkg
515
                         , isNothing (fakeLookupInstalledPackageId fakeMap index pkg') ]
516 517
  , not (null missing) ]

518 519 520
-- | Variant of 'lookupInstalledPackageId' which accepts a 'FakeMap'.  See Note [FakeMap].
fakeLookupInstalledPackageId :: PackageInstalled a => FakeMap -> PackageIndex a -> InstalledPackageId -> Maybe a
fakeLookupInstalledPackageId fakeMap index pkg = lookupInstalledPackageId index (Map.findWithDefault pkg pkg fakeMap)
521

522
-- | Tries to take the transitive closure of the package dependencies.
523
--
524
-- If the transitive closure is complete then it returns that subset of the
525 526 527
-- 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
528
-- the original given 'PackageId's do not occur in the index.
529
--
530
dependencyClosure :: PackageInstalled a => PackageIndex a
531
                  -> [InstalledPackageId]
532 533
                  -> Either (PackageIndex a)
                            [(a, [InstalledPackageId])]
534 535 536 537 538 539 540 541 542
dependencyClosure = dependencyClosure' Map.empty

-- | Variant of 'dependencyClosure' which accepts a 'FakeMap'.  See Note [FakeMap].
dependencyClosure' :: PackageInstalled a => FakeMap
                  -> PackageIndex a
                  -> [InstalledPackageId]
                  -> Either (PackageIndex a)
                            [(a, [InstalledPackageId])]
dependencyClosure' fakeMap index pkgids0 = case closure mempty [] pkgids0 of
543 544
  (completed, []) -> Left completed
  (completed, _)  -> Right (brokenPackages completed)
545
 where
546
    closure completed failed []             = (completed, failed)
547
    closure completed failed (pkgid:pkgids) = case fakeLookupInstalledPackageId fakeMap index pkgid of
548
      Nothing   -> closure completed (pkgid:failed) pkgids
549
      Just pkg  -> case fakeLookupInstalledPackageId fakeMap completed (installedPackageId pkg) of
550 551
        Just _  -> closure completed  failed pkgids
        Nothing -> closure completed' failed pkgids'
Duncan Coutts's avatar
Duncan Coutts committed
552
          where completed' = insert pkg completed
553
                pkgids'    = installedDepends pkg ++ pkgids
554

555
-- | Takes the transitive closure of the packages reverse dependencies.
556
--
Duncan Coutts's avatar
Duncan Coutts committed
557
-- * The given 'PackageId's must be in the index.
558
--
559
reverseDependencyClosure :: PackageInstalled a => PackageIndex a
560
                         -> [InstalledPackageId]
561
                         -> [a]
562 563 564 565 566 567 568 569
reverseDependencyClosure = reverseDependencyClosure' Map.empty

-- | Variant of 'reverseDependencyClosure' which accepts a 'FakeMap'.  See Note [FakeMap].
reverseDependencyClosure' :: PackageInstalled a => FakeMap
                         -> PackageIndex a
                         -> [InstalledPackageId]
                         -> [a]
reverseDependencyClosure' fakeMap 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' fakeMap 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
                    InstalledPackageId -> Maybe Graph.Vertex)
602 603 604 605 606 607 608 609 610
dependencyGraph = dependencyGraph' Map.empty

-- | Variant of 'dependencyGraph' which accepts a 'FakeMap'.  See Note [FakeMap].
dependencyGraph' :: PackageInstalled a => FakeMap
                -> PackageIndex a
                -> (Graph.Graph,
                    Graph.Vertex -> a,
                    InstalledPackageId -> Maybe Graph.Vertex)
dependencyGraph' fakeMap index = (graph, vertex_to_pkg, id_to_vertex)
611 612
  where
    graph = Array.listArray bounds
613
              [ [ v | Just v <- map id_to_vertex (installedDepends pkg) ]
614
              | pkg <- pkgs ]
615

Duncan Coutts's avatar
Duncan Coutts committed
616
    pkgs             = sortBy (comparing packageId) (allPackages index)
617 618
    vertices         = zip (map installedPackageId pkgs) [0..]
    vertex_map       = Map.fromList vertices
619
    id_to_vertex pid = Map.lookup (Map.findWithDefault pid pid fakeMap) vertex_map
620 621

    vertex_to_pkg vertex = pkgTable ! vertex
622 623 624 625 626

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

627 628 629 630 631 632 633 634 635 636
-- | 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.
--
637
dependencyInconsistencies :: PackageInstalled a => PackageIndex a
Duncan Coutts's avatar
Duncan Coutts committed
638
                          -> [(PackageName, [(PackageId, Version)])]
639 640 641 642 643 644
dependencyInconsistencies = dependencyInconsistencies' Map.empty

-- | Variant of 'dependencyInconsistencies' which accepts a 'FakeMap'.  See Note [FakeMap].
dependencyInconsistencies' :: PackageInstalled a => FakeMap -> PackageIndex a
                          -> [(PackageName, [(PackageId, Version)])]
dependencyInconsistencies' fakeMap 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 659
          , ipid <- fakeInstalledDepends fakeMap pkg
          , Just dep <- [fakeLookupInstalledPackageId fakeMap index ipid]
660 661
          ]

662
        reallyIsInconsistent :: PackageInstalled a => [a] -> Bool
663 664 665
        reallyIsInconsistent []       = False
        reallyIsInconsistent [_p]     = False
        reallyIsInconsistent [p1, p2] =
666 667 668 669
          let pid1 = installedPackageId p1
              pid2 = installedPackageId p2
          in Map.findWithDefault pid1 pid1 fakeMap `notElem` fakeInstalledDepends fakeMap p2
          && Map.findWithDefault pid2 pid2 fakeMap `notElem` fakeInstalledDepends fakeMap p1
670
        reallyIsInconsistent _ = True
Duncan Coutts's avatar
Duncan Coutts committed
671

672 673 674
-- | Variant of 'installedDepends' which accepts a 'FakeMap'.  See Note [FakeMap].
fakeInstalledDepends :: PackageInstalled a => FakeMap -> a -> [InstalledPackageId]
fakeInstalledDepends fakeMap = map (\pid -> Map.findWithDefault pid pid fakeMap) . installedDepends
Duncan Coutts's avatar
Duncan Coutts committed
675

676
-- | A rough approximation of GHC's module finder, takes a 'InstalledPackageIndex' and
677 678
-- turns it into a map from module names to their source packages.  It's used to
-- initialize the @build-deps@ field in @cabal init@.
679
moduleNameIndex :: InstalledPackageIndex -> Map ModuleName [InstalledPackageInfo]
Duncan Coutts's avatar
Duncan Coutts committed
680
moduleNameIndex index =
681 682
  Map.fromListWith (++) . concat $
    [ [(m,  [pkg]) | m <- IPI.exposedModules pkg ] ++
683 684 685 686
      [(m', [pkg]) | IPI.ModuleReexport {
                       IPI.moduleReexportDefiningName = m,
                       IPI.moduleReexportName         = m'
                     } <- IPI.reexportedModules pkg
687 688 689 690 691 692 693
                   , m /= m' ]
        -- 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!
    | pkg        <- allPackages index ]