Decompose Settings/Builders/Ghc.hs (see #60).
[hadrian.git] / src / Settings / Builders / GhcCabal.hs
1 module Settings.Builders.GhcCabal (
2 cabalArgs, ghcCabalHsColourArgs, bootPackageDbArgs,
3 customPackageArgs, ccArgs, cppArgs, ccWarnings, argStagedSettingList, needDll0
4 ) where
5
6 import Base
7 import Oracles.Config.Setting
8 import Oracles.Config.Flag
9 import GHC
10 import Package
11 import Way
12 import Builder
13 import Stage
14 import Expression
15 import Predicates hiding (stage)
16 import Settings
17 import Settings.Builders.Common
18
19 cabalArgs :: Args
20 cabalArgs = builder GhcCabal ? do
21 path <- getPackagePath
22 dir <- getTargetDirectory
23 mconcat [ arg "configure"
24 , arg path
25 , arg dir
26 , dll0Args
27 , withStaged Ghc
28 , withStaged GhcPkg
29 , bootPackageDbArgs
30 , libraryArgs
31 , with HsColour
32 , configureArgs
33 , packageConstraints
34 , withStaged Gcc
35 , notStage0 ? with Ld
36 , with Ar
37 , with Alex
38 , with Happy ]
39
40 ghcCabalHsColourArgs :: Args
41 ghcCabalHsColourArgs = builder GhcCabalHsColour ? do
42 path <- getPackagePath
43 dir <- getTargetDirectory
44 mconcat [ arg "hscolour"
45 , arg path
46 , arg dir ]
47
48 -- TODO: Isn't vanilla always built? If yes, some conditions are redundant.
49 -- TODO: Need compiler_stage1_CONFIGURE_OPTS += --disable-library-for-ghci?
50 libraryArgs :: Args
51 libraryArgs = do
52 ways <- getWays
53 withGhci <- lift ghcWithInterpreter
54 append [ if vanilla `elem` ways
55 then "--enable-library-vanilla"
56 else "--disable-library-vanilla"
57 , if vanilla `elem` ways && withGhci && not dynamicGhcPrograms
58 then "--enable-library-for-ghci"
59 else "--disable-library-for-ghci"
60 , if profiling `elem` ways
61 then "--enable-library-profiling"
62 else "--disable-library-profiling"
63 , if dynamic `elem` ways
64 then "--enable-shared"
65 else "--disable-shared" ]
66
67 -- TODO: LD_OPTS?
68 configureArgs :: Args
69 configureArgs = do
70 let conf key = appendSubD $ "--configure-option=" ++ key
71 cFlags = mconcat [ ccArgs
72 , remove ["-Werror"]
73 , argStagedSettingList ConfCcArgs ]
74 ldFlags = ldArgs <> (argStagedSettingList ConfGccLinkerArgs)
75 cppFlags = cppArgs <> (argStagedSettingList ConfCppArgs)
76 mconcat
77 [ conf "CFLAGS" cFlags
78 , conf "LDFLAGS" ldFlags
79 , conf "CPPFLAGS" cppFlags
80 , appendSubD "--gcc-options" $ cFlags <> ldFlags
81 , conf "--with-iconv-includes" $ argSettingList IconvIncludeDirs
82 , conf "--with-iconv-libraries" $ argSettingList IconvLibDirs
83 , conf "--with-gmp-includes" $ argSettingList GmpIncludeDirs
84 , conf "--with-gmp-libraries" $ argSettingList GmpLibDirs
85 , crossCompiling ? (conf "--host" $ argSetting TargetPlatformFull)
86 , conf "--with-cc" $ argStagedBuilderPath Gcc ]
87
88 bootPackageDbArgs :: Args
89 bootPackageDbArgs = stage0 ? do
90 path <- getSetting GhcSourcePath
91 lift $ need [bootstrappingConfInitialised]
92 prefix <- ifM builderGhc (return "-package-db ") (return "--package-db=")
93 arg $ prefix ++ path -/- bootstrappingConf
94
95 packageConstraints :: Args
96 packageConstraints = stage0 ? do
97 constraints <- lift . readFileLines $ bootPackageConstraints
98 append $ concat [ ["--constraint", c] | c <- constraints ]
99
100 -- TODO: should be in a different file
101 -- TODO: put all validating options together in one file
102 ccArgs :: Args
103 ccArgs = validating ? ccWarnings
104
105 -- TODO: should be in a different file
106 ccWarnings :: Args
107 ccWarnings = do
108 let gccGe46 = notM $ (flag GccIsClang ||^ flag GccLt46)
109 mconcat [ turnWarningsIntoErrors ? arg "-Werror"
110 , arg "-Wall"
111 , flag GccIsClang ? arg "-Wno-unknown-pragmas"
112 , gccGe46 ? notM windowsHost ? arg "-Werror=unused-but-set-variable"
113 , gccGe46 ? arg "-Wno-error=inline" ]
114
115 ldArgs :: Args
116 ldArgs = mempty
117
118 cppArgs :: Args
119 cppArgs = includesArgs
120
121 -- TODO: Is this needed?
122 -- ifeq "$(GMP_PREFER_FRAMEWORK)" "YES"
123 -- libraries/integer-gmp_CONFIGURE_OPTS += --with-gmp-framework-preferred
124 -- endif
125
126 -- TODO: move this somewhere
127 customPackageArgs :: Args
128 customPackageArgs = do
129 stage <- getStage
130 rtsWays <- getRtsWays
131 mconcat
132 [ package integerGmp ?
133 mconcat [ windowsHost ? builder GhcCabal ?
134 arg "--configure-option=--with-intree-gmp"
135 , appendCcArgs ["-I" ++ pkgPath integerGmp -/- "gmp"] ]
136
137 , package base ?
138 builder GhcCabal ?
139 arg ("--flags=" ++ takeFileName (pkgPath integerLibrary))
140
141 , package ghcPrim ?
142 builder GhcCabal ? arg "--flag=include-ghc-prim"
143
144 , package compiler ?
145 builder GhcCabal ?
146 mconcat [ arg $ "--ghc-option=-DSTAGE=" ++ show (fromEnum stage + 1)
147 , arg "--disable-library-for-ghci"
148 , anyTargetOs ["openbsd"] ? arg "--ld-options=-E"
149 , flag GhcUnregisterised ? arg "--ghc-option=-DNO_REGS"
150 , notM ghcWithSMP ? arg "--ghc-option=-DNOSMP"
151 , notM ghcWithSMP ? arg "--ghc-option=-optc-DNOSMP"
152 , (threaded `elem` rtsWays) ?
153 notStage0 ? arg "--ghc-option=-optc-DTHREADED_RTS"
154 , ghcWithNativeCodeGen ? arg "--flags=ncg"
155 , ghcWithInterpreter ?
156 notStage0 ? arg "--flags=ghci"
157 , ghcWithInterpreter ?
158 ghcEnableTablesNextToCode ?
159 notM (flag GhcUnregisterised) ?
160 notStage0 ? arg "--ghc-option=-DGHCI_TABLES_NEXT_TO_CODE"
161 , ghcWithInterpreter ?
162 ghciWithDebugger ?
163 notStage0 ? arg "--ghc-option=-DDEBUGGER"
164 , ghcProfiled ?
165 notStage0 ? arg "--ghc-pkg-option=--force" ]
166
167 , package ghc ?
168 builder GhcCabal ?
169 mconcat [ ghcWithInterpreter ?
170 notStage0 ? arg "--flags=ghci" ]
171
172 , package haddock ?
173 builder GhcCabal ? append ["--flag", "in-ghc-tree"] ]
174
175 withBuilderKey :: Builder -> String
176 withBuilderKey b = case b of
177 Ar -> "--with-ar="
178 Ld -> "--with-ld="
179 Gcc _ -> "--with-gcc="
180 Ghc _ -> "--with-ghc="
181 Alex -> "--with-alex="
182 Happy -> "--with-happy="
183 GhcPkg _ -> "--with-ghc-pkg="
184 HsColour -> "--with-hscolour="
185 _ -> error "withBuilderKey: not supported builder"
186
187 -- Expression 'with Gcc' appends "--with-gcc=/path/to/gcc" and needs Gcc.
188 with :: Builder -> Args
189 with b = specified b ? do
190 top <- getSetting GhcSourcePath
191 path <- getBuilderPath b
192 lift $ needBuilder laxDependencies b
193 append [withBuilderKey b ++ top -/- path]
194
195 withStaged :: (Stage -> Builder) -> Args
196 withStaged sb = (with . sb) =<< getStage
197
198 argM :: Action String -> Args
199 argM = (arg =<<) . lift
200
201 argSetting :: Setting -> Args
202 argSetting = argM . setting
203
204 argSettingList :: SettingList -> Args
205 argSettingList = (append =<<) . lift . settingList
206
207 argStagedSettingList :: (Stage -> SettingList) -> Args
208 argStagedSettingList ss = (argSettingList . ss) =<< getStage
209
210 argStagedBuilderPath :: (Stage -> Builder) -> Args
211 argStagedBuilderPath sb = (argM . builderPath . sb) =<< getStage
212
213 -- Pass arguments to Gcc and corresponding lists of sub-arguments of GhcCabal
214 -- TODO: simplify
215 appendCcArgs :: [String] -> Args
216 appendCcArgs xs = do
217 mconcat [ stagedBuilder Gcc ? append xs
218 , stagedBuilder GccM ? append xs
219 , builder GhcCabal ? appendSub "--configure-option=CFLAGS" xs
220 , builder GhcCabal ? appendSub "--gcc-options" xs ]
221
222 needDll0 :: Stage -> Package -> Action Bool
223 needDll0 stage pkg = do
224 windows <- windowsHost
225 return $ windows && pkg == compiler && stage == Stage1
226
227 -- This is a positional argument, hence:
228 -- * if it is empty, we need to emit one empty string argument;
229 -- * otherwise, we must collapse it into one space-separated string.
230 dll0Args :: Args
231 dll0Args = do
232 stage <- getStage
233 pkg <- getPackage
234 dll0 <- lift $ needDll0 stage pkg
235 withGhci <- lift ghcWithInterpreter
236 arg . unwords . concat $ [ modules | dll0 ]
237 ++ [ ghciModules | dll0 && withGhci ] -- see #9552
238 where
239 modules = [ "Annotations"
240 , "ApiAnnotation"
241 , "Avail"
242 , "Bag"
243 , "BasicTypes"
244 , "Binary"
245 , "BooleanFormula"
246 , "BreakArray"
247 , "BufWrite"
248 , "Class"
249 , "CmdLineParser"
250 , "CmmType"
251 , "CoAxiom"
252 , "ConLike"
253 , "Coercion"
254 , "Config"
255 , "Constants"
256 , "CoreArity"
257 , "CoreFVs"
258 , "CoreSubst"
259 , "CoreSyn"
260 , "CoreTidy"
261 , "CoreUnfold"
262 , "CoreUtils"
263 , "CoreSeq"
264 , "CoreStats"
265 , "CostCentre"
266 , "Ctype"
267 , "DataCon"
268 , "Demand"
269 , "Digraph"
270 , "DriverPhases"
271 , "DynFlags"
272 , "Encoding"
273 , "ErrUtils"
274 , "Exception"
275 , "ExtsCompat46"
276 , "FamInstEnv"
277 , "FastFunctions"
278 , "FastMutInt"
279 , "FastString"
280 , "FastTypes"
281 , "Fingerprint"
282 , "FiniteMap"
283 , "ForeignCall"
284 , "Hooks"
285 , "HsBinds"
286 , "HsDecls"
287 , "HsDoc"
288 , "HsExpr"
289 , "HsImpExp"
290 , "HsLit"
291 , "PlaceHolder"
292 , "HsPat"
293 , "HsSyn"
294 , "HsTypes"
295 , "HsUtils"
296 , "HscTypes"
297 , "IOEnv"
298 , "Id"
299 , "IdInfo"
300 , "IfaceSyn"
301 , "IfaceType"
302 , "InstEnv"
303 , "Kind"
304 , "Lexeme"
305 , "Lexer"
306 , "ListSetOps"
307 , "Literal"
308 , "Maybes"
309 , "MkCore"
310 , "MkId"
311 , "Module"
312 , "MonadUtils"
313 , "Name"
314 , "NameEnv"
315 , "NameSet"
316 , "OccName"
317 , "OccurAnal"
318 , "OptCoercion"
319 , "OrdList"
320 , "Outputable"
321 , "PackageConfig"
322 , "Packages"
323 , "Pair"
324 , "Panic"
325 , "PatSyn"
326 , "PipelineMonad"
327 , "Platform"
328 , "PlatformConstants"
329 , "PprCore"
330 , "PrelNames"
331 , "PrelRules"
332 , "Pretty"
333 , "PrimOp"
334 , "RdrName"
335 , "Rules"
336 , "Serialized"
337 , "SrcLoc"
338 , "StaticFlags"
339 , "StringBuffer"
340 , "TcEvidence"
341 , "TcRnTypes"
342 , "TcType"
343 , "TrieMap"
344 , "TyCon"
345 , "Type"
346 , "TypeRep"
347 , "TysPrim"
348 , "TysWiredIn"
349 , "Unify"
350 , "UniqFM"
351 , "UniqSet"
352 , "UniqSupply"
353 , "Unique"
354 , "Util"
355 , "Var"
356 , "VarEnv"
357 , "VarSet" ]
358 ghciModules = [ "Bitmap"
359 , "BlockId"
360 , "ByteCodeAsm"
361 , "ByteCodeInstr"
362 , "ByteCodeItbls"
363 , "CLabel"
364 , "Cmm"
365 , "CmmCallConv"
366 , "CmmExpr"
367 , "CmmInfo"
368 , "CmmMachOp"
369 , "CmmNode"
370 , "CmmSwitch"
371 , "CmmUtils"
372 , "CodeGen.Platform"
373 , "CodeGen.Platform.ARM"
374 , "CodeGen.Platform.ARM64"
375 , "CodeGen.Platform.NoRegs"
376 , "CodeGen.Platform.PPC"
377 , "CodeGen.Platform.PPC_Darwin"
378 , "CodeGen.Platform.SPARC"
379 , "CodeGen.Platform.X86"
380 , "CodeGen.Platform.X86_64"
381 , "FastBool"
382 , "Hoopl"
383 , "Hoopl.Dataflow"
384 , "InteractiveEvalTypes"
385 , "MkGraph"
386 , "PprCmm"
387 , "PprCmmDecl"
388 , "PprCmmExpr"
389 , "Reg"
390 , "RegClass"
391 , "SMRep"
392 , "StgCmmArgRep"
393 , "StgCmmClosure"
394 , "StgCmmEnv"
395 , "StgCmmLayout"
396 , "StgCmmMonad"
397 , "StgCmmProf"
398 , "StgCmmTicky"
399 , "StgCmmUtils"
400 , "StgSyn"
401 , "Stream" ]