Use Cabal directly in place of ghc-cabal + make build root configurable (#531)
[hadrian.git] / src / Settings / Builders / Ghc.hs
1 module Settings.Builders.Ghc (ghcBuilderArgs, haddockGhcArgs) where
2
3 import Flavour
4 import GHC
5 import Settings.Builders.Common
6 import Settings.Warnings
7
8 import Hadrian.Haskell.Cabal
9 import Hadrian.Haskell.Cabal.PackageData as PD
10
11 ghcBuilderArgs :: Args
12 ghcBuilderArgs = mconcat [compileAndLinkHs, compileC, findHsDependencies]
13
14 compileAndLinkHs :: Args
15 compileAndLinkHs = (builder (Ghc CompileHs) ||^ builder (Ghc LinkHs)) ? do
16 mconcat [ arg "-Wall"
17 , commonGhcArgs
18 , splitObjects <$> flavour ? arg "-split-objs"
19 , ghcLinkArgs
20 , defaultGhcWarningsArgs
21 , builder (Ghc CompileHs) ? arg "-c"
22 , getInputs
23 , arg "-o", arg =<< getOutput ]
24
25 compileC :: Args
26 compileC = builder (Ghc CompileCWithGhc) ? do
27 way <- getWay
28 let ccArgs = [ getPackageData PD.ccOpts
29 , getStagedSettingList ConfCcArgs
30 , cIncludeArgs
31 , Dynamic `wayUnit` way ? pure [ "-fPIC", "-DDYNAMIC" ] ]
32 mconcat [ arg "-Wall"
33 , ghcLinkArgs
34 , commonGhcArgs
35 , mconcat (map (map ("-optc" ++) <$>) ccArgs)
36 , defaultGhcWarningsArgs
37 , arg "-c"
38 , getInputs
39 , arg "-o"
40 , arg =<< getOutput ]
41
42 ghcLinkArgs :: Args
43 ghcLinkArgs = builder (Ghc LinkHs) ? do
44 way <- getWay
45 pkg <- getPackage
46 libs <- pkg == hp2ps ? pure ["m"]
47 intLib <- getIntegerPackage
48 gmpLibs <- notStage0 ? intLib == integerGmp ? pure ["gmp"]
49 mconcat [ (Dynamic `wayUnit` way) ?
50 pure [ "-shared", "-dynamic", "-dynload", "deploy" ]
51 , arg "-no-auto-link-packages"
52 , nonHsMainPackage pkg ? arg "-no-hs-main"
53 , not (nonHsMainPackage pkg) ? arg "-rtsopts"
54 , pure [ "-optl-l" ++ lib | lib <- libs ++ gmpLibs ]
55 ]
56
57 findHsDependencies :: Args
58 findHsDependencies = builder (Ghc FindHsDependencies) ? do
59 ways <- getLibraryWays
60 mconcat [ arg "-M"
61 , commonGhcArgs
62 , arg "-include-pkg-deps"
63 , arg "-dep-makefile", arg =<< getOutput
64 , pure $ concat [ ["-dep-suffix", wayPrefix w] | w <- ways ]
65 , getInputs ]
66
67 haddockGhcArgs :: Args
68 haddockGhcArgs = mconcat [ commonGhcArgs, getPackageData PD.hcOpts ]
69
70 -- Used in ghcBuilderArgs, ghcCBuilderArgs, ghcMBuilderArgs and haddockGhcArgs.
71 commonGhcArgs :: Args
72 commonGhcArgs = do
73 way <- getWay
74 path <- getBuildPath
75 pkg <- getPackage
76 ghcVersion <- expr $ ghcVersionH
77 mconcat [ arg "-hisuf", arg $ hisuf way
78 , arg "-osuf" , arg $ osuf way
79 , arg "-hcsuf", arg $ hcsuf way
80 , wayGhcArgs
81 , packageGhcArgs
82 , includeGhcArgs
83 -- when compiling the rts for stage1 or stage2
84 -- we do not have the rts in the package db at
85 -- the time of builind it. As such we need to
86 -- explicity supply the path to the ghc-version
87 -- file, to prevent ghc from trying to open the
88 -- rts package from the package db, and failing
89 -- over while doing so.
90 , (pkg == rts) ? notStage0 ? arg ("-ghcversion-file=" ++ ghcVersion)
91 , map ("-optc" ++) <$> getStagedSettingList ConfCcArgs
92 , map ("-optP" ++) <$> getStagedSettingList ConfCppArgs
93 , map ("-optP" ++) <$> getPackageData PD.cppOpts
94 , arg "-odir" , arg path
95 , arg "-hidir" , arg path
96 , arg "-stubdir" , arg path ]
97
98 -- TODO: Do '-ticky' in all debug ways?
99 wayGhcArgs :: Args
100 wayGhcArgs = do
101 way <- getWay
102 mconcat [ if (Dynamic `wayUnit` way)
103 then pure ["-fPIC", "-dynamic"]
104 else arg "-static"
105 , (Threaded `wayUnit` way) ? arg "-optc-DTHREADED_RTS"
106 , (Debug `wayUnit` way) ? arg "-optc-DDEBUG"
107 , (Profiling `wayUnit` way) ? arg "-prof"
108 , (Logging `wayUnit` way) ? arg "-eventlog"
109 , (way == debug || way == debugDynamic) ?
110 pure ["-ticky", "-DTICKY_TICKY"] ]
111
112 packageGhcArgs :: Args
113 packageGhcArgs = withHsPackage $ \ctx -> do
114 pkgId <- expr $ pkgIdentifier ctx
115 mconcat [ arg "-hide-all-packages"
116 , arg "-no-user-package-db"
117 , packageDatabaseArgs
118 , libraryPackage ? arg ("-this-unit-id " ++ pkgId)
119 , map ("-package-id " ++) <$> getPackageData PD.depIpIds ]
120
121 includeGhcArgs :: Args
122 includeGhcArgs = do
123 pkg <- getPackage
124 path <- getBuildPath
125 root <- getBuildRoot
126 context <- getContext
127 srcDirs <- getPackageData PD.srcDirs
128 autogen <- expr $ autogenPath context
129 mconcat [ arg "-i"
130 , arg $ "-i" ++ path
131 , arg $ "-i" ++ autogen
132 , pure [ "-i" ++ pkgPath pkg -/- dir | dir <- srcDirs ]
133 , cIncludeArgs
134 , arg $ "-I" ++ root -/- generatedDir
135 , arg $ "-optc-I" ++ root -/- generatedDir
136 , (not $ nonCabalContext context) ?
137 pure [ "-optP-include", "-optP" ++ autogen -/- "cabal_macros.h" ] ]