Use ghc-local types for packages, rather than Cabal types
[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 packageConfigId,
14
15 -- * The PackageConfig type: information about a package
16 PackageConfig,
17 InstalledPackageInfo(..),
18 InstalledPackageId(..),
19 SourcePackageId(..),
20 PackageName(..),
21 Version(..),
22 defaultPackageConfig,
23 installedPackageIdString,
24 sourcePackageIdString,
25 packageNameString,
26 showInstalledPackageInfo,
27 ) where
28
29 #include "HsVersions.h"
30
31 import GHC.PackageDb
32 import qualified Data.ByteString.Char8 as BS
33 import Data.Version
34
35 import Outputable
36 import Module
37
38 -- -----------------------------------------------------------------------------
39 -- Our PackageConfig type is the InstalledPackageInfo from bin-package-db,
40 -- which is similar to a subset of the InstalledPackageInfo type from Cabal.
41
42 type PackageConfig = InstalledPackageInfo
43 InstalledPackageId
44 SourcePackageId
45 PackageName
46 Module.PackageKey
47 Module.ModuleName
48
49 newtype InstalledPackageId = InstalledPackageId String deriving (Eq, Ord, Show)
50 newtype SourcePackageId = SourcePackageId String deriving (Eq, Ord, Show)
51 newtype PackageName = PackageName String deriving (Eq, Ord, Show)
52
53 instance BinaryStringRep InstalledPackageId where
54 fromStringRep = InstalledPackageId . BS.unpack
55 toStringRep (InstalledPackageId s) = BS.pack s
56
57 instance BinaryStringRep SourcePackageId where
58 fromStringRep = SourcePackageId . BS.unpack
59 toStringRep (SourcePackageId s) = BS.pack s
60
61 instance BinaryStringRep PackageName where
62 fromStringRep = PackageName . BS.unpack
63 toStringRep (PackageName s) = BS.pack s
64
65 instance BinaryStringRep PackageKey where
66 fromStringRep = Module.stringToPackageKey . BS.unpack
67 toStringRep = BS.pack . Module.packageKeyString
68
69 instance BinaryStringRep Module.ModuleName where
70 fromStringRep = Module.mkModuleName . BS.unpack
71 toStringRep = BS.pack . Module.moduleNameString
72
73 instance Outputable InstalledPackageId where
74 ppr (InstalledPackageId str) = text str
75
76 instance Outputable SourcePackageId where
77 ppr (SourcePackageId str) = text str
78
79 instance Outputable PackageName where
80 ppr (PackageName str) = text str
81
82 defaultPackageConfig :: PackageConfig
83 defaultPackageConfig = emptyInstalledPackageInfo
84
85 installedPackageIdString :: PackageConfig -> String
86 installedPackageIdString pkg = str
87 where
88 InstalledPackageId str = installedPackageId pkg
89
90 sourcePackageIdString :: PackageConfig -> String
91 sourcePackageIdString pkg = str
92 where
93 SourcePackageId str = sourcePackageId pkg
94
95 packageNameString :: PackageConfig -> String
96 packageNameString pkg = str
97 where
98 PackageName str = packageName pkg
99
100 showInstalledPackageInfo :: PackageConfig -> String
101 showInstalledPackageInfo = show
102
103 instance Show ModuleName where
104 show = moduleNameString
105
106 instance Show PackageKey where
107 show = packageKeyString
108
109
110 -- -----------------------------------------------------------------------------
111 -- PackageKey (package names, versions and dep hash)
112
113 -- $package_naming
114 -- #package_naming#
115 -- Mostly the compiler deals in terms of 'PackageKey's, which are md5 hashes
116 -- of a package ID, keys of its dependencies, and Cabal flags. You're expected
117 -- to pass in the package key in the @-this-package-key@ flag. However, for
118 -- wired-in packages like @base@ & @rts@, we don't necessarily know what the
119 -- version is, so these are handled specially; see #wired_in_packages#.
120
121 -- | Get the GHC 'PackageKey' right out of a Cabalish 'PackageConfig'
122 packageConfigId :: PackageConfig -> PackageKey
123 packageConfigId = packageKey
124