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