Hadrian: remove unneeded imports.
[ghc.git] / hadrian / src / Settings / Builders / Ghc.hs
1 module Settings.Builders.Ghc (ghcBuilderArgs, haddockGhcArgs) where
2
3 import Hadrian.Haskell.Cabal
4 import Hadrian.Haskell.Cabal.Type
5
6 import Packages
7 import Settings.Builders.Common
8 import Settings.Warnings
9 import qualified Context as Context
10 import Rules.Libffi (libffiName)
11
12 ghcBuilderArgs :: Args
13 ghcBuilderArgs = mconcat [ compileAndLinkHs, compileC, findHsDependencies
14 , toolArgs]
15
16 toolArgs :: Args
17 toolArgs = do
18 builder (Ghc ToolArgs) ? mconcat
19 [ packageGhcArgs
20 , includeGhcArgs
21 , map ("-optc" ++) <$> getStagedSettingList ConfCcArgs
22 , map ("-optP" ++) <$> getStagedSettingList ConfCppArgs
23 , map ("-optP" ++) <$> getContextData cppOpts
24 ]
25
26 compileAndLinkHs :: Args
27 compileAndLinkHs = (builder (Ghc CompileHs) ||^ builder (Ghc LinkHs)) ? do
28 mconcat [ arg "-Wall"
29 , commonGhcArgs
30 , ghcLinkArgs
31 , defaultGhcWarningsArgs
32 , builder (Ghc CompileHs) ? arg "-c"
33 , getInputs
34 , arg "-o", arg =<< getOutput ]
35
36 compileC :: Args
37 compileC = builder (Ghc CompileCWithGhc) ? do
38 way <- getWay
39 let ccArgs = [ getContextData ccOpts
40 , getStagedSettingList ConfCcArgs
41 , cIncludeArgs
42 , Dynamic `wayUnit` way ? pure [ "-fPIC", "-DDYNAMIC" ] ]
43 mconcat [ arg "-Wall"
44 , ghcLinkArgs
45 , commonGhcArgs
46 , mconcat (map (map ("-optc" ++) <$>) ccArgs)
47 , defaultGhcWarningsArgs
48 , arg "-c"
49 , getInputs
50 , arg "-o"
51 , arg =<< getOutput ]
52
53 ghcLinkArgs :: Args
54 ghcLinkArgs = builder (Ghc LinkHs) ? do
55 pkg <- getPackage
56 libs <- getContextData extraLibs
57 libDirs <- getContextData extraLibDirs
58 fmwks <- getContextData frameworks
59 darwin <- expr osxHost
60 way <- getWay
61
62 -- Relative path from the output (rpath $ORIGIN).
63 originPath <- dropFileName <$> getOutput
64 context <- getContext
65 libPath' <- expr (libPath context)
66 st <- getStage
67 distDir <- expr (Context.distDir st)
68
69 useSystemFfi <- expr (flag UseSystemFfi)
70 buildPath <- getBuildPath
71 libffiName' <- libffiName
72
73 let
74 dynamic = Dynamic `wayUnit` way
75 distPath = libPath' -/- distDir
76 originToLibsDir = makeRelativeNoSysLink originPath distPath
77 rpath | darwin = "@loader_path" -/- originToLibsDir
78 | otherwise = "$ORIGIN" -/- originToLibsDir
79
80 -- TODO: an alternative would be to generalize by linking with extra
81 -- bundled libraries, but currently the rts is the only use case. It is
82 -- a special case when `useSystemFfi == True`: the ffi library files
83 -- are not actually bundled with the rts. Perhaps ffi should be part of
84 -- rts's extra libraries instead of extra bundled libraries in that
85 -- case. Care should be take as to not break the make build.
86 rtsFfiArg = package rts ? not useSystemFfi ? mconcat
87 [ arg ("-L" ++ buildPath)
88 , arg ("-l" ++ libffiName')
89 ]
90
91 mconcat [ dynamic ? mconcat
92 [ arg "-dynamic"
93 -- TODO what about windows?
94 , isLibrary pkg ? pure [ "-shared", "-dynload", "deploy" ]
95 , hostSupportsRPaths ? arg ("-optl-Wl,-rpath," ++ rpath)
96 , hostSupportsRPaths ? arg ("-optl-Wl,-rpath,$ORIGIN")
97 ]
98 , arg "-no-auto-link-packages"
99 , nonHsMainPackage pkg ? arg "-no-hs-main"
100 , not (nonHsMainPackage pkg) ? arg "-rtsopts"
101 , pure [ "-l" ++ lib | lib <- libs ]
102 , pure [ "-L" ++ libDir | libDir <- libDirs ]
103 , rtsFfiArg
104 , darwin ? pure (concat [ ["-framework", fmwk] | fmwk <- fmwks ])
105 ]
106
107 findHsDependencies :: Args
108 findHsDependencies = builder (Ghc FindHsDependencies) ? do
109 ways <- getLibraryWays
110 mconcat [ arg "-M"
111 , commonGhcArgs
112 , arg "-include-pkg-deps"
113 , arg "-dep-makefile", arg =<< getOutput
114 , pure $ concat [ ["-dep-suffix", wayPrefix w] | w <- ways ]
115 , getInputs ]
116
117 haddockGhcArgs :: Args
118 haddockGhcArgs = mconcat [ commonGhcArgs
119 , getContextData hcOpts
120 , ghcWarningsArgs ]
121
122 -- | Common GHC command line arguments used in 'ghcBuilderArgs',
123 -- 'ghcCBuilderArgs', 'ghcMBuilderArgs' and 'haddockGhcArgs'.
124 commonGhcArgs :: Args
125 commonGhcArgs = do
126 way <- getWay
127 path <- getBuildPath
128 ghcVersion <- expr ghcVersionH
129 mconcat [ arg "-hisuf", arg $ hisuf way
130 , arg "-osuf" , arg $ osuf way
131 , arg "-hcsuf", arg $ hcsuf way
132 , wayGhcArgs
133 , packageGhcArgs
134 , includeGhcArgs
135 -- When compiling RTS for Stage1 or Stage2 we do not have it (yet)
136 -- in the package database. We therefore explicity supply the path
137 -- to the @ghc-version@ file, to prevent GHC from trying to open the
138 -- RTS package in the package database and failing.
139 , package rts ? notStage0 ? arg ("-ghcversion-file=" ++ ghcVersion)
140 , map ("-optc" ++) <$> getStagedSettingList ConfCcArgs
141 , map ("-optP" ++) <$> getStagedSettingList ConfCppArgs
142 , map ("-optP" ++) <$> getContextData cppOpts
143 , arg "-outputdir", arg path ]
144
145 -- TODO: Do '-ticky' in all debug ways?
146 wayGhcArgs :: Args
147 wayGhcArgs = do
148 way <- getWay
149 mconcat [ if Dynamic `wayUnit` way
150 then pure ["-fPIC", "-dynamic"]
151 else arg "-static"
152 , (Threaded `wayUnit` way) ? arg "-optc-DTHREADED_RTS"
153 , (Debug `wayUnit` way) ? arg "-optc-DDEBUG"
154 , (Profiling `wayUnit` way) ? arg "-prof"
155 , (Logging `wayUnit` way) ? arg "-eventlog"
156 , (way == debug || way == debugDynamic) ?
157 pure ["-ticky", "-DTICKY_TICKY"] ]
158
159 packageGhcArgs :: Args
160 packageGhcArgs = do
161 package <- getPackage
162 pkgId <- expr $ pkgIdentifier package
163 mconcat [ arg "-hide-all-packages"
164 , arg "-no-user-package-db"
165 , packageDatabaseArgs
166 , libraryPackage ? arg ("-this-unit-id " ++ pkgId)
167 , map ("-package-id " ++) <$> getContextData depIds ]
168
169 includeGhcArgs :: Args
170 includeGhcArgs = do
171 pkg <- getPackage
172 path <- getBuildPath
173 root <- getBuildRoot
174 context <- getContext
175 srcDirs <- getContextData srcDirs
176 autogen <- expr $ autogenPath context
177 let cabalMacros = autogen -/- "cabal_macros.h"
178 expr $ need [cabalMacros]
179 mconcat [ arg "-i"
180 , arg $ "-i" ++ path
181 , arg $ "-i" ++ autogen
182 , pure [ "-i" ++ pkgPath pkg -/- dir | dir <- srcDirs ]
183 , cIncludeArgs
184 , arg $ "-I" ++ root -/- generatedDir
185 , arg $ "-optc-I" ++ root -/- generatedDir
186 , pure ["-optP-include", "-optP" ++ cabalMacros] ]