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