94b5b211c18a0e813f3234fdbf7fdb444b1b6207
[ghc.git] / src / Settings / Builders / Ghc.hs
1 module Settings.Builders.Ghc (
2 ghcBuilderArgs, ghcCBuilderArgs, ghcMBuilderArgs, haddockGhcArgs
3 ) where
4
5 import Hadrian.Haskell.Cabal
6
7 import Flavour
8 import Rules.Gmp
9 import Settings.Builders.Common
10
11 ghcBuilderArgs :: Args
12 ghcBuilderArgs = (builder (Ghc CompileHs) ||^ builder (Ghc LinkHs)) ? do
13 needTouchy
14 mconcat [ arg "-Wall"
15 , commonGhcArgs
16 , splitObjectsArgs
17 , ghcLinkArgs
18 , builder (Ghc CompileHs) ? arg "-c"
19 , getInputs
20 , arg "-o", arg =<< getOutput ]
21
22 needTouchy :: Expr ()
23 needTouchy = notStage0 ? windowsHost ? do
24 touchyPath <- expr $ programPath (vanillaContext Stage0 touchy)
25 expr $ need [touchyPath]
26
27 ghcCBuilderArgs :: Args
28 ghcCBuilderArgs = builder (Ghc CompileCWithGhc) ? do
29 way <- getWay
30 let ccArgs = [ getPkgDataList CcArgs
31 , getStagedSettingList ConfCcArgs
32 , cIncludeArgs
33 , arg "-Werror"
34 , Dynamic `wayUnit` way ? pure [ "-fPIC", "-DDYNAMIC" ] ]
35
36 mconcat [ arg "-Wall"
37 , ghcLinkArgs
38 , commonGhcArgs
39 , mconcat (map (map ("-optc" ++) <$>) ccArgs)
40 , arg "-c"
41 , getInputs
42 , arg "-o"
43 , arg =<< getOutput ]
44
45 ghcLinkArgs :: Args
46 ghcLinkArgs = builder (Ghc LinkHs) ? do
47 stage <- getStage
48 way <- getWay
49 pkg <- getPackage
50 libs <- getPkgDataList DepExtraLibs
51 libDirs <- getPkgDataList DepLibDirs
52 intLib <- expr (integerLibrary =<< flavour)
53 gmpLibs <- if stage > Stage0 && intLib == integerGmp
54 then do -- TODO: get this data more gracefully
55 let strip = fromMaybe "" . stripPrefix "extra-libraries: "
56 buildInfo <- expr $ readFileLines gmpBuildInfoPath
57 return $ concatMap (words . strip) buildInfo
58 else return []
59 mconcat [ (Dynamic `wayUnit` way) ?
60 pure [ "-shared", "-dynamic", "-dynload", "deploy" ]
61 , arg "-no-auto-link-packages"
62 , nonHsMainPackage pkg ? arg "-no-hs-main"
63 , not (nonHsMainPackage pkg) ? arg "-rtsopts"
64 , pure [ "-optl-l" ++ lib | lib <- libs ++ gmpLibs ]
65 , pure [ "-optl-L" ++ unifyPath dir | dir <- libDirs ] ]
66
67 splitObjectsArgs :: Args
68 splitObjectsArgs = splitObjects <$> flavour ? do
69 expr $ need [ghcSplitPath]
70 arg "-split-objs"
71
72 ghcMBuilderArgs :: Args
73 ghcMBuilderArgs = builder (Ghc FindHsDependencies) ? do
74 ways <- getLibraryWays
75 mconcat [ arg "-M"
76 , commonGhcArgs
77 , arg "-include-pkg-deps"
78 , arg "-dep-makefile", arg =<< getOutput
79 , pure $ concat [ ["-dep-suffix", wayPrefix w] | w <- ways ]
80 , getInputs ]
81
82 haddockGhcArgs :: Args
83 haddockGhcArgs = mconcat [ commonGhcArgs, getPkgDataList HsArgs ]
84
85 -- Used in ghcBuilderArgs, ghcCBuilderArgs, ghcMBuilderArgs and haddockGhcArgs.
86 commonGhcArgs :: Args
87 commonGhcArgs = do
88 way <- getWay
89 path <- getBuildPath
90 pkg <- getPackage
91 when (isLibrary pkg) $ do
92 context <- getContext
93 conf <- expr $ pkgConfFile context
94 expr $ need [conf]
95 mconcat [ arg "-hisuf", arg $ hisuf way
96 , arg "-osuf" , arg $ osuf way
97 , arg "-hcsuf", arg $ hcsuf way
98 , wayGhcArgs
99 , packageGhcArgs
100 , includeGhcArgs
101 , map ("-optc" ++) <$> getStagedSettingList ConfCcArgs
102 , map ("-optP" ++) <$> getStagedSettingList ConfCppArgs
103 , map ("-optP" ++) <$> getPkgDataList CppArgs
104 , arg "-odir" , arg path
105 , arg "-hidir" , arg path
106 , arg "-stubdir" , arg path ]
107
108 -- TODO: Do '-ticky' in all debug ways?
109 wayGhcArgs :: Args
110 wayGhcArgs = do
111 way <- getWay
112 mconcat [ if (Dynamic `wayUnit` way)
113 then pure ["-fPIC", "-dynamic"]
114 else arg "-static"
115 , (Threaded `wayUnit` way) ? arg "-optc-DTHREADED_RTS"
116 , (Debug `wayUnit` way) ? arg "-optc-DDEBUG"
117 , (Profiling `wayUnit` way) ? arg "-prof"
118 , (Logging `wayUnit` way) ? arg "-eventlog"
119 , (way == debug || way == debugDynamic) ?
120 pure ["-ticky", "-DTICKY_TICKY"] ]
121
122 packageGhcArgs :: Args
123 packageGhcArgs = withHsPackage $ \cabalFile -> do
124 pkgId <- expr $ pkgIdentifier cabalFile
125 mconcat [ arg "-hide-all-packages"
126 , arg "-no-user-package-db"
127 , bootPackageDatabaseArgs
128 , libraryPackage ? arg ("-this-unit-id " ++ pkgId)
129 , map ("-package-id " ++) <$> getPkgDataList DepIds ]
130
131 includeGhcArgs :: Args
132 includeGhcArgs = do
133 pkg <- getPackage
134 path <- getBuildPath
135 root <- getBuildRoot
136 context <- getContext
137 srcDirs <- getPkgDataList SrcDirs
138 autogen <- expr $ autogenPath context
139 mconcat [ arg "-i"
140 , arg $ "-i" ++ path
141 , arg $ "-i" ++ autogen
142 , pure [ "-i" ++ pkgPath pkg -/- dir | dir <- srcDirs ]
143 , cIncludeArgs
144 , arg $ "-I" ++ root -/- generatedDir
145 , arg $ "-optc-I" ++ root -/- generatedDir
146 , (not $ nonCabalContext context) ?
147 pure [ "-optP-include", "-optP" ++ autogen -/- "cabal_macros.h" ] ]