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