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