Improve infrastructure for Cabal file parsing
[hadrian.git] / src / Settings / Builders / GhcCabal.hs
1 module Settings.Builders.GhcCabal (
2 ghcCabalBuilderArgs, ghcCabalHsColourBuilderArgs
3 ) where
4
5 import Hadrian.Haskell.Cabal
6
7 import Context
8 import Flavour
9 import Settings.Builders.Common hiding (package)
10 import Utilities
11
12 ghcCabalBuilderArgs :: Args
13 ghcCabalBuilderArgs = builder GhcCabal ? do
14 verbosity <- expr getVerbosity
15 top <- expr topDirectory
16 context <- getContext
17 path <- getBuildPath
18 when (package context /= deriveConstants) $ expr (need inplaceLibCopyTargets)
19 mconcat [ arg "configure"
20 , arg =<< pkgPath <$> getPackage
21 , arg $ top -/- path
22 , dll0Args
23 , withStaged $ Ghc CompileHs
24 , withStaged (GhcPkg Update)
25 , bootPackageDatabaseArgs
26 , libraryArgs
27 , with HsColour
28 , configureArgs
29 , bootPackageConstraints
30 , withStaged $ Cc CompileC
31 , notStage0 ? with Ld
32 , withStaged Ar
33 , with Alex
34 , with Happy
35 , verbosity < Chatty ? pure [ "-v0", "--configure-option=--quiet"
36 , "--configure-option=--disable-option-checking" ] ]
37
38 ghcCabalHsColourBuilderArgs :: Args
39 ghcCabalHsColourBuilderArgs = builder GhcCabalHsColour ? do
40 srcPath <- pkgPath <$> getPackage
41 top <- expr topDirectory
42 path <- getBuildPath
43 pure [ "hscolour", srcPath, top -/- path ]
44
45 -- TODO: Isn't vanilla always built? If yes, some conditions are redundant.
46 -- TODO: Need compiler_stage1_CONFIGURE_OPTS += --disable-library-for-ghci?
47 libraryArgs :: Args
48 libraryArgs = do
49 ways <- getLibraryWays
50 withGhci <- expr ghcWithInterpreter
51 dynPrograms <- dynamicGhcPrograms <$> expr flavour
52 pure [ if vanilla `elem` ways
53 then "--enable-library-vanilla"
54 else "--disable-library-vanilla"
55 , if vanilla `elem` ways && withGhci && not dynPrograms
56 then "--enable-library-for-ghci"
57 else "--disable-library-for-ghci"
58 , if profiling `elem` ways
59 then "--enable-library-profiling"
60 else "--disable-library-profiling"
61 , if dynamic `elem` ways
62 then "--enable-shared"
63 else "--disable-shared" ]
64
65 -- TODO: LD_OPTS?
66 configureArgs :: Args
67 configureArgs = do
68 top <- expr topDirectory
69 root <- getBuildRoot
70 let conf key expr = do
71 values <- unwords <$> expr
72 not (null values) ?
73 arg ("--configure-option=" ++ key ++ "=" ++ values)
74 cFlags = mconcat [ remove ["-Werror"] cArgs
75 , getStagedSettingList ConfCcArgs
76 , arg $ "-I" ++ top -/- root -/- generatedDir ]
77 ldFlags = ldArgs <> (getStagedSettingList ConfGccLinkerArgs)
78 cppFlags = cppArgs <> (getStagedSettingList ConfCppArgs)
79 cldFlags <- unwords <$> (cFlags <> ldFlags)
80 mconcat
81 [ conf "CFLAGS" cFlags
82 , conf "LDFLAGS" ldFlags
83 , conf "CPPFLAGS" cppFlags
84 , not (null cldFlags) ? arg ("--gcc-options=" ++ cldFlags)
85 , conf "--with-iconv-includes" $ arg =<< getSetting IconvIncludeDir
86 , conf "--with-iconv-libraries" $ arg =<< getSetting IconvLibDir
87 , conf "--with-gmp-includes" $ arg =<< getSetting GmpIncludeDir
88 , conf "--with-gmp-libraries" $ arg =<< getSetting GmpLibDir
89 , conf "--with-curses-libraries" $ arg =<< getSetting CursesLibDir
90 , crossCompiling ? (conf "--host" $ arg =<< getSetting TargetPlatformFull)
91 , conf "--with-cc" $ arg =<< getBuilderPath . (Cc CompileC) =<< getStage ]
92
93 bootPackageConstraints :: Args
94 bootPackageConstraints = stage0 ? do
95 bootPkgs <- expr $ stagePackages Stage0
96 let pkgs = filter (\p -> p /= compiler && isLibrary p) bootPkgs
97 constraints <- expr $ forM (sort pkgs) $ \pkg -> do
98 (name, version) <- pkgNameVersion pkg
99 return (name ++ " == " ++ version)
100 pure $ concat [ ["--constraint", c] | c <- constraints ]
101
102 cppArgs :: Args
103 cppArgs = do
104 root <- getBuildRoot
105 arg $ "-I" ++ root -/- generatedDir
106
107 withBuilderKey :: Builder -> String
108 withBuilderKey b = case b of
109 Ar _ -> "--with-ar="
110 Ld -> "--with-ld="
111 Cc _ _ -> "--with-gcc="
112 Ghc _ _ -> "--with-ghc="
113 Alex -> "--with-alex="
114 Happy -> "--with-happy="
115 GhcPkg _ _ -> "--with-ghc-pkg="
116 HsColour -> "--with-hscolour="
117 _ -> error $ "withBuilderKey: not supported builder " ++ show b
118
119 -- Expression 'with Alex' appends "--with-alex=/path/to/alex" and needs Alex.
120 with :: Builder -> Args
121 with b = isSpecified b ? do
122 top <- expr topDirectory
123 path <- getBuilderPath b
124 expr $ needBuilder b
125 arg $ withBuilderKey b ++ unifyPath (top </> path)
126
127 withStaged :: (Stage -> Builder) -> Args
128 withStaged sb = with . sb =<< getStage
129
130 -- This is a positional argument, hence:
131 -- * if it is empty, we need to emit one empty string argument;
132 -- * otherwise, we must collapse it into one space-separated string.
133 dll0Args :: Args
134 dll0Args = do
135 context <- getContext
136 dll0 <- expr $ buildDll0 context
137 withGhci <- expr ghcWithInterpreter
138 arg . unwords . concat $ [ modules | dll0 ]
139 ++ [ ghciModules | dll0 && withGhci ] -- see #9552
140 where
141 modules = [ "Annotations"
142 , "ApiAnnotation"
143 , "Avail"
144 , "Bag"
145 , "BasicTypes"
146 , "Binary"
147 , "BooleanFormula"
148 , "BreakArray"
149 , "BufWrite"
150 , "Class"
151 , "CmdLineParser"
152 , "CmmType"
153 , "CoAxiom"
154 , "ConLike"
155 , "Coercion"
156 , "Config"
157 , "Constants"
158 , "CoreArity"
159 , "CoreFVs"
160 , "CoreSubst"
161 , "CoreSyn"
162 , "CoreTidy"
163 , "CoreUnfold"
164 , "CoreUtils"
165 , "CoreSeq"
166 , "CoreStats"
167 , "CostCentre"
168 , "Ctype"
169 , "DataCon"
170 , "Demand"
171 , "Digraph"
172 , "DriverPhases"
173 , "DynFlags"
174 , "Encoding"
175 , "ErrUtils"
176 , "Exception"
177 , "ExtsCompat46"
178 , "FamInstEnv"
179 , "FastFunctions"
180 , "FastMutInt"
181 , "FastString"
182 , "FastTypes"
183 , "Fingerprint"
184 , "FiniteMap"
185 , "ForeignCall"
186 , "Hooks"
187 , "HsBinds"
188 , "HsDecls"
189 , "HsDoc"
190 , "HsExpr"
191 , "HsImpExp"
192 , "HsLit"
193 , "PlaceHolder"
194 , "HsPat"
195 , "HsSyn"
196 , "HsTypes"
197 , "HsUtils"
198 , "HscTypes"
199 , "IOEnv"
200 , "Id"
201 , "IdInfo"
202 , "IfaceSyn"
203 , "IfaceType"
204 , "InstEnv"
205 , "Kind"
206 , "Lexeme"
207 , "Lexer"
208 , "ListSetOps"
209 , "Literal"
210 , "Maybes"
211 , "MkCore"
212 , "MkId"
213 , "Module"
214 , "MonadUtils"
215 , "Name"
216 , "NameEnv"
217 , "NameSet"
218 , "OccName"
219 , "OccurAnal"
220 , "OptCoercion"
221 , "OrdList"
222 , "Outputable"
223 , "PackageConfig"
224 , "Packages"
225 , "Pair"
226 , "Panic"
227 , "PatSyn"
228 , "PipelineMonad"
229 , "Platform"
230 , "PlatformConstants"
231 , "PprCore"
232 , "PrelNames"
233 , "PrelRules"
234 , "Pretty"
235 , "PrimOp"
236 , "RdrName"
237 , "Rules"
238 , "Serialized"
239 , "SrcLoc"
240 , "StaticFlags"
241 , "StringBuffer"
242 , "TcEvidence"
243 , "TcRnTypes"
244 , "TcType"
245 , "TrieMap"
246 , "TyCon"
247 , "Type"
248 , "TypeRep"
249 , "TysPrim"
250 , "TysWiredIn"
251 , "Unify"
252 , "UniqFM"
253 , "UniqSet"
254 , "UniqSupply"
255 , "Unique"
256 , "Util"
257 , "Var"
258 , "VarEnv"
259 , "VarSet" ]
260 ghciModules = [ "Bitmap"
261 , "BlockId"
262 , "ByteCodeAsm"
263 , "ByteCodeInstr"
264 , "ByteCodeItbls"
265 , "CLabel"
266 , "Cmm"
267 , "CmmCallConv"
268 , "CmmExpr"
269 , "CmmInfo"
270 , "CmmMachOp"
271 , "CmmNode"
272 , "CmmSwitch"
273 , "CmmUtils"
274 , "CodeGen.Platform"
275 , "CodeGen.Platform.ARM"
276 , "CodeGen.Platform.ARM64"
277 , "CodeGen.Platform.NoRegs"
278 , "CodeGen.Platform.PPC"
279 , "CodeGen.Platform.PPC_Darwin"
280 , "CodeGen.Platform.SPARC"
281 , "CodeGen.Platform.X86"
282 , "CodeGen.Platform.X86_64"
283 , "FastBool"
284 , "InteractiveEvalTypes"
285 , "MkGraph"
286 , "PprCmm"
287 , "PprCmmDecl"
288 , "PprCmmExpr"
289 , "Reg"
290 , "RegClass"
291 , "SMRep"
292 , "StgCmmArgRep"
293 , "StgCmmClosure"
294 , "StgCmmEnv"
295 , "StgCmmLayout"
296 , "StgCmmMonad"
297 , "StgCmmProf"
298 , "StgCmmTicky"
299 , "StgCmmUtils"
300 , "StgSyn"
301 , "Stream" ]