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