Remove more validation errors (#628)
[hadrian.git] / src / Settings / Builders / GhcCabal.hs
1 module Settings.Builders.GhcCabal (
2 ghcCabalBuilderArgs
3 ) where
4
5 import Data.Maybe (fromJust)
6
7 import Builder ( ArMode ( Pack ) )
8 import Context
9 import Flavour
10 import GHC.Packages
11 import Hadrian.Builder (getBuilderPath, needBuilder )
12 import Hadrian.Haskell.Cabal
13 import Settings.Builders.Common
14
15 ghcCabalBuilderArgs :: Args
16 ghcCabalBuilderArgs = mconcat
17 [ builder (GhcCabal Conf) ? do
18 verbosity <- expr getVerbosity
19 top <- expr topDirectory
20 path <- getContextPath
21 stage <- getStage
22 mconcat [ arg "configure"
23 -- don't strip libraries when cross compiling.
24 -- XXX we need to set --with-strip= (stripCmdPath :: Action FilePath), and if it's ':' disable
25 -- stripping as well. As it is now, I believe we might have issues with stripping on
26 -- windows, as I can't see a consumer of `stripCmdPath`.
27 -- TODO: See https://github.com/snowleopard/hadrian/issues/549.
28 , flag CrossCompiling ? pure [ "--disable-executable-stripping"
29 , "--disable-library-stripping" ]
30 , arg "--cabal-file"
31 , arg =<< fromJust . pkgCabalFile <$> getPackage
32 , arg "--distdir"
33 , arg $ top -/- path
34 , arg "--ipid"
35 , arg "$pkg-$version"
36 , arg "--prefix"
37 , arg "${pkgroot}/.."
38 , withStaged $ Ghc CompileHs
39 , withStaged (GhcPkg Update)
40 , withBuilderArgs (GhcPkg Update stage)
41 , bootPackageDatabaseArgs
42 , libraryArgs
43 , configureArgs
44 , bootPackageConstraints
45 , withStaged $ Cc CompileC
46 , notStage0 ? with (Ld stage)
47 , withStaged (Ar Pack)
48 , with Alex
49 , with Happy
50 , verbosity < Chatty ?
51 pure [ "-v0", "--configure-option=--quiet"
52 , "--configure-option=--disable-option-checking"
53 ]
54 ]
55 ]
56
57 -- TODO: Isn't vanilla always built? If yes, some conditions are redundant.
58 -- TODO: Need compiler_stage1_CONFIGURE_OPTS += --disable-library-for-ghci?
59 -- TODO: should `elem` be `wayUnit`?
60 -- This approach still doesn't work. Previously libraries were build only in the
61 -- Default flavours and not using context.
62 libraryArgs :: Args
63 libraryArgs = do
64 flavourWays <- getLibraryWays
65 contextWay <- getWay
66 withGhci <- expr ghcWithInterpreter
67 dynPrograms <- dynamicGhcPrograms <$> expr flavour
68 let ways = flavourWays ++ [contextWay]
69 pure [ if vanilla `elem` ways
70 then "--enable-library-vanilla"
71 else "--disable-library-vanilla"
72 , if vanilla `elem` ways && withGhci && not dynPrograms
73 then "--enable-library-for-ghci"
74 else "--disable-library-for-ghci"
75 , if or [Profiling `wayUnit` way | way <- ways]
76 then "--enable-library-profiling"
77 else "--disable-library-profiling"
78 , if or [Dynamic `wayUnit` way | way <- ways]
79 then "--enable-shared"
80 else "--disable-shared" ]
81
82 -- TODO: LD_OPTS?
83 configureArgs :: Args
84 configureArgs = do
85 top <- expr topDirectory
86 root <- getBuildRoot
87 pkg <- getPackage
88 let conf key expr = do
89 values <- unwords <$> expr
90 not (null values) ?
91 arg ("--configure-option=" ++ key ++ "=" ++ values)
92 cFlags = mconcat [ remove ["-Werror"] cArgs
93 , getStagedSettingList ConfCcArgs
94 , arg $ "-I" ++ top -/- root -/- generatedDir
95 -- See https://github.com/snowleopard/hadrian/issues/523
96 , arg $ "-I" ++ top -/- pkgPath pkg
97 , arg $ "-I" ++ top -/- "includes" ]
98 ldFlags = ldArgs <> (getStagedSettingList ConfGccLinkerArgs)
99 cppFlags = cppArgs <> (getStagedSettingList ConfCppArgs)
100 cldFlags <- unwords <$> (cFlags <> ldFlags)
101 mconcat
102 [ conf "CFLAGS" cFlags
103 , conf "LDFLAGS" ldFlags
104 , conf "CPPFLAGS" cppFlags
105 , not (null cldFlags) ? arg ("--gcc-options=" ++ cldFlags)
106 , conf "--with-iconv-includes" $ arg =<< getSetting IconvIncludeDir
107 , conf "--with-iconv-libraries" $ arg =<< getSetting IconvLibDir
108 , conf "--with-gmp-includes" $ arg =<< getSetting GmpIncludeDir
109 , conf "--with-gmp-libraries" $ arg =<< getSetting GmpLibDir
110 , conf "--with-curses-libraries" $ arg =<< getSetting CursesLibDir
111 , flag CrossCompiling ? (conf "--host" $ arg =<< getSetting TargetPlatformFull)
112 , conf "--with-cc" $ arg =<< getBuilderPath . (Cc CompileC) =<< getStage
113 , notStage0 ? (arg =<< ("--ghc-option=-ghcversion-file=" ++) <$> expr ((-/-) <$> topDirectory <*> ghcVersionH))]
114
115 bootPackageConstraints :: Args
116 bootPackageConstraints = stage0 ? do
117 bootPkgs <- expr $ stagePackages Stage0
118 let pkgs = filter (\p -> p /= compiler && isLibrary p) bootPkgs
119 ctx <- getContext
120 constraints <- expr $ fmap catMaybes $ forM (sort pkgs) $ \pkg -> do
121 version <- pkgVersion (ctx { Context.package = pkg})
122 return $ fmap ((pkgName pkg ++ " == ") ++) version
123 pure $ concat [ ["--constraint", c] | c <- constraints ]
124
125 cppArgs :: Args
126 cppArgs = do
127 root <- getBuildRoot
128 arg $ "-I" ++ root -/- generatedDir
129
130 withBuilderKey :: Builder -> String
131 withBuilderKey b = case b of
132 Ar _ _ -> "--with-ar="
133 Ld _ -> "--with-ld="
134 Cc _ _ -> "--with-gcc="
135 Ghc _ _ -> "--with-ghc="
136 Alex -> "--with-alex="
137 Happy -> "--with-happy="
138 GhcPkg _ _ -> "--with-ghc-pkg="
139 _ -> error $ "withBuilderKey: not supported builder " ++ show b
140
141 -- Adds arguments to builders if needed.
142 withBuilderArgs :: Builder -> Args
143 withBuilderArgs b = case b of
144 GhcPkg _ stage -> do
145 top <- expr topDirectory
146 pkgDb <- expr $ packageDbPath stage
147 notStage0 ? arg ("--ghc-pkg-option=--global-package-db=" ++ top -/- pkgDb)
148 _ -> return [] -- no arguments
149
150
151 -- Expression 'with Alex' appends "--with-alex=/path/to/alex" and needs Alex.
152 with :: Builder -> Args
153 with b = do
154 path <- getBuilderPath b
155 if (null path) then mempty else do
156 top <- expr topDirectory
157 expr $ needBuilder b
158 arg $ withBuilderKey b ++ unifyPath (top </> path)
159
160 withStaged :: (Stage -> Builder) -> Args
161 withStaged sb = with . sb =<< getStage
162