8b7fdb8ae1cd3f3d00d7de232a905550df1c2973
[ghc.git] / src / GHC.hs
1 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
2 module GHC (
3 -- * GHC packages
4 array, base, binary, bytestring, cabal, checkApiAnnotations, compareSizes,
5 compiler, containers, deepseq, deriveConstants, directory, dllSplit, filepath,
6 genapply, genprimopcode, ghc, ghcBoot, ghcBootTh, ghcCabal, ghcCompact, ghci,
7 ghcPkg, ghcPrim, ghcTags, ghcSplit, haddock, haskeline, hsc2hs, hp2ps,
8 hpc, hpcBin, integerGmp, integerSimple, iservBin, libffi, mtl, parsec,
9 parallel, pretty, primitive, process, rts, runGhc, stm, templateHaskell,
10 terminfo, text, time, touchy, transformers, unlit, unix, win32, xhtml,
11 defaultKnownPackages, defaultPackages,
12
13 -- * Package information
14 builderProvenance, programName, nonCabalContext, nonHsMainPackage, autogenPath,
15
16 -- * Miscellaneous
17 systemBuilderPath, ghcSplitPath, stripCmdPath, inplaceInstallPath, buildDll0
18 ) where
19
20 import Hadrian.Oracles.Path
21 import Hadrian.Oracles.TextFile
22
23 import Base
24 import CommandLine
25 import Context
26 import Oracles.Setting
27
28 -- | These are all GHC packages we know about. Build rules will be generated for
29 -- all of them. However, not all of these packages will be built. For example,
30 -- package 'win32' is built only on Windows. "Settings.Default" defines default
31 -- conditions for building each package, which can be overridden in
32 -- @hadrian/src/UserSettings.hs@.
33 defaultKnownPackages :: [Package]
34 defaultKnownPackages =
35 [ array, base, binary, bytestring, cabal, checkApiAnnotations, compareSizes
36 , compiler, containers, deepseq, deriveConstants, directory, dllSplit
37 , filepath, genapply, genprimopcode, ghc, ghcBoot, ghcBootTh, ghcCabal
38 , ghcCompact, ghci, ghcPkg, ghcPrim, ghcTags, haddock, haskeline, hsc2hs
39 , hp2ps, hpc, hpcBin, integerGmp, integerSimple, iservBin, libffi
40 , mtl, parsec, parallel, pretty, primitive, process, rts, runGhc, stm
41 , templateHaskell, terminfo, text, time, touchy, transformers, unlit, unix
42 , win32, xhtml ]
43
44 -- | Package definitions, see 'Package'.
45 array = hsLib "array"
46 base = hsLib "base"
47 binary = hsLib "binary"
48 bytestring = hsLib "bytestring"
49 cabal = hsLib "Cabal" `setPath` "libraries/Cabal/Cabal"
50 checkApiAnnotations = hsUtil "check-api-annotations"
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 dllSplit = hsUtil "dll-split"
58 filepath = hsLib "filepath"
59 genapply = hsUtil "genapply"
60 genprimopcode = hsUtil "genprimopcode"
61 ghc = hsPrg "ghc-bin" `setPath` "ghc"
62 ghcBoot = hsLib "ghc-boot"
63 ghcBootTh = hsLib "ghc-boot-th"
64 ghcCabal = hsUtil "ghc-cabal"
65 ghcCompact = hsLib "ghc-compact"
66 ghci = hsLib "ghci"
67 ghcPkg = hsUtil "ghc-pkg"
68 ghcPrim = hsLib "ghc-prim"
69 ghcTags = hsUtil "ghctags"
70 ghcSplit = hsUtil "ghc-split"
71 haddock = hsUtil "haddock"
72 haskeline = hsLib "haskeline"
73 hsc2hs = hsUtil "hsc2hs"
74 hp2ps = cUtil "hp2ps"
75 hpc = hsLib "hpc"
76 hpcBin = hsUtil "hpc-bin" `setPath` "utils/hpc"
77 integerGmp = hsLib "integer-gmp"
78 integerSimple = hsLib "integer-simple"
79 iservBin = hsPrg "iserv-bin" `setPath` "iserv"
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 return $ [ binary
141 , cabal
142 , checkApiAnnotations
143 , compareSizes
144 , compiler
145 , deriveConstants
146 , dllSplit
147 , genapply
148 , genprimopcode
149 , ghc
150 , ghcBoot
151 , ghcBootTh
152 , ghcCabal
153 , ghci
154 , ghcPkg
155 , ghcTags
156 , hsc2hs
157 , hp2ps
158 , hpc
159 , mtl
160 , parsec
161 , templateHaskell
162 , text
163 , transformers
164 , unlit ]
165 ++ [ terminfo | not win, not ios ]
166 ++ [ touchy | win ]
167
168 stage1Packages :: Action [Package]
169 stage1Packages = do
170 win <- windowsHost
171 doc <- cmdBuildHaddock
172 intSimple <- cmdIntegerSimple
173 libraries0 <- filter isLibrary <$> stage0Packages
174 return $ libraries0 -- Build all Stage0 libraries in Stage1
175 ++ [ array
176 , base
177 , bytestring
178 , containers
179 , deepseq
180 , directory
181 , filepath
182 , ghc
183 , ghcCabal
184 , ghcCompact
185 , ghcPrim
186 , haskeline
187 , hpcBin
188 , hsc2hs
189 , if intSimple then integerSimple else integerGmp
190 , pretty
191 , process
192 , rts
193 , runGhc
194 , time ]
195 ++ [ iservBin | not win ]
196 ++ [ unix | not win ]
197 ++ [ win32 | win ]
198 ++ [ xhtml | doc ]
199
200 stage2Packages :: Action [Package]
201 stage2Packages = do
202 doc <- cmdBuildHaddock
203 return [ haddock | doc ]
204
205 -- | Some builders are built by this very build system, in which case
206 -- 'builderProvenance' returns the corresponding build 'Context' (which includes
207 -- 'Stage' and GHC 'Package').
208 builderProvenance :: Builder -> Maybe Context
209 builderProvenance = \case
210 DeriveConstants -> context Stage0 deriveConstants
211 GenApply -> context Stage0 genapply
212 GenPrimopCode -> context Stage0 genprimopcode
213 Ghc _ Stage0 -> Nothing
214 Ghc _ stage -> context (pred stage) ghc
215 GhcCabal -> context Stage0 ghcCabal
216 GhcCabalHsColour -> builderProvenance $ GhcCabal
217 GhcPkg _ Stage0 -> Nothing
218 GhcPkg _ _ -> context Stage0 ghcPkg
219 Haddock -> context Stage2 haddock
220 Hpc -> context Stage1 hpcBin
221 Hsc2Hs -> context Stage0 hsc2hs
222 Unlit -> context Stage0 unlit
223 _ -> Nothing
224 where
225 context s p = Just $ vanillaContext s p
226
227 -- | Determine the location of a system 'Builder'.
228 systemBuilderPath :: Builder -> Action FilePath
229 systemBuilderPath builder = case builder of
230 Alex -> fromKey "alex"
231 Ar Stage0 -> fromKey "system-ar"
232 Ar _ -> fromKey "ar"
233 Cc _ Stage0 -> fromKey "system-cc"
234 Cc _ _ -> fromKey "cc"
235 -- We can't ask configure for the path to configure!
236 Configure _ -> return "sh configure"
237 Ghc _ Stage0 -> fromKey "system-ghc"
238 GhcPkg _ Stage0 -> fromKey "system-ghc-pkg"
239 Happy -> fromKey "happy"
240 HsColour -> fromKey "hscolour"
241 HsCpp -> fromKey "hs-cpp"
242 Ld -> fromKey "ld"
243 Make _ -> fromKey "make"
244 Nm -> fromKey "nm"
245 Objdump -> fromKey "objdump"
246 Patch -> fromKey "patch"
247 Perl -> fromKey "perl"
248 Ranlib -> fromKey "ranlib"
249 Tar -> fromKey "tar"
250 _ -> error $ "No entry for " ++ show builder ++ inCfg
251 where
252 inCfg = " in " ++ quote configFile ++ " file."
253 fromKey key = do
254 let unpack = fromMaybe . error $ "Cannot find path to builder "
255 ++ quote key ++ inCfg ++ " Did you skip configure?"
256 path <- unpack <$> lookupValue configFile key
257 if null path
258 then do
259 unless (isOptional builder) . error $ "Non optional builder "
260 ++ quote key ++ " is not specified" ++ inCfg
261 return "" -- TODO: Use a safe interface.
262 else fixAbsolutePathOnWindows =<< lookupInPath path
263
264 -- | Given a 'Context', compute the name of the program that is built in it
265 -- assuming that the corresponding package's type is 'Program'. For example, GHC
266 -- built in 'Stage0' is called @ghc-stage1@. If the given package is a
267 -- 'Library', the function simply returns its name.
268 programName :: Context -> String
269 programName Context {..}
270 | package == ghc = "ghc-stage" ++ show (fromEnum stage + 1)
271 | package == hpcBin = "hpc"
272 | package == runGhc = "runhaskell"
273 | package == iservBin = "ghc-iserv"
274 | otherwise = pkgName package
275
276 -- | Some contexts are special: their packages do not have @.cabal@ metadata or
277 -- we cannot run @ghc-cabal@ on them, e.g. because the latter hasn't been built
278 -- yet (this is the case with the 'ghcCabal' package in 'Stage0').
279 nonCabalContext :: Context -> Bool
280 nonCabalContext Context {..} = (package `elem` [hp2ps, rts, touchy, unlit])
281 || package == ghcCabal && stage == Stage0
282
283 -- | Some program packages should not be linked with Haskell main function.
284 nonHsMainPackage :: Package -> Bool
285 nonHsMainPackage = (`elem` [ghc, hp2ps, iservBin, touchy, unlit])
286
287 -- | Path to the autogen directory generated by @ghc-cabal@ of a given 'Context'.
288 autogenPath :: Context -> Action FilePath
289 autogenPath context@Context {..}
290 | isLibrary package = autogen "build"
291 | package == ghc = autogen "build/ghc"
292 | package == hpcBin = autogen "build/hpc"
293 | package == iservBin = autogen "build/iserv"
294 | otherwise = autogen $ "build" -/- pkgName package
295 where
296 autogen dir = buildPath context <&> (-/- dir -/- "autogen")
297
298 -- | Given a 'Package', return the path where the corresponding program is
299 -- installed. Most programs are installed in 'programInplacePath'.
300 inplaceInstallPath :: Package -> FilePath
301 inplaceInstallPath pkg
302 | pkg == touchy = inplaceLibBinPath
303 | pkg == unlit = inplaceLibBinPath
304 | pkg == iservBin = inplaceLibBinPath
305 | otherwise = inplaceBinPath
306
307 -- | @ghc-split@ is a Perl script used by GHC with @-split-objs@ flag. It is
308 -- generated in "Rules.Generators.GhcSplit".
309 ghcSplitPath :: FilePath
310 ghcSplitPath = inplaceLibBinPath -/- "ghc-split"
311
312 -- ref: mk/config.mk
313 -- | Command line tool for stripping.
314 stripCmdPath :: Action FilePath
315 stripCmdPath = do
316 targetPlatform <- setting TargetPlatform
317 top <- topDirectory
318 case targetPlatform of
319 "x86_64-unknown-mingw32" ->
320 return (top -/- "inplace/mingw/bin/strip.exe")
321 "arm-unknown-linux" ->
322 return ":" -- HACK: from the make-based system, see the ref above
323 _ -> return "strip"
324
325 buildDll0 :: Context -> Action Bool
326 buildDll0 Context {..} = do
327 windows <- windowsHost
328 return $ windows && stage == Stage1 && package == compiler