3fdb0af1d337e59925a7ebe8557f6c62398666c2
[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 -- * UnitId
13 packageConfigId,
14
15 -- * The PackageConfig type: information about a package
16 PackageConfig,
17 InstalledPackageInfo(..),
18 ComponentId(..),
19 SourcePackageId(..),
20 PackageName(..),
21 Version(..),
22 defaultPackageConfig,
23 componentIdString,
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 ghc-boot,
41 -- which is similar to a subset of the InstalledPackageInfo type from Cabal.
42
43 type PackageConfig = InstalledPackageInfo
44 ComponentId
45 SourcePackageId
46 PackageName
47 Module.UnitId
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 ComponentId = ComponentId FastString deriving (Eq, Ord)
55 newtype SourcePackageId = SourcePackageId FastString deriving (Eq, Ord)
56 newtype PackageName = PackageName FastString deriving (Eq, Ord)
57
58 instance BinaryStringRep ComponentId where
59 fromStringRep = ComponentId . mkFastStringByteString
60 toStringRep (ComponentId 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 ComponentId where
71 getUnique (ComponentId 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 ComponentId where
80 ppr (ComponentId 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 componentIdString :: PackageConfig -> String
111 componentIdString pkg = unpackFS str
112 where
113 ComponentId str = componentId 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 componentId),
131 field "exposed" (ppr exposed),
132 field "exposed-modules"
133 (if all isExposedModule exposedModules
134 then fsep (map pprExposedModule exposedModules)
135 else pprWithCommas pprExposedModule exposedModules),
136 field "hidden-modules" (fsep (map ppr hiddenModules)),
137 field "trusted" (ppr trusted),
138 field "import-dirs" (fsep (map text importDirs)),
139 field "library-dirs" (fsep (map text libraryDirs)),
140 field "hs-libraries" (fsep (map text hsLibraries)),
141 field "extra-libraries" (fsep (map text extraLibraries)),
142 field "extra-ghci-libraries" (fsep (map text extraGHCiLibraries)),
143 field "include-dirs" (fsep (map text includeDirs)),
144 field "includes" (fsep (map text includes)),
145 field "depends" (fsep (map ppr depends)),
146 field "cc-options" (fsep (map text ccOptions)),
147 field "ld-options" (fsep (map text ldOptions)),
148 field "framework-dirs" (fsep (map text frameworkDirs)),
149 field "frameworks" (fsep (map text frameworks)),
150 field "haddock-interfaces" (fsep (map text haddockInterfaces)),
151 field "haddock-html" (fsep (map text haddockHTMLs))
152 ]
153 where
154 field name body = text name <> colon <+> nest 4 body
155 isExposedModule (ExposedModule _ Nothing Nothing) = True
156 isExposedModule _ = False
157
158
159 -- -----------------------------------------------------------------------------
160 -- UnitId (package names, versions and dep hash)
161
162 -- $package_naming
163 -- #package_naming#
164 -- Mostly the compiler deals in terms of 'UnitId's, which are md5 hashes
165 -- of a package ID, keys of its dependencies, and Cabal flags. You're expected
166 -- to pass in the unit id in the @-this-package-key@ flag. However, for
167 -- wired-in packages like @base@ & @rts@, we don't necessarily know what the
168 -- version is, so these are handled specially; see #wired_in_packages#.
169
170 -- | Get the GHC 'UnitId' right out of a Cabalish 'PackageConfig'
171 packageConfigId :: PackageConfig -> UnitId
172 packageConfigId = unitId