8212b5fbcf34e48e2e5a2bde32a84e0c690a3095
[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 Flavour
7 import Packages
8 import Settings.Builders.Common
9 import Settings.Warnings
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 = [ getContextData 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, getContextData hcOpts ]
69
70 -- | Common GHC command line arguments used in 'ghcBuilderArgs',
71 -- 'ghcCBuilderArgs', 'ghcMBuilderArgs' and 'haddockGhcArgs'.
72 commonGhcArgs :: Args
73 commonGhcArgs = do
74 way <- getWay
75 path <- getBuildPath
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 RTS for Stage1 or Stage2 we do not have it (yet)
84 -- in the package database. We therefore explicity supply the path
85 -- to the @ghc-version@ file, to prevent GHC from trying to open the
86 -- RTS package in the package database and failing.
87 , package rts ? notStage0 ? arg ("-ghcversion-file=" ++ ghcVersion)
88 , map ("-optc" ++) <$> getStagedSettingList ConfCcArgs
89 , map ("-optP" ++) <$> getStagedSettingList ConfCppArgs
90 , map ("-optP" ++) <$> getContextData cppOpts
91 , arg "-odir" , arg path
92 , arg "-hidir" , arg path
93 , arg "-stubdir" , arg path ]
94
95 -- TODO: Do '-ticky' in all debug ways?
96 wayGhcArgs :: Args
97 wayGhcArgs = do
98 way <- getWay
99 mconcat [ if (Dynamic `wayUnit` way)
100 then pure ["-fPIC", "-dynamic"]
101 else arg "-static"
102 , (Threaded `wayUnit` way) ? arg "-optc-DTHREADED_RTS"
103 , (Debug `wayUnit` way) ? arg "-optc-DDEBUG"
104 , (Profiling `wayUnit` way) ? arg "-prof"
105 , (Logging `wayUnit` way) ? arg "-eventlog"
106 , (way == debug || way == debugDynamic) ?
107 pure ["-ticky", "-DTICKY_TICKY"] ]
108
109 packageGhcArgs :: Args
110 packageGhcArgs = do
111 package <- getPackage
112 pkgId <- expr $ pkgIdentifier package
113 mconcat [ arg "-hide-all-packages"
114 , arg "-no-user-package-db"
115 , packageDatabaseArgs
116 , libraryPackage ? arg ("-this-unit-id " ++ pkgId)
117 , map ("-package-id " ++) <$> getContextData depIds ]
118
119 includeGhcArgs :: Args
120 includeGhcArgs = do
121 pkg <- getPackage
122 path <- getBuildPath
123 root <- getBuildRoot
124 context <- getContext
125 srcDirs <- getContextData srcDirs
126 autogen <- expr $ autogenPath context
127 mconcat [ arg "-i"
128 , arg $ "-i" ++ path
129 , arg $ "-i" ++ autogen
130 , pure [ "-i" ++ pkgPath pkg -/- dir | dir <- srcDirs ]
131 , cIncludeArgs
132 , arg $ "-I" ++ root -/- generatedDir
133 , arg $ "-optc-I" ++ root -/- generatedDir
134 , pure ["-optP-include", "-optP" ++ autogen -/- "cabal_macros.h"] ]