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