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