Clean up.
[hadrian.git] / src / Settings / Builders / GhcCabal.hs
1 module Settings.Builders.GhcCabal (
2 cabalArgs, ghcCabalHsColourArgs, bootPackageDbArgs, customPackageArgs,
3 ccArgs, ccWarnings, argStagedSettingList
4 ) where
5
6 import Expression
7 import Predicates
8 import Settings
9
10 cabalArgs :: Args
11 cabalArgs = builder GhcCabal ? do
12 path <- getPackagePath
13 dir <- getTargetDirectory
14 mconcat [ arg "configure"
15 , arg path
16 , arg dir
17 , dllArgs
18 , withStaged Ghc
19 , withStaged GhcPkg
20 , stage0 ? bootPackageDbArgs
21 , libraryArgs
22 , with HsColour
23 , configureArgs
24 , packageConstraints
25 , withStaged Gcc
26 , notStage0 ? with Ld
27 , with Ar
28 , with Alex
29 , with Happy ]
30
31 ghcCabalHsColourArgs :: Args
32 ghcCabalHsColourArgs = builder GhcCabalHsColour ? do
33 path <- getPackagePath
34 dir <- getTargetDirectory
35 mconcat [ arg "hscolour"
36 , arg path
37 , arg dir ]
38
39 -- TODO: Isn't vanilla always built? If yes, some conditions are redundant.
40 -- TODO: Need compiler_stage1_CONFIGURE_OPTS += --disable-library-for-ghci?
41 libraryArgs :: Args
42 libraryArgs = do
43 ways <- getWays
44 ghcInt <- lift $ ghcWithInterpreter
45 append [ if vanilla `elem` ways
46 then "--enable-library-vanilla"
47 else "--disable-library-vanilla"
48 , if vanilla `elem` ways && ghcInt && not dynamicGhcPrograms
49 then "--enable-library-for-ghci"
50 else "--disable-library-for-ghci"
51 , if profiling `elem` ways
52 then "--enable-library-profiling"
53 else "--disable-library-profiling"
54 , if dynamic `elem` ways
55 then "--enable-shared"
56 else "--disable-shared" ]
57
58 -- TODO: LD_OPTS?
59 configureArgs :: Args
60 configureArgs = do
61 let conf key = appendSubD $ "--configure-option=" ++ key
62 cFlags = mconcat [ ccArgs
63 , remove ["-Werror"]
64 , argStagedSettingList ConfCcArgs ]
65 ldFlags = ldArgs <> (argStagedSettingList ConfGccLinkerArgs)
66 cppFlags = cppArgs <> (argStagedSettingList ConfCppArgs)
67 mconcat
68 [ conf "CFLAGS" cFlags
69 , conf "LDFLAGS" ldFlags
70 , conf "CPPFLAGS" cppFlags
71 , appendSubD "--gcc-options" $ cFlags <> ldFlags
72 , conf "--with-iconv-includes" $ argSettingList IconvIncludeDirs
73 , conf "--with-iconv-libraries" $ argSettingList IconvLibDirs
74 , conf "--with-gmp-includes" $ argSettingList GmpIncludeDirs
75 , conf "--with-gmp-libraries" $ argSettingList GmpLibDirs
76 , crossCompiling ? (conf "--host" $ argSetting TargetPlatformFull)
77 , conf "--with-cc" $ argStagedBuilderPath Gcc ]
78
79 bootPackageDbArgs :: Args
80 bootPackageDbArgs = do
81 path <- getSetting GhcSourcePath
82 arg $ "--package-db=" ++ path -/- "libraries/bootstrapping.conf"
83
84 -- This is a positional argument, hence:
85 -- * if it is empty, we need to emit one empty string argument;
86 -- * otherwise, we must collapse it into one space-separated string.
87 -- TODO: should be non-empty for compiler
88 dllArgs :: Args
89 dllArgs = arg ""
90
91 packageConstraints :: Args
92 packageConstraints = stage0 ? do
93 constraints <- lift . readFileLines $ bootPackageConstraints
94 append $ concat [ ["--constraint", c] | c <- constraints ]
95
96 -- TODO: should be in a different file
97 -- TODO: put all validating options together in one file
98 ccArgs :: Args
99 ccArgs = validating ? ccWarnings
100
101 -- TODO: should be in a different file
102 ccWarnings :: Args
103 ccWarnings = do
104 let notClang = fmap not gccIsClang
105 mconcat [ arg "-Werror"
106 , arg "-Wall"
107 , gccIsClang ? arg "-Wno-unknown-pragmas"
108 , notClang ? gccGe46 ? notWindowsHost ? arg "-Werror=unused-but-set-variable"
109 , notClang ? gccGe46 ? arg "-Wno-error=inline" ]
110
111 ldArgs :: Args
112 ldArgs = mempty
113
114 ghcIncludeDirs :: [FilePath]
115 ghcIncludeDirs = [ "includes", "includes/dist"
116 , "includes/dist-derivedconstants/header"
117 , "includes/dist-ghcconstants/header" ]
118
119 cppArgs :: Args
120 cppArgs = append $ map ("-I" ++) ghcIncludeDirs
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 nextStage <- fmap succ 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 nextStage
148 , arg $ "--flags=stage" ++ show nextStage
149 , arg "--disable-library-for-ghci"
150 , targetOs "openbsd" ? arg "--ld-options=-E"
151 , flag GhcUnregisterised ? arg "--ghc-option=-DNO_REGS"
152 , fmap not ghcWithSMP ? arg "--ghc-option=-DNOSMP"
153 , fmap not ghcWithSMP ? arg "--ghc-option=-optc-DNOSMP"
154 , (threaded `elem` rtsWays) ?
155 notStage0 ? arg "--ghc-option=-optc-DTHREADED_RTS"
156 , ghcWithNativeCodeGen ? arg "--flags=ncg"
157 , ghcWithInterpreter ?
158 notStage0 ? arg "--flags=ghci"
159 , ghcWithInterpreter ?
160 ghcEnableTablesNextToCode ?
161 fmap not (flag GhcUnregisterised) ?
162 notStage0 ? arg "--ghc-option=-DGHCI_TABLES_NEXT_TO_CODE"
163 , ghcWithInterpreter ?
164 ghciWithDebugger ?
165 notStage0 ? arg "--ghc-option=-DDEBUGGER"
166 , ghcProfiled ?
167 notStage0 ? arg "--ghc-pkg-option=--force"
168 ]
169 ]
170
171 withBuilderKey :: Builder -> String
172 withBuilderKey b = case b of
173 Ar -> "--with-ar="
174 Ld -> "--with-ld="
175 Gcc _ -> "--with-gcc="
176 Ghc _ -> "--with-ghc="
177 Alex -> "--with-alex="
178 Happy -> "--with-happy="
179 GhcPkg _ -> "--with-ghc-pkg="
180 HsColour -> "--with-hscolour="
181 _ -> error "withBuilderKey: not supported builder"
182
183 -- Expression 'with Gcc' appends "--with-gcc=/path/to/gcc" and needs Gcc.
184 with :: Builder -> Args
185 with b = specified b ? do
186 path <- lift $ builderPath b
187 lift $ needBuilder laxDependencies b
188 append [withBuilderKey b ++ path]
189
190 withStaged :: (Stage -> Builder) -> Args
191 withStaged sb = (with . sb) =<< getStage
192
193 argM :: Action String -> Args
194 argM = (arg =<<) . lift
195
196 argSetting :: Setting -> Args
197 argSetting = argM . setting
198
199 argSettingList :: SettingList -> Args
200 argSettingList = (append =<<) . lift . settingList
201
202 argStagedSettingList :: (Stage -> SettingList) -> Args
203 argStagedSettingList ss = (argSettingList . ss) =<< getStage
204
205 argStagedBuilderPath :: (Stage -> Builder) -> Args
206 argStagedBuilderPath sb = (argM . builderPath . sb) =<< getStage
207
208 -- Pass arguments to Gcc and corresponding lists of sub-arguments of GhcCabal
209 appendCcArgs :: [String] -> Args
210 appendCcArgs xs = do
211 mconcat [ stagedBuilder Gcc ? append xs
212 , stagedBuilder GccM ? append xs
213 , builder GhcCabal ? appendSub "--configure-option=CFLAGS" xs
214 , builder GhcCabal ? appendSub "--gcc-options" xs ]
215