Add iserv library (#504)
[hadrian.git] / src / GHC.hs
1 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
2 module GHC (
3 -- * GHC packages
4 array, base, binary, bytestring, cabal, compareSizes, compiler, containers,
5 deepseq, deriveConstants, directory, filepath, genapply, genprimopcode, ghc,
6 ghcBoot, ghcBootTh, ghcCabal, ghcCompact, ghci, ghcPkg, ghcPrim, ghcTags,
7 ghcSplit, haddock, haskeline, hsc2hs, hp2ps, hpc, hpcBin, integerGmp,
8 integerSimple, iservBin, iservLib, libffi, mtl, parsec, parallel, pretty,
9 primitive, process, rts, runGhc, stm, templateHaskell, terminfo, text, time,
10 touchy, transformers, unlit, unix, win32, xhtml, ghcPackages, isGhcPackage,
11 defaultPackages,
12
13 -- * Package information
14 programName, nonCabalContext, nonHsMainPackage, autogenPath, installStage,
15
16 -- * Miscellaneous
17 programPath, buildDll0
18 ) where
19
20 import Base
21 import CommandLine
22 import Context
23 import Oracles.Flag
24 import Oracles.Setting
25
26 -- | These are all GHC packages we know about. Build rules will be generated for
27 -- all of them. However, not all of these packages will be built. For example,
28 -- package 'win32' is built only on Windows. 'defaultPackages' defines default
29 -- conditions for building each package. Users can add their own packages and
30 -- modify build default build conditions in "UserSettings".
31 ghcPackages :: [Package]
32 ghcPackages =
33 [ array, base, binary, bytestring, cabal, compareSizes, compiler, containers
34 , deepseq, deriveConstants, directory, filepath, genapply, genprimopcode
35 , ghc, ghcBoot, ghcBootTh, ghcCabal, ghcCompact, ghci, ghcPkg, ghcPrim
36 , ghcTags, haddock, haskeline, hsc2hs, hp2ps, hpc, hpcBin, integerGmp
37 , integerSimple, iservBin, iservLib, libffi, mtl, parsec, parallel, pretty
38 , primitive, process, rts, runGhc, stm, templateHaskell, terminfo, text
39 , time, touchy, transformers, unlit, unix, win32, xhtml ]
40
41 -- TODO: Optimise by switching to sets of packages.
42 isGhcPackage :: Package -> Bool
43 isGhcPackage = (`elem` ghcPackages)
44
45 -- | Package definitions, see 'Package'.
46 array = hsLib "array"
47 base = hsLib "base"
48 binary = hsLib "binary"
49 bytestring = hsLib "bytestring"
50 cabal = hsLib "Cabal" `setPath` "libraries/Cabal/Cabal"
51 compareSizes = hsUtil "compareSizes" `setPath` "utils/compare_sizes"
52 compiler = hsTop "ghc" `setPath` "compiler"
53 containers = hsLib "containers"
54 deepseq = hsLib "deepseq"
55 deriveConstants = hsUtil "deriveConstants"
56 directory = hsLib "directory"
57 filepath = hsLib "filepath"
58 genapply = hsUtil "genapply"
59 genprimopcode = hsUtil "genprimopcode"
60 ghc = hsPrg "ghc-bin" `setPath` "ghc"
61 ghcBoot = hsLib "ghc-boot"
62 ghcBootTh = hsLib "ghc-boot-th"
63 ghcCabal = hsUtil "ghc-cabal"
64 ghcCompact = hsLib "ghc-compact"
65 ghci = hsLib "ghci"
66 ghcPkg = hsUtil "ghc-pkg"
67 ghcPrim = hsLib "ghc-prim"
68 ghcTags = hsUtil "ghctags"
69 ghcSplit = hsUtil "ghc-split"
70 haddock = hsUtil "haddock"
71 haskeline = hsLib "haskeline"
72 hsc2hs = hsUtil "hsc2hs"
73 hp2ps = cUtil "hp2ps"
74 hpc = hsLib "hpc"
75 hpcBin = hsUtil "hpc-bin" `setPath` "utils/hpc"
76 integerGmp = hsLib "integer-gmp"
77 integerSimple = hsLib "integer-simple"
78 iservBin = hsUtil "iserv"
79 iservLib = hsLib "libiserv"
80 libffi = cTop "libffi"
81 mtl = hsLib "mtl"
82 parsec = hsLib "parsec"
83 parallel = hsLib "parallel"
84 pretty = hsLib "pretty"
85 primitive = hsLib "primitive"
86 process = hsLib "process"
87 rts = cTop "rts"
88 runGhc = hsUtil "runghc"
89 stm = hsLib "stm"
90 templateHaskell = hsLib "template-haskell"
91 terminfo = hsLib "terminfo"
92 text = hsLib "text"
93 time = hsLib "time"
94 touchy = cUtil "touchy"
95 transformers = hsLib "transformers"
96 unlit = cUtil "unlit"
97 unix = hsLib "unix"
98 win32 = hsLib "Win32"
99 xhtml = hsLib "xhtml"
100
101 -- | Construct a Haskell library package, e.g. @array@.
102 hsLib :: PackageName -> Package
103 hsLib name = hsLibrary name ("libraries" -/- name)
104
105 -- | Construct a top-level Haskell library package, e.g. @compiler@.
106 hsTop :: PackageName -> Package
107 hsTop name = hsLibrary name name
108
109 -- | Construct a top-level C library package, e.g. @rts@.
110 cTop :: PackageName -> Package
111 cTop name = cLibrary name name
112
113 -- | Construct a top-level Haskell program package, e.g. @ghc@.
114 hsPrg :: PackageName -> Package
115 hsPrg name = hsProgram name name
116
117 -- | Construct a Haskell utility package, e.g. @haddock@.
118 hsUtil :: PackageName -> Package
119 hsUtil name = hsProgram name ("utils" -/- name)
120
121 -- | Construct a C utility package, e.g. @haddock@.
122 cUtil :: PackageName -> Package
123 cUtil name = cProgram name ("utils" -/- name)
124
125 -- | Amend a package path if it doesn't conform to a typical pattern.
126 setPath :: Package -> FilePath -> Package
127 setPath pkg path = pkg { pkgPath = path }
128
129 -- | Packages that are built by default. You can change this in "UserSettings".
130 defaultPackages :: Stage -> Action [Package]
131 defaultPackages Stage0 = stage0Packages
132 defaultPackages Stage1 = stage1Packages
133 defaultPackages Stage2 = stage2Packages
134 defaultPackages Stage3 = return []
135
136 stage0Packages :: Action [Package]
137 stage0Packages = do
138 win <- windowsHost
139 ios <- iosHost
140 cross <- crossCompiling
141 return $ [ binary
142 , cabal
143 , compareSizes
144 , compiler
145 , deriveConstants
146 , genapply
147 , genprimopcode
148 , ghc
149 , ghcBoot
150 , ghcBootTh
151 , ghcCabal
152 , ghci
153 , ghcPkg
154 , ghcTags
155 , hsc2hs
156 , hp2ps
157 , hpc
158 , mtl
159 , parsec
160 , templateHaskell
161 , text
162 , transformers
163 , unlit ]
164 ++ [ terminfo | not win, not ios, not cross ]
165 ++ [ touchy | win ]
166
167 stage1Packages :: Action [Package]
168 stage1Packages = do
169 win <- windowsHost
170 intSimple <- cmdIntegerSimple
171 libraries0 <- filter isLibrary <$> stage0Packages
172 return $ libraries0 -- Build all Stage0 libraries in Stage1
173 ++ [ array
174 , base
175 , bytestring
176 , containers
177 , deepseq
178 , directory
179 , filepath
180 , ghc
181 , ghcCabal
182 , ghcCompact
183 , ghcPrim
184 , haskeline
185 , hpcBin
186 , hsc2hs
187 , if intSimple then integerSimple else integerGmp
188 , pretty
189 , process
190 , rts
191 , runGhc
192 , stm
193 , time
194 , xhtml ]
195 ++ [ iservBin | not win ]
196 ++ [ iservLib | not win ]
197 ++ [ unix | not win ]
198 ++ [ win32 | win ]
199
200 stage2Packages :: Action [Package]
201 stage2Packages = return [haddock]
202
203 -- | Given a 'Context', compute the name of the program that is built in it
204 -- assuming that the corresponding package's type is 'Program'. For example, GHC
205 -- built in 'Stage0' is called @ghc-stage1@. If the given package is a
206 -- 'Library', the function simply returns its name.
207 programName :: Context -> String
208 programName Context {..}
209 | package == ghc = "ghc-stage" ++ show (fromEnum stage + 1)
210 | package == hpcBin = "hpc"
211 | package == runGhc = "runhaskell"
212 | package == iservBin = "ghc-iserv"
213 | otherwise = pkgName package
214
215 -- | The build stage whose results are used when installing a package, or
216 -- @Nothing@ if the package is not installed, e.g. because it is a user package.
217 -- The current implementation installs the /latest/ build stage of a package.
218 installStage :: Package -> Action (Maybe Stage)
219 installStage pkg
220 | not (isGhcPackage pkg) = return Nothing -- Only GHC packages are installed
221 | otherwise = do
222 stages <- filterM (fmap (pkg `elem`) . defaultPackages) [Stage0 ..]
223 return $ if null stages then Nothing else Just (maximum stages)
224
225 -- | Is the program corresponding to a given context built 'inplace', i.e. in
226 -- the @inplace/bin@ directory? For most programs, only their /latest/ build
227 -- stages are built 'inplace'. The only exception is the GHC itself, which is
228 -- built 'inplace' in all stages. The function returns @False@ for libraries and
229 -- all user packages.
230 isBuiltInplace :: Context -> Action Bool
231 isBuiltInplace Context {..}
232 | isLibrary package = return False
233 | not (isGhcPackage package) = return False
234 | package == ghc = return True
235 | otherwise = (Just stage ==) <$> installStage package
236
237 -- | The 'FilePath' to a program executable in a given 'Context'.
238 programPath :: Context -> Action FilePath
239 programPath context@Context {..} = do
240 path <- buildPath context
241 inplace <- isBuiltInplace context
242 let contextPath = if inplace then inplacePath else path
243 return $ contextPath -/- programName context <.> exe
244 where
245 inplacePath | package `elem` [touchy, unlit, iservBin] = inplaceLibBinPath
246 | otherwise = inplaceBinPath
247
248 -- | Some contexts are special: their packages do not have @.cabal@ metadata or
249 -- we cannot run @ghc-cabal@ on them, e.g. because the latter hasn't been built
250 -- yet (this is the case with the 'ghcCabal' package in 'Stage0').
251 nonCabalContext :: Context -> Bool
252 nonCabalContext Context {..} = (package `elem` [hp2ps, rts, touchy, unlit])
253 || package == ghcCabal && stage == Stage0
254
255 -- | Some program packages should not be linked with Haskell main function.
256 nonHsMainPackage :: Package -> Bool
257 nonHsMainPackage = (`elem` [ghc, hp2ps, iservBin, touchy, unlit])
258
259 -- | Path to the autogen directory generated by @ghc-cabal@ of a given 'Context'.
260 autogenPath :: Context -> Action FilePath
261 autogenPath context@Context {..}
262 | isLibrary package = autogen "build"
263 | package == ghc = autogen "build/ghc"
264 | package == hpcBin = autogen "build/hpc"
265 | package == iservBin = autogen "build/iserv"
266 | otherwise = autogen $ "build" -/- pkgName package
267 where
268 autogen dir = buildPath context <&> (-/- dir -/- "autogen")
269
270 buildDll0 :: Context -> Action Bool
271 buildDll0 Context {..} = do
272 windows <- windowsHost
273 return $ windows && stage == Stage1 && package == compiler