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