Follow Cabal changes
[ghc-hetmet.git] / compiler / main / PackageConfig.hs
1 --
2 -- (c) The University of Glasgow, 2004
3 --
4
5 module PackageConfig (
6         -- * PackageId
7         mkPackageId, packageConfigId, unpackPackageId,
8         
9         -- * The PackageConfig type: information about a package
10         PackageConfig,
11         InstalledPackageInfo_(..), display,
12         Version(..),
13         PackageIdentifier(..),
14         defaultPackageConfig,
15     packageConfigToInstalledPackageInfo,
16     installedPackageInfoToPackageConfig,
17   ) where
18
19 #include "HsVersions.h"
20
21 import Data.Maybe
22 import Module
23 import Distribution.InstalledPackageInfo
24 import Distribution.ModuleName
25 import Distribution.Package
26 import Distribution.Text
27 import Distribution.Version
28 import Distribution.Compat.ReadP
29
30 -- -----------------------------------------------------------------------------
31 -- Our PackageConfig type is just InstalledPackageInfo from Cabal.  Later we
32 -- might need to extend it with some GHC-specific stuff, but for now it's fine.
33
34 type PackageConfig = InstalledPackageInfo_ Module.ModuleName
35 defaultPackageConfig :: PackageConfig
36 defaultPackageConfig = emptyInstalledPackageInfo
37
38 -- -----------------------------------------------------------------------------
39 -- PackageId (package names with versions)
40
41 -- Mostly the compiler deals in terms of PackageNames, which don't
42 -- have the version suffix.  This is so that we don't need to know the
43 -- version for the -package-name flag, or know the versions of
44 -- wired-in packages like base & rts.  Versions are confined to the
45 -- package sub-system.
46 --
47 -- This means that in theory you could have multiple base packages installed
48 -- (for example), and switch between them using -package/-hide-package.
49 --
50 -- A PackageId is a string of the form <pkg>-<version>.
51
52 mkPackageId :: PackageIdentifier -> PackageId
53 mkPackageId = stringToPackageId . display
54
55 packageConfigId :: PackageConfig -> PackageId
56 packageConfigId = mkPackageId . package
57
58 unpackPackageId :: PackageId -> Maybe PackageIdentifier
59 unpackPackageId p
60   = case [ pid | (pid,"") <- readP_to_S parse str ] of
61         []      -> Nothing
62         (pid:_) -> Just pid
63   where str = packageIdString p
64
65 packageConfigToInstalledPackageInfo :: PackageConfig -> InstalledPackageInfo
66 packageConfigToInstalledPackageInfo
67     (pkgconf@(InstalledPackageInfo { exposedModules = e,
68                                      hiddenModules = h })) =
69         pkgconf{ exposedModules = map convert e,
70                  hiddenModules  = map convert h }
71     where convert :: Module.ModuleName -> Distribution.ModuleName.ModuleName
72           convert = fromJust . simpleParse . moduleNameString
73
74 installedPackageInfoToPackageConfig :: InstalledPackageInfo -> PackageConfig
75 installedPackageInfoToPackageConfig
76     (pkgconf@(InstalledPackageInfo { exposedModules = e,
77                                      hiddenModules = h })) =
78         pkgconf{ exposedModules = map convert e,
79                  hiddenModules  = map convert h }
80     where convert :: Distribution.ModuleName.ModuleName -> Module.ModuleName
81           convert = mkModuleName . display