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