Typos in comments
[ghc.git] / compiler / main / PackageConfig.hs
1 {-# LANGUAGE CPP, RecordWildCards, FlexibleInstances, 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 expandedPackageConfigId,
15 definitePackageConfigId,
16 installedPackageConfigId,
17
18 -- * The PackageConfig type: information about a package
19 PackageConfig,
20 InstalledPackageInfo(..),
21 ComponentId(..),
22 SourcePackageId(..),
23 PackageName(..),
24 Version(..),
25 defaultPackageConfig,
26 sourcePackageIdString,
27 packageNameString,
28 pprPackageConfig,
29 ) where
30
31 #include "HsVersions.h"
32
33 import GHC.PackageDb
34 import Data.Version
35
36 import FastString
37 import Outputable
38 import Module
39 import Unique
40
41 -- -----------------------------------------------------------------------------
42 -- Our PackageConfig type is the InstalledPackageInfo from ghc-boot,
43 -- which is similar to a subset of the InstalledPackageInfo type from Cabal.
44
45 type PackageConfig = InstalledPackageInfo
46 ComponentId
47 SourcePackageId
48 PackageName
49 Module.InstalledUnitId
50 Module.UnitId
51 Module.ModuleName
52 Module.Module
53
54 -- TODO: there's no need for these to be FastString, as we don't need the uniq
55 -- feature, but ghc doesn't currently have convenient support for any
56 -- other compact string types, e.g. plain ByteString or Text.
57
58 newtype SourcePackageId = SourcePackageId FastString deriving (Eq, Ord)
59 newtype PackageName = PackageName FastString deriving (Eq, Ord)
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 SourcePackageId where
70 getUnique (SourcePackageId n) = getUnique n
71
72 instance Uniquable PackageName where
73 getUnique (PackageName n) = getUnique n
74
75 instance Outputable SourcePackageId where
76 ppr (SourcePackageId str) = ftext str
77
78 instance Outputable PackageName where
79 ppr (PackageName str) = ftext str
80
81 defaultPackageConfig :: PackageConfig
82 defaultPackageConfig = emptyInstalledPackageInfo
83
84 sourcePackageIdString :: PackageConfig -> String
85 sourcePackageIdString pkg = unpackFS str
86 where
87 SourcePackageId str = sourcePackageId pkg
88
89 packageNameString :: PackageConfig -> String
90 packageNameString pkg = unpackFS str
91 where
92 PackageName str = packageName pkg
93
94 pprPackageConfig :: PackageConfig -> SDoc
95 pprPackageConfig InstalledPackageInfo {..} =
96 vcat [
97 field "name" (ppr packageName),
98 field "version" (text (showVersion packageVersion)),
99 field "id" (ppr unitId),
100 field "exposed" (ppr exposed),
101 field "exposed-modules" (ppr exposedModules),
102 field "hidden-modules" (fsep (map ppr hiddenModules)),
103 field "trusted" (ppr trusted),
104 field "import-dirs" (fsep (map text importDirs)),
105 field "library-dirs" (fsep (map text libraryDirs)),
106 field "dynamic-library-dirs" (fsep (map text libraryDynDirs)),
107 field "hs-libraries" (fsep (map text hsLibraries)),
108 field "extra-libraries" (fsep (map text extraLibraries)),
109 field "extra-ghci-libraries" (fsep (map text extraGHCiLibraries)),
110 field "include-dirs" (fsep (map text includeDirs)),
111 field "includes" (fsep (map text includes)),
112 field "depends" (fsep (map ppr depends)),
113 field "cc-options" (fsep (map text ccOptions)),
114 field "ld-options" (fsep (map text ldOptions)),
115 field "framework-dirs" (fsep (map text frameworkDirs)),
116 field "frameworks" (fsep (map text frameworks)),
117 field "haddock-interfaces" (fsep (map text haddockInterfaces)),
118 field "haddock-html" (fsep (map text haddockHTMLs))
119 ]
120 where
121 field name body = text name <> colon <+> nest 4 body
122
123 -- -----------------------------------------------------------------------------
124 -- UnitId (package names, versions and dep hash)
125
126 -- $package_naming
127 -- #package_naming#
128 -- Mostly the compiler deals in terms of 'UnitId's, which are md5 hashes
129 -- of a package ID, keys of its dependencies, and Cabal flags. You're expected
130 -- to pass in the unit id in the @-this-unit-id@ flag. However, for
131 -- wired-in packages like @base@ & @rts@, we don't necessarily know what the
132 -- version is, so these are handled specially; see #wired_in_packages#.
133
134 -- | Get the GHC 'UnitId' right out of a Cabalish 'PackageConfig'
135 installedPackageConfigId :: PackageConfig -> InstalledUnitId
136 installedPackageConfigId = unitId
137
138 packageConfigId :: PackageConfig -> UnitId
139 packageConfigId p =
140 if indefinite p
141 then newUnitId (componentId p) (instantiatedWith p)
142 else DefiniteUnitId (DefUnitId (unitId p))
143
144 expandedPackageConfigId :: PackageConfig -> UnitId
145 expandedPackageConfigId p =
146 newUnitId (componentId p) (instantiatedWith p)
147
148 definitePackageConfigId :: PackageConfig -> Maybe DefUnitId
149 definitePackageConfigId p =
150 case packageConfigId p of
151 DefiniteUnitId def_uid -> Just def_uid
152 _ -> Nothing