2 % (c) The University of Glasgow, 2000
4 \section{Package manipulation}
10 -- * The PackageConfigMap
11 PackageConfigMap, emptyPackageConfigMap, lookupPackage,
12 extendPackageConfigMap, dumpPackages,
14 -- * Reading the package config, and processing cmdline args
15 PackageIdH(..), isHomePackage,
19 checkForPackageConflicts,
20 lookupModuleInAllPackages,
22 HomeModules, mkHomeModules, isHomeModule,
24 -- * Inspecting the set of packages in scope
25 getPackageIncludePath,
27 getPackageLibraryPath,
29 getPackageExtraCcOpts,
30 getPackageFrameworkPath,
32 getExplicitPackagesAnd,
39 #include "HsVersions.h"
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 )
53 import Maybes ( expectJust, MaybeErr(..) )
57 #if __GLASGOW_HASKELL__ >= 603
58 import System.Directory ( getAppUserDataDirectory )
60 import Compat.Directory ( getAppUserDataDirectory )
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 )
71 #ifdef mingw32_TARGET_OS
72 import Data.List ( isPrefixOf )
76 import EXCEPTION ( throwDyn )
77 import ErrUtils ( debugTraceMsg, putMsg, Message )
79 -- ---------------------------------------------------------------------------
82 -- Package state is all stored in DynFlags, including the details of
83 -- all packages, which packages are exposed, and which modules they
86 -- The package state is computed by initPackages, and kept in DynFlags.
88 -- * -package <pkg> causes <pkg> to become exposed, and all other packages
89 -- with the same name to become hidden.
91 -- * -hide-package <pkg> causes <pkg> to become hidden.
93 -- * Let exposedPackages be the set of packages thus exposed.
94 -- Let depExposedPackages be the transitive closure from exposedPackages of
95 -- their dependencies.
97 -- * It is an error for any two packages in depExposedPackages to provide the
100 -- * When searching for a module from an explicit import declaration,
101 -- only the exposed modules in exposedPackages are valid.
103 -- * When searching for a module from an implicit import, all modules
104 -- from depExposedPackages are valid.
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.
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:
120 -- * generating cross-DLL calls is different from intra-DLL calls
122 -- * we don't record version information in interface files for entities
123 -- in a different package.
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.
134 data PackageState = PackageState {
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.
141 pkgIdMap :: PackageConfigMap, -- PackageId -> PackageConfig
142 -- mapping derived from the package databases and
143 -- command-line package flags.
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.
150 -- The PackageIds of some known packages
151 basePackageId :: PackageIdH,
152 rtsPackageId :: PackageIdH,
153 haskell98PackageId :: PackageIdH,
154 thPackageId :: PackageIdH
158 = HomePackage -- The "home" package is the package curently
160 | ExtPackage PackageId -- An "external" package is any other package
163 isHomePackage :: PackageIdH -> Bool
164 isHomePackage HomePackage = True
165 isHomePackage (ExtPackage _) = False
167 -- A PackageConfigMap maps a PackageId to a PackageConfig
168 type PackageConfigMap = UniqFM PackageConfig
170 emptyPackageConfigMap :: PackageConfigMap
171 emptyPackageConfigMap = emptyUFM
173 lookupPackage :: PackageConfigMap -> PackageId -> Maybe PackageConfig
174 lookupPackage = lookupUFM
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
182 getPackageDetails :: PackageState -> PackageId -> PackageConfig
183 getPackageDetails dflags ps = expectJust "getPackageDetails" (lookupPackage (pkgIdMap dflags) ps)
185 -- ----------------------------------------------------------------------------
186 -- Loading the package config files and building up the package state
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 }
198 -- -----------------------------------------------------------------------------
199 -- Reading the package database(s)
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
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
211 (exists, pkgconf) <- catch (do
212 appdir <- getAppUserDataDirectory "ghc"
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
225 -- Read all the ones mentioned in -package-conf flags
226 pkg_map <- foldM (readPackageConfig dflags) pkg_map2
227 (extraPkgConfs dflags)
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
238 let pkg_configs1 = mungePackagePaths top_dir proto_pkg_configs
239 pkg_configs2 = maybeHidePackages dflags pkg_configs1
240 return (extendPackageConfigMap pkg_map pkg_configs2)
242 maybeHidePackages :: DynFlags -> [PackageConfig] -> [PackageConfig]
243 maybeHidePackages dflags pkgs
244 | dopt Opt_HideAllPackages dflags = map hide pkgs
247 hide pkg = pkg{ exposed = False }
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
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) }
259 munge_paths = map munge_path
262 | Just p' <- maybePrefixMatch "$topdir" p = top_dir ++ p'
266 -- -----------------------------------------------------------------------------
267 -- When all the command-line options are in, we can process our package
268 -- settings and populate the package state.
270 mkPackageState :: DynFlags -> PackageConfigMap -> IO PackageState
271 mkPackageState dflags orig_pkg_db = do
273 -- Modify the package database according to the command-line flags
274 -- (-package, -hide-package, -ignore-package, -hide-all-packages).
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.
283 flags = reverse (packageFlags dflags)
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.
306 -- A package named on the command line can either include the
307 -- version, or just the name if it is unambiguous.
309 = str == showPackageId (package p)
310 || str == pkgName (package p)
312 (pkgs1,explicit) <- procflags (eltsUFM orig_pkg_db) emptyUniqSet flags
315 elimDanglingDeps pkgs =
316 case partition (hasDanglingDeps pkgs) pkgs of
318 (ps,qs) -> elimDanglingDeps qs
320 hasDanglingDeps pkgs p = any dangling (depends p)
321 where dangling pid = pid `notElem` all_pids
322 all_pids = map package pkgs
324 -- Eliminate any packages which have dangling dependencies (perhaps
325 -- because the package was removed by -ignore-package).
327 let pkgs = elimDanglingDeps pkgs1
328 pkg_db = extendPackageConfigMap emptyPackageConfigMap pkgs
330 -- Find the transitive closure of dependencies of exposed
332 let exposed_pkgids = [ packageConfigId p | p <- pkgs, exposed p ]
333 dep_exposed <- closeDeps pkg_db exposed_pkgids
335 -- Look up some known PackageIds
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
344 (p:ps) -> ExtPackage (mkPackageId (package p))
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
354 -- add base & rts to the explicit packages
355 basicLinkedPackages = [basePackageId,rtsPackageId]
356 explicit' = addListToUniqSet explicit
357 [ p | ExtPackage p <- basicLinkedPackages ]
359 -- Close the explicit packages with their dependencies
361 dep_explicit <- closeDeps pkg_db (uniqSetToList explicit')
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.
367 let mod_map = mkModuleMap orig_pkg_db dep_exposed
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
379 basePackageName = FSLIT("base")
380 rtsPackageName = FSLIT("rts")
381 haskell98PackageName = FSLIT("haskell98")
382 thPackageName = FSLIT("template-haskell")
383 -- Template Haskell libraries in here
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))
395 -> ModuleEnv [(PackageConfig, Bool)]
396 mkModuleMap pkg_db pkgs = foldr extend_modmap emptyUFM pkgs
398 extend_modmap pkgname modmap =
399 addListToUFM_C (++) modmap
400 [(m, [(pkg, m `elem` exposed_mods)]) | m <- all_mods]
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
407 -- -----------------------------------------------------------------------------
408 -- Check for conflicts in the program.
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").
415 checkForPackageConflicts
417 -> [Module] -- modules in the home package
418 -> [PackageId] -- packages on which the program depends
419 -> MaybeErr Message ()
421 checkForPackageConflicts dflags mods pkgs = do
423 state = pkgState dflags
424 pkg_db = pkgIdMap state
426 dep_pkgs <- closeDepsErr pkg_db pkgs
429 extend_modmap pkgname modmap =
430 addListToFM_C (++) modmap
431 [(m, [(pkg, m `elem` exposed_mods)]) | m <- all_mods]
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
439 mod_map = foldr extend_modmap emptyFM pkgs
440 mod_map_list :: [(Module,[(PackageConfig,Bool)])]
441 mod_map_list = fmToList mod_map
443 overlaps = [ (m, map fst ps) | (m,ps@(_:_:_)) <- mod_map_list ]
445 if not (null overlaps)
446 then Failed (pkgOverlapError overlaps)
450 overlap_mods = [ (mod,pkg)
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)
460 pkgOverlapError overlaps = vcat (map msg overlaps)
463 text "conflict: module" <+> quotes (ppr mod)
464 <+> ptext SLIT("is present in multiple packages:")
465 <+> hsep (punctuate comma (map (text.showPackageId.package) pkgs))
467 modOverlapError overlaps = vcat (map msg overlaps)
469 msg (mod,pkg) = fsep [
470 text "conflict: module",
472 ptext SLIT("belongs to the current program/library"),
473 ptext SLIT("and also to package"),
474 text (showPackageId (package pkg)) ]
476 -- -----------------------------------------------------------------------------
477 -- Extracting information from the packages in scope
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
487 getPackageIncludePath :: DynFlags -> [PackageId] -> IO [String]
488 getPackageIncludePath dflags pkgs = do
489 ps <- getExplicitPackagesAnd dflags pkgs
490 return (nub (filter notNull (concatMap includeDirs ps)))
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))))
497 getPackageLibraryPath :: DynFlags -> [PackageId] -> IO [String]
498 getPackageLibraryPath dflags pkgs = do
499 ps <- getExplicitPackagesAnd dflags pkgs
500 return (nub (filter notNull (concatMap libraryDirs ps)))
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
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
512 suffix = if null tag then "" else '_':tag
513 rts_suffix = if null rts_tag then "" else '_':rts_tag
515 addSuffix rts@"HSrts" = rts ++ rts_suffix
516 addSuffix other_lib = other_lib ++ suffix
518 return (concat (map all_opts ps))
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]
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]
539 # if !defined(mingw32_TARGET_OS) && !defined(cygwin32_TARGET_OS)
542 = if "HSbase1" `elem` libs && "HSbase2" `elem` libs && "HSbase3" `elem` libs
543 then "HSbase" : filter (not.(isPrefixOf "HSbase")) libs
545 if "HSwin_321" `elem` libs && "HSwin_322" `elem` libs
546 then "HSwin_32" : filter (not.(isPrefixOf "HSwin_32")) libs
548 if "HSobjectio1" `elem` libs && "HSobjectio2" `elem` libs && "HSobjectio3" `elem` libs && "HSobjectio4" `elem` libs
549 then "HSobjectio" : filter (not.(isPrefixOf "HSobjectio")) libs
554 getPackageExtraCcOpts :: DynFlags -> [PackageId] -> IO [String]
555 getPackageExtraCcOpts dflags pkgs = do
556 ps <- getExplicitPackagesAnd dflags pkgs
557 return (concatMap ccOptions ps)
559 getPackageFrameworkPath :: DynFlags -> [PackageId] -> IO [String]
560 getPackageFrameworkPath dflags pkgs = do
561 ps <- getExplicitPackagesAnd dflags pkgs
562 return (nub (filter notNull (concatMap frameworkDirs ps)))
564 getPackageFrameworks :: DynFlags -> [PackageId] -> IO [String]
565 getPackageFrameworks dflags pkgs = do
566 ps <- getExplicitPackagesAnd dflags pkgs
567 return (concatMap frameworks ps)
569 -- -----------------------------------------------------------------------------
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
581 getExplicitPackagesAnd :: DynFlags -> [PackageId] -> IO [PackageConfig]
582 getExplicitPackagesAnd dflags pkgids =
584 state = pkgState dflags
585 pkg_map = pkgIdMap state
586 expl = explicitPackages state
588 all_pkgs <- throwErr (foldM (add_package pkg_map) expl pkgids)
589 return (map (getPackageDetails state) all_pkgs)
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)
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
601 closeDepsErr :: PackageConfigMap -> [PackageId]
602 -> MaybeErr Message [PackageId]
603 closeDepsErr pkg_map ps = foldM (add_package pkg_map) [] ps
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
611 case lookupPackage pkg_db p of
612 Nothing -> Failed (missingPackageErr (packageIdString p))
614 -- Add the package's dependents also
615 let deps = map mkPackageId (depends pkg)
616 ps' <- foldM (add_package pkg_db) ps deps
619 missingPackageErr p = throwDyn (CmdLineError (showSDoc (missingPackageMsg p)))
620 missingPackageMsg p = ptext SLIT("unknown package:") <> text p
622 -- -----------------------------------------------------------------------------
623 -- The home module set
625 newtype HomeModules = HomeModules ModuleSet
627 mkHomeModules :: [Module] -> HomeModules
628 mkHomeModules = HomeModules . mkModuleSet
630 isHomeModule :: HomeModules -> Module -> Bool
631 isHomeModule (HomeModules set) mod = elemModuleSet mod set
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.
637 isDllName :: HomeModules -> Name -> Bool
640 | Just mod <- nameModule_maybe name = not (isHomeModule pdeps mod)
641 | otherwise = False -- no, it is not even an external name
643 -- -----------------------------------------------------------------------------
644 -- Displaying packages
646 dumpPackages :: DynFlags -> IO ()
647 -- Show package info on console, if verbosity is >= 3
649 = do let pkg_map = pkgIdMap (pkgState dflags)
651 vcat (map (text.showInstalledPackageInfo) (eltsUFM pkg_map))