Use GHC to compile C files (#380)
[hadrian.git] / src / Settings / Packages / Rts.hs
1 module Settings.Packages.Rts (rtsPackageArgs, rtsLibffiLibrary) where
2
3 import Base
4 import GHC
5 import Oracles.Config.Flag
6 import Oracles.Config.Setting
7 import Predicate
8 import Settings
9 import Settings.Path
10
11 rtsLibffiLibraryName :: Action FilePath
12 rtsLibffiLibraryName = do
13 useSystemFfi <- flag UseSystemFfi
14 windows <- windowsHost
15 return $ case (useSystemFfi, windows) of
16 (True , False) -> "ffi"
17 (False, False) -> "Cffi"
18 (_ , True ) -> "Cffi-6"
19
20 rtsLibffiLibrary :: Way -> Action FilePath
21 rtsLibffiLibrary way = do
22 name <- rtsLibffiLibraryName
23 suf <- libsuf way
24 return $ rtsBuildPath -/- "lib" ++ name ++ suf
25
26 rtsPackageArgs :: Args
27 rtsPackageArgs = package rts ? do
28 let yesNo = expr . fmap (\x -> if x then "YES" else "NO")
29 projectVersion <- getSetting ProjectVersion
30 hostPlatform <- getSetting HostPlatform
31 hostArch <- getSetting HostArch
32 hostOs <- getSetting HostOs
33 hostVendor <- getSetting HostVendor
34 buildPlatform <- getSetting BuildPlatform
35 buildArch <- getSetting BuildArch
36 buildOs <- getSetting BuildOs
37 buildVendor <- getSetting BuildVendor
38 targetPlatform <- getSetting TargetPlatform
39 targetArch <- getSetting TargetArch
40 targetOs <- getSetting TargetOs
41 targetVendor <- getSetting TargetVendor
42 ghcUnreg <- yesNo $ flag GhcUnregisterised
43 ghcEnableTNC <- yesNo ghcEnableTablesNextToCode
44 way <- getWay
45 path <- getBuildPath
46 top <- getTopDirectory
47 libffiName <- expr rtsLibffiLibraryName
48 ffiIncludeDir <- getSetting FfiIncludeDir
49 ffiLibraryDir <- getSetting FfiLibDir
50 ghclibDir <- expr installGhcLibDir
51 let cArgs =
52 [ arg "-Irts"
53 , arg $ "-I" ++ path
54 , arg $ "-DRtsWay=\"rts_" ++ show way ++ "\""
55 -- rts *must* be compiled with optimisations. The INLINE_HEADER macro
56 -- requires that functions are inlined to work as expected. Inlining
57 -- only happens for optimised builds. Otherwise we can assume that
58 -- there is a non-inlined variant to use instead. But rts does not
59 -- provide non-inlined alternatives and hence needs the function to
60 -- be inlined. See also #90.
61 , arg "-O2"
62
63 , Debug `wayUnit` way ? arg "-DDEBUG"
64 , way `elem` [debug, debugDynamic] ? arg "-DTICKY_TICKY"
65 , Profiling `wayUnit` way ? arg "-DPROFILING"
66 , Threaded `wayUnit` way ? arg "-DTHREADED_RTS"
67
68 , inputs ["//RtsMessages.c", "//Trace.c"] ?
69 arg ("-DProjectVersion=" ++ show projectVersion)
70
71 , input "//RtsUtils.c" ? append
72 [ "-DProjectVersion=" ++ show projectVersion
73 , "-DHostPlatform=" ++ show hostPlatform
74 , "-DHostArch=" ++ show hostArch
75 , "-DHostOS=" ++ show hostOs
76 , "-DHostVendor=" ++ show hostVendor
77 , "-DBuildPlatform=" ++ show buildPlatform
78 , "-DBuildArch=" ++ show buildArch
79 , "-DBuildOS=" ++ show buildOs
80 , "-DBuildVendor=" ++ show buildVendor
81 , "-DTargetPlatform=" ++ show targetPlatform
82 , "-DTargetArch=" ++ show targetArch
83 , "-DTargetOS=" ++ show targetOs
84 , "-DTargetVendor=" ++ show targetVendor
85 , "-DGhcUnregisterised=" ++ show ghcUnreg
86 , "-DGhcEnableTablesNextToCode=" ++ show ghcEnableTNC ]
87
88 , inputs ["//Evac.c", "//Evac_thr.c"] ? arg "-funroll-loops"
89
90 , inputs ["//Evac_thr.c", "//Scav_thr.c"] ?
91 append [ "-DPARALLEL_GC", "-Irts/sm" ]
92
93 , input "//StgCRun.c" ? windowsHost ? arg "-Wno-return-local-addr"
94 , input "//RetainerProfile.c" ? flag GccIsClang ?
95 append [ "-Wno-incompatible-pointer-types" ]
96 ]
97
98 mconcat
99 [ builder (Cc FindCDependencies) ? mconcat cArgs
100 , builder (Ghc CompileCWithGhc) ? mconcat (map (map ("-optc" ++) <$>) cArgs)
101 , builder Ghc ? arg "-Irts"
102 , builder HsCpp ? append
103 [ "-DTOP=" ++ show top
104 , "-DFFI_INCLUDE_DIR=" ++ show ffiIncludeDir
105 , "-DFFI_LIB_DIR=" ++ show ffiLibraryDir
106 , "-DFFI_LIB=" ++ show libffiName ]
107
108 , builder HsCpp ?
109 input "//package.conf.in" ?
110 output "//package.conf.install.raw" ?
111 append
112 [ "-DINSTALLING"
113 , "-DLIB_DIR=\"" ++ destDir ++ ghclibDir ++ "\""
114 , "-DINCLUDE_DIR=\"" ++ destDir ++ ghclibDir -/- "include\""
115 ]
116 ]
117
118 -- # If we're compiling on windows, enforce that we only support XP+
119 -- # Adding this here means it doesn't have to be done in individual .c files
120 -- # and also centralizes the versioning.
121 -- ifeq "$$(TargetOS_CPP)" "mingw32"
122 -- rts_dist_$1_CC_OPTS += -DWINVER=$(rts_WINVER)
123 -- endif
124
125 -- #-----------------------------------------------------------------------------
126 -- # Use system provided libffi
127
128 -- ifeq "$(UseSystemLibFFI)" "YES"
129
130 -- rts_PACKAGE_CPP_OPTS += -DFFI_INCLUDE_DIR=$(FFIIncludeDir)
131 -- rts_PACKAGE_CPP_OPTS += -DFFI_LIB_DIR=$(FFILibDir)
132 -- rts_PACKAGE_CPP_OPTS += '-DFFI_LIB='
133
134 -- endif
135
136 -- #-----------------------------------------------------------------------------
137 -- # Add support for reading DWARF debugging information, if available
138
139 -- ifeq "$(GhcRtsWithLibdw)" "YES"
140 -- rts_CC_OPTS += -DUSE_LIBDW
141 -- rts_PACKAGE_CPP_OPTS += -DUSE_LIBDW
142 -- endif
143
144 -- # If -DDEBUG is in effect, adjust package conf accordingly..
145 -- ifneq "$(strip $(filter -optc-DDEBUG,$(GhcRtsHcOpts)))" ""
146 -- rts_PACKAGE_CPP_OPTS += -DDEBUG
147 -- endif
148
149 -- ifeq "$(HaveLibMingwEx)" "YES"
150 -- rts_PACKAGE_CPP_OPTS += -DHAVE_LIBMINGWEX
151 -- endif
152
153
154
155 -- #-----------------------------------------------------------------------------
156 -- # Flags for compiling specific files
157 -- #
158 -- #
159
160 -- # Compile various performance-critical pieces *without* -fPIC -dynamic
161 -- # even when building a shared library. If we don't do this, then the
162 -- # GC runs about 50% slower on x86 due to the overheads of PIC. The
163 -- # cost of doing this is a little runtime linking and less sharing, but
164 -- # not much.
165 -- #
166 -- # On x86_64 this doesn't work, because all objects in a shared library
167 -- # must be compiled with -fPIC (since the 32-bit relocations generated
168 -- # by the default small memory can't be resolved at runtime). So we
169 -- # only do this on i386.
170 -- #
171 -- # This apparently doesn't work on OS X (Darwin) nor on Solaris.
172 -- # On Darwin we get errors of the form
173 -- #
174 -- # ld: absolute addressing (perhaps -mdynamic-no-pic) used in _stg_ap_0_fast from rts/dist/build/Apply.dyn_o not allowed in slidable image
175 -- #
176 -- # and lots of these warnings:
177 -- #
178 -- # ld: warning codegen in _stg_ap_pppv_fast (offset 0x0000005E) prevents image from loading in dyld shared cache
179 -- #
180 -- # On Solaris we get errors like:
181 -- #
182 -- # Text relocation remains referenced
183 -- # against symbol offset in file
184 -- # .rodata (section) 0x11 rts/dist/build/Apply.dyn_o
185 -- # ...
186 -- # ld: fatal: relocations remain against allocatable but non-writable sections
187 -- # collect2: ld returned 1 exit status
188
189 -- ifeq "$(TargetArch_CPP)" "i386"
190 -- i386_SPEED_HACK := "YES"
191 -- ifeq "$(TargetOS_CPP)" "darwin"
192 -- i386_SPEED_HACK := "NO"
193 -- endif
194 -- ifeq "$(TargetOS_CPP)" "solaris2"
195 -- i386_SPEED_HACK := "NO"
196 -- endif
197 -- endif
198
199 -- ifeq "$(TargetArch_CPP)" "i386"
200 -- ifeq "$(i386_SPEED_HACK)" "YES"
201 -- rts/sm/Evac_HC_OPTS += -fno-PIC
202 -- rts/sm/Evac_thr_HC_OPTS += -fno-PIC
203 -- rts/sm/Scav_HC_OPTS += -fno-PIC
204 -- rts/sm/Scav_thr_HC_OPTS += -fno-PIC
205 -- rts/sm/Compact_HC_OPTS += -fno-PIC
206 -- rts/sm/GC_HC_OPTS += -fno-PIC
207
208 -- # -static is also necessary for these bits, otherwise the NCG
209 -- # -generates dynamic references:
210 -- rts/Updates_HC_OPTS += -fno-PIC -static
211 -- rts/StgMiscClosures_HC_OPTS += -fno-PIC -static
212 -- rts/PrimOps_HC_OPTS += -fno-PIC -static
213 -- rts/Apply_HC_OPTS += -fno-PIC -static
214 -- rts/dist/build/AutoApply_HC_OPTS += -fno-PIC -static
215 -- endif
216 -- endif
217
218 -- # add CFLAGS for libffi
219 -- # ffi.h triggers prototype warnings, so disable them here:
220 -- ifeq "$(UseSystemLibFFI)" "YES"
221 -- LIBFFI_CFLAGS = $(addprefix -I,$(FFIIncludeDir))
222 -- else
223 -- LIBFFI_CFLAGS =
224 -- endif
225 -- rts/Interpreter_CC_OPTS += -Wno-strict-prototypes $(LIBFFI_CFLAGS)
226 -- rts/Adjustor_CC_OPTS += -Wno-strict-prototypes $(LIBFFI_CFLAGS)
227 -- rts/sm/Storage_CC_OPTS += -Wno-strict-prototypes $(LIBFFI_CFLAGS)
228
229 -- # inlining warnings happen in Compact
230 -- rts/sm/Compact_CC_OPTS += -Wno-inline
231
232 -- # emits warnings about call-clobbered registers on x86_64
233 -- rts/StgCRun_CC_OPTS += -w
234
235 -- rts/RetainerProfile_CC_OPTS += -w
236 -- rts/RetainerSet_CC_OPTS += -Wno-format
237 -- # On Windows:
238 -- rts/win32/ConsoleHandler_CC_OPTS += -w
239 -- rts/win32/ThrIOManager_CC_OPTS += -w
240 -- # The above warning suppression flags are a temporary kludge.
241 -- # While working on this module you are encouraged to remove it and fix
242 -- # any warnings in the module. See
243 -- # http://ghc.haskell.org/trac/ghc/wiki/WorkingConventions#Warnings
244 -- # for details
245
246 -- # Without this, thread_obj will not be inlined (at least on x86 with GCC 4.1.0)
247 -- ifneq "$(CC_CLANG_BACKEND)" "1"
248 -- rts/sm/Compact_CC_OPTS += -finline-limit=2500
249 -- endif