2 % (c) The University of Glasgow, 2006
5 -- | Package manipulation
9 -- * The PackageConfigMap
10 PackageConfigMap, emptyPackageConfigMap, lookupPackage,
11 extendPackageConfigMap, dumpPackages,
13 -- * Reading the package config, and processing cmdline args
17 lookupModuleInAllPackages,
19 -- * Inspecting the set of packages in scope
20 getPackageIncludePath,
21 getPackageLibraryPath,
23 getPackageExtraCcOpts,
24 getPackageFrameworkPath,
26 getPreloadPackagesAnd,
28 collectIncludeDirs, collectLibraryPaths, collectLinkOpts,
36 #include "HsVersions.h"
39 import ParsePkgConf ( loadPackageConfig )
40 import DynFlags ( dopt, DynFlag(..), DynFlags(..), PackageFlag(..) )
42 import Config ( cProjectVersion )
43 import Name ( Name, nameModule_maybe )
52 import System.Environment ( getEnv )
53 import Distribution.InstalledPackageInfo
54 import Distribution.Package hiding (PackageId,depends)
56 import ErrUtils ( debugTraceMsg, putMsg, Message )
59 import System.Directory
60 import System.FilePath
63 import Data.List as List
65 -- ---------------------------------------------------------------------------
68 -- | Package state is all stored in 'DynFlag's, including the details of
69 -- all packages, which packages are exposed, and which modules they
72 -- The package state is computed by 'initPackages', and kept in DynFlags.
74 -- * @-package <pkg>@ causes @<pkg>@ to become exposed, and all other packages
75 -- with the same name to become hidden.
77 -- * @-hide-package <pkg>@ causes @<pkg>@ to become hidden.
79 -- * Let @exposedPackages@ be the set of packages thus exposed.
80 -- Let @depExposedPackages@ be the transitive closure from @exposedPackages@ of
81 -- their dependencies.
83 -- * When searching for a module from an preload import declaration,
84 -- only the exposed modules in @exposedPackages@ are valid.
86 -- * When searching for a module from an implicit import, all modules
87 -- from @depExposedPackages@ are valid.
89 -- * When linking in a compilation manager mode, we link in packages the
90 -- program depends on (the compiler knows this list by the
91 -- time it gets to the link step). Also, we link in all packages
92 -- which were mentioned with preload @-package@ flags on the command-line,
93 -- or are a transitive dependency of same, or are \"base\"\/\"rts\".
94 -- The reason for this is that we might need packages which don't
95 -- contain any Haskell modules, and therefore won't be discovered
96 -- by the normal mechanism of dependency tracking.
100 -- When compiling module A, which imports module B, we need to
101 -- know whether B will be in the same DLL as A.
102 -- If it's in the same DLL, we refer to B_f_closure
103 -- If it isn't, we refer to _imp__B_f_closure
104 -- When compiling A, we record in B's Module value whether it's
105 -- in a different DLL, by setting the DLL flag.
107 data PackageState = PackageState {
108 pkgIdMap :: PackageConfigMap, -- PackageId -> PackageConfig
109 -- The exposed flags are adjusted according to -package and
110 -- -hide-package flags, and -ignore-package removes packages.
112 preloadPackages :: [PackageId],
113 -- The packages we're going to link in eagerly. This list
114 -- should be in reverse dependency order; that is, a package
115 -- is always mentioned before the packages it depends on.
117 moduleToPkgConfAll :: UniqFM [(PackageConfig,Bool)], -- ModuleEnv mapping
118 -- Derived from pkgIdMap.
119 -- Maps Module to (pkgconf,exposed), where pkgconf is the
120 -- PackageConfig for the package containing the module, and
121 -- exposed is True if the package exposes that module.
123 installedPackageIdMap :: FiniteMap InstalledPackageId PackageId
126 -- | A PackageConfigMap maps a 'PackageId' to a 'PackageConfig'
127 type PackageConfigMap = UniqFM PackageConfig
129 emptyPackageConfigMap :: PackageConfigMap
130 emptyPackageConfigMap = emptyUFM
132 -- | Find the package we know about with the given id (e.g. \"foo-1.0\"), if any
133 lookupPackage :: PackageConfigMap -> PackageId -> Maybe PackageConfig
134 lookupPackage = lookupUFM
136 extendPackageConfigMap
137 :: PackageConfigMap -> [PackageConfig] -> PackageConfigMap
138 extendPackageConfigMap pkg_map new_pkgs
139 = foldl add pkg_map new_pkgs
140 where add pkg_map p = addToUFM pkg_map (packageConfigId p) p
142 -- | Looks up the package with the given id in the package state, panicing if it is
144 getPackageDetails :: PackageState -> PackageId -> PackageConfig
145 getPackageDetails ps pid = expectJust "getPackageDetails" (lookupPackage (pkgIdMap ps) pid)
147 -- ----------------------------------------------------------------------------
148 -- Loading the package config files and building up the package state
150 -- | Call this after 'DynFlags.parseDynFlags'. It reads the package
151 -- configuration files, and sets up various internal tables of package
152 -- information, according to the package-related flags on the
153 -- command-line (@-package@, @-hide-package@ etc.)
155 -- Returns a list of packages to link in if we're doing dynamic linking.
156 -- This list contains the packages that the user explicitly mentioned with
159 -- 'initPackages' can be called again subsequently after updating the
160 -- 'packageFlags' field of the 'DynFlags', and it will update the
161 -- 'pkgState' in 'DynFlags' and return a list of packages to
163 initPackages :: DynFlags -> IO (DynFlags, [PackageId])
164 initPackages dflags = do
165 pkg_db <- case pkgDatabase dflags of
166 Nothing -> readPackageConfigs dflags
168 (pkg_state, preload, this_pkg)
169 <- mkPackageState dflags pkg_db [] (thisPackage dflags)
170 return (dflags{ pkgDatabase = Just pkg_db,
171 pkgState = pkg_state,
172 thisPackage = this_pkg },
175 -- -----------------------------------------------------------------------------
176 -- Reading the package database(s)
178 readPackageConfigs :: DynFlags -> IO PackageConfigMap
179 readPackageConfigs dflags = do
180 e_pkg_path <- tryIO (getEnv "GHC_PACKAGE_PATH")
181 system_pkgconfs <- getSystemPackageConfigs dflags
183 let pkgconfs = case e_pkg_path of
184 Left _ -> system_pkgconfs
186 | last cs == "" -> init cs ++ system_pkgconfs
188 where cs = parseSearchPath path
189 -- if the path ends in a separator (eg. "/foo/bar:")
190 -- the we tack on the system paths.
192 -- Read all the ones mentioned in -package-conf flags
193 pkg_map <- foldM (readPackageConfig dflags) emptyPackageConfigMap
194 (reverse pkgconfs ++ extraPkgConfs dflags)
199 getSystemPackageConfigs :: DynFlags -> IO [FilePath]
200 getSystemPackageConfigs dflags = do
201 -- System one always comes first
202 let system_pkgconf = systemPackageConfig dflags
204 -- allow package.conf.d to contain a bunch of .conf files
205 -- containing package specifications. This is an easier way
206 -- to maintain the package database on systems with a package
207 -- management system, or systems that don't want to run ghc-pkg
208 -- to register or unregister packages. Undocumented feature for now.
209 let system_pkgconf_dir = system_pkgconf <.> "d"
210 system_pkgconf_dir_exists <- doesDirectoryExist system_pkgconf_dir
212 if system_pkgconf_dir_exists
213 then do files <- getDirectoryContents system_pkgconf_dir
214 return [ system_pkgconf_dir </> file
216 , takeExtension file == ".conf" ]
219 -- Read user's package conf (eg. ~/.ghc/i386-linux-6.3/package.conf)
220 -- unless the -no-user-package-conf flag was given.
221 -- We only do this when getAppUserDataDirectory is available
224 appdir <- getAppUserDataDirectory "ghc"
227 </> (TARGET_ARCH ++ '-':TARGET_OS ++ '-':cProjectVersion)
229 flg <- doesFileExist pkgconf
230 if (flg && dopt Opt_ReadUserPackageConf dflags)
231 then return [pkgconf]
233 `catchIO` (\_ -> return [])
235 return (user_pkgconf ++ system_pkgconfs ++ [system_pkgconf])
239 :: DynFlags -> PackageConfigMap -> FilePath -> IO PackageConfigMap
240 readPackageConfig dflags pkg_map conf_file = do
241 debugTraceMsg dflags 2 (text "Using package config file:" <+> text conf_file)
242 proto_pkg_configs <- loadPackageConfig dflags conf_file
243 let top_dir = topDir dflags
244 pkg_configs1 = mungePackagePaths top_dir proto_pkg_configs
245 pkg_configs2 = maybeHidePackages dflags pkg_configs1
246 return (extendPackageConfigMap pkg_map pkg_configs2)
248 maybeHidePackages :: DynFlags -> [PackageConfig] -> [PackageConfig]
249 maybeHidePackages dflags pkgs
250 | dopt Opt_HideAllPackages dflags = map hide pkgs
253 hide pkg = pkg{ exposed = False }
255 mungePackagePaths :: String -> [PackageConfig] -> [PackageConfig]
256 -- Replace the string "$topdir" at the beginning of a path
257 -- with the current topdir (obtained from the -B option).
258 mungePackagePaths top_dir ps = map munge_pkg ps
260 munge_pkg p = p{ importDirs = munge_paths (importDirs p),
261 includeDirs = munge_paths (includeDirs p),
262 libraryDirs = munge_paths (libraryDirs p),
263 frameworkDirs = munge_paths (frameworkDirs p),
264 haddockInterfaces = munge_paths (haddockInterfaces p),
265 haddockHTMLs = munge_paths (haddockHTMLs p)
268 munge_paths = map munge_path
271 | Just p' <- stripPrefix "$topdir" p = top_dir ++ p'
272 | Just p' <- stripPrefix "$httptopdir" p = toHttpPath top_dir ++ p'
275 toHttpPath p = "file:///" ++ p
278 -- -----------------------------------------------------------------------------
279 -- Modify our copy of the package database based on a package flag
280 -- (-package, -hide-package, -ignore-package).
283 :: [PackageConfig] -- Initial database
284 -> PackageFlag -- flag to apply
285 -> IO [PackageConfig] -- new database
287 applyPackageFlag pkgs flag =
290 case matchingPackages str pkgs of
291 Nothing -> missingPackageErr str
292 Just ([], _) -> panic "applyPackageFlag"
293 Just (p:ps,qs) -> return (p':ps')
294 where p' = p {exposed=True}
295 ps' = hideAll (pkgName (package p)) (ps++qs)
298 case matchingPackages str pkgs of
299 Nothing -> missingPackageErr str
300 Just (ps,qs) -> return (map hide ps ++ qs)
301 where hide p = p {exposed=False}
304 case matchingPackages str pkgs of
305 Nothing -> return pkgs
306 Just (_, qs) -> return qs
307 -- missing package is not an error for -ignore-package,
308 -- because a common usage is to -ignore-package P as
309 -- a preventative measure just in case P exists.
311 -- When a package is requested to be exposed, we hide all other
312 -- packages with the same name.
313 hideAll name ps = map maybe_hide ps
314 where maybe_hide p | pkgName (package p) == name = p {exposed=False}
318 matchingPackages :: String -> [PackageConfig]
319 -> Maybe ([PackageConfig], [PackageConfig])
320 matchingPackages str pkgs
321 = case partition (packageMatches str) pkgs of
323 (ps,rest) -> Just (sortByVersion ps, rest)
325 -- A package named on the command line can either include the
326 -- version, or just the name if it is unambiguous.
327 packageMatches :: String -> PackageConfig -> Bool
329 = str == display (package p)
330 || str == display (pkgName (package p))
332 pickPackages :: [PackageConfig] -> [String] -> [PackageConfig]
333 pickPackages pkgs strs =
334 [ p | p <- strs, Just (p:_, _) <- [matchingPackages p pkgs] ]
336 sortByVersion :: [InstalledPackageInfo_ m] -> [InstalledPackageInfo_ m]
337 sortByVersion = sortBy (flip (comparing (pkgVersion.package)))
339 comparing :: Ord a => (t -> a) -> t -> t -> Ordering
340 comparing f a b = f a `compare` f b
342 -- -----------------------------------------------------------------------------
343 -- Hide old versions of packages
346 -- hide all packages for which there is also a later version
347 -- that is already exposed. This just makes it non-fatal to have two
348 -- versions of a package exposed, which can happen if you install a
349 -- later version of a package in the user database, for example.
351 hideOldPackages :: DynFlags -> [PackageConfig] -> IO [PackageConfig]
352 hideOldPackages dflags pkgs = mapM maybe_hide pkgs
354 | not (exposed p) = return p
355 | (p' : _) <- later_versions = do
356 debugTraceMsg dflags 2 $
357 (ptext (sLit "hiding package") <+>
358 text (display (package p)) <+>
359 ptext (sLit "to avoid conflict with later version") <+>
360 text (display (package p')))
361 return (p {exposed=False})
362 | otherwise = return p
363 where myname = pkgName (package p)
364 myversion = pkgVersion (package p)
365 later_versions = [ p | p <- pkgs, exposed p,
367 pkgName pkg == myname,
368 pkgVersion pkg > myversion ]
370 -- -----------------------------------------------------------------------------
375 -> [PackageConfig] -- database
376 -> IO [PackageConfig]
378 findWiredInPackages dflags pkgs = do
380 -- Now we must find our wired-in packages, and rename them to
381 -- their canonical names (eg. base-1.0 ==> base).
384 wired_in_pkgids :: [String]
385 wired_in_pkgids = map packageIdString
395 matches :: PackageConfig -> String -> Bool
396 pc `matches` pid = display (pkgName (package pc)) == pid
398 -- find which package corresponds to each wired-in package
399 -- delete any other packages with the same name
400 -- update the package and any dependencies to point to the new
403 -- When choosing which package to map to a wired-in package
404 -- name, we prefer exposed packages, and pick the latest
405 -- version. To override the default choice, -hide-package
406 -- could be used to hide newer versions.
408 findWiredInPackage :: [PackageConfig] -> String
409 -> IO (Maybe InstalledPackageId)
410 findWiredInPackage pkgs wired_pkg =
411 let all_ps = [ p | p <- pkgs, p `matches` wired_pkg ] in
414 many -> pick (head (sortByVersion many))
417 debugTraceMsg dflags 2 $
418 ptext (sLit "wired-in package ")
420 <> ptext (sLit " not found.")
422 pick :: InstalledPackageInfo_ ModuleName
423 -> IO (Maybe InstalledPackageId)
425 debugTraceMsg dflags 2 $
426 ptext (sLit "wired-in package ")
428 <> ptext (sLit " mapped to ")
429 <> text (display (package pkg))
430 return (Just (installedPackageId pkg))
433 mb_wired_in_ids <- mapM (findWiredInPackage pkgs) wired_in_pkgids
435 wired_in_ids = catMaybes mb_wired_in_ids
437 -- this is old: we used to assume that if there were
438 -- multiple versions of wired-in packages installed that
439 -- they were mutually exclusive. Now we're assuming that
440 -- you have one "main" version of each wired-in package
441 -- (the latest version), and the others are backward-compat
442 -- wrappers that depend on this one. e.g. base-4.0 is the
443 -- latest, base-3.0 is a compat wrapper depending on base-4.0.
445 deleteOtherWiredInPackages pkgs = filterOut bad pkgs
446 where bad p = any (p `matches`) wired_in_pkgids
447 && package p `notElem` map fst wired_in_ids
450 updateWiredInDependencies pkgs = map upd_pkg pkgs
452 | installedPackageId p `elem` wired_in_ids
453 = p { package = (package p){ pkgVersion = Version [] [] } }
457 return $ updateWiredInDependencies pkgs
459 -- ----------------------------------------------------------------------------
461 -- Detect any packages that have missing dependencies, and also any
462 -- mutually-recursive groups of packages (loops in the package graph
463 -- are not allowed). We do this by taking the least fixpoint of the
464 -- dependency graph, repeatedly adding packages whose dependencies are
465 -- satisfied until no more can be added.
470 -> [PackageId] -- ignored packages
471 -> IO [PackageConfig]
473 elimDanglingDeps dflags pkgs ignored = go [] pkgs'
475 pkgs' = filter (\p -> packageConfigId p `notElem` ignored) pkgs
478 case partitionWith (depsAvailable avail) not_avail of
479 ([], not_avail) -> do mapM_ reportElim not_avail; return avail
480 (new_avail, not_avail) -> go (new_avail ++ avail) (map fst not_avail)
482 depsAvailable :: [PackageConfig] -> PackageConfig
483 -> Either PackageConfig (PackageConfig, [InstalledPackageId])
484 depsAvailable pkgs_ok pkg
485 | null dangling = Left pkg
486 | otherwise = Right (pkg, dangling)
487 where dangling = filter (`notElem` pids) (depends pkg)
488 pids = map installedPackageId pkgs_ok
490 reportElim (p, deps) =
491 debugTraceMsg dflags 2 $
492 (ptext (sLit "package") <+> pprPkg p <+>
493 ptext (sLit "will be ignored due to missing or recursive dependencies:") $$
494 nest 2 (hsep (map (text.display) deps)))
496 -- -----------------------------------------------------------------------------
497 -- When all the command-line options are in, we can process our package
498 -- settings and populate the package state.
502 -> PackageConfigMap -- initial database
503 -> [PackageId] -- preloaded packages
504 -> PackageId -- this package
506 [PackageId], -- new packages to preload
507 PackageId) -- this package, might be modified if the current
509 -- package is a wired-in package.
511 mkPackageState dflags orig_pkg_db preload0 this_package = do
513 -- Modify the package database according to the command-line flags
514 -- (-package, -hide-package, -ignore-package, -hide-all-packages).
516 let flags = reverse (packageFlags dflags)
517 let pkgs0 = eltsUFM orig_pkg_db
518 pkgs1 <- foldM applyPackageFlag pkgs0 flags
520 -- Here we build up a set of the packages mentioned in -package
521 -- flags on the command line; these are called the "preload"
522 -- packages. we link these packages in eagerly. The preload set
523 -- should contain at least rts & base, which is why we pretend that
524 -- the command line contains -package rts & -package base.
526 let preload1 = map installedPackageId $
527 pickPackages pkgs0 [ p | ExposePackage p <- flags ]
529 -- hide packages that are subsumed by later versions
530 pkgs2 <- hideOldPackages dflags pkgs1
532 -- sort out which packages are wired in
533 pkgs3 <- findWiredInPackages dflags pkgs2
535 let ignored = map packageConfigId $
536 pickPackages pkgs0 [ p | IgnorePackage p <- flags ]
537 pkgs <- elimDanglingDeps dflags pkgs3 ignored
539 let pkg_db = extendPackageConfigMap emptyPackageConfigMap pkgs
541 ipid_map = listToFM [ (installedPackageId p, packageConfigId p)
544 lookupIPID ipid@(InstalledPackageId str)
545 | Just pid <- lookupFM ipid_map ipid = return pid
546 | otherwise = missingPackageErr str
548 preload2 <- mapM lookupIPID preload1
551 -- add base & rts to the preload packages
553 | dopt Opt_AutoLinkPackages dflags
554 = filter (flip elemUFM pkg_db) [basePackageId, rtsPackageId]
556 -- but in any case remove the current package from the set of
557 -- preloaded packages so that base/rts does not end up in the
558 -- set up preloaded package when we are just building it
559 preload3 = nub $ filter (/= this_package)
560 $ (basicLinkedPackages ++ preload2)
562 -- Close the preload packages with their dependencies
563 dep_preload <- closeDeps pkg_db ipid_map (zip preload3 (repeat Nothing))
564 let new_dep_preload = filter (`notElem` preload0) dep_preload
566 let pstate = PackageState{ preloadPackages = dep_preload,
568 moduleToPkgConfAll = mkModuleMap pkg_db,
569 installedPackageIdMap = ipid_map
572 return (pstate, new_dep_preload, this_package)
575 -- -----------------------------------------------------------------------------
576 -- Make the mapping from module to package info
580 -> UniqFM [(PackageConfig, Bool)]
581 mkModuleMap pkg_db = foldr extend_modmap emptyUFM pkgids
583 pkgids = map packageConfigId (eltsUFM pkg_db)
585 extend_modmap pkgid modmap =
586 addListToUFM_C (++) modmap
587 ([(m, [(pkg, True)]) | m <- exposed_mods] ++
588 [(m, [(pkg, False)]) | m <- hidden_mods])
590 pkg = expectJust "mkModuleMap" (lookupPackage pkg_db pkgid)
591 exposed_mods = exposedModules pkg
592 hidden_mods = hiddenModules pkg
594 pprPkg :: PackageConfig -> SDoc
595 pprPkg p = text (display (package p))
597 -- -----------------------------------------------------------------------------
598 -- Extracting information from the packages in scope
600 -- Many of these functions take a list of packages: in those cases,
601 -- the list is expected to contain the "dependent packages",
602 -- i.e. those packages that were found to be depended on by the
603 -- current module/program. These can be auto or non-auto packages, it
604 -- doesn't really matter. The list is always combined with the list
605 -- of preload (command-line) packages to determine which packages to
608 -- | Find all the include directories in these and the preload packages
609 getPackageIncludePath :: DynFlags -> [PackageId] -> IO [String]
610 getPackageIncludePath dflags pkgs =
611 collectIncludeDirs `fmap` getPreloadPackagesAnd dflags pkgs
613 collectIncludeDirs :: [PackageConfig] -> [FilePath]
614 collectIncludeDirs ps = nub (filter notNull (concatMap includeDirs ps))
616 -- | Find all the library paths in these and the preload packages
617 getPackageLibraryPath :: DynFlags -> [PackageId] -> IO [String]
618 getPackageLibraryPath dflags pkgs =
619 collectLibraryPaths `fmap` getPreloadPackagesAnd dflags pkgs
621 collectLibraryPaths :: [PackageConfig] -> [FilePath]
622 collectLibraryPaths ps = nub (filter notNull (concatMap libraryDirs ps))
624 -- | Find all the link options in these and the preload packages
625 getPackageLinkOpts :: DynFlags -> [PackageId] -> IO [String]
626 getPackageLinkOpts dflags pkgs =
627 collectLinkOpts dflags `fmap` getPreloadPackagesAnd dflags pkgs
629 collectLinkOpts :: DynFlags -> [PackageConfig] -> [String]
630 collectLinkOpts dflags ps = concat (map all_opts ps)
632 libs p = packageHsLibs dflags p ++ extraLibraries p
633 all_opts p = map ("-l" ++) (libs p) ++ ldOptions p
635 packageHsLibs :: DynFlags -> PackageConfig -> [String]
636 packageHsLibs dflags p = map (mkDynName . addSuffix) (hsLibraries p)
638 non_dyn_ways = filter ((/= WayDyn) . wayName) (ways dflags)
639 -- the name of a shared library is libHSfoo-ghc<version>.so
640 -- we leave out the _dyn, because it is superfluous
642 tag = mkBuildTag (filter (not . wayRTSOnly) non_dyn_ways)
643 rts_tag = mkBuildTag non_dyn_ways
645 mkDynName | opt_Static = id
646 | otherwise = (++ ("-ghc" ++ cProjectVersion))
648 addSuffix rts@"HSrts" = rts ++ (expandTag rts_tag)
649 addSuffix other_lib = other_lib ++ (expandTag tag)
651 expandTag t | null t = ""
654 -- | Find all the C-compiler options in these and the preload packages
655 getPackageExtraCcOpts :: DynFlags -> [PackageId] -> IO [String]
656 getPackageExtraCcOpts dflags pkgs = do
657 ps <- getPreloadPackagesAnd dflags pkgs
658 return (concatMap ccOptions ps)
660 -- | Find all the package framework paths in these and the preload packages
661 getPackageFrameworkPath :: DynFlags -> [PackageId] -> IO [String]
662 getPackageFrameworkPath dflags pkgs = do
663 ps <- getPreloadPackagesAnd dflags pkgs
664 return (nub (filter notNull (concatMap frameworkDirs ps)))
666 -- | Find all the package frameworks in these and the preload packages
667 getPackageFrameworks :: DynFlags -> [PackageId] -> IO [String]
668 getPackageFrameworks dflags pkgs = do
669 ps <- getPreloadPackagesAnd dflags pkgs
670 return (concatMap frameworks ps)
672 -- -----------------------------------------------------------------------------
675 -- | Takes a 'Module', and if the module is in a package returns
676 -- @(pkgconf, exposed)@ where pkgconf is the PackageConfig for that package,
677 -- and exposed is @True@ if the package exposes the module.
678 lookupModuleInAllPackages :: DynFlags -> ModuleName -> [(PackageConfig,Bool)]
679 lookupModuleInAllPackages dflags m =
680 case lookupUFM (moduleToPkgConfAll (pkgState dflags)) m of
684 -- | Find all the 'PackageConfig' in both the preload packages from 'DynFlags' and corresponding to the list of
686 getPreloadPackagesAnd :: DynFlags -> [PackageId] -> IO [PackageConfig]
687 getPreloadPackagesAnd dflags pkgids =
689 state = pkgState dflags
690 pkg_map = pkgIdMap state
691 ipid_map = installedPackageIdMap state
692 preload = preloadPackages state
693 pairs = zip pkgids (repeat Nothing)
695 all_pkgs <- throwErr (foldM (add_package pkg_map ipid_map) preload pairs)
696 return (map (getPackageDetails state) all_pkgs)
698 -- Takes a list of packages, and returns the list with dependencies included,
699 -- in reverse dependency order (a package appears before those it depends on).
700 closeDeps :: PackageConfigMap
701 -> FiniteMap InstalledPackageId PackageId
702 -> [(PackageId, Maybe PackageId)]
704 closeDeps pkg_map ipid_map ps = throwErr (closeDepsErr pkg_map ipid_map ps)
706 throwErr :: MaybeErr Message a -> IO a
707 throwErr m = case m of
708 Failed e -> ghcError (CmdLineError (showSDoc e))
709 Succeeded r -> return r
711 closeDepsErr :: PackageConfigMap
712 -> FiniteMap InstalledPackageId PackageId
713 -> [(PackageId,Maybe PackageId)]
714 -> MaybeErr Message [PackageId]
715 closeDepsErr pkg_map ipid_map ps = foldM (add_package pkg_map ipid_map) [] ps
718 add_package :: PackageConfigMap
719 -> FiniteMap InstalledPackageId PackageId
721 -> (PackageId,Maybe PackageId)
722 -> MaybeErr Message [PackageId]
723 add_package pkg_db ipid_map ps (p, mb_parent)
724 | p `elem` ps = return ps -- Check if we've already added this package
726 case lookupPackage pkg_db p of
727 Nothing -> Failed (missingPackageMsg (packageIdString p) <>
728 missingDependencyMsg mb_parent)
730 -- Add the package's dependents also
731 ps' <- foldM add_package_ipid ps (depends pkg)
734 add_package_ipid ps ipid@(InstalledPackageId str)
735 | Just pid <- lookupFM ipid_map ipid
736 = add_package pkg_db ipid_map ps (pid, Just p)
738 = Failed (missingPackageMsg str <> missingDependencyMsg mb_parent)
740 missingPackageErr :: String -> IO a
741 missingPackageErr p = ghcError (CmdLineError (showSDoc (missingPackageMsg p)))
743 missingPackageMsg :: String -> SDoc
744 missingPackageMsg p = ptext (sLit "unknown package:") <+> text p
746 missingDependencyMsg :: Maybe PackageId -> SDoc
747 missingDependencyMsg Nothing = empty
748 missingDependencyMsg (Just parent)
749 = space <> parens (ptext (sLit "dependency of") <+> ftext (packageIdFS parent))
751 -- -----------------------------------------------------------------------------
753 -- | Will the 'Name' come from a dynamically linked library?
754 isDllName :: PackageId -> Name -> Bool
755 isDllName this_pkg name
757 | Just mod <- nameModule_maybe name = modulePackageId mod /= this_pkg
758 | otherwise = False -- no, it is not even an external name
760 -- -----------------------------------------------------------------------------
761 -- Displaying packages
763 -- | Show package info on console, if verbosity is >= 3
764 dumpPackages :: DynFlags -> IO ()
766 = do let pkg_map = pkgIdMap (pkgState dflags)
768 vcat (map (text . showInstalledPackageInfo
769 . packageConfigToInstalledPackageInfo)