6a23bb7ab3495c6f3e87cb28eac8473aba08a82d
[hadrian.git] / src / Settings / Packages.hs
1 module Settings.Packages (packageArgs) where
2
3 import Expression
4 import Flavour
5 import GHC
6 import Oracles.Setting
7 import Oracles.Flag
8 import Rules.Gmp
9 import Settings
10
11 -- | Package-specific command-line arguments.
12 packageArgs :: Args
13 packageArgs = do
14 intLib <- getIntegerPackage
15 stage <- getStage
16 path <- getBuildPath
17 rtsWays <- getRtsWays
18 compilerBuildPath <- expr $ buildPath (vanillaContext stage compiler)
19 gmpBuildPath <- expr gmpBuildPath
20
21 let includeGmp = "-I" ++ gmpBuildPath -/- "include"
22
23 mconcat
24 --------------------------------- base ---------------------------------
25 [ package base ? mconcat
26 [ builder CabalFlags ? arg ('+' : pkgName intLib)
27
28 -- This fixes the 'unknown symbol stat' issue.
29 -- See: https://github.com/snowleopard/hadrian/issues/259.
30 , builder (Ghc CompileCWithGhc) ? arg "-optc-O2" ]
31
32 ------------------------------ bytestring ------------------------------
33 , package bytestring ?
34 builder CabalFlags ? intLib == integerSimple ? arg "integer-simple"
35
36 --------------------------------- cabal --------------------------------
37 -- Cabal is a large library and slow to compile. Moreover, we build it
38 -- for Stage0 only so we can link ghc-pkg against it, so there is little
39 -- reason to spend the effort to optimise it.
40 , package cabal ?
41 stage0 ? builder Ghc ? arg "-O0"
42
43 ------------------------------- compiler -------------------------------
44 , package compiler ? mconcat
45 [ builder Alex ? arg "--latin1"
46
47 , builder (Ghc CompileHs) ? mconcat
48 [ inputs ["//GHC.hs", "//GhcMake.hs"] ? arg "-fprof-auto"
49 , input "//Parser.hs" ?
50 pure ["-O0", "-fno-ignore-interface-pragmas", "-fcmm-sink" ] ]
51
52 , builder (GhcCabal Conf) ? mconcat
53 [ arg $ "--ghc-option=-DSTAGE=" ++ show (fromEnum stage + 1)
54 , arg "--disable-library-for-ghci"
55 , anyTargetOs ["openbsd"] ? arg "--ld-options=-E"
56 , flag GhcUnregisterised ? arg "--ghc-option=-DNO_REGS"
57 , notM ghcWithSMP ? arg "--ghc-option=-DNOSMP"
58 , notM ghcWithSMP ? arg "--ghc-option=-optc-DNOSMP"
59 , (any (wayUnit Threaded) rtsWays) ?
60 notStage0 ? arg "--ghc-option=-optc-DTHREADED_RTS"
61 , ghcWithInterpreter ?
62 ghcEnableTablesNextToCode ?
63 notM (flag GhcUnregisterised) ?
64 notStage0 ? arg "--ghc-option=-DGHCI_TABLES_NEXT_TO_CODE"
65 , ghcWithInterpreter ?
66 ghciWithDebugger <$> flavour ?
67 notStage0 ? arg "--ghc-option=-DDEBUGGER"
68 , ghcProfiled <$> flavour ?
69 notStage0 ? arg "--ghc-pkg-option=--force" ]
70
71 , builder CabalFlags ? mconcat
72 [ ghcWithNativeCodeGen ? arg "ncg"
73 , ghcWithInterpreter ? notStage0 ? arg "ghci"
74 , flag CrossCompiling ? arg "-terminfo" ]
75
76 , builder (Haddock BuildPackage) ? arg ("--optghc=-I" ++ path) ]
77
78 ---------------------------------- ghc ---------------------------------
79 , package ghc ? mconcat
80 [ builder Ghc ? arg ("-I" ++ compilerBuildPath)
81
82 , builder CabalFlags ? mconcat
83 [ ghcWithInterpreter ? notStage0 ? arg "ghci"
84 , flag CrossCompiling ? arg "-terminfo" ] ]
85
86 -------------------------------- ghcPkg --------------------------------
87 , package ghcPkg ?
88 builder CabalFlags ? flag CrossCompiling ? arg "-terminfo"
89
90 -------------------------------- ghcPrim -------------------------------
91 , package ghcPrim ? mconcat
92 [ builder CabalFlags ? arg "include-ghc-prim"
93
94 , builder (Cc CompileC) ? (not <$> flag GccIsClang) ?
95 input "//cbits/atomic.c" ? arg "-Wno-sync-nand" ]
96
97 --------------------------------- ghci ---------------------------------
98 -- TODO: This should not be @not <$> flag CrossCompiling@. Instead we
99 -- should ensure that the bootstrap compiler has the same version as the
100 -- one we are building.
101
102 -- TODO: In that case we also do not need to build most of the Stage1
103 -- libraries, as we already know that the compiler comes with the most
104 -- recent versions.
105
106 -- TODO: The use case here is that we want to build @ghc-proxy@ for the
107 -- cross compiler. That one needs to be compiled by the bootstrap
108 -- compiler as it needs to run on the host. Hence @libiserv@ needs
109 -- @GHCi.TH@, @GHCi.Message@ and @GHCi.Run@ from @ghci@. And those are
110 -- behind the @-fghci@ flag.
111 , package ghci ? mconcat
112 [ notStage0 ? builder CabalFlags ? arg "ghci"
113 , flag CrossCompiling ? stage0 ? builder CabalFlags ? arg "ghci" ]
114
115 -------------------------------- haddock -------------------------------
116 , package haddock ?
117 builder CabalFlags ? arg "in-ghc-tree"
118
119 ------------------------------- haskeline ------------------------------
120 , package haskeline ?
121 builder CabalFlags ? flag CrossCompiling ? arg "-terminfo"
122
123 -------------------------------- hsc2hs --------------------------------
124 , package hsc2hs ?
125 builder CabalFlags ? arg "in-ghc-tree"
126
127 ------------------------------ integerGmp ------------------------------
128 , package integerGmp ? mconcat
129 [ builder Cc ? arg includeGmp
130
131 , builder (GhcCabal Conf) ? mconcat
132 [ -- TODO: This should respect some settings flag "InTreeGmp".
133 -- Depending on @IncludeDir@ and @LibDir@ is bound to fail, since
134 -- these are only set if the configure script was explicilty
135 -- called with GMP include and lib dirs. Their absense as such
136 -- does not imply @in-tree-gmp@.
137 -- (null gmpIncludeDir && null gmpLibDir) ?
138 -- arg "--configure-option=--with-intree-gmp"
139 arg ("--configure-option=CFLAGS=" ++ includeGmp)
140 , arg ("--gcc-options=" ++ includeGmp) ] ]
141
142 ---------------------------------- rts ---------------------------------
143 , package rts ? rtsPackageArgs -- RTS deserves a separate function
144
145 -------------------------------- runGhc --------------------------------
146 , package runGhc ?
147 builder Ghc ? input "//Main.hs" ?
148 (\version -> ["-cpp", "-DVERSION=" ++ show version]) <$> getSetting ProjectVersion
149
150 --------------------------------- text ---------------------------------
151 -- The package @text@ is rather tricky. It's a boot library, and it
152 -- tries to determine on its own if it should link against @integer-gmp@
153 -- or @integer-simple@. For Stage0, we need to use the integer library
154 -- that the bootstrap compiler has (since @interger@ is not a boot
155 -- library) and therefore we copy it over into the Stage0 package-db.
156 -- Maybe we should stop doing this? And subsequently @text@ for Stage1
157 -- detects the same integer library again, even though we don't build it
158 -- in Stage1, and at that point the configuration is just wrong.
159 , package text ?
160 builder CabalFlags ? notStage0 ? intLib == integerSimple ?
161 pure [ "+integer-simple", "-bytestring-builder"] ]
162
163 -- | RTS-specific command line arguments.
164 rtsPackageArgs :: Args
165 rtsPackageArgs = package rts ? do
166 projectVersion <- getSetting ProjectVersion
167 hostPlatform <- getSetting HostPlatform
168 hostArch <- getSetting HostArch
169 hostOs <- getSetting HostOs
170 hostVendor <- getSetting HostVendor
171 buildPlatform <- getSetting BuildPlatform
172 buildArch <- getSetting BuildArch
173 buildOs <- getSetting BuildOs
174 buildVendor <- getSetting BuildVendor
175 targetPlatform <- getSetting TargetPlatform
176 targetArch <- getSetting TargetArch
177 targetOs <- getSetting TargetOs
178 targetVendor <- getSetting TargetVendor
179 ghcUnreg <- expr $ yesNo <$> flag GhcUnregisterised
180 ghcEnableTNC <- expr $ yesNo <$> ghcEnableTablesNextToCode
181 rtsWays <- getRtsWays
182 way <- getWay
183 path <- getBuildPath
184 top <- expr topDirectory
185 libffiName <- expr libffiLibraryName
186 ffiIncludeDir <- getSetting FfiIncludeDir
187 ffiLibraryDir <- getSetting FfiLibDir
188 ghclibDir <- expr installGhcLibDir
189 destDir <- expr getDestDir
190 let cArgs = mconcat
191 [ arg "-Irts"
192 , rtsWarnings
193 , arg $ "-I" ++ path
194 , flag UseSystemFfi ? arg ("-I" ++ ffiIncludeDir)
195 , arg $ "-DRtsWay=\"rts_" ++ show way ++ "\""
196 -- Set the namespace for the rts fs functions
197 , arg $ "-DFS_NAMESPACE=rts"
198 , arg $ "-DCOMPILING_RTS"
199 -- RTS *must* be compiled with optimisations. The INLINE_HEADER macro
200 -- requires that functions are inlined to work as expected. Inlining
201 -- only happens for optimised builds. Otherwise we can assume that
202 -- there is a non-inlined variant to use instead. But RTS does not
203 -- provide non-inlined alternatives and hence needs the function to
204 -- be inlined. See https://github.com/snowleopard/hadrian/issues/90.
205 , arg "-O2"
206 , arg "-fomit-frame-pointer"
207 , arg "-g"
208
209 , Debug `wayUnit` way ? pure [ "-DDEBUG"
210 , "-fno-omit-frame-pointer"
211 , "-g"
212 , "-O0" ]
213 , way `elem` [debug, debugDynamic] ? arg "-DTICKY_TICKY"
214 , Profiling `wayUnit` way ? arg "-DPROFILING"
215 , Threaded `wayUnit` way ? arg "-DTHREADED_RTS"
216
217 , inputs ["//RtsMessages.c", "//Trace.c"] ?
218 arg ("-DProjectVersion=" ++ show projectVersion)
219
220 , input "//RtsUtils.c" ? pure
221 [ "-DProjectVersion=" ++ show projectVersion
222 , "-DHostPlatform=" ++ show hostPlatform
223 , "-DHostArch=" ++ show hostArch
224 , "-DHostOS=" ++ show hostOs
225 , "-DHostVendor=" ++ show hostVendor
226 , "-DBuildPlatform=" ++ show buildPlatform
227 , "-DBuildArch=" ++ show buildArch
228 , "-DBuildOS=" ++ show buildOs
229 , "-DBuildVendor=" ++ show buildVendor
230 , "-DTargetPlatform=" ++ show targetPlatform
231 , "-DTargetArch=" ++ show targetArch
232 , "-DTargetOS=" ++ show targetOs
233 , "-DTargetVendor=" ++ show targetVendor
234 , "-DGhcUnregisterised=" ++ show ghcUnreg
235 , "-DGhcEnableTablesNextToCode=" ++ show ghcEnableTNC ]
236
237 -- We're after pur performance here. So make sure fast math and
238 -- vectorization is enabled.
239 , input "//xxhash.c" ? pure
240 [ "-O3"
241 , "-ffast-math"
242 , "-ftree-vectorize" ]
243
244 , inputs ["//Evac.c", "//Evac_thr.c"] ? arg "-funroll-loops"
245
246 , speedHack ?
247 inputs [ "//Evac.c", "//Evac_thr.c"
248 , "//Scav.c", "//Scav_thr.c"
249 , "//Compact.c", "//GC.c" ] ? arg "-fno-PIC"
250 -- @-static@ is necessary for these bits, as otherwise the NCG
251 -- generates dynamic references.
252 , speedHack ?
253 inputs [ "//Updates.c", "//StgMiscClosures.c"
254 , "//PrimOps.c", "//Apply.c"
255 , "//AutoApply.c" ] ? pure ["-fno-PIC", "-static"]
256
257 -- inlining warnings happen in Compact
258 , inputs ["//Compact.c"] ? arg "-Wno-inline"
259
260 -- emits warnings about call-clobbered registers on x86_64
261 , inputs [ "//RetainerProfile.c", "//StgCRun.c"
262 , "//win32/ConsoleHandler.c", "//win32/ThrIOManager.c"] ? arg "-w"
263 -- The above warning suppression flags are a temporary kludge.
264 -- While working on this module you are encouraged to remove it and fix
265 -- any warnings in the module. See:
266 -- http://ghc.haskell.org/trac/ghc/wiki/WorkingConventions#Warnings
267
268 , (not <$> flag GccIsClang) ?
269 inputs ["//Compact.c"] ? arg "-finline-limit=2500"
270
271 , input "//RetainerProfile.c" ? flag GccIsClang ?
272 arg "-Wno-incompatible-pointer-types"
273 , windowsHost ? arg ("-DWINVER=" ++ windowsVersion)
274
275 -- libffi's ffi.h triggers various warnings
276 , inputs [ "//Interpreter.c", "//Storage.c", "//Adjustor.c" ] ?
277 arg "-Wno-strict-prototypes"
278 , inputs ["//Interpreter.c", "//Adjustor.c", "//sm/Storage.c"] ?
279 anyTargetArch ["powerpc"] ? arg "-Wno-undef" ]
280
281 mconcat
282 [ builder CabalFlags ? (any (wayUnit Profiling) rtsWays) ? arg "profiling"
283 , builder (Cc FindCDependencies) ? cArgs
284 , builder (Ghc CompileCWithGhc) ? map ("-optc" ++) <$> cArgs
285 , builder Ghc ? arg "-Irts"
286
287 , builder HsCpp ? pure
288 [ "-DTOP=" ++ show top
289 , "-DFFI_INCLUDE_DIR=" ++ show ffiIncludeDir
290 , "-DFFI_LIB_DIR=" ++ show ffiLibraryDir
291 , "-DFFI_LIB=" ++ show libffiName ]
292
293 , builder HsCpp ?
294 input "//package.conf.in" ?
295 output "//package.conf.install.raw" ?
296 pure [ "-DINSTALLING"
297 , "-DLIB_DIR=\"" ++ destDir ++ ghclibDir ++ "\""
298 , "-DINCLUDE_DIR=\"" ++ destDir ++ ghclibDir -/- "include\"" ]
299
300 , builder HsCpp ? flag HaveLibMingwEx ? arg "-DHAVE_LIBMINGWEX" ]
301
302 -- Compile various performance-critical pieces *without* -fPIC -dynamic
303 -- even when building a shared library. If we don't do this, then the
304 -- GC runs about 50% slower on x86 due to the overheads of PIC. The
305 -- cost of doing this is a little runtime linking and less sharing, but
306 -- not much.
307 --
308 -- On x86_64 this doesn't work, because all objects in a shared library
309 -- must be compiled with -fPIC (since the 32-bit relocations generated
310 -- by the default small memory can't be resolved at runtime). So we
311 -- only do this on i386.
312 --
313 -- This apparently doesn't work on OS X (Darwin) nor on Solaris.
314 -- On Darwin we get errors of the form
315 --
316 -- ld: absolute addressing (perhaps -mdynamic-no-pic) used in _stg_ap_0_fast
317 -- from rts/dist/build/Apply.dyn_o not allowed in slidable image
318 --
319 -- and lots of these warnings:
320 --
321 -- ld: warning codegen in _stg_ap_pppv_fast (offset 0x0000005E) prevents image
322 -- from loading in dyld shared cache
323 --
324 -- On Solaris we get errors like:
325 --
326 -- Text relocation remains referenced
327 -- against symbol offset in file
328 -- .rodata (section) 0x11 rts/dist/build/Apply.dyn_o
329 -- ...
330 -- ld: fatal: relocations remain against allocatable but non-writable sections
331 -- collect2: ld returned 1 exit status
332 speedHack :: Action Bool
333 speedHack = do
334 i386 <- anyTargetArch ["i386"]
335 goodOS <- not <$> anyTargetOs ["darwin", "solaris2"]
336 return $ i386 && goodOS
337
338 -- See @rts/ghc.mk@.
339 rtsWarnings :: Args
340 rtsWarnings = mconcat
341 [ arg "-Wall"
342 , arg "-Wextra"
343 , arg "-Wstrict-prototypes"
344 , arg "-Wmissing-prototypes"
345 , arg "-Wmissing-declarations"
346 , arg "-Winline"
347 , arg "-Waggregate-return"
348 , arg "-Wpointer-arith"
349 , arg "-Wmissing-noreturn"
350 , arg "-Wnested-externs"
351 , arg "-Wredundant-decls"
352 , arg "-Wundef"
353 , arg "-fno-strict-aliasing" ]
354
355 -- These numbers can be found at:
356 -- https://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs.85).aspx
357 -- If we're compiling on windows, enforce that we only support Vista SP1+
358 -- Adding this here means it doesn't have to be done in individual .c files
359 -- and also centralizes the versioning.
360 -- | Minimum supported Windows version.
361 windowsVersion :: String
362 windowsVersion = "0x06000100"