771d37ec8f88e72c9968cd56670e72ee19f239ea
[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, libffi, mtl, parsec, parallel, pretty, primitive,
9 process, rts, runGhc, stm, templateHaskell, terminfo, text, time, touchy,
10 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, libffi, mtl, parsec, parallel, pretty, primitive
38 , process, rts, runGhc, stm, templateHaskell, terminfo, text, time, touchy
39 , 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 = hsPrg "iserv-bin" `setPath` "iserv"
79 libffi = cTop "libffi"
80 mtl = hsLib "mtl"
81 parsec = hsLib "parsec"
82 parallel = hsLib "parallel"
83 pretty = hsLib "pretty"
84 primitive = hsLib "primitive"
85 process = hsLib "process"
86 rts = cTop "rts"
87 runGhc = hsUtil "runghc"
88 stm = hsLib "stm"
89 templateHaskell = hsLib "template-haskell"
90 terminfo = hsLib "terminfo"
91 text = hsLib "text"
92 time = hsLib "time"
93 touchy = cUtil "touchy"
94 transformers = hsLib "transformers"
95 unlit = cUtil "unlit"
96 unix = hsLib "unix"
97 win32 = hsLib "Win32"
98 xhtml = hsLib "xhtml"
99
100 -- | Construct a Haskell library package, e.g. @array@.
101 hsLib :: PackageName -> Package
102 hsLib name = hsLibrary name ("libraries" -/- name)
103
104 -- | Construct a top-level Haskell library package, e.g. @compiler@.
105 hsTop :: PackageName -> Package
106 hsTop name = hsLibrary name name
107
108 -- | Construct a top-level C library package, e.g. @rts@.
109 cTop :: PackageName -> Package
110 cTop name = cLibrary name name
111
112 -- | Construct a top-level Haskell program package, e.g. @ghc@.
113 hsPrg :: PackageName -> Package
114 hsPrg name = hsProgram name name
115
116 -- | Construct a Haskell utility package, e.g. @haddock@.
117 hsUtil :: PackageName -> Package
118 hsUtil name = hsProgram name ("utils" -/- name)
119
120 -- | Construct a C utility package, e.g. @haddock@.
121 cUtil :: PackageName -> Package
122 cUtil name = cProgram name ("utils" -/- name)
123
124 -- | Amend a package path if it doesn't conform to a typical pattern.
125 setPath :: Package -> FilePath -> Package
126 setPath pkg path = pkg { pkgPath = path }
127
128 -- | Packages that are built by default. You can change this in "UserSettings".
129 defaultPackages :: Stage -> Action [Package]
130 defaultPackages Stage0 = stage0Packages
131 defaultPackages Stage1 = stage1Packages
132 defaultPackages Stage2 = stage2Packages
133 defaultPackages Stage3 = return []
134
135 stage0Packages :: Action [Package]
136 stage0Packages = do
137 win <- windowsHost
138 ios <- iosHost
139 cross <- crossCompiling
140 return $ [ binary
141 , cabal
142 , compareSizes
143 , compiler
144 , deriveConstants
145 , genapply
146 , genprimopcode
147 , ghc
148 , ghcBoot
149 , ghcBootTh
150 , ghcCabal
151 , ghci
152 , ghcPkg
153 , ghcTags
154 , hsc2hs
155 , hp2ps
156 , hpc
157 , mtl
158 , parsec
159 , templateHaskell
160 , text
161 , transformers
162 , unlit ]
163 ++ [ terminfo | not win, not ios, not cross ]
164 ++ [ touchy | win ]
165
166 stage1Packages :: Action [Package]
167 stage1Packages = do
168 win <- windowsHost
169 intSimple <- cmdIntegerSimple
170 libraries0 <- filter isLibrary <$> stage0Packages
171 return $ libraries0 -- Build all Stage0 libraries in Stage1
172 ++ [ array
173 , base
174 , bytestring
175 , containers
176 , deepseq
177 , directory
178 , filepath
179 , ghc
180 , ghcCabal
181 , ghcCompact
182 , ghcPrim
183 , haskeline
184 , hpcBin
185 , hsc2hs
186 , if intSimple then integerSimple else integerGmp
187 , pretty
188 , process
189 , rts
190 , runGhc
191 , stm
192 , time
193 , xhtml ]
194 ++ [ iservBin | not win ]
195 ++ [ unix | not win ]
196 ++ [ win32 | win ]
197
198 stage2Packages :: Action [Package]
199 stage2Packages = return [haddock]
200
201 -- | Given a 'Context', compute the name of the program that is built in it
202 -- assuming that the corresponding package's type is 'Program'. For example, GHC
203 -- built in 'Stage0' is called @ghc-stage1@. If the given package is a
204 -- 'Library', the function simply returns its name.
205 programName :: Context -> String
206 programName Context {..}
207 | package == ghc = "ghc-stage" ++ show (fromEnum stage + 1)
208 | package == hpcBin = "hpc"
209 | package == runGhc = "runhaskell"
210 | package == iservBin = "ghc-iserv"
211 | otherwise = pkgName package
212
213 -- | The build stage whose results are used when installing a package, or
214 -- @Nothing@ if the package is not installed, e.g. because it is a user package.
215 -- The current implementation installs the /latest/ build stage of a package.
216 installStage :: Package -> Action (Maybe Stage)
217 installStage pkg
218 | not (isGhcPackage pkg) = return Nothing -- Only GHC packages are installed
219 | otherwise = do
220 stages <- filterM (fmap (pkg `elem`) . defaultPackages) [Stage0 ..]
221 return $ if null stages then Nothing else Just (maximum stages)
222
223 -- | Is the program corresponding to a given context built 'inplace', i.e. in
224 -- the @inplace/bin@ directory? For most programs, only their /latest/ build
225 -- stages are built 'inplace'. The only exception is the GHC itself, which is
226 -- built 'inplace' in all stages. The function returns @False@ for libraries and
227 -- all user packages.
228 isBuiltInplace :: Context -> Action Bool
229 isBuiltInplace Context {..}
230 | isLibrary package = return False
231 | not (isGhcPackage package) = return False
232 | package == ghc = return True
233 | otherwise = (Just stage ==) <$> installStage package
234
235 -- | The 'FilePath' to a program executable in a given 'Context'.
236 programPath :: Context -> Action FilePath
237 programPath context@Context {..} = do
238 path <- buildPath context
239 inplace <- isBuiltInplace context
240 let contextPath = if inplace then inplacePath else path
241 return $ contextPath -/- programName context <.> exe
242 where
243 inplacePath | package `elem` [touchy, unlit, iservBin] = inplaceLibBinPath
244 | otherwise = inplaceBinPath
245
246 -- | Some contexts are special: their packages do not have @.cabal@ metadata or
247 -- we cannot run @ghc-cabal@ on them, e.g. because the latter hasn't been built
248 -- yet (this is the case with the 'ghcCabal' package in 'Stage0').
249 nonCabalContext :: Context -> Bool
250 nonCabalContext Context {..} = (package `elem` [hp2ps, rts, touchy, unlit])
251 || package == ghcCabal && stage == Stage0
252
253 -- | Some program packages should not be linked with Haskell main function.
254 nonHsMainPackage :: Package -> Bool
255 nonHsMainPackage = (`elem` [ghc, hp2ps, iservBin, touchy, unlit])
256
257 -- | Path to the autogen directory generated by @ghc-cabal@ of a given 'Context'.
258 autogenPath :: Context -> Action FilePath
259 autogenPath context@Context {..}
260 | isLibrary package = autogen "build"
261 | package == ghc = autogen "build/ghc"
262 | package == hpcBin = autogen "build/hpc"
263 | package == iservBin = autogen "build/iserv"
264 | otherwise = autogen $ "build" -/- pkgName package
265 where
266 autogen dir = buildPath context <&> (-/- dir -/- "autogen")
267
268 buildDll0 :: Context -> Action Bool
269 buildDll0 Context {..} = do
270 windows <- windowsHost
271 return $ windows && stage == Stage1 && package == compiler