2 -- (c) The University of Glasgow, 2004
5 -- | Package configuration information: essentially the interface to Cabal, with some utilities
10 mkPackageId, packageConfigId, unpackPackageId,
12 -- * The PackageConfig type: information about a package
14 InstalledPackageInfo_(..), display,
16 PackageIdentifier(..),
18 packageConfigToInstalledPackageInfo,
19 installedPackageInfoToPackageConfig,
22 #include "HsVersions.h"
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
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.
37 type PackageConfig = InstalledPackageInfo_ Module.ModuleName
38 defaultPackageConfig :: PackageConfig
39 defaultPackageConfig = emptyInstalledPackageInfo
41 -- -----------------------------------------------------------------------------
42 -- PackageId (package names with versions)
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.
52 -- This means that in theory you could have multiple base packages installed
53 -- (for example), and switch between them using @-package@\/@-hide-package@.
55 -- A 'PackageId' is a string of the form @<pkg>-<version>@.
57 -- | Turn a Cabal 'PackageIdentifier' into a GHC 'PackageId'
58 mkPackageId :: PackageIdentifier -> PackageId
59 mkPackageId = stringToPackageId . display
61 -- | Get the GHC 'PackageId' right out of a Cabalish 'PackageConfig'
62 packageConfigId :: PackageConfig -> PackageId
63 packageConfigId = mkPackageId . package
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
69 = case [ pid | (pid,"") <- readP_to_S parse str ] of
72 where str = packageIdString p
74 -- | Turn a 'PackageConfig', which contains GHC 'Module.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 = (expectJust "packageConfigToInstalledPackageInfo") . simpleParse . moduleNameString
85 -- | Turn an 'InstalledPackageInfo', which contains Cabal 'Distribution.ModuleName.ModuleName's
86 -- into a GHC specific 'PackageConfig' which contains GHC 'Module.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