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