1 # -----------------------------------------------------------------------------
3 # (c) 2009 The University of Glasgow
5 # This file is part of the GHC build system.
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
11 # -----------------------------------------------------------------------------
13 # -----------------------------------------------------------------------------
16 # We build the RTS with stage 1
17 rts_dist_HC = $(GHC_STAGE1)
19 # merge GhcLibWays and GhcRTSWays but strip out duplicates
20 rts_WAYS = $(GhcLibWays) $(filter-out $(GhcLibWays),$(GhcRTSWays))
21 rts_dist_WAYS = $(rts_WAYS)
23 ALL_RTS_LIBS = rts/dist/build/libHSrtsmain.a \
24 $(foreach way,$(rts_WAYS),rts/dist/build/libHSrts$($(way)_libsuf))
25 all_rts : $(ALL_RTS_LIBS)
27 # -----------------------------------------------------------------------------
28 # Defining the sources
30 ALL_DIRS = hooks parallel sm eventlog
32 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
39 EXCLUDED_SRCS += rts/Main.c
40 EXCLUDED_SRCS += rts/parallel/SysMan.c
41 EXCLUDED_SRCS += $(wildcard rts/Vis*.c)
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)
46 # Don't compile .S files when bootstrapping a new arch
47 ifneq "$(PORTING_HOST)" "YES"
48 ifneq "$(findstring $(TargetArch_CPP), powerpc powerpc64)" ""
49 rts_S_SRCS += rts/AdjustorAsm.S
51 ifneq "$(findstring $(TargetOS_CPP), darwin)" ""
52 rts_S_SRCS += rts/AdjustorAsm.S
57 ifeq "$(GhcUnregisterised)" "YES"
61 rts_AUTO_APPLY_CMM = rts/dist/build/AutoApply.cmm
63 $(rts_AUTO_APPLY_CMM): $(GENAPPLY_INPLACE)
64 "$(GENAPPLY_INPLACE)" >$@
66 rts/dist/build/sm/Evac_thr.c : rts/sm/Evac.c | $$(dir $$@)/.
68 rts/dist/build/sm/Scav_thr.c : rts/sm/Scav.c | $$(dir $$@)/.
71 rts_H_FILES := $(wildcard rts/*.h)
73 ifeq "$(USE_DTRACE)" "YES"
74 DTRACEPROBES_H = rts/dist/build/RtsProbes.h
75 rts_H_FILES += $(DTRACEPROBES_H)
78 # collect the -l flags that we need to link the rts dyn lib.
79 rts/libs.depend : $(GHC_PKG_INPLACE)
80 "$(GHC_PKG_INPLACE)" field rts extra-libraries \
81 | sed -e 's/^extra-libraries: //' -e 's/\([a-z0-9]*\)[ ]*/-l\1 /g' > $@
84 # ----------------------------------------------------------------------------
85 # On Windows, as the RTS and base libraries have recursive imports,
86 # we have to break the loop with "import libraries".
87 # These are made from rts/win32/libHS*.def which contain lists of
88 # all the symbols in those libraries used by the RTS.
90 ifneq "$$(findstring dyn, $1)" ""
91 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
93 ALL_RTS_DEF_LIBNAMES = base ghc-prim
95 rts/dist/build/win32/libHSbase.dll.a \
96 rts/dist/build/win32/libHSghc-prim.dll.a \
97 rts/dist/build/win32/libHSffi.dll.a
99 # -- import libs for the regular Haskell libraries
100 define make-importlib-def # args $1 = lib name
101 rts/dist/build/win32/libHS$1.def : rts/win32/libHS$1.def
102 cat rts/win32/libHS$1.def \
103 | sed "s/@LibVersion@/$$(libraries/$1_dist-install_VERSION)/" \
104 | sed "s/@ProjectVersion@/$(ProjectVersion)/" \
105 > rts/dist/build/win32/libHS$1.def
107 rts/dist/build/win32/libHS$1.dll.a : rts/dist/build/win32/libHS$1.def
108 "$$(DLLTOOL)" -d rts/dist/build/win32/libHS$1.def \
109 -l rts/dist/build/win32/libHS$1.dll.a
111 $(foreach lib,$(ALL_RTS_DEF_LIBNAMES),$(eval $(call make-importlib-def,$(lib))))
114 # -- import libs for libffi
115 rts/dist/build/win32/libHSffi.def : rts/win32/libHSffi.def
116 cat rts/win32/libHSffi.def \
117 | sed "s/@ProjectVersion@/$(ProjectVersion)/" \
118 > rts/dist/build/win32/libHSffi.def
120 rts/dist/build/win32/libHSffi.dll.a : rts/dist/build/win32/libHSffi.def
121 "$(DLLTOOL)" -d rts/dist/build/win32/libHSffi.def \
122 -l rts/dist/build/win32/libHSffi.dll.a
127 #-----------------------------------------------------------------------------
129 define build-rts-way # args: $1 = way
131 ifneq "$$(BINDIST)" "YES"
133 # The per-way CC_OPTS
134 ifneq "$$(findstring debug, $1)" ""
135 rts_dist_$1_HC_OPTS =
136 rts_dist_$1_CC_OPTS = -g -O0
138 rts_dist_$1_HC_OPTS = $$(GhcRtsHcOpts)
139 rts_dist_$1_CC_OPTS = $$(GhcRtsCcOpts)
142 ifneq "$$(findstring thr, $1)" ""
143 rts_$1_EXTRA_C_SRCS = rts/dist/build/sm/Evac_thr.c rts/dist/build/sm/Scav_thr.c
146 $(call distdir-way-opts,rts,dist,$1)
147 $(call c-suffix-rules,rts,dist,$1,YES)
148 $(call cmm-suffix-rules,rts,dist,$1)
149 $(call hs-suffix-rules-srcdir,rts,dist,$1,.)
150 # hs-suffix-rules-srcdir is needed when BootingFromHc to get the .hc rules
152 rts_$1_LIB_NAME = libHSrts$$($1_libsuf)
153 rts_$1_LIB = rts/dist/build/$$(rts_$1_LIB_NAME)
155 rts_$1_C_OBJS = $$(patsubst rts/%.c,rts/dist/build/%.$$($1_osuf),$$(rts_C_SRCS)) $$(patsubst %.c,%.$$($1_osuf),$$(rts_$1_EXTRA_C_SRCS))
156 rts_$1_S_OBJS = $$(patsubst rts/%.S,rts/dist/build/%.$$($1_osuf),$$(rts_S_SRCS))
157 rts_$1_CMM_OBJS = $$(patsubst rts/%.cmm,rts/dist/build/%.$$($1_osuf),$$(rts_CMM_SRCS)) $$(patsubst %.cmm,%.$$($1_osuf),$$(rts_AUTO_APPLY_CMM))
159 rts_$1_OBJS = $$(rts_$1_C_OBJS) $$(rts_$1_S_OBJS) $$(rts_$1_CMM_OBJS)
161 rts_dist_$1_CC_OPTS += -DRtsWay=\"rts_$1\"
163 # Making a shared library for the RTS.
164 ifneq "$$(findstring dyn, $1)" ""
165 ifeq "$$(HOSTPLATFORM)" "i386-unknown-mingw32"
166 $$(rts_$1_LIB) : $$(rts_$1_OBJS) $$(ALL_RTS_DEF_LIBS) rts/libs.depend
167 "$$(RM)" $$(RM_OPTS) $$@
168 "$$(rts_dist_HC)" -package-name rts -shared -dynamic -dynload deploy \
169 -no-auto-link-packages `cat rts/libs.depend` $$(rts_$1_OBJS) $$(ALL_RTS_DEF_LIBS) -o $$@
171 $$(rts_$1_LIB) : $$(rts_$1_OBJS) rts/libs.depend
172 "$$(RM)" $$(RM_OPTS) $$@
173 "$$(rts_dist_HC)" -package-name rts -shared -dynamic -dynload deploy \
174 -no-auto-link-packages `cat rts/libs.depend` $$(rts_$1_OBJS) -o $$@
175 ifeq "$$(darwin_HOST_OS)" "1"
176 # Ensure library's install name is correct before anyone links with it.
177 install_name_tool -id $(ghclibdir)/$$(rts_$1_LIB_NAME) $$@
181 $$(rts_$1_LIB) : $$(rts_$1_OBJS)
182 "$$(RM)" $$(RM_OPTS) $$@
183 echo $$(rts_$1_OBJS) | "$$(XARGS)" $$(XARGS_OPTS) "$$(AR)" $$(AR_OPTS) $$(EXTRA_AR_ARGS) $$@
190 # And expand the above for each way:
191 $(foreach way,$(rts_WAYS),$(eval $(call build-rts-way,$(way))))
193 #-----------------------------------------------------------------------------
194 # Flags for compiling every file
196 # We like plenty of warnings.
197 WARNING_OPTS += -Wall
198 ifeq "$(GccLT34)" "YES"
201 WARNING_OPTS += -Wextra
203 WARNING_OPTS += -Wstrict-prototypes
204 WARNING_OPTS += -Wmissing-prototypes
205 WARNING_OPTS += -Wmissing-declarations
206 WARNING_OPTS += -Winline
207 WARNING_OPTS += -Waggregate-return
208 WARNING_OPTS += -Wpointer-arith
209 WARNING_OPTS += -Wmissing-noreturn
210 WARNING_OPTS += -Wnested-externs
211 WARNING_OPTS += -Wredundant-decls
213 # These ones are hard to avoid:
214 #WARNING_OPTS += -Wconversion
215 #WARNING_OPTS += -Wbad-function-cast
216 #WARNING_OPTS += -Wshadow
217 #WARNING_OPTS += -Wcast-qual
219 # This one seems buggy on GCC 4.1.2, which is the only GCC version we
220 # have that can bootstrap the SPARC build. We end up with lots of supurious
221 # warnings of the form "cast increases required alignment of target type".
222 # Some legitimate warnings can be fixed by adding an intermediate cast to
223 # (void*), but we get others in rts/sm/GCUtils.c concerning the gct var
224 # that look innocuous to me. We could enable this again once we deprecate
225 # support for registerised builds on this arch. -- BL 2010/02/03
226 # WARNING_OPTS += -Wcast-align
228 STANDARD_OPTS += -Iincludes -Irts
229 # COMPILING_RTS is only used when building Win32 DLL support.
230 STANDARD_OPTS += -DCOMPILING_RTS
232 # HC_OPTS is included in both .c and .cmm compilations, whereas CC_OPTS is
233 # only included in .c compilations. HC_OPTS included the WAY_* opts, which
234 # must be included in both types of compilations.
236 rts_CC_OPTS += $(WARNING_OPTS)
237 rts_CC_OPTS += $(STANDARD_OPTS)
239 rts_HC_OPTS += $(STANDARD_OPTS) -package-name rts
241 ifneq "$(GhcWithSMP)" "YES"
242 rts_CC_OPTS += -DNOSMP
243 rts_HC_OPTS += -optc-DNOSMP
246 ifeq "$(UseLibFFIForAdjustors)" "YES"
247 rts_CC_OPTS += -DUSE_LIBFFI_FOR_ADJUSTORS
250 # Mac OS X: make sure we compile for the right OS version
251 rts_CC_OPTS += $(MACOSX_DEPLOYMENT_CC_OPTS)
252 rts_HC_OPTS += $(addprefix -optc, $(MACOSX_DEPLOYMENT_CC_OPTS))
253 rts_LD_OPTS += $(addprefix -optl, $(MACOSX_DEPLOYMENT_LD_OPTS))
255 # We *want* type-checking of hand-written cmm.
256 rts_HC_OPTS += -dcmm-lint
258 # -fno-strict-aliasing is required for the runtime, because we often
259 # use a variety of types to represent closure pointers (StgPtr,
260 # StgClosure, StgMVar, etc.), and without -fno-strict-aliasing gcc is
261 # allowed to assume that these pointers do not alias. eg. without
262 # this flag we get problems in sm/Evac.c:copy() with gcc 3.4.3, the
263 # upd_evacee() assigments get moved before the object copy.
264 rts_CC_OPTS += -fno-strict-aliasing
266 rts_CC_OPTS += -fno-common
268 ifeq "$(BeConservative)" "YES"
269 rts_CC_OPTS += -DBE_CONSERVATIVE
272 #-----------------------------------------------------------------------------
273 # Flags for compiling specific files
275 # If RtsMain.c is built with optimisation then the SEH exception stuff on
276 # Windows gets confused.
277 # This has to be in HC rather than CC opts, as otherwise there's a
278 # -optc-O2 that comes after it.
279 rts/RtsMain_HC_OPTS += -optc-O0
281 rts/RtsMessages_CC_OPTS += -DProjectVersion=\"$(ProjectVersion)\"
282 rts/RtsUtils_CC_OPTS += -DProjectVersion=\"$(ProjectVersion)\"
284 rts/RtsUtils_CC_OPTS += -DHostPlatform=\"$(HOSTPLATFORM)\"
285 rts/RtsUtils_CC_OPTS += -DHostArch=\"$(HostArch_CPP)\"
286 rts/RtsUtils_CC_OPTS += -DHostOS=\"$(HostOS_CPP)\"
287 rts/RtsUtils_CC_OPTS += -DHostVendor=\"$(HostVendor_CPP)\"
289 rts/RtsUtils_CC_OPTS += -DBuildPlatform=\"$(BUILDPLATFORM)\"
290 rts/RtsUtils_CC_OPTS += -DBuildArch=\"$(BuildArch_CPP)\"
291 rts/RtsUtils_CC_OPTS += -DBuildOS=\"$(BuildOS_CPP)\"
292 rts/RtsUtils_CC_OPTS += -DBuildVendor=\"$(BuildVendor_CPP)\"
294 rts/RtsUtils_CC_OPTS += -DTargetPlatform=\"$(TARGETPLATFORM)\"
295 rts/RtsUtils_CC_OPTS += -DTargetArch=\"$(TargetArch_CPP)\"
296 rts/RtsUtils_CC_OPTS += -DTargetOS=\"$(TargetOS_CPP)\"
297 rts/RtsUtils_CC_OPTS += -DTargetVendor=\"$(TargetVendor_CPP)\"
299 rts/RtsUtils_CC_OPTS += -DGhcUnregisterised=\"$(GhcUnregisterised)\"
300 rts/RtsUtils_CC_OPTS += -DGhcEnableTablesNextToCode=\"$(GhcEnableTablesNextToCode)\"
302 # Compile various performance-critical pieces *without* -fPIC -dynamic
303 # even when building a shared library. If we don't do this, then the
304 # GC runs about 50% slower on x86 due to the overheads of PIC. The
305 # cost of doing this is a little runtime linking and less sharing, but
308 # On x86_64 this doesn't work, because all objects in a shared library
309 # must be compiled with -fPIC (since the 32-bit relocations generated
310 # by the default small memory can't be resolved at runtime). So we
311 # only do this on i386.
313 # This apparently doesn't work on OS X (Darwin) nor on Solaris.
314 # On Darwin we get errors of the form
316 # ld: absolute addressing (perhaps -mdynamic-no-pic) used in _stg_ap_0_fast from rts/dist/build/Apply.dyn_o not allowed in slidable image
318 # and lots of these warnings:
320 # ld: warning codegen in _stg_ap_pppv_fast (offset 0x0000005E) prevents image from loading in dyld shared cache
322 # On Solaris we get errors like:
324 # Text relocation remains referenced
325 # against symbol offset in file
326 # .rodata (section) 0x11 rts/dist/build/Apply.dyn_o
328 # ld: fatal: relocations remain against allocatable but non-writable sections
329 # collect2: ld returned 1 exit status
331 ifeq "$(TargetArch_CPP)" "i386"
332 i386_SPEED_HACK := "YES"
333 ifeq "$(TargetOS_CPP)" "darwin"
334 i386_SPEED_HACK := "NO"
336 ifeq "$(TargetOS_CPP)" "solaris2"
337 i386_SPEED_HACK := "NO"
341 ifeq "$(TargetArch_CPP)" "i386"
342 ifeq "$(i386_SPEED_HACK)" "YES"
343 rts/sm/Evac_HC_OPTS += -fno-PIC
344 rts/sm/Evac_thr_HC_OPTS += -fno-PIC
345 rts/sm/Scav_HC_OPTS += -fno-PIC
346 rts/sm/Scav_thr_HC_OPTS += -fno-PIC
347 rts/sm/Compact_HC_OPTS += -fno-PIC
348 rts/sm/GC_HC_OPTS += -fno-PIC
350 # -static is also necessary for these bits, otherwise the NCG
351 # -generates dynamic references:
352 rts/Updates_HC_OPTS += -fno-PIC -static
353 rts/StgMiscClosures_HC_OPTS += -fno-PIC -static
354 rts/PrimOps_HC_OPTS += -fno-PIC -static
355 rts/Apply_HC_OPTS += -fno-PIC -static
356 rts/dist/build/AutoApply_HC_OPTS += -fno-PIC -static
360 # ffi.h triggers prototype warnings, so disable them here:
361 rts/Interpreter_CC_OPTS += -Wno-strict-prototypes
362 rts/Adjustor_CC_OPTS += -Wno-strict-prototypes
363 rts/sm/Storage_CC_OPTS += -Wno-strict-prototypes
365 # inlining warnings happen in Compact
366 rts/sm/Compact_CC_OPTS += -Wno-inline
368 # emits warnings about call-clobbered registers on x86_64
369 rts/StgCRun_CC_OPTS += -w
371 rts/RetainerProfile_CC_OPTS += -w
372 rts/RetainerSet_CC_OPTS += -Wno-format
374 rts/win32/ConsoleHandler_CC_OPTS += -w
375 rts/win32/ThrIOManager_CC_OPTS += -w
376 # The above warning supression flags are a temporary kludge.
377 # While working on this module you are encouraged to remove it and fix
378 # any warnings in the module. See
379 # http://hackage.haskell.org/trac/ghc/wiki/WorkingConventions#Warnings
382 # Without this, thread_obj will not be inlined (at least on x86 with GCC 4.1.0)
383 rts/sm/Compact_CC_OPTS += -finline-limit=2500
385 # -O3 helps unroll some loops (especially in copy() with a constant argument).
386 rts/sm/Evac_CC_OPTS += -funroll-loops
387 rts/dist/build/sm/Evac_thr_HC_OPTS += -optc-funroll-loops
389 # These files are just copies of sm/Evac.c and sm/Scav.c respectively,
390 # but compiled with -DPARALLEL_GC.
391 rts/dist/build/sm/Evac_thr_CC_OPTS += -DPARALLEL_GC -Irts/sm
392 rts/dist/build/sm/Scav_thr_CC_OPTS += -DPARALLEL_GC -Irts/sm
394 #-----------------------------------------------------------------------------
395 # Add PAPI library if needed
397 ifeq "$(GhcRtsWithPapi)" "YES"
399 rts_CC_OPTS += -DUSE_PAPI
401 rts_PACKAGE_CPP_OPTS += -DUSE_PAPI
402 rts_PACKAGE_CPP_OPTS += -DPAPI_INCLUDE_DIR=$(PapiIncludeDir)
403 rts_PACKAGE_CPP_OPTS += -DPAPI_LIB_DIR=$(PapiLibDir)
405 ifneq "$(PapiIncludeDir)" ""
406 rts_HC_OPTS += -I$(PapiIncludeDir)
407 rts_CC_OPTS += -I$(PapiIncludeDir)
408 rts_HSC2HS_OPTS += -I$(PapiIncludeDir)
410 ifneq "$(PapiLibDirs)" ""
411 rts_LD_OPTS += -L$(PapiLibDirs)
414 else # GhcRtsWithPapi==YES
416 rts_PACKAGE_CPP_OPTS += -DPAPI_INCLUDE_DIR=""
417 rts_PACKAGE_CPP_OPTS += -DPAPI_LIB_DIR=""
421 # -----------------------------------------------------------------------------
424 rts_WAYS_DASHED = $(subst $(space),,$(patsubst %,-%,$(strip $(rts_WAYS))))
425 rts_dist_depfile_base = rts/dist/build/.depend$(rts_WAYS_DASHED)
427 rts_dist_C_SRCS = $(rts_C_SRCS) $(rts_thr_EXTRA_C_SRCS)
428 rts_dist_S_SRCS = $(rts_S_SRCS)
429 rts_dist_C_FILES = $(rts_C_SRCS) $(rts_thr_EXTRA_C_SRCS) $(rts_S_SRCS)
431 # Hack: we define every way-related option here, so that we get (hopefully)
432 # a superset of the dependencies. To do this properly, we should generate
433 # a different set of dependencies for each way. Further hack: PROFILING an
435 # TICKY_TICKY can't be used together, so we omit TICKY_TICKY for now.
436 rts_dist_MKDEPENDC_OPTS += -DPROFILING -DTHREADED_RTS -DDEBUG
438 ifeq "$(USE_DTRACE)" "YES"
440 rts_dist_MKDEPENDC_OPTS += -Irts/dist/build
444 $(eval $(call build-dependencies,rts,dist,1))
446 $(rts_dist_depfile_c_asm) : libffi/dist-install/build/ffi.h $(DTRACEPROBES_H)
448 #-----------------------------------------------------------------------------
451 rts_CC_OPTS += -Ilibffi/build/include
452 rts_HC_OPTS += -Ilibffi/build/include
453 rts_HSC2HS_OPTS += -Ilibffi/build/include
454 rts_LD_OPTS += -Llibffi/build/include
456 # -----------------------------------------------------------------------------
457 # compile dtrace probes if dtrace is supported
459 ifeq "$(USE_DTRACE)" "YES"
461 rts_CC_OPTS += -DDTRACE
462 rts_HC_OPTS += -DDTRACE
464 # Apple's dtrace (the only one supported by ghc at the moment) uses
465 # gcc as its preprocessor. If gcc isn't at /usr/bin/gcc, or we need
466 # to force it to use a different gcc, we need to give the path in
467 # the option cpppath.
469 DTRACEPROBES_SRC = rts/RtsProbes.d
470 $(DTRACEPROBES_H): $(DTRACEPROBES_SRC) includes/ghcplatform.h | $(dir $@)/.
471 "$(DTRACE)" $(filter -I%,$(rts_CC_OPTS)) -C -x cpppath=$(WhatGccIsCalled) -h -o $@ -s $<
475 # -----------------------------------------------------------------------------
476 # build the static lib containing the C main symbol
478 ifneq "$(BINDIST)" "YES"
479 rts/dist/build/libHSrtsmain.a : rts/dist/build/Main.o
480 "$(RM)" $(RM_OPTS) $@
481 "$(AR)" $(AR_OPTS) $(EXTRA_AR_ARGS) $@ $<
484 # -----------------------------------------------------------------------------
485 # The RTS package config
487 # If -DDEBUG is in effect, adjust package conf accordingly..
488 ifneq "$(strip $(filter -optc-DDEBUG,$(GhcRtsHcOpts)))" ""
489 rts_PACKAGE_CPP_OPTS += -DDEBUG
492 ifeq "$(HaveLibMingwEx)" "YES"
493 rts_PACKAGE_CPP_OPTS += -DHAVE_LIBMINGWEX
496 $(eval $(call manual-package-config,rts))
498 ifneq "$(BootingFromHc)" "YES"
499 rts/package.conf.inplace : $(includes_H_CONFIG) $(includes_H_PLATFORM)
502 # -----------------------------------------------------------------------------
505 INSTALL_LIBS += $(ALL_RTS_LIBS)
507 # -----------------------------------------------------------------------------
510 $(eval $(call clean-target,rts,dist,rts/dist))
512 BINDIST_EXTRAS += rts/package.conf.in
513 BINDIST_EXTRAS += $(ALL_RTS_LIBS)