Simplify ghc-boot database representation with new type class.
[ghc.git] / compiler / main / PackageConfig.hs
1 {-# LANGUAGE CPP, RecordWildCards, MultiParamTypeClasses #-}
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 Module.Module
48
49 -- TODO: there's no need for these to be FastString, as we don't need the uniq
50 -- feature, but ghc doesn't currently have convenient support for any
51 -- other compact string types, e.g. plain ByteString or Text.
52
53 newtype ComponentId = ComponentId FastString deriving (Eq, Ord)
54 newtype SourcePackageId = SourcePackageId FastString deriving (Eq, Ord)
55 newtype PackageName = PackageName FastString deriving (Eq, Ord)
56
57 instance BinaryStringRep ComponentId where
58 fromStringRep = ComponentId . mkFastStringByteString
59 toStringRep (ComponentId s) = fastStringToByteString s
60
61 instance BinaryStringRep SourcePackageId where
62 fromStringRep = SourcePackageId . mkFastStringByteString
63 toStringRep (SourcePackageId s) = fastStringToByteString s
64
65 instance BinaryStringRep PackageName where
66 fromStringRep = PackageName . mkFastStringByteString
67 toStringRep (PackageName s) = fastStringToByteString s
68
69 instance Uniquable ComponentId where
70 getUnique (ComponentId n) = getUnique n
71
72 instance Uniquable SourcePackageId where
73 getUnique (SourcePackageId n) = getUnique n
74
75 instance Uniquable PackageName where
76 getUnique (PackageName n) = getUnique n
77
78 instance Outputable ComponentId where
79 ppr (ComponentId str) = ftext str
80
81 instance Outputable SourcePackageId where
82 ppr (SourcePackageId str) = ftext str
83
84 instance Outputable PackageName where
85 ppr (PackageName str) = ftext str
86
87 defaultPackageConfig :: PackageConfig
88 defaultPackageConfig = emptyInstalledPackageInfo
89
90 sourcePackageIdString :: PackageConfig -> String
91 sourcePackageIdString pkg = unpackFS str
92 where
93 SourcePackageId str = sourcePackageId pkg
94
95 packageNameString :: PackageConfig -> String
96 packageNameString pkg = unpackFS str
97 where
98 PackageName str = packageName pkg
99
100 pprPackageConfig :: PackageConfig -> SDoc
101 pprPackageConfig InstalledPackageInfo {..} =
102 vcat [
103 field "name" (ppr packageName),
104 field "version" (text (showVersion packageVersion)),
105 field "id" (ppr unitId),
106 field "exposed" (ppr exposed),
107 field "exposed-modules" (ppr exposedModules),
108 field "hidden-modules" (fsep (map ppr hiddenModules)),
109 field "trusted" (ppr trusted),
110 field "import-dirs" (fsep (map text importDirs)),
111 field "library-dirs" (fsep (map text libraryDirs)),
112 field "hs-libraries" (fsep (map text hsLibraries)),
113 field "extra-libraries" (fsep (map text extraLibraries)),
114 field "extra-ghci-libraries" (fsep (map text extraGHCiLibraries)),
115 field "include-dirs" (fsep (map text includeDirs)),
116 field "includes" (fsep (map text includes)),
117 field "depends" (fsep (map ppr depends)),
118 field "cc-options" (fsep (map text ccOptions)),
119 field "ld-options" (fsep (map text ldOptions)),
120 field "framework-dirs" (fsep (map text frameworkDirs)),
121 field "frameworks" (fsep (map text frameworks)),
122 field "haddock-interfaces" (fsep (map text haddockInterfaces)),
123 field "haddock-html" (fsep (map text haddockHTMLs))
124 ]
125 where
126 field name body = text name <> colon <+> nest 4 body
127
128
129 -- -----------------------------------------------------------------------------
130 -- UnitId (package names, versions and dep hash)
131
132 -- $package_naming
133 -- #package_naming#
134 -- Mostly the compiler deals in terms of 'UnitId's, which are md5 hashes
135 -- of a package ID, keys of its dependencies, and Cabal flags. You're expected
136 -- to pass in the unit id in the @-this-unit-id@ flag. However, for
137 -- wired-in packages like @base@ & @rts@, we don't necessarily know what the
138 -- version is, so these are handled specially; see #wired_in_packages#.
139
140 -- | Get the GHC 'UnitId' right out of a Cabalish 'PackageConfig'
141 packageConfigId :: PackageConfig -> UnitId
142 packageConfigId = unitId