864980be9da55f2e46ab072359886ad261ef9b3d
[ghc.git] / compiler / main / PackageConfig.hs
1 {-# LANGUAGE CPP #-}
2
3 -- |
4 -- Package configuration information: essentially the interface to Cabal, with
5 -- some utilities
6 --
7 -- (c) The University of Glasgow, 2004
8 --
9 module PackageConfig (
10 -- $package_naming
11
12 -- * PackageKey
13 mkPackageKey, packageConfigId,
14
15 -- * The PackageConfig type: information about a package
16 PackageConfig,
17 InstalledPackageInfo_(..), display,
18 Version(..),
19 PackageIdentifier(..),
20 defaultPackageConfig,
21 packageConfigToInstalledPackageInfo,
22 installedPackageInfoToPackageConfig
23 ) where
24
25 #include "HsVersions.h"
26
27 import Distribution.InstalledPackageInfo
28 import Distribution.ModuleName
29 import Distribution.Package hiding (PackageKey, mkPackageKey)
30 import qualified Distribution.Package as Cabal
31 import Distribution.Text
32 import Distribution.Version
33
34 import Maybes
35 import Module
36
37 -- -----------------------------------------------------------------------------
38 -- Our PackageConfig type is just InstalledPackageInfo from Cabal. Later we
39 -- might need to extend it with some GHC-specific stuff, but for now it's fine.
40
41 type PackageConfig = InstalledPackageInfo_ Module.ModuleName
42
43 defaultPackageConfig :: PackageConfig
44 defaultPackageConfig = emptyInstalledPackageInfo
45
46 -- -----------------------------------------------------------------------------
47 -- PackageKey (package names, versions and dep hash)
48
49 -- $package_naming
50 -- #package_naming#
51 -- Mostly the compiler deals in terms of 'PackageKey's, which are md5 hashes
52 -- of a package ID, keys of its dependencies, and Cabal flags. You're expected
53 -- to pass in the package key in the @-this-package-key@ flag. However, for
54 -- wired-in packages like @base@ & @rts@, we don't necessarily know what the
55 -- version is, so these are handled specially; see #wired_in_packages#.
56
57 -- | Turn a Cabal 'PackageIdentifier' into a GHC 'PackageKey'
58 mkPackageKey :: Cabal.PackageKey -> PackageKey
59 mkPackageKey = stringToPackageKey . display
60
61 -- | Get the GHC 'PackageKey' right out of a Cabalish 'PackageConfig'
62 packageConfigId :: PackageConfig -> PackageKey
63 packageConfigId = mkPackageKey . packageKey
64
65 -- | Turn a 'PackageConfig', which contains GHC 'Module.ModuleName's into a Cabal specific
66 -- 'InstalledPackageInfo' which contains Cabal 'Distribution.ModuleName.ModuleName's
67 packageConfigToInstalledPackageInfo :: PackageConfig -> InstalledPackageInfo
68 packageConfigToInstalledPackageInfo
69 (pkgconf@(InstalledPackageInfo { exposedModules = e,
70 reexportedModules = r,
71 hiddenModules = h })) =
72 pkgconf{ exposedModules = map convert e,
73 reexportedModules = map (fmap convert) r,
74 hiddenModules = map convert h }
75 where convert :: Module.ModuleName -> Distribution.ModuleName.ModuleName
76 convert = (expectJust "packageConfigToInstalledPackageInfo") . simpleParse . moduleNameString
77
78 -- | Turn an 'InstalledPackageInfo', which contains Cabal 'Distribution.ModuleName.ModuleName's
79 -- into a GHC specific 'PackageConfig' which contains GHC 'Module.ModuleName's
80 installedPackageInfoToPackageConfig :: InstalledPackageInfo_ String -> PackageConfig
81 installedPackageInfoToPackageConfig
82 (pkgconf@(InstalledPackageInfo { exposedModules = e,
83 reexportedModules = r,
84 hiddenModules = h })) =
85 pkgconf{ exposedModules = map mkModuleName e,
86 reexportedModules = map (fmap mkModuleName) r,
87 hiddenModules = map mkModuleName h }
88