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