[project @ 2005-06-21 10:44:37 by simonmar]
[ghc-hetmet.git] / ghc / compiler / main / Packages.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4 \section{Package manipulation}
5
6 \begin{code}
7 module Packages (
8         module PackageConfig,
9
10         -- * The PackageConfigMap
11         PackageConfigMap, emptyPackageConfigMap, lookupPackage,
12         extendPackageConfigMap, dumpPackages,
13
14         -- * Reading the package config, and processing cmdline args
15         PackageIdH(..), isHomePackage,
16         PackageState(..), 
17         initPackages,
18         getPackageDetails,
19         checkForPackageConflicts,
20         lookupModuleInAllPackages,
21
22         HomeModules, mkHomeModules, isHomeModule,
23
24         -- * Inspecting the set of packages in scope
25         getPackageIncludePath,
26         getPackageCIncludes,
27         getPackageLibraryPath,
28         getPackageLinkOpts,
29         getPackageExtraCcOpts,
30         getPackageFrameworkPath,
31         getPackageFrameworks,
32         getExplicitPackagesAnd,
33
34         -- * Utils
35         isDllName
36     )
37 where
38
39 #include "HsVersions.h"
40
41 import PackageConfig    
42 import SysTools         ( getTopDir, getPackageConfigPath )
43 import ParsePkgConf     ( loadPackageConfig )
44 import DynFlags         ( dopt, DynFlag(..), DynFlags(..), PackageFlag(..) )
45 import StaticFlags      ( opt_Static )
46 import Config           ( cProjectVersion )
47 import Name             ( Name, nameModule_maybe )
48 import UniqFM
49 import Module
50 import FiniteMap
51 import UniqSet
52 import Util
53 import Maybes           ( expectJust, MaybeErr(..) )
54 import Panic
55 import Outputable
56
57 #if __GLASGOW_HASKELL__ >= 603
58 import System.Directory ( getAppUserDataDirectory )
59 #else
60 import Compat.Directory ( getAppUserDataDirectory )
61 #endif
62
63 import Distribution.InstalledPackageInfo
64 import Distribution.Package
65 import Distribution.Version
66 import Data.Maybe       ( isNothing )
67 import System.Directory ( doesFileExist )
68 import Control.Monad    ( foldM )
69 import Data.List        ( nub, partition )
70
71 #ifdef mingw32_TARGET_OS
72 import Data.List        ( isPrefixOf )
73 #endif
74
75 import FastString
76 import EXCEPTION        ( throwDyn )
77 import ErrUtils         ( debugTraceMsg, putMsg, Message )
78
79 -- ---------------------------------------------------------------------------
80 -- The Package state
81
82 -- Package state is all stored in DynFlags, including the details of
83 -- all packages, which packages are exposed, and which modules they
84 -- provide.
85
86 -- The package state is computed by initPackages, and kept in DynFlags.
87 --
88 --   * -package <pkg> causes <pkg> to become exposed, and all other packages 
89 --      with the same name to become hidden.
90 -- 
91 --   * -hide-package <pkg> causes <pkg> to become hidden.
92 -- 
93 --   * Let exposedPackages be the set of packages thus exposed.  
94 --     Let depExposedPackages be the transitive closure from exposedPackages of
95 --     their dependencies.
96 --
97 --   * It is an error for any two packages in depExposedPackages to provide the
98 --     same module.
99 -- 
100 --   * When searching for a module from an explicit import declaration,
101 --     only the exposed modules in exposedPackages are valid.
102 --
103 --   * When searching for a module from an implicit import, all modules
104 --     from depExposedPackages are valid.
105 --
106 --   * When linking in a comp manager mode, we link in packages the
107 --     program depends on (the compiler knows this list by the
108 --     time it gets to the link step).  Also, we link in all packages
109 --     which were mentioned with explicit -package flags on the command-line,
110 --     or are a transitive dependency of same, or are "base"/"rts".
111 --     The reason for (b) is that we might need packages which don't
112 --     contain any Haskell modules, and therefore won't be discovered
113 --     by the normal mechanism of dependency tracking.
114
115
116 -- One important thing that the package state provides is a way to
117 -- tell, for a given module, whether it is part of the current package
118 -- or not.  We need to know this for two reasons:
119 --
120 --  * generating cross-DLL calls is different from intra-DLL calls 
121 --    (see below).
122 --  * we don't record version information in interface files for entities
123 --    in a different package.
124 -- 
125 -- Notes on DLLs
126 -- ~~~~~~~~~~~~~
127 -- When compiling module A, which imports module B, we need to 
128 -- know whether B will be in the same DLL as A.  
129 --      If it's in the same DLL, we refer to B_f_closure
130 --      If it isn't, we refer to _imp__B_f_closure
131 -- When compiling A, we record in B's Module value whether it's
132 -- in a different DLL, by setting the DLL flag.
133
134 data PackageState = PackageState {
135
136   explicitPackages      :: [PackageId],
137         -- The packages we're going to link in eagerly.  This list
138         -- should be in reverse dependency order; that is, a package
139         -- is always mentioned before the packages it depends on.
140
141   pkgIdMap              :: PackageConfigMap, -- PackageId   -> PackageConfig
142         -- mapping derived from the package databases and
143         -- command-line package flags.
144
145   moduleToPkgConfAll    :: ModuleEnv [(PackageConfig,Bool)],
146         -- Maps Module to (pkgconf,exposed), where pkgconf is the
147         -- PackageConfig for the package containing the module, and
148         -- exposed is True if the package exposes that module.
149
150   -- The PackageIds of some known packages
151   basePackageId         :: PackageIdH,
152   rtsPackageId          :: PackageIdH,
153   haskell98PackageId    :: PackageIdH,
154   thPackageId           :: PackageIdH
155   }
156
157 data PackageIdH 
158    = HomePackage                -- The "home" package is the package curently
159                                 -- being compiled
160    | ExtPackage PackageId       -- An "external" package is any other package
161
162
163 isHomePackage :: PackageIdH -> Bool
164 isHomePackage HomePackage    = True
165 isHomePackage (ExtPackage _) = False
166
167 -- A PackageConfigMap maps a PackageId to a PackageConfig
168 type PackageConfigMap = UniqFM PackageConfig
169
170 emptyPackageConfigMap :: PackageConfigMap
171 emptyPackageConfigMap = emptyUFM
172
173 lookupPackage :: PackageConfigMap -> PackageId -> Maybe PackageConfig
174 lookupPackage = lookupUFM
175
176 extendPackageConfigMap
177    :: PackageConfigMap -> [PackageConfig] -> PackageConfigMap
178 extendPackageConfigMap pkg_map new_pkgs 
179   = foldl add pkg_map new_pkgs
180   where add pkg_map p = addToUFM pkg_map (packageConfigId p) p
181
182 getPackageDetails :: PackageState -> PackageId -> PackageConfig
183 getPackageDetails dflags ps = expectJust "getPackageDetails" (lookupPackage (pkgIdMap dflags) ps)
184
185 -- ----------------------------------------------------------------------------
186 -- Loading the package config files and building up the package state
187
188 -- | Call this after parsing the DynFlags.  It reads the package
189 -- configuration files, and sets up various internal tables of package
190 -- information, according to the package-related flags on the
191 -- command-line (@-package@, @-hide-package@ etc.)
192 initPackages :: DynFlags -> IO DynFlags
193 initPackages dflags = do 
194   pkg_map <- readPackageConfigs dflags; 
195   state <- mkPackageState dflags pkg_map
196   return dflags{ pkgState = state }
197
198 -- -----------------------------------------------------------------------------
199 -- Reading the package database(s)
200
201 readPackageConfigs :: DynFlags -> IO PackageConfigMap
202 readPackageConfigs dflags = do
203         -- System one always comes first
204    system_pkgconf <- getPackageConfigPath
205    pkg_map1 <- readPackageConfig dflags emptyPackageConfigMap system_pkgconf
206
207         -- Read user's package conf (eg. ~/.ghc/i386-linux-6.3/package.conf)
208         -- unless the -no-user-package-conf flag was given.
209         -- We only do this when getAppUserDataDirectory is available 
210         -- (GHC >= 6.3).
211    (exists, pkgconf) <- catch (do
212       appdir <- getAppUserDataDirectory "ghc"
213       let 
214          pkgconf = appdir
215                    `joinFileName` (TARGET_ARCH ++ '-':TARGET_OS ++ '-':cProjectVersion)
216                    `joinFileName` "package.conf"
217       flg <- doesFileExist pkgconf
218       return (flg, pkgconf))
219        -- gobble them all up and turn into False.
220       (\ _ -> return (False, ""))
221    pkg_map2 <- if (dopt Opt_ReadUserPackageConf dflags && exists)
222                   then readPackageConfig dflags pkg_map1 pkgconf
223                   else return pkg_map1
224
225         -- Read all the ones mentioned in -package-conf flags
226    pkg_map <- foldM (readPackageConfig dflags) pkg_map2
227                  (extraPkgConfs dflags)
228
229    return pkg_map
230
231
232 readPackageConfig
233    :: DynFlags -> PackageConfigMap -> FilePath -> IO PackageConfigMap
234 readPackageConfig dflags pkg_map conf_file = do
235   debugTraceMsg dflags 2 ("Using package config file: " ++ conf_file)
236   proto_pkg_configs <- loadPackageConfig conf_file
237   top_dir           <- getTopDir
238   let pkg_configs1 = mungePackagePaths top_dir proto_pkg_configs
239       pkg_configs2 = maybeHidePackages dflags pkg_configs1
240   return (extendPackageConfigMap pkg_map pkg_configs2)
241
242 maybeHidePackages :: DynFlags -> [PackageConfig] -> [PackageConfig]
243 maybeHidePackages dflags pkgs
244   | dopt Opt_HideAllPackages dflags = map hide pkgs
245   | otherwise                       = pkgs
246   where
247     hide pkg = pkg{ exposed = False }
248
249 mungePackagePaths :: String -> [PackageConfig] -> [PackageConfig]
250 -- Replace the string "$topdir" at the beginning of a path
251 -- with the current topdir (obtained from the -B option).
252 mungePackagePaths top_dir ps = map munge_pkg ps
253  where 
254   munge_pkg p = p{ importDirs  = munge_paths (importDirs p),
255                    includeDirs = munge_paths (includeDirs p),
256                    libraryDirs = munge_paths (libraryDirs p),
257                    frameworkDirs = munge_paths (frameworkDirs p) }
258
259   munge_paths = map munge_path
260
261   munge_path p 
262           | Just p' <- maybePrefixMatch "$topdir" p = top_dir ++ p'
263           | otherwise                               = p
264
265
266 -- -----------------------------------------------------------------------------
267 -- When all the command-line options are in, we can process our package
268 -- settings and populate the package state.
269
270 mkPackageState :: DynFlags -> PackageConfigMap -> IO PackageState
271 mkPackageState dflags orig_pkg_db = do
272   --
273   -- Modify the package database according to the command-line flags
274   -- (-package, -hide-package, -ignore-package, -hide-all-packages).
275   --
276   -- Also, here we build up a set of the packages mentioned in -package
277   -- flags on the command line; these are called the "explicit" packages.
278   -- we link these packages in eagerly.  The explicit set should contain
279   -- at least rts & base, which is why we pretend that the command line
280   -- contains -package rts & -package base.
281   --
282   let
283         flags = reverse (packageFlags dflags)
284
285         procflags pkgs expl [] = return (pkgs,expl)
286         procflags pkgs expl (ExposePackage str : flags) = do
287            case partition (matches str) pkgs of
288                 ([],_)   -> missingPackageErr str
289                 ([p],ps) -> procflags (p':ps) (addOneToUniqSet expl pkgid) flags
290                   where pkgid = packageConfigId p
291                         p' = p {exposed=True}
292                 (ps,_)   -> multiplePackagesErr str ps
293         procflags pkgs expl (HidePackage str : flags) = do
294            case partition (matches str) pkgs of
295                 ([],_)   -> missingPackageErr str
296                 ([p],ps) -> procflags (p':ps) expl flags
297                   where p' = p {exposed=False}
298                 (ps,_)   -> multiplePackagesErr str ps
299         procflags pkgs expl (IgnorePackage str : flags) = do
300            case partition (matches str) pkgs of
301                 (ps,qs) -> procflags qs expl flags
302                 -- missing package is not an error for -ignore-package,
303                 -- because a common usage is to -ignore-package P as
304                 -- a preventative measure just in case P exists.
305
306         -- A package named on the command line can either include the
307         -- version, or just the name if it is unambiguous.
308         matches str p
309                 =  str == showPackageId (package p)
310                 || str == pkgName (package p)
311   --
312   (pkgs1,explicit) <- procflags (eltsUFM orig_pkg_db) emptyUniqSet flags
313   --
314   let
315         elimDanglingDeps pkgs = 
316            case partition (hasDanglingDeps pkgs) pkgs of
317               ([],ps) -> ps
318               (ps,qs) -> elimDanglingDeps qs
319
320         hasDanglingDeps pkgs p = any dangling (depends p)
321           where dangling pid = pid `notElem` all_pids
322                 all_pids = map package pkgs
323   --
324   -- Eliminate any packages which have dangling dependencies (perhaps
325   -- because the package was removed by -ignore-package).
326   --
327   let pkgs = elimDanglingDeps pkgs1
328       pkg_db = extendPackageConfigMap emptyPackageConfigMap pkgs
329   --
330   -- Find the transitive closure of dependencies of exposed
331   --
332   let exposed_pkgids = [ packageConfigId p | p <- pkgs, exposed p ]
333   dep_exposed <- closeDeps pkg_db exposed_pkgids
334   --
335   -- Look up some known PackageIds
336   --
337   let
338         lookupPackageByName :: FastString -> PackageIdH
339         lookupPackageByName nm = 
340           case [ conf | p <- dep_exposed,
341                         Just conf <- [lookupPackage pkg_db p],
342                         nm == mkFastString (pkgName (package conf)) ] of
343                 []     -> HomePackage
344                 (p:ps) -> ExtPackage (mkPackageId (package p))
345
346         -- Get the PackageIds for some known packages (we know the names,
347         -- but we don't know the versions).  Some of these packages might
348         -- not exist in the database, so they are Maybes.
349         basePackageId           = lookupPackageByName basePackageName
350         rtsPackageId            = lookupPackageByName rtsPackageName
351         haskell98PackageId      = lookupPackageByName haskell98PackageName
352         thPackageId             = lookupPackageByName thPackageName
353
354         -- add base & rts to the explicit packages
355         basicLinkedPackages = [basePackageId,rtsPackageId]
356         explicit' = addListToUniqSet explicit 
357                         [ p | ExtPackage p <- basicLinkedPackages ]
358   --
359   -- Close the explicit packages with their dependencies
360   --
361   dep_explicit <- closeDeps pkg_db (uniqSetToList explicit')
362   --
363   -- Build up a mapping from Module -> PackageConfig for all modules.
364   -- Discover any conflicts at the same time, and factor in the new exposed
365   -- status of each package.
366   --
367   let mod_map = mkModuleMap orig_pkg_db dep_exposed
368
369   return PackageState{ explicitPackages     = dep_explicit,
370                        pkgIdMap             = orig_pkg_db,
371                        moduleToPkgConfAll   = mod_map,
372                        basePackageId        = basePackageId,
373                        rtsPackageId         = rtsPackageId,
374                        haskell98PackageId   = haskell98PackageId,
375                        thPackageId          = thPackageId
376                      }
377   -- done!
378
379 basePackageName      = FSLIT("base")
380 rtsPackageName       = FSLIT("rts")
381 haskell98PackageName = FSLIT("haskell98")
382 thPackageName        = FSLIT("template-haskell")
383                                 -- Template Haskell libraries in here
384
385 multiplePackagesErr str ps =
386   throwDyn (CmdLineError (showSDoc (
387                    text "Error; multiple packages match" <+> 
388                         text str <> colon <+>
389                     sep (punctuate comma (map (text.showPackageId.package) ps))
390                 )))
391
392 mkModuleMap
393   :: PackageConfigMap
394   -> [PackageId]
395   -> ModuleEnv [(PackageConfig, Bool)]
396 mkModuleMap pkg_db pkgs = foldr extend_modmap emptyUFM pkgs
397   where
398         extend_modmap pkgname modmap =
399                 addListToUFM_C (++) modmap 
400                     [(m, [(pkg, m `elem` exposed_mods)]) | m <- all_mods]
401           where
402                 pkg = expectJust "mkModuleMap" (lookupPackage pkg_db pkgname)
403                 exposed_mods = map mkModule (exposedModules pkg)
404                 hidden_mods  = map mkModule (hiddenModules pkg)
405                 all_mods = exposed_mods ++ hidden_mods
406
407 -- -----------------------------------------------------------------------------
408 -- Check for conflicts in the program.
409
410 -- | A conflict arises if the program contains two modules with the same
411 -- name, which can arise if the program depends on multiple packages that
412 -- expose the same module, or if the program depends on a package that
413 -- contains a module also present in the program (the "home package").
414 --
415 checkForPackageConflicts
416    :: DynFlags
417    -> [Module]          -- modules in the home package
418    -> [PackageId]       -- packages on which the program depends
419    -> MaybeErr Message ()
420
421 checkForPackageConflicts dflags mods pkgs = do
422     let 
423         state   = pkgState dflags
424         pkg_db  = pkgIdMap state
425     --
426     dep_pkgs <- closeDepsErr pkg_db pkgs
427
428     let 
429         extend_modmap pkgname modmap  =
430                 addListToFM_C (++) modmap
431                     [(m, [(pkg, m `elem` exposed_mods)]) | m <- all_mods]
432           where
433                 pkg = expectJust "checkForPackageConflicts" 
434                                 (lookupPackage pkg_db pkgname)
435                 exposed_mods = map mkModule (exposedModules pkg)
436                 hidden_mods  = map mkModule (hiddenModules pkg)
437                 all_mods = exposed_mods ++ hidden_mods
438
439         mod_map = foldr extend_modmap emptyFM pkgs
440         mod_map_list :: [(Module,[(PackageConfig,Bool)])]
441         mod_map_list = fmToList mod_map
442
443         overlaps = [ (m, map fst ps) | (m,ps@(_:_:_)) <- mod_map_list ]
444     --
445     if not (null overlaps)
446         then Failed (pkgOverlapError overlaps)
447         else do
448
449     let 
450         overlap_mods = [ (mod,pkg)
451                        | mod <- mods,
452                          Just ((pkg,_):_) <- [lookupFM mod_map mod] ]    
453                                 -- will be only one package here
454     if not (null overlap_mods)
455         then Failed (modOverlapError overlap_mods)
456         else do
457
458     return ()
459        
460 pkgOverlapError overlaps =  vcat (map msg overlaps)
461   where 
462         msg (mod,pkgs) =
463            text "conflict: module" <+> quotes (ppr mod)
464                  <+> ptext SLIT("is present in multiple packages:")
465                  <+> hsep (punctuate comma (map (text.showPackageId.package) pkgs))
466
467 modOverlapError overlaps =   vcat (map msg overlaps)
468   where 
469         msg (mod,pkg) = fsep [
470                 text "conflict: module",
471                 quotes (ppr mod),
472                 ptext SLIT("belongs to the current program/library"),
473                 ptext SLIT("and also to package"),
474                 text (showPackageId (package pkg)) ]
475
476 -- -----------------------------------------------------------------------------
477 -- Extracting information from the packages in scope
478
479 -- Many of these functions take a list of packages: in those cases,
480 -- the list is expected to contain the "dependent packages",
481 -- i.e. those packages that were found to be depended on by the
482 -- current module/program.  These can be auto or non-auto packages, it
483 -- doesn't really matter.  The list is always combined with the list
484 -- of explicit (command-line) packages to determine which packages to
485 -- use.
486
487 getPackageIncludePath :: DynFlags -> [PackageId] -> IO [String]
488 getPackageIncludePath dflags pkgs = do
489   ps <- getExplicitPackagesAnd dflags pkgs
490   return (nub (filter notNull (concatMap includeDirs ps)))
491
492         -- includes are in reverse dependency order (i.e. rts first)
493 getPackageCIncludes :: [PackageConfig] -> IO [String]
494 getPackageCIncludes pkg_configs = do
495   return (reverse (nub (filter notNull (concatMap includes pkg_configs))))
496
497 getPackageLibraryPath :: DynFlags -> [PackageId] -> IO [String]
498 getPackageLibraryPath dflags pkgs = do 
499   ps <- getExplicitPackagesAnd dflags pkgs
500   return (nub (filter notNull (concatMap libraryDirs ps)))
501
502 getPackageLinkOpts :: DynFlags -> [PackageId] -> IO [String]
503 getPackageLinkOpts dflags pkgs = do
504   ps <- getExplicitPackagesAnd dflags pkgs
505   let tag = buildTag dflags
506       rts_tag = rtsBuildTag dflags
507   let 
508         imp        = if opt_Static then "" else "_dyn"
509         libs p     = map ((++imp) . addSuffix) (hACK (hsLibraries p)) ++ extraLibraries p
510         all_opts p = map ("-l" ++) (libs p) ++ ldOptions p
511
512         suffix     = if null tag then "" else  '_':tag
513         rts_suffix = if null rts_tag then "" else  '_':rts_tag
514
515         addSuffix rts@"HSrts"    = rts       ++ rts_suffix
516         addSuffix other_lib      = other_lib ++ suffix
517
518   return (concat (map all_opts ps))
519   where
520
521      -- This is a totally horrible (temporary) hack, for Win32.  Problem is
522      -- that package.conf for Win32 says that the main prelude lib is 
523      -- split into HSbase1, HSbase2 and HSbase3, which is needed due to a bug
524      -- in the GNU linker (PEi386 backend). However, we still only
525      -- have HSbase.a for static linking, not HSbase{1,2,3}.a
526      -- getPackageLibraries is called to find the .a's to add to the static
527      -- link line.  On Win32, this hACK detects HSbase{1,2,3} and 
528      -- replaces them with HSbase, so static linking still works.
529      -- Libraries needed for dynamic (GHCi) linking are discovered via
530      -- different route (in InteractiveUI.linkPackage).
531      -- See driver/PackageSrc.hs for the HSbase1/HSbase2 split definition.
532      -- THIS IS A STRICTLY TEMPORARY HACK (famous last words ...)
533      -- JRS 04 Sept 01: Same appalling hack for HSwin32[1,2]
534      -- KAA 29 Mar  02: Same appalling hack for HSobjectio[1,2,3,4]
535      --
536      -- [sof 03/05: Renamed the (moribund) HSwin32 to HSwin_32 so as to
537      --  avoid filename conflicts with the 'Win32' package on a case-insensitive filesystem]
538      hACK libs
539 #      if !defined(mingw32_TARGET_OS) && !defined(cygwin32_TARGET_OS)
540        = libs
541 #      else
542        = if   "HSbase1" `elem` libs && "HSbase2" `elem` libs && "HSbase3" `elem` libs
543          then "HSbase" : filter (not.(isPrefixOf "HSbase")) libs
544          else
545          if   "HSwin_321" `elem` libs && "HSwin_322" `elem` libs
546          then "HSwin_32" : filter (not.(isPrefixOf "HSwin_32")) libs
547          else 
548          if   "HSobjectio1" `elem` libs && "HSobjectio2" `elem` libs && "HSobjectio3" `elem` libs && "HSobjectio4" `elem` libs
549          then "HSobjectio" : filter (not.(isPrefixOf "HSobjectio")) libs
550          else 
551          libs
552 #      endif
553
554 getPackageExtraCcOpts :: DynFlags -> [PackageId] -> IO [String]
555 getPackageExtraCcOpts dflags pkgs = do
556   ps <- getExplicitPackagesAnd dflags pkgs
557   return (concatMap ccOptions ps)
558
559 getPackageFrameworkPath  :: DynFlags -> [PackageId] -> IO [String]
560 getPackageFrameworkPath dflags pkgs = do
561   ps <- getExplicitPackagesAnd dflags pkgs
562   return (nub (filter notNull (concatMap frameworkDirs ps)))
563
564 getPackageFrameworks  :: DynFlags -> [PackageId] -> IO [String]
565 getPackageFrameworks dflags pkgs = do
566   ps <- getExplicitPackagesAnd dflags pkgs
567   return (concatMap frameworks ps)
568
569 -- -----------------------------------------------------------------------------
570 -- Package Utils
571
572 -- | Takes a Module, and if the module is in a package returns 
573 -- @(pkgconf,exposed)@ where pkgconf is the PackageConfig for that package,
574 -- and exposed is True if the package exposes the module.
575 lookupModuleInAllPackages :: DynFlags -> Module -> [(PackageConfig,Bool)]
576 lookupModuleInAllPackages dflags m =
577   case lookupModuleEnv (moduleToPkgConfAll (pkgState dflags)) m of
578         Nothing -> []
579         Just ps -> ps
580
581 getExplicitPackagesAnd :: DynFlags -> [PackageId] -> IO [PackageConfig]
582 getExplicitPackagesAnd dflags pkgids =
583   let 
584       state   = pkgState dflags
585       pkg_map = pkgIdMap state
586       expl    = explicitPackages state
587   in do
588   all_pkgs <- throwErr (foldM (add_package pkg_map) expl pkgids)
589   return (map (getPackageDetails state) all_pkgs)
590
591 -- Takes a list of packages, and returns the list with dependencies included,
592 -- in reverse dependency order (a package appears before those it depends on).
593 closeDeps :: PackageConfigMap -> [PackageId] -> IO [PackageId]
594 closeDeps pkg_map ps = throwErr (closeDepsErr pkg_map ps)
595
596 throwErr :: MaybeErr Message a -> IO a
597 throwErr m = case m of
598                 Failed e    -> throwDyn (CmdLineError (showSDoc e))
599                 Succeeded r -> return r
600
601 closeDepsErr :: PackageConfigMap -> [PackageId]
602         -> MaybeErr Message [PackageId]
603 closeDepsErr pkg_map ps = foldM (add_package pkg_map) [] ps
604
605 -- internal helper
606 add_package :: PackageConfigMap -> [PackageId] -> PackageId 
607         -> MaybeErr Message [PackageId]
608 add_package pkg_db ps p
609   | p `elem` ps = return ps     -- Check if we've already added this package
610   | otherwise =
611       case lookupPackage pkg_db p of
612         Nothing -> Failed (missingPackageErr (packageIdString p))
613         Just pkg -> do
614            -- Add the package's dependents also
615            let deps = map mkPackageId (depends pkg)
616            ps' <- foldM (add_package pkg_db) ps deps
617            return (p : ps')
618
619 missingPackageErr p = throwDyn (CmdLineError (showSDoc (missingPackageMsg p)))
620 missingPackageMsg p = ptext SLIT("unknown package:") <> text p
621
622 -- -----------------------------------------------------------------------------
623 -- The home module set
624
625 newtype HomeModules = HomeModules ModuleSet
626
627 mkHomeModules :: [Module] -> HomeModules
628 mkHomeModules = HomeModules . mkModuleSet
629
630 isHomeModule :: HomeModules -> Module -> Bool
631 isHomeModule (HomeModules set) mod  = elemModuleSet mod set
632
633 -- Determining whether a Name refers to something in another package or not.
634 -- Cross-package references need to be handled differently when dynamically-
635 -- linked libraries are involved.
636
637 isDllName :: HomeModules -> Name -> Bool
638 isDllName pdeps name
639   | opt_Static = False
640   | Just mod <- nameModule_maybe name = not (isHomeModule pdeps mod)
641   | otherwise = False  -- no, it is not even an external name
642
643 -- -----------------------------------------------------------------------------
644 -- Displaying packages
645
646 dumpPackages :: DynFlags -> IO ()
647 -- Show package info on console, if verbosity is >= 3
648 dumpPackages dflags
649   = do  let pkg_map = pkgIdMap (pkgState dflags)
650         putMsg $ showSDoc $
651               vcat (map (text.showInstalledPackageInfo) (eltsUFM pkg_map))
652 \end{code}