GHC new build system megapatch
[ghc-hetmet.git] / rts / ghc.mk
1 # -----------------------------------------------------------------------------
2 # Building the RTS
3
4 # We build the RTS with stage 1
5 rts_dist_HC = $(GHC_STAGE1)
6
7 rts_WAYS = $(strip $(GhcLibWays) $(GhcRTSWays))
8
9 ALL_RTS_LIBS = $(foreach way,$(rts_WAYS),rts/dist/build/libHSrts$($(way)__way).a)
10 all_rts : $(ALL_RTS_LIBS)
11
12 # The per-dir options
13 $(eval $(call distdir-opts,rts,dist))
14
15 # -----------------------------------------------------------------------------
16 # Defining the sources
17
18 ALL_DIRS = hooks parallel sm
19
20 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
21 ALL_DIRS += win32
22 else
23 ALL_DIRS += posix
24 endif
25
26 EXCLUDED_SRCS += rts/parallel/SysMan.c
27 EXCLUDED_SRCS += rts/dyn-wrapper.c
28 EXCLUDED_SRCS += $(wildcard rts/Vis*.c)
29
30 rts_C_SRCS = $(filter-out $(EXCLUDED_SRCS),$(wildcard rts/*.c $(foreach dir,$(ALL_DIRS),rts/$(dir)/*.c)))
31 rts_CMM_SRCS = $(wildcard rts/*.cmm)
32
33 ifneq "$(findstring $(TargetArch_CPP), powerpc powerpc64)" ""
34 rts_S_SRCS += rts/AdjustorAsm.S
35 else
36 ifneq "$(findstring $(TargetOS_CPP), darwin)" ""
37 rts_S_SRCS += rts/AdjustorAsm.S
38 endif
39 endif
40
41 ifeq "$(GhcUnregisterised)" "YES"
42 GENAPPLY_OPTS = -u
43 endif
44
45 rts_AUTO_APPLY_CMM = rts/dist/build/AutoApply.cmm
46
47 $(rts_AUTO_APPLY_CMM): $(GENAPPLY_INPLACE)
48         $(RM) $@
49         $(GENAPPLY_INPLACE) >$@
50
51 rts/dist/build/sm/Evac_thr.c : rts/sm/Evac.c
52         $(MKDIRHIER) $(dir $@)
53         cp $< $@
54 rts/dist/build/sm/Scav_thr.c : rts/sm/Scav.c
55         $(MKDIRHIER) $(dir $@)
56         cp $< $@
57
58 rts_H_FILES = $(wildcard $(GHC_INCLUDE_DIR)/*.h) $(wildcard rts/*.h)
59
60 #-----------------------------------------------------------------------------
61 # Building one way
62
63 define cmm-suffix-rules
64 # $1 = dir
65 # $2 = distdir
66 # $3 = way
67
68 # .cmm files depend on all the .h files, to a first approximation.
69
70 $1/$2/build/%.$$($3_way_)o : $1/%.cmm $$(rts_H_FILES) $$($1_$2_HC)
71         $$($1_$2_HC) $$($1_$2_$3_MOST_HC_OPTS) -c $$< -o $$@
72
73 $1/$2/build/%.$$($3_way_)o : $1/$2/build/%.cmm $$(rts_H_FILES) $$($1_$2_HC)
74         $$($1_$2_HC) $$($1_$2_$3_MOST_HC_OPTS) -c $$< -o $$@
75
76 $1/$2/build/%.$$($3_way_)hc : $1/%.cmm $$(rts_H_FILES) $$($1_$2_HC)
77         $$($1_$2_HC) $$($1_$2_$3_MOST_HC_OPTS) -c $$< -o $$@
78
79 $1/$2/build/%.$$($3_way_)hc : $1/$2/build/%.cmm $$(rts_H_FILES) $$($1_$2_HC)
80         $$($1_$2_HC) $$($1_$2_$3_MOST_HC_OPTS) -c $$< -o $$@
81
82 $1/$2/build/%.$$($3_way_)s : $1/%.cmm $$(rts_H_FILES) $$($1_$2_HC)
83         $$($1_$2_HC) $$($1_$2_$3_MOST_HC_OPTS) -c $$< -o $$@
84
85 $1/$2/build/%.$$($3_way_)s : $1/$2/build/%.cmm $$(rts_H_FILES) $$($1_$2_HC)
86         $$($1_$2_HC) $$($1_$2_$3_MOST_HC_OPTS) -c $$< -o $$@
87
88 endef
89
90
91 define build-rts-way # args: $1 = way
92
93 # The per-way CC_OPTS
94 ifneq "$$(findstring debug, $1)" ""
95 rts_dist_$1_HC_OPTS =
96 rts_dist_$1_CC_OPTS = -g -O0
97 else
98 rts_dist_$1_HC_OPTS = $(GhcRtsHcOpts)
99 rts_dist_$1_CC_OPTS = $(GhcRtsCcOpts)
100 endif
101
102 ifneq "$$(findstring thr, $1)" ""
103 rts_$1_EXTRA_C_SRCS  =  rts/dist/build/sm/Evac_thr.c rts/dist/build/sm/Scav_thr.c
104 endif
105
106 $(call distdir-way-opts,rts,dist,$1)
107 $(call c-suffix-rules,rts,dist,$1,YES)
108 $(call cmm-suffix-rules,rts,dist,$1)
109
110 rts_$1_LIB = rts/dist/build/libHSrts$$($1__way).a
111
112 rts_$1_C_OBJS   = $$(patsubst rts/%.c,rts/dist/build/%.$$($1_osuf),$$(rts_C_SRCS)) $$(patsubst %.c,%.$$($1_osuf),$$(rts_$1_EXTRA_C_SRCS))
113 rts_$1_S_OBJS   = $$(patsubst rts/%.S,rts/dist/build/%.$$($1_osuf),$$(rts_S_SRCS))
114 rts_$1_CMM_OBJS = $$(patsubst rts/%.cmm,rts/dist/build/%.$$($1_osuf),$$(rts_CMM_SRCS)) $$(patsubst %.cmm,%.$$($1_osuf),$(rts_AUTO_APPLY_CMM))
115
116 rts_$1_OBJS = $$(rts_$1_C_OBJS) $$(rts_$1_S_OBJS) $$(rts_$1_CMM_OBJS)
117
118 $$(rts_$1_LIB) : $$(rts_$1_OBJS)
119         $$(RM) $$@
120         echo $$(rts_$1_OBJS) | xargs $$(AR) $$(EXTRA_AR_ARGS) $$@
121
122 endef
123
124 # And expand the above for each way:
125 $(foreach way,$(rts_WAYS),$(eval $(call build-rts-way,$(way))))
126
127 #-----------------------------------------------------------------------------
128 # Flags for compiling every file
129
130 # gcc provides lots of useful warnings if you ask it.
131 # This is a pretty good list to start with - use a # to comment out
132 # any you don't like.
133 WARNING_OPTS += -Wall
134 WARNING_OPTS += -W
135 WARNING_OPTS += -Wstrict-prototypes 
136 WARNING_OPTS += -Wmissing-prototypes 
137 WARNING_OPTS += -Wmissing-declarations
138 WARNING_OPTS += -Winline
139 WARNING_OPTS += -Waggregate-return
140 #WARNING_OPTS += -Wpointer-arith
141 #WARNING_OPTS += -Wbad-function-cast
142 #WARNING_OPTS += -Wcast-align
143 #WARNING_OPTS += -Wnested-externs
144 #WARNING_OPTS += -Wshadow
145 #WARNING_OPTS += -Wcast-qual
146 #WARNING_OPTS += -Wno-unused 
147 #WARNING_OPTS += -Wredundant-decls 
148 #WARNING_OPTS += -Wconversion
149
150 STANDARD_OPTS += -I$(GHC_INCLUDE_DIR) -I$(GHC_RTS_DIR) -Irts/parallel -Irts/sm -Irts/eventlog
151 # COMPILING_RTS is only used when building Win32 DLL support.
152 STANDARD_OPTS += -DCOMPILING_RTS
153
154 # HC_OPTS is included in both .c and .cmm compilations, whereas CC_OPTS is
155 # only included in .c compilations.  HC_OPTS included the WAY_* opts, which
156 # must be included in both types of compilations.
157
158 rts_CC_OPTS += $(WARNING_OPTS)
159 rts_CC_OPTS += $(STANDARD_OPTS)
160
161 rts_HC_OPTS += $(STANDARD_OPTS) -package-name rts
162
163 ifneq "$(GhcWithSMP)" "YES"
164 rts_CC_OPTS += -DNOSMP
165 rts_HC_OPTS += -optc-DNOSMP
166 endif
167
168 ifeq "$(UseLibFFIForAdjustors)" "YES"
169 rts_CC_OPTS += -DUSE_LIBFFI_FOR_ADJUSTORS
170 endif
171
172 ifeq "$(Windows)" "YES"
173 # SDM: when compiled with -fasm the RTS currently has bogus references to 
174 # __imp_base_ things, so working around for now:
175 rts_HC_OPTS += -fvia-C
176 endif
177
178 ifneq "$(DYNAMIC_RTS)" "YES"
179 rts_HC_OPTS += -static
180 else
181 $(error ToDo: DYNAMIC_RTS)
182 endif
183
184 # Mac OS X: make sure we compile for the right OS version
185 rts_CC_OPTS += $(MACOSX_DEPLOYMENT_CC_OPTS)
186 rts_HC_OPTS += $(addprefix -optc, $(MACOSX_DEPLOYMENT_CC_OPTS))
187 rts_LD_OPTS += $(addprefix -optl, $(MACOSX_DEPLOYMENT_LD_OPTS))
188
189 # Otherwise the stack-smash handler gets triggered.
190 ifeq "$(TargetOS_CPP)" "openbsd"
191 rts_HC_OPTS += -optc-fno-stack-protector
192 endif
193
194 # We *want* type-checking of hand-written cmm.
195 rts_HC_OPTS += -dcmm-lint 
196
197 # -fno-strict-aliasing is required for the runtime, because we often
198 # use a variety of types to represent closure pointers (StgPtr,
199 # StgClosure, StgMVar, etc.), and without -fno-strict-aliasing gcc is
200 # allowed to assume that these pointers do not alias.  eg. without
201 # this flag we get problems in sm/Evac.c:copy() with gcc 3.4.3, the
202 # upd_evacee() assigments get moved before the object copy.
203 rts_CC_OPTS += -fno-strict-aliasing
204
205 ifeq "$(BeConservative)" "YES"
206 rts_CC_OPTS += -DBE_CONSERVATIVE
207 endif
208
209 #-----------------------------------------------------------------------------
210 # Flags for compiling specific files
211
212 # XXX DQ is now the same on all platforms, so get rid of it
213 DQ = \"
214
215 # If Main.c is built with optimisation then the SEH exception stuff on
216 # Windows gets confused.
217 # This has to be in HC rather than CC opts, as otherwise there's a
218 # -optc-O2 that comes after it.
219 Main_HC_OPTS += -optc-O0
220
221 RtsMessages_CC_OPTS += -DProjectVersion=$(DQ)$(ProjectVersion)$(DQ)
222 RtsUtils_CC_OPTS += -DProjectVersion=$(DQ)$(ProjectVersion)$(DQ)
223 RtsUtils_CC_OPTS += -DRtsWay=$(DQ)rts$(_way)$(DQ)
224 #
225 RtsUtils_CC_OPTS += -DHostPlatform=$(DQ)$(HOSTPLATFORM)$(DQ)
226 RtsUtils_CC_OPTS += -DHostArch=$(DQ)$(HostArch_CPP)$(DQ)
227 RtsUtils_CC_OPTS += -DHostOS=$(DQ)$(HostOS_CPP)$(DQ)
228 RtsUtils_CC_OPTS += -DHostVendor=$(DQ)$(HostVendor_CPP)$(DQ)
229 #
230 RtsUtils_CC_OPTS += -DBuildPlatform=$(DQ)$(BUILDPLATFORM)$(DQ)
231 RtsUtils_CC_OPTS += -DBuildArch=$(DQ)$(BuildArch_CPP)$(DQ)
232 RtsUtils_CC_OPTS += -DBuildOS=$(DQ)$(BuildOS_CPP)$(DQ)
233 RtsUtils_CC_OPTS += -DBuildVendor=$(DQ)$(BuildVendor_CPP)$(DQ)
234 #
235 RtsUtils_CC_OPTS += -DTargetPlatform=$(DQ)$(TARGETPLATFORM)$(DQ)
236 RtsUtils_CC_OPTS += -DTargetArch=$(DQ)$(TargetArch_CPP)$(DQ)
237 RtsUtils_CC_OPTS += -DTargetOS=$(DQ)$(TargetOS_CPP)$(DQ)
238 RtsUtils_CC_OPTS += -DTargetVendor=$(DQ)$(TargetVendor_CPP)$(DQ)
239 #
240 RtsUtils_CC_OPTS += -DGhcUnregisterised=$(DQ)$(GhcUnregisterised)$(DQ)
241 RtsUtils_CC_OPTS += -DGhcEnableTablesNextToCode=$(DQ)$(GhcEnableTablesNextToCode)$(DQ)
242
243 # ffi.h triggers prototype warnings, so disable them here:
244 Interpreter_CC_OPTS += -Wno-strict-prototypes
245 Adjustor_CC_OPTS += -Wno-strict-prototypes
246 sm/Storage_CC_OPTS += -Wno-strict-prototypes
247
248 StgCRun_CC_OPTS += -w
249 Typeable_CC_OPTS += -w
250 RetainerProfile_CC_OPTS += -w
251 sm/Compact_CC_OPTS += -w
252 # On Windows:
253 win32/ConsoleHandler_CC_OPTS += -w
254 win32/ThrIOManager_CC_OPTS += -w
255 win32/Ticker_CC_OPTS += -w
256 # The above warning supression flags are a temporary kludge.
257 # While working on this module you are encouraged to remove it and fix
258 # any warnings in the module. See
259 #     http://hackage.haskell.org/trac/ghc/wiki/WorkingConventions#Warnings
260 # for details
261
262 # Without this, thread_obj will not be inlined (at least on x86 with GCC 4.1.0)
263 sm/Compact_CC_OPTS += -finline-limit=2500
264
265 # -O3 helps unroll some loops (especially in copy() with a constant argument).
266 sm/Evac_CC_OPTS += -funroll-loops
267 sm/Evac_thr_HC_OPTS += -optc-funroll-loops
268
269 # These files are just copies of sm/Evac.c and sm/Scav.c respectively,
270 # but compiled with -DPARALLEL_GC.
271 sm/Evac_thr_HC_OPTS += -optc-DPARALLEL_GC
272 sm/Scav_thr_HC_OPTS += -optc-DPARALLEL_GC
273
274 #-----------------------------------------------------------------------------
275 # Add PAPI library if needed
276
277 ifeq "$(GhcRtsWithPapi)" "YES"
278
279 rts_CC_OPTS             += -DUSE_PAPI
280
281 rts_PACKAGE_CPP_OPTS    += -DUSE_PAPI
282 rts_PACKAGE_CPP_OPTS    += -DPAPI_INCLUDE_DIR=$(PapiIncludeDir)
283 rts_PACKAGE_CPP_OPTS    += -DPAPI_LIB_DIR=$(PapiLibDir)
284
285 ifneq "$(PapiIncludeDir)" ""
286 rts_HC_OPTS     += -I$(PapiIncludeDir)
287 rts_CC_OPTS     += -I$(PapiIncludeDir)
288 rts_HSC2HS_OPTS += -I$(PapiIncludeDir)
289 endif
290 ifneq "$(PapiLibDirs)" ""
291 rts_LD_OPTS     += -L$(PapiLibDirs)
292 endif
293
294 else # GhcRtsWithPapi==YES
295
296 rts_PACKAGE_CPP_OPTS += -DPAPI_INCLUDE_DIR=""
297 rts_PACKAGE_CPP_OPTS += -DPAPI_LIB_DIR=""
298
299 endif
300
301 # -----------------------------------------------------------------------------
302 # dependencies
303
304 # Hack: we define every way-related option here, so that we get (hopefully)
305 # a superset of the dependencies.  To do this properly, we should generate
306 # a different set of dependencies for each way.  Further hack: PROFILING and
307 # TICKY_TICKY can't be used together, so we omit TICKY_TICKY for now.
308 rts_MKDEPENDC_OPTS += -DPROFILING -DTHREADED_RTS -DDEBUG
309
310 rts_WAYS_DASHED = $(subst $(space),,$(patsubst %,-%,$(strip $(rts_WAYS))))
311 rts_dist_depfile = rts/dist/build/.depend$(rts_WAYS_DASHED)
312
313 rts_dist_C_SRCS  = $(rts_C_SRCS)
314 rts_dist_S_SRCS =  $(rts_S_SRCS)
315 rts_dist_C_FILES = $(rts_C_SRCS) $(rts_S_SRCS)
316
317 $(eval $(call build-dependencies,rts,dist))
318
319 $(rts_dist_depfile) : libffi/ffi.h
320
321 # -----------------------------------------------------------------------------
322
323 # Need to get the GMP vars in through CPP to package.conf.in, and put
324 # quotes around each element.
325
326 rts_PACKAGE_CPP_OPTS += -DGMP_INCLUDE_DIRS='$(subst $(space),$(comma),$(patsubst %,"%",$(strip $(GMP_INCLUDE_DIRS))))'
327 rts_PACKAGE_CPP_OPTS += -DGMP_LIB_DIRS='$(subst $(space),$(comma),$(patsubst %,"%",$(strip $(GMP_LIB_DIRS))))'
328
329 ifneq "$(GMP_INCLUDE_DIRS)" ""
330 rts_HC_OPTS += -I$(GMP_INCLUDE_DIRS)
331 rts_CC_OPTS += -I$(GMP_INCLUDE_DIRS)
332 rts_HSC2HS_OPTS += -I$(GMP_INCLUDE_DIRS)
333 endif
334 ifneq "$(GMP_LIB_DIRS)" ""
335 rts_LD_OPTS += -L$(GMP_LIB_DIRS)
336 endif
337
338 rts_CC_OPTS     += -Igmp/gmpbuild
339 rts_HC_OPTS     += -Igmp/gmpbuild
340 rts_HSC2HS_OPTS += -Igmp/gmpbuild
341 rts_LD_OPTS     += -Lgmp/gmpbuild
342
343 #-----------------------------------------------------------------------------
344 # libffi stuff
345
346 rts_CC_OPTS     += -Ilibffi/build/include
347 rts_HC_OPTS     += -Ilibffi/build/include
348 rts_HSC2HS_OPTS += -Ilibffi/build/include
349 rts_LD_OPTS     += -Llibffi/build/include
350
351 # -----------------------------------------------------------------------------
352 # compile generic patchable dyn-wrapper
353
354 DYNWRAPPER_SRC = rts/dyn-wrapper.c
355 DYNWRAPPER_PROG = rts/dyn-wrapper$(exeext)
356 $(DYNWRAPPER_PROG): $(DYNWRAPPER_SRC)
357         $(HC) -cpp -optc-include -optcdyn-wrapper-patchable-behaviour.h $(INPLACE_EXTRA_FLAGS) $< -o $@
358
359 # -----------------------------------------------------------------------------
360 # The RTS package config
361
362 # If -DDEBUG is in effect, adjust package conf accordingly..
363 ifneq "$(strip $(filter -optc-DDEBUG,$(GhcRtsHcOpts)))" ""
364 rts_PACKAGE_CPP_OPTS += -DDEBUG
365 endif
366
367 ifeq "$(HaveLibMingwEx)" "YES"
368 rts_PACKAGE_CPP_OPTS += -DHAVE_LIBMINGWEX
369 endif
370
371 $(eval $(call manual-package-config,rts))
372
373 rts/package.conf.inplace : $(includes_H_CONFIG) $(includes_H_PLATFORM)
374
375 # -----------------------------------------------------------------------------
376 # installing
377
378 install : install_rts
379
380 .PHONY: install_rts
381 install_rts:
382         $(MKDIRHIER) $(DESTDIR)$(libdir)
383         $(MKDIRHIER) $(DESTDIR)$(libdir)/include
384         $(CP) $(ALL_RTS_LIBS) $(DESTDIR)$(libdir)
385
386 # -----------------------------------------------------------------------------
387 # cleaning
388
389 $(eval $(call clean-target,rts,dist,rts/dist))
390
391 BINDIST_EXTRAS += rts/package.conf.in
392 BINDIST_EXTRAS += $(ALL_RTS_LIBS)
393