f3cdac77da682c842bf25e631eb3b78bb325aa94
[ghc.git] / compiler / main / PackageConfig.hs
1 {-# LANGUAGE CPP, RecordWildCards #-}
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 pprPackageConfig,
27 ) where
28
29 #include "HsVersions.h"
30
31 import GHC.PackageDb
32 import Data.Version
33
34 import FastString
35 import Outputable
36 import Module
37 import Unique
38
39 -- -----------------------------------------------------------------------------
40 -- Our PackageConfig type is the InstalledPackageInfo from bin-package-db,
41 -- which is similar to a subset of the InstalledPackageInfo type from Cabal.
42
43 type PackageConfig = InstalledPackageInfo
44 InstalledPackageId
45 SourcePackageId
46 PackageName
47 Module.PackageKey
48 Module.ModuleName
49
50 -- TODO: there's no need for these to be FastString, as we don't need the uniq
51 -- feature, but ghc doesn't currently have convenient support for any
52 -- other compact string types, e.g. plain ByteString or Text.
53
54 newtype InstalledPackageId = InstalledPackageId FastString deriving (Eq, Ord)
55 newtype SourcePackageId = SourcePackageId FastString deriving (Eq, Ord)
56 newtype PackageName = PackageName FastString deriving (Eq, Ord)
57
58 instance BinaryStringRep InstalledPackageId where
59 fromStringRep = InstalledPackageId . mkFastStringByteString
60 toStringRep (InstalledPackageId s) = fastStringToByteString s
61
62 instance BinaryStringRep SourcePackageId where
63 fromStringRep = SourcePackageId . mkFastStringByteString
64 toStringRep (SourcePackageId s) = fastStringToByteString s
65
66 instance BinaryStringRep PackageName where
67 fromStringRep = PackageName . mkFastStringByteString
68 toStringRep (PackageName s) = fastStringToByteString s
69
70 instance Uniquable InstalledPackageId where
71 getUnique (InstalledPackageId n) = getUnique n
72
73 instance Uniquable SourcePackageId where
74 getUnique (SourcePackageId n) = getUnique n
75
76 instance Uniquable PackageName where
77 getUnique (PackageName n) = getUnique n
78
79 instance Outputable InstalledPackageId where
80 ppr (InstalledPackageId str) = ftext str
81
82 instance Outputable SourcePackageId where
83 ppr (SourcePackageId str) = ftext str
84
85 instance Outputable PackageName where
86 ppr (PackageName str) = ftext str
87
88 -- | Pretty-print an 'ExposedModule' in the same format used by the textual
89 -- installed package database.
90 pprExposedModule :: (Outputable a, Outputable b) => ExposedModule a b -> SDoc
91 pprExposedModule (ExposedModule exposedName exposedReexport exposedSignature) =
92 sep [ ppr exposedName
93 , case exposedReexport of
94 Just m -> sep [text "from", pprOriginalModule m]
95 Nothing -> empty
96 , case exposedSignature of
97 Just m -> sep [text "is", pprOriginalModule m]
98 Nothing -> empty
99 ]
100
101 -- | Pretty-print an 'OriginalModule' in the same format used by the textual
102 -- installed package database.
103 pprOriginalModule :: (Outputable a, Outputable b) => OriginalModule a b -> SDoc
104 pprOriginalModule (OriginalModule originalPackageId originalModuleName) =
105 ppr originalPackageId <> char ':' <> ppr originalModuleName
106
107 defaultPackageConfig :: PackageConfig
108 defaultPackageConfig = emptyInstalledPackageInfo
109
110 installedPackageIdString :: PackageConfig -> String
111 installedPackageIdString pkg = unpackFS str
112 where
113 InstalledPackageId str = installedPackageId pkg
114
115 sourcePackageIdString :: PackageConfig -> String
116 sourcePackageIdString pkg = unpackFS str
117 where
118 SourcePackageId str = sourcePackageId pkg
119
120 packageNameString :: PackageConfig -> String
121 packageNameString pkg = unpackFS str
122 where
123 PackageName str = packageName pkg
124
125 pprPackageConfig :: PackageConfig -> SDoc
126 pprPackageConfig InstalledPackageInfo {..} =
127 vcat [
128 field "name" (ppr packageName),
129 field "version" (text (showVersion packageVersion)),
130 field "id" (ppr installedPackageId),
131 field "key" (ppr packageKey),
132 field "exposed" (ppr exposed),
133 field "exposed-modules"
134 (if all isExposedModule exposedModules
135 then fsep (map pprExposedModule exposedModules)
136 else pprWithCommas pprExposedModule exposedModules),
137 field "hidden-modules" (fsep (map ppr hiddenModules)),
138 field "trusted" (ppr trusted),
139 field "import-dirs" (fsep (map text importDirs)),
140 field "library-dirs" (fsep (map text libraryDirs)),
141 field "hs-libraries" (fsep (map text hsLibraries)),
142 field "extra-libraries" (fsep (map text extraLibraries)),
143 field "extra-ghci-libraries" (fsep (map text extraGHCiLibraries)),
144 field "include-dirs" (fsep (map text includeDirs)),
145 field "includes" (fsep (map text includes)),
146 field "depends" (fsep (map ppr depends)),
147 field "cc-options" (fsep (map text ccOptions)),
148 field "ld-options" (fsep (map text ldOptions)),
149 field "framework-dirs" (fsep (map text frameworkDirs)),
150 field "frameworks" (fsep (map text frameworks)),
151 field "haddock-interfaces" (fsep (map text haddockInterfaces)),
152 field "haddock-html" (fsep (map text haddockHTMLs))
153 ]
154 where
155 field name body = text name <> colon <+> nest 4 body
156 isExposedModule (ExposedModule _ Nothing Nothing) = True
157 isExposedModule _ = False
158
159
160 -- -----------------------------------------------------------------------------
161 -- PackageKey (package names, versions and dep hash)
162
163 -- $package_naming
164 -- #package_naming#
165 -- Mostly the compiler deals in terms of 'PackageKey's, which are md5 hashes
166 -- of a package ID, keys of its dependencies, and Cabal flags. You're expected
167 -- to pass in the package key in the @-this-package-key@ flag. However, for
168 -- wired-in packages like @base@ & @rts@, we don't necessarily know what the
169 -- version is, so these are handled specially; see #wired_in_packages#.
170
171 -- | Get the GHC 'PackageKey' right out of a Cabalish 'PackageConfig'
172 packageConfigId :: PackageConfig -> PackageKey
173 packageConfigId = packageKey