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