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