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