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