2 % (c) The University of Glasgow, 2006
10 -- * The PackageConfigMap
11 PackageConfigMap, emptyPackageConfigMap, lookupPackage,
12 extendPackageConfigMap, dumpPackages,
14 -- * Reading the package config, and processing cmdline args
18 lookupModuleInAllPackages,
20 -- * Inspecting the set of packages in scope
21 getPackageIncludePath,
22 getPackageLibraryPath,
24 getPackageExtraCcOpts,
25 getPackageFrameworkPath,
27 getPreloadPackagesAnd,
34 #include "HsVersions.h"
37 import ParsePkgConf ( loadPackageConfig )
38 import DynFlags ( dopt, DynFlag(..), DynFlags(..), PackageFlag(..) )
39 import StaticFlags ( opt_Static )
40 import Config ( cProjectVersion )
41 import Name ( Name, nameModule_maybe )
45 import Maybes ( expectJust, MaybeErr(..) )
49 import System.Environment ( getEnv )
50 import Distribution.InstalledPackageInfo hiding (depends)
51 import Distribution.Package hiding (depends)
52 import Distribution.Text
53 import Distribution.Version
55 import ErrUtils ( debugTraceMsg, putMsg, Message )
57 import System.Directory
58 import System.FilePath
62 import Control.Exception ( throwDyn )
64 -- ---------------------------------------------------------------------------
67 -- Package state is all stored in DynFlags, including the details of
68 -- all packages, which packages are exposed, and which modules they
71 -- The package state is computed by initPackages, and kept in DynFlags.
73 -- * -package <pkg> causes <pkg> to become exposed, and all other packages
74 -- with the same name to become hidden.
76 -- * -hide-package <pkg> causes <pkg> to become hidden.
78 -- * Let exposedPackages be the set of packages thus exposed.
79 -- Let depExposedPackages be the transitive closure from exposedPackages of
80 -- their dependencies.
82 -- * When searching for a module from an preload import declaration,
83 -- only the exposed modules in exposedPackages are valid.
85 -- * When searching for a module from an implicit import, all modules
86 -- from depExposedPackages are valid.
88 -- * When linking in a comp manager mode, we link in packages the
89 -- program depends on (the compiler knows this list by the
90 -- time it gets to the link step). Also, we link in all packages
91 -- which were mentioned with preload -package flags on the command-line,
92 -- or are a transitive dependency of same, or are "base"/"rts".
93 -- The reason for (b) is that we might need packages which don't
94 -- contain any Haskell modules, and therefore won't be discovered
95 -- by the normal mechanism of dependency tracking.
99 -- When compiling module A, which imports module B, we need to
100 -- know whether B will be in the same DLL as A.
101 -- If it's in the same DLL, we refer to B_f_closure
102 -- If it isn't, we refer to _imp__B_f_closure
103 -- When compiling A, we record in B's Module value whether it's
104 -- in a different DLL, by setting the DLL flag.
106 data PackageState = PackageState {
107 pkgIdMap :: PackageConfigMap, -- PackageId -> PackageConfig
108 -- The exposed flags are adjusted according to -package and
109 -- -hide-package flags, and -ignore-package removes packages.
111 preloadPackages :: [PackageId],
112 -- The packages we're going to link in eagerly. This list
113 -- should be in reverse dependency order; that is, a package
114 -- is always mentioned before the packages it depends on.
116 moduleToPkgConfAll :: UniqFM [(PackageConfig,Bool)] -- ModuleEnv mapping
117 -- Derived from pkgIdMap.
118 -- Maps Module to (pkgconf,exposed), where pkgconf is the
119 -- PackageConfig for the package containing the module, and
120 -- exposed is True if the package exposes that module.
123 -- A PackageConfigMap maps a PackageId to a PackageConfig
124 type PackageConfigMap = UniqFM PackageConfig
126 emptyPackageConfigMap :: PackageConfigMap
127 emptyPackageConfigMap = emptyUFM
129 lookupPackage :: PackageConfigMap -> PackageId -> Maybe PackageConfig
130 lookupPackage = lookupUFM
132 extendPackageConfigMap
133 :: PackageConfigMap -> [PackageConfig] -> PackageConfigMap
134 extendPackageConfigMap pkg_map new_pkgs
135 = foldl add pkg_map new_pkgs
136 where add pkg_map p = addToUFM pkg_map (packageConfigId p) p
138 getPackageDetails :: PackageState -> PackageId -> PackageConfig
139 getPackageDetails dflags ps = expectJust "getPackageDetails" (lookupPackage (pkgIdMap dflags) ps)
141 -- ----------------------------------------------------------------------------
142 -- Loading the package config files and building up the package state
144 -- | Call this after 'DynFlags.parseDynFlags'. It reads the package
145 -- configuration files, and sets up various internal tables of package
146 -- information, according to the package-related flags on the
147 -- command-line (@-package@, @-hide-package@ etc.)
149 -- Returns a list of packages to link in if we're doing dynamic linking.
150 -- This list contains the packages that the user explicitly mentioned with
153 -- 'initPackages' can be called again subsequently after updating the
154 -- 'packageFlags' field of the 'DynFlags', and it will update the
155 -- 'packageState' in 'DynFlags' and return a list of packages to
157 initPackages :: DynFlags -> IO (DynFlags, [PackageId])
158 initPackages dflags = do
159 pkg_db <- case pkgDatabase dflags of
160 Nothing -> readPackageConfigs dflags
162 (pkg_state, preload, this_pkg)
163 <- mkPackageState dflags pkg_db [] (thisPackage dflags)
164 return (dflags{ pkgDatabase = Just pkg_db,
165 pkgState = pkg_state,
166 thisPackage = this_pkg },
169 -- -----------------------------------------------------------------------------
170 -- Reading the package database(s)
172 readPackageConfigs :: DynFlags -> IO PackageConfigMap
173 readPackageConfigs dflags = do
174 e_pkg_path <- try (getEnv "GHC_PACKAGE_PATH")
175 system_pkgconfs <- getSystemPackageConfigs dflags
177 let pkgconfs = case e_pkg_path of
178 Left _ -> system_pkgconfs
180 | last cs == "" -> init cs ++ system_pkgconfs
182 where cs = parseSearchPath path
183 -- if the path ends in a separator (eg. "/foo/bar:")
184 -- the we tack on the system paths.
186 -- Read all the ones mentioned in -package-conf flags
187 pkg_map <- foldM (readPackageConfig dflags) emptyPackageConfigMap
188 (reverse pkgconfs ++ extraPkgConfs dflags)
193 getSystemPackageConfigs :: DynFlags -> IO [FilePath]
194 getSystemPackageConfigs dflags = do
195 -- System one always comes first
196 let system_pkgconf = systemPackageConfig dflags
198 -- allow package.conf.d to contain a bunch of .conf files
199 -- containing package specifications. This is an easier way
200 -- to maintain the package database on systems with a package
201 -- management system, or systems that don't want to run ghc-pkg
202 -- to register or unregister packages. Undocumented feature for now.
203 let system_pkgconf_dir = system_pkgconf <.> "d"
204 system_pkgconf_dir_exists <- doesDirectoryExist system_pkgconf_dir
206 if system_pkgconf_dir_exists
207 then do files <- getDirectoryContents system_pkgconf_dir
208 return [ system_pkgconf_dir </> file
210 , takeExtension file == ".conf" ]
213 -- Read user's package conf (eg. ~/.ghc/i386-linux-6.3/package.conf)
214 -- unless the -no-user-package-conf flag was given.
215 -- We only do this when getAppUserDataDirectory is available
217 user_pkgconf <- handle (\_ -> return []) $ do
218 appdir <- getAppUserDataDirectory "ghc"
221 </> (TARGET_ARCH ++ '-':TARGET_OS ++ '-':cProjectVersion)
223 flg <- doesFileExist pkgconf
224 if (flg && dopt Opt_ReadUserPackageConf dflags)
225 then return [pkgconf]
228 return (user_pkgconf ++ system_pkgconfs ++ [system_pkgconf])
232 :: DynFlags -> PackageConfigMap -> FilePath -> IO PackageConfigMap
233 readPackageConfig dflags pkg_map conf_file = do
234 debugTraceMsg dflags 2 (text "Using package config file:" <+> text conf_file)
235 proto_pkg_configs <- loadPackageConfig dflags conf_file
236 let top_dir = topDir dflags
237 pkg_configs1 = mungePackagePaths top_dir proto_pkg_configs
238 pkg_configs2 = maybeHidePackages dflags pkg_configs1
239 return (extendPackageConfigMap pkg_map pkg_configs2)
241 maybeHidePackages :: DynFlags -> [PackageConfig] -> [PackageConfig]
242 maybeHidePackages dflags pkgs
243 | dopt Opt_HideAllPackages dflags = map hide pkgs
246 hide pkg = pkg{ exposed = False }
248 mungePackagePaths :: String -> [PackageConfig] -> [PackageConfig]
249 -- Replace the string "$topdir" at the beginning of a path
250 -- with the current topdir (obtained from the -B option).
251 mungePackagePaths top_dir ps = map munge_pkg ps
253 munge_pkg p = p{ importDirs = munge_paths (importDirs p),
254 includeDirs = munge_paths (includeDirs p),
255 libraryDirs = munge_paths (libraryDirs p),
256 frameworkDirs = munge_paths (frameworkDirs p),
257 haddockInterfaces = munge_paths (haddockInterfaces p),
258 haddockHTMLs = munge_paths (haddockHTMLs p)
261 munge_paths = map munge_path
264 | Just p' <- maybePrefixMatch "$topdir" p = top_dir ++ p'
265 | Just p' <- maybePrefixMatch "$httptopdir" p = toHttpPath top_dir ++ p'
268 toHttpPath p = "file:///" ++ p
271 -- -----------------------------------------------------------------------------
272 -- Modify our copy of the package database based on a package flag
273 -- (-package, -hide-package, -ignore-package).
276 :: [PackageConfig] -- Initial database
277 -> PackageFlag -- flag to apply
278 -> IO [PackageConfig] -- new database
280 applyPackageFlag pkgs flag =
283 case matchingPackages str pkgs of
284 Nothing -> missingPackageErr str
285 Just ([], _) -> panic "applyPackageFlag"
286 Just (p:ps,qs) -> return (p':ps')
287 where p' = p {exposed=True}
288 ps' = hideAll (pkgName (package p)) (ps++qs)
291 case matchingPackages str pkgs of
292 Nothing -> missingPackageErr str
293 Just (ps,qs) -> return (map hide ps ++ qs)
294 where hide p = p {exposed=False}
297 case matchingPackages str pkgs of
298 Nothing -> return pkgs
299 Just (_, qs) -> return qs
300 -- missing package is not an error for -ignore-package,
301 -- because a common usage is to -ignore-package P as
302 -- a preventative measure just in case P exists.
304 -- When a package is requested to be exposed, we hide all other
305 -- packages with the same name.
306 hideAll name ps = map maybe_hide ps
307 where maybe_hide p | pkgName (package p) == name = p {exposed=False}
311 matchingPackages :: String -> [PackageConfig]
312 -> Maybe ([PackageConfig], [PackageConfig])
313 matchingPackages str pkgs
314 = case partition (matches str) pkgs of
316 (ps,rest) -> Just (sortByVersion ps, rest)
318 -- A package named on the command line can either include the
319 -- version, or just the name if it is unambiguous.
321 = str == display (package p)
322 || str == display (pkgName (package p))
324 pickPackages :: [PackageConfig] -> [String] -> [PackageConfig]
325 pickPackages pkgs strs =
326 [ p | p <- strs, Just (p:_, _) <- [matchingPackages p pkgs] ]
328 sortByVersion :: [InstalledPackageInfo_ m] -> [InstalledPackageInfo_ m]
329 sortByVersion = sortBy (flip (comparing (pkgVersion.package)))
331 comparing :: Ord a => (t -> a) -> t -> t -> Ordering
332 comparing f a b = f a `compare` f b
334 -- -----------------------------------------------------------------------------
335 -- Hide old versions of packages
338 -- hide all packages for which there is also a later version
339 -- that is already exposed. This just makes it non-fatal to have two
340 -- versions of a package exposed, which can happen if you install a
341 -- later version of a package in the user database, for example.
343 hideOldPackages :: DynFlags -> [PackageConfig] -> IO [PackageConfig]
344 hideOldPackages dflags pkgs = mapM maybe_hide pkgs
346 | not (exposed p) = return p
347 | (p' : _) <- later_versions = do
348 debugTraceMsg dflags 2 $
349 (ptext (sLit "hiding package") <+>
350 text (display (package p)) <+>
351 ptext (sLit "to avoid conflict with later version") <+>
352 text (display (package p')))
353 return (p {exposed=False})
354 | otherwise = return p
355 where myname = pkgName (package p)
356 myversion = pkgVersion (package p)
357 later_versions = [ p | p <- pkgs, exposed p,
359 pkgName pkg == myname,
360 pkgVersion pkg > myversion ]
362 -- -----------------------------------------------------------------------------
367 -> [PackageConfig] -- database
368 -> [PackageIdentifier] -- preload packages
369 -> PackageId -- this package
370 -> IO ([PackageConfig],
374 findWiredInPackages dflags pkgs preload this_package = do
376 -- Now we must find our wired-in packages, and rename them to
377 -- their canonical names (eg. base-1.0 ==> base).
380 wired_in_pkgids :: [(PackageId, [String])]
381 wired_in_pkgids = [ (primPackageId, [""]),
382 (integerPackageId, [""]),
383 (basePackageId, [""]),
384 (rtsPackageId, [""]),
385 (haskell98PackageId, [""]),
387 (ndpPackageId, ["-seq", "-par"]) ]
389 matches :: PackageConfig -> (PackageId, [String]) -> Bool
390 pc `matches` (pid, suffixes)
391 = display (pkgName (package pc)) `elem`
392 (map (packageIdString pid ++) suffixes)
394 -- find which package corresponds to each wired-in package
395 -- delete any other packages with the same name
396 -- update the package and any dependencies to point to the new
399 -- When choosing which package to map to a wired-in package
400 -- name, we prefer exposed packages, and pick the latest
401 -- version. To override the default choice, -hide-package
402 -- could be used to hide newer versions.
404 findWiredInPackage :: [PackageConfig] -> (PackageId, [String])
405 -> IO (Maybe (PackageIdentifier, PackageId))
406 findWiredInPackage pkgs wired_pkg =
407 let all_ps = [ p | p <- pkgs, p `matches` wired_pkg ] in
408 case filter exposed all_ps of
411 many -> pick (head (sortByVersion many))
412 many -> pick (head (sortByVersion many))
414 suffixes = snd wired_pkg
416 debugTraceMsg dflags 2 $
417 ptext (sLit "wired-in package ")
418 <> ppr (fst wired_pkg)
421 else text (show suffixes))
422 <> ptext (sLit " not found.")
424 pick :: InstalledPackageInfo_ ModuleName
425 -> IO (Maybe (PackageIdentifier, PackageId))
427 debugTraceMsg dflags 2 $
428 ptext (sLit "wired-in package ")
429 <> ppr (fst wired_pkg)
430 <> ptext (sLit " mapped to ")
431 <> text (display (package pkg))
432 return (Just (package pkg, fst wired_pkg))
435 mb_wired_in_ids <- mapM (findWiredInPackage pkgs) wired_in_pkgids
437 wired_in_ids = catMaybes mb_wired_in_ids
439 deleteOtherWiredInPackages pkgs = filterOut bad pkgs
440 where bad p = any (p `matches`) wired_in_pkgids
441 && package p `notElem` map fst wired_in_ids
443 updateWiredInDependencies pkgs = map upd_pkg pkgs
444 where upd_pkg p = p{ package = upd_pid (package p),
445 depends = map upd_pid (depends p) }
447 upd_pid pid = case filter ((== pid) . fst) wired_in_ids of
449 ((x, y):_) -> x{ pkgName = PackageName (packageIdString y),
450 pkgVersion = Version [] [] }
452 pkgs1 = deleteOtherWiredInPackages pkgs
454 pkgs2 = updateWiredInDependencies pkgs1
456 preload1 = map upd_pid preload
458 -- we must return an updated thisPackage, just in case we
459 -- are actually compiling one of the wired-in packages
460 Just old_this_pkg = unpackPackageId this_package
461 new_this_pkg = mkPackageId (upd_pid old_this_pkg)
463 return (pkgs2, preload1, new_this_pkg)
465 -- ----------------------------------------------------------------------------
467 -- Detect any packages that have missing dependencies, and also any
468 -- mutually-recursive groups of packages (loops in the package graph
469 -- are not allowed). We do this by taking the least fixpoint of the
470 -- dependency graph, repeatedly adding packages whose dependencies are
471 -- satisfied until no more can be added.
476 -> [PackageId] -- ignored packages
477 -> IO [PackageConfig]
479 elimDanglingDeps dflags pkgs ignored = go [] pkgs'
481 pkgs' = filter (\p -> packageConfigId p `notElem` ignored) pkgs
484 case partitionWith (depsAvailable avail) not_avail of
485 ([], not_avail) -> do mapM_ reportElim not_avail; return avail
486 (new_avail, not_avail) -> go (new_avail ++ avail) (map fst not_avail)
488 depsAvailable :: [PackageConfig] -> PackageConfig
489 -> Either PackageConfig (PackageConfig, [PackageIdentifier])
490 depsAvailable pkgs_ok pkg
491 | null dangling = Left pkg
492 | otherwise = Right (pkg, dangling)
493 where dangling = filter (`notElem` pids) (depends pkg)
494 pids = map package pkgs_ok
496 reportElim (p, deps) =
497 debugTraceMsg dflags 2 $
498 (ptext (sLit "package") <+> pprPkg p <+>
499 ptext (sLit "will be ignored due to missing or recursive dependencies:") $$
500 nest 2 (hsep (map (text.display) deps)))
502 -- -----------------------------------------------------------------------------
503 -- When all the command-line options are in, we can process our package
504 -- settings and populate the package state.
508 -> PackageConfigMap -- initial database
509 -> [PackageId] -- preloaded packages
510 -> PackageId -- this package
512 [PackageId], -- new packages to preload
513 PackageId) -- this package, might be modified if the current
515 -- package is a wired-in package.
517 mkPackageState dflags orig_pkg_db preload0 this_package = do
519 -- Modify the package database according to the command-line flags
520 -- (-package, -hide-package, -ignore-package, -hide-all-packages).
522 let flags = reverse (packageFlags dflags)
523 let pkgs0 = eltsUFM orig_pkg_db
524 pkgs1 <- foldM applyPackageFlag pkgs0 flags
526 -- Here we build up a set of the packages mentioned in -package
527 -- flags on the command line; these are called the "preload"
528 -- packages. we link these packages in eagerly. The preload set
529 -- should contain at least rts & base, which is why we pretend that
530 -- the command line contains -package rts & -package base.
532 let new_preload_packages =
533 map package (pickPackages pkgs0 [ p | ExposePackage p <- flags ])
535 -- hide packages that are subsumed by later versions
536 pkgs2 <- hideOldPackages dflags pkgs1
538 -- sort out which packages are wired in
539 (pkgs3, preload1, new_this_pkg)
540 <- findWiredInPackages dflags pkgs2 new_preload_packages this_package
542 let ignored = map packageConfigId $
543 pickPackages pkgs0 [ p | IgnorePackage p <- flags ]
544 pkgs <- elimDanglingDeps dflags pkgs3 ignored
546 let pkg_db = extendPackageConfigMap emptyPackageConfigMap pkgs
548 -- add base & rts to the preload packages
549 basicLinkedPackages = filter (flip elemUFM pkg_db)
550 [basePackageId,rtsPackageId]
551 -- but in any case remove the current package from the set of
552 -- preloaded packages so that base/rts does not end up in the
553 -- set up preloaded package when we are just building it
554 preload2 = nub (filter (/= new_this_pkg)
555 (basicLinkedPackages ++ map mkPackageId preload1))
557 -- Close the preload packages with their dependencies
558 dep_preload <- closeDeps pkg_db (zip preload2 (repeat Nothing))
559 let new_dep_preload = filter (`notElem` preload0) dep_preload
561 let pstate = PackageState{ preloadPackages = dep_preload,
563 moduleToPkgConfAll = mkModuleMap pkg_db
566 return (pstate, new_dep_preload, new_this_pkg)
569 -- -----------------------------------------------------------------------------
570 -- Make the mapping from module to package info
574 -> UniqFM [(PackageConfig, Bool)]
575 mkModuleMap pkg_db = foldr extend_modmap emptyUFM pkgids
577 pkgids = map packageConfigId (eltsUFM pkg_db)
579 extend_modmap pkgid modmap =
580 addListToUFM_C (++) modmap
581 ([(m, [(pkg, True)]) | m <- exposed_mods] ++
582 [(m, [(pkg, False)]) | m <- hidden_mods])
584 pkg = expectJust "mkModuleMap" (lookupPackage pkg_db pkgid)
585 exposed_mods = exposedModules pkg
586 hidden_mods = hiddenModules pkg
588 pprPkg :: PackageConfig -> SDoc
589 pprPkg p = text (display (package p))
591 -- -----------------------------------------------------------------------------
592 -- Extracting information from the packages in scope
594 -- Many of these functions take a list of packages: in those cases,
595 -- the list is expected to contain the "dependent packages",
596 -- i.e. those packages that were found to be depended on by the
597 -- current module/program. These can be auto or non-auto packages, it
598 -- doesn't really matter. The list is always combined with the list
599 -- of preload (command-line) packages to determine which packages to
602 getPackageIncludePath :: DynFlags -> [PackageId] -> IO [String]
603 getPackageIncludePath dflags pkgs = do
604 ps <- getPreloadPackagesAnd dflags pkgs
605 return (nub (filter notNull (concatMap includeDirs ps)))
607 getPackageLibraryPath :: DynFlags -> [PackageId] -> IO [String]
608 getPackageLibraryPath dflags pkgs = do
609 ps <- getPreloadPackagesAnd dflags pkgs
610 return (nub (filter notNull (concatMap libraryDirs ps)))
612 getPackageLinkOpts :: DynFlags -> [PackageId] -> IO [String]
613 getPackageLinkOpts dflags pkgs = do
614 ps <- getPreloadPackagesAnd dflags pkgs
615 let tag = buildTag dflags
616 rts_tag = rtsBuildTag dflags
618 mkDynName | opt_Static = id
619 | otherwise = (++ ("-ghc" ++ cProjectVersion))
620 libs p = map (mkDynName . addSuffix) (hsLibraries p)
622 all_opts p = map ("-l" ++) (libs p) ++ ldOptions p
624 addSuffix rts@"HSrts" = rts ++ (expandTag rts_tag)
625 addSuffix other_lib = other_lib ++ (expandTag tag)
627 expandTag t | null t = ""
630 return (concat (map all_opts ps))
632 getPackageExtraCcOpts :: DynFlags -> [PackageId] -> IO [String]
633 getPackageExtraCcOpts dflags pkgs = do
634 ps <- getPreloadPackagesAnd dflags pkgs
635 return (concatMap ccOptions ps)
637 getPackageFrameworkPath :: DynFlags -> [PackageId] -> IO [String]
638 getPackageFrameworkPath dflags pkgs = do
639 ps <- getPreloadPackagesAnd dflags pkgs
640 return (nub (filter notNull (concatMap frameworkDirs ps)))
642 getPackageFrameworks :: DynFlags -> [PackageId] -> IO [String]
643 getPackageFrameworks dflags pkgs = do
644 ps <- getPreloadPackagesAnd dflags pkgs
645 return (concatMap frameworks ps)
647 -- -----------------------------------------------------------------------------
650 -- | Takes a Module, and if the module is in a package returns
651 -- @(pkgconf,exposed)@ where pkgconf is the PackageConfig for that package,
652 -- and exposed is True if the package exposes the module.
653 lookupModuleInAllPackages :: DynFlags -> ModuleName -> [(PackageConfig,Bool)]
654 lookupModuleInAllPackages dflags m =
655 case lookupUFM (moduleToPkgConfAll (pkgState dflags)) m of
659 getPreloadPackagesAnd :: DynFlags -> [PackageId] -> IO [PackageConfig]
660 getPreloadPackagesAnd dflags pkgids =
662 state = pkgState dflags
663 pkg_map = pkgIdMap state
664 preload = preloadPackages state
665 pairs = zip pkgids (repeat Nothing)
667 all_pkgs <- throwErr (foldM (add_package pkg_map) preload pairs)
668 return (map (getPackageDetails state) all_pkgs)
670 -- Takes a list of packages, and returns the list with dependencies included,
671 -- in reverse dependency order (a package appears before those it depends on).
672 closeDeps :: PackageConfigMap -> [(PackageId, Maybe PackageId)]
674 closeDeps pkg_map ps = throwErr (closeDepsErr pkg_map ps)
676 throwErr :: MaybeErr Message a -> IO a
677 throwErr m = case m of
678 Failed e -> throwDyn (CmdLineError (showSDoc e))
679 Succeeded r -> return r
681 closeDepsErr :: PackageConfigMap -> [(PackageId,Maybe PackageId)]
682 -> MaybeErr Message [PackageId]
683 closeDepsErr pkg_map ps = foldM (add_package pkg_map) [] ps
686 add_package :: PackageConfigMap -> [PackageId] -> (PackageId,Maybe PackageId)
687 -> MaybeErr Message [PackageId]
688 add_package pkg_db ps (p, mb_parent)
689 | p `elem` ps = return ps -- Check if we've already added this package
691 case lookupPackage pkg_db p of
692 Nothing -> Failed (missingPackageMsg (packageIdString p) <>
693 missingDependencyMsg mb_parent)
695 -- Add the package's dependents also
696 let deps = map mkPackageId (depends pkg)
697 ps' <- foldM (add_package pkg_db) ps (zip deps (repeat (Just p)))
700 missingPackageErr :: String -> IO [PackageConfig]
701 missingPackageErr p = throwDyn (CmdLineError (showSDoc (missingPackageMsg p)))
703 missingPackageMsg :: String -> SDoc
704 missingPackageMsg p = ptext (sLit "unknown package:") <+> text p
706 missingDependencyMsg :: Maybe PackageId -> SDoc
707 missingDependencyMsg Nothing = empty
708 missingDependencyMsg (Just parent)
709 = space <> parens (ptext (sLit "dependency of") <+> ftext (packageIdFS parent))
711 -- -----------------------------------------------------------------------------
713 isDllName :: PackageId -> Name -> Bool
714 isDllName this_pkg name
716 | Just mod <- nameModule_maybe name = modulePackageId mod /= this_pkg
717 | otherwise = False -- no, it is not even an external name
719 -- -----------------------------------------------------------------------------
720 -- Displaying packages
722 dumpPackages :: DynFlags -> IO ()
723 -- Show package info on console, if verbosity is >= 3
725 = do let pkg_map = pkgIdMap (pkgState dflags)
727 vcat (map (text . showInstalledPackageInfo
728 . packageConfigToInstalledPackageInfo)