Optionally use libffi to implement 'foreign import "wrapper"' (#793)
[ghc-hetmet.git] / rts / Makefile
index 9828f55..74a37fd 100644 (file)
@@ -7,9 +7,6 @@
 #  .hc files are "Haskellized-C", compiled using the C compiler and
 #      (possibly) the assembly-mangler.  The GHC driver script
 #      knows how to compile this stuff.
-#
-#  Other sorta independent, compile-once subdirs are:
-#      gmp             -- GNU multi-precision library (for Integer)
 
 #-----------------------------------------------------------------------------
 # Preamble
@@ -28,6 +25,14 @@ PACKAGE = rts
 
 HC=$(GHC_INPLACE)
 
+
+# Setting _way here is a nasty hack to make sure we get libHSrts*.a etc
+# rather than just libHSrts.a when we are making and installing
+# bindists.
+ifeq "$(DOING_BIN_DIST)" "YES"
+_way = *
+endif
+
 # -----------------------------------------------------------------------------
 # RTS ways
 
@@ -35,16 +40,25 @@ WAYS=$(GhcLibWays) $(GhcRTSWays)
 
 ifneq "$(findstring debug, $(way))" ""
 GhcRtsHcOpts=
-GhcRtsCcOpts=-g
+GhcRtsCcOpts=-g -O0
 endif
 
+ifeq "$(BeConservative)" "YES"
+GhcRtsCcOpts += -DBE_CONSERVATIVE
+endif
+
+# -----------------------------------------------------------------------------
+
+# There's nothing for Haddock here...
+override HADDOCK_DOCS = NO
+
 # -----------------------------------------------------------------------------
 
 # Tells the build system not to add various Haskellish options to $(SRC_HC_OPTS)
 NON_HS_PACKAGE = YES
 
 # grab sources from these subdirectories
-ALL_DIRS = hooks parallel
+ALL_DIRS = hooks parallel sm
 
 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
 ALL_DIRS += win32
@@ -52,10 +66,20 @@ else
 ALL_DIRS += posix
 endif
 
-ifneq "$(DLLized)" "YES"
-EXCLUDED_SRCS += RtsDllMain.c
+ifneq "$(findstring dyn, $(way))" ""
+DYNAMIC_RTS=YES
 else
+DYNAMIC_RTS=NO
+endif
+
+ifeq "$(DYNAMIC_RTS) $(HOSTPLATFORM)" "YES i386-unknown-mingw32"
 EXCLUDED_SRCS += Main.c
+# It's not included in the DLL, but we need to compile it up separately.
+
+all :: Main.$(way_)o
+INSTALL_LIBS += Main.$(way_)o
+else
+EXCLUDED_SRCS += RtsDllMain.c
 endif
 
 # This file ends up being empty unless we're building for a powerpc
@@ -88,7 +112,7 @@ H_FILES = $(wildcard ../includes/*.h) $(wildcard *.h)
 # gcc provides lots of useful warnings if you ask it.
 # This is a pretty good list to start with - use a # to comment out
 # any you don't like.
-WARNING_OPTS += -Wall 
+WARNING_OPTS += -Wall
 WARNING_OPTS += -W
 WARNING_OPTS += -Wstrict-prototypes 
 WARNING_OPTS += -Wmissing-prototypes 
@@ -96,7 +120,7 @@ WARNING_OPTS += -Wmissing-declarations
 WARNING_OPTS += -Winline
 WARNING_OPTS += -Waggregate-return
 #WARNING_OPTS += -Wpointer-arith
-WARNING_OPTS += -Wbad-function-cast
+#WARNING_OPTS += -Wbad-function-cast
 #WARNING_OPTS += -Wcast-align
 #WARNING_OPTS += -Wnested-externs
 #WARNING_OPTS += -Wshadow
@@ -105,7 +129,7 @@ WARNING_OPTS += -Wbad-function-cast
 #WARNING_OPTS += -Wredundant-decls 
 #WARNING_OPTS += -Wconversion
 
-STANDARD_OPTS += -I../includes -I. -Iparallel
+STANDARD_OPTS += -I../includes -I. -Iparallel -Ism
 # COMPILING_RTS is only used when building Win32 DLL support.
 STANDARD_OPTS += -DCOMPILING_RTS
 
@@ -117,19 +141,72 @@ SRC_CC_OPTS += $(WARNING_OPTS)
 SRC_CC_OPTS += $(STANDARD_OPTS)
 
 SRC_CC_OPTS += $(GhcRtsCcOpts)
-SRC_HC_OPTS += $(GhcRtsHcOpts)
+SRC_HC_OPTS += $(GhcRtsHcOpts) $(STANDARD_OPTS) -package-name rts
 
 ifneq "$(GhcWithSMP)" "YES"
 SRC_CC_OPTS += -DNOSMP
 SRC_HC_OPTS += -optc-DNOSMP
 endif
 
-ifneq "$(DLLized)" "YES"
+ifeq "$(UseLibFFI)" "YES"
+SRC_CC_OPTS += -DUSE_LIBFFI
+PACKAGE_CPP_OPTS += -DUSE_LIBFFI
+endif
+
+ifneq "$(DYNAMIC_RTS)" "YES"
 SRC_HC_OPTS += -static
+else
+LIB_LD_OPTS += -ignore-package base -ignore-package rts
+ifeq "$(DYNAMIC_RTS) $(HOSTPLATFORM)" "YES i386-unknown-mingw32"
+LIB_DEPS=buildbase
+.PHONY: buildbase
+
+BASE_DIST_LIB=$(TOP)/libraries/base/dist/build
+
+# The following must be a one liner otherwise its evaluation won't be delayed until base/rts packages are properly registered
+LIB_LD_OPTS = -L$(shell $(GHC_PKG_INPLACE) field base library-dirs | sed -e 's/library-dirs: //') -l$(shell $(GHC_PKG_INPLACE) field base hs-libraries | sed -e 's/hs-libraries: //')-ghc$(ProjectVersion)$(soext) $(foreach lib,$(shell $(GHC_PKG_INPLACE) field rts extra-libraries | sed -e s/extra-libraries://),"-l$(lib)") $(foreach libdir,$(shell $(GHC_PKG_INPLACE) field rts library-dirs | sed -e s/library-dirs://),"-L$(libdir)")
+endif
 endif
-# SRC_HC_OPTS += -fPIC
 
-RtsMessages_CC_OPTS += -DProjectVersion=\"$(ProjectVersion)\"
+ifeq "$(Windows)" "YES"
+# On Windows GHC does its own commandline parsing, so we need extra
+# doublequote protection. Sigh.
+DQ = \\\"
+else
+DQ = \"
+endif
+
+# If Main.c is built with optimisation then the SEH exception stuff on
+# Windows gets confused.
+# This has to be in HC rather than CC opts, as otherwise there's a
+# -optc-O2 that comes after it.
+Main_HC_OPTS += -optc-O0
+
+RtsMessages_CC_OPTS += -DProjectVersion=$(DQ)$(ProjectVersion)$(DQ)
+RtsUtils_CC_OPTS += -DProjectVersion=$(DQ)$(ProjectVersion)$(DQ)
+RtsUtils_CC_OPTS += -DRtsWay=$(DQ)rts$(_way)$(DQ)
+RtsUtils_CC_OPTS += -DHostPlatform=$(DQ)$(HOSTPLATFORM)$(DQ)
+RtsUtils_CC_OPTS += -DBuildPlatform=$(DQ)$(BUILDPLATFORM)$(DQ)
+RtsUtils_CC_OPTS += -DTargetPlatform=$(DQ)$(TARGETPLATFORM)$(DQ)
+RtsUtils_CC_OPTS += -DGhcUnregisterised=$(DQ)$(GhcUnregisterised)$(DQ)
+RtsUtils_CC_OPTS += -DGhcEnableTablesNextToCode=$(DQ)$(GhcEnableTablesNextToCode)$(DQ)
+
+StgCRun_CC_OPTS += -w
+Typeable_CC_OPTS += -w
+RetainerProfile_CC_OPTS += -w
+sm/Compact_CC_OPTS += -w
+# On Windows:
+win32/ConsoleHandler_CC_OPTS += -w
+win32/ThrIOManager_CC_OPTS += -w
+win32/Ticker_CC_OPTS += -w
+Threads_CC_OPTS += -w
+Capability_CC_OPTS += -w
+Schedule_CC_OPTS += -w
+# The above warning supression flags are a temporary kludge.
+# While working on this module you are encouraged to remove it and fix
+# any warnings in the module. See
+#     http://hackage.haskell.org/trac/ghc/wiki/WorkingConventions#Warnings
+# for details
 
 ifeq "$(way)" "mp"
 SRC_HC_OPTS += -I$$PVM_ROOT/include
@@ -144,6 +221,10 @@ ifeq "$(HaveLibMingwEx)" "YES"
 PACKAGE_CPP_OPTS += -DHAVE_LIBMINGWEX
 endif
 
+ifeq "$(GhciWithDebugger)" "YES"
+STANDARD_OPTS += -DDEBUGGER
+endif
+
 ifeq "$(DotnetSupport)" "YES"
 
 # 
@@ -179,6 +260,14 @@ EXCLUDED_SRCS              += $(wildcard Vis*.c)
 endif
 
 #-----------------------------------------------------------------------------
+# Add PAPI library if needed
+
+ifeq "$(GhcRtsWithPapi)" "YES"
+SRC_HC_OPTS            += -optc-DUSE_PAPI
+PACKAGE_CPP_OPTS       += -DUSE_PAPI
+endif
+
+#-----------------------------------------------------------------------------
 # make depend setup
 
 SRC_MKDEPENDC_OPTS += -I. -I../includes
@@ -195,6 +284,8 @@ SRC_MKDEPENDC_OPTS += -DPROFILING -DTHREADED_RTS -DDEBUG
 # We want a slightly different version for the unregisterised way, so we make
 # AutoApply on a per-way basis (eg. AutoApply_p.cmm).
 
+ifneq "$(DOING_BIN_DIST)" "YES"
+
 AUTO_APPLY_CMM = AutoApply$(_way).cmm
 
 ifneq "$(BootingFromHc)" "YES"
@@ -207,29 +298,6 @@ EXTRA_CMM_SRCS += $(AUTO_APPLY_CMM)
 
 CLEAN_FILES += $(AUTO_APPLY_CMM)
 
-# -----------------------------------------------------------------------------
-#
-#  Building DLLs is only supported on mingw32 at the moment.
-#
-ifeq "$(DLLized)" "YES"
-SRC_BLD_DLL_OPTS += -lHS_imp_stub -lgmp_imp
-
-# It's not included in the DLL, but we need to compile it up separately.
-all :: Main.dll_o
-
-# Need an import library containing the symbols the RTS uses from the Prelude.
-# So, to avoid bootstrapping trouble, we build one containing just the syms
-# we need. Weirdly named to avoid clashing later on when compiling the contents
-# of ghc/lib/..
-#
-# Note: if you do change the name of the Prelude DLL, the "--dllname <nm>.dll"
-# below will need to be updated as well.
-
-$(DLL_PEN)/HSrts$(_way).dll :: libHS_imp_stub.a
-
-libHS_imp_stub.a :
-       dlltool --output-lib libHS_imp_stub.a --def HSprel.def --dllname HSstd.dll
-
 endif
 
 # -----------------------------------------------------------------------------
@@ -238,47 +306,30 @@ endif
 # We use GMP's own configuration stuff, because it's all rather hairy
 # and not worth re-implementing in our Makefile framework.
 
-ifneq "$(HaveLibGmp)" "YES"
-ifneq "$(HaveFrameworkGMP)" "YES"
-boot ::
-       if [ -f gmp/config.status ]; then \
-          cd gmp && CC=$(WhatGccIsCalled) ./config.status; \
-        else \
-          cd gmp && CC=$(WhatGccIsCalled) ./configure --enable-shared=no \
-                       --host=`echo $(HOSTPLATFORM) | sed 's/i[567]86/i486/g'`; \
-       fi
-
-# Slight cheatage here to pass host as target, but x-compilation isn't supported by ghc.
-
-ifeq "$(way)" ""
-all :: gmp/libgmp.a
-
-ifeq "$(DLLized)" "YES"
-all :: $(DLL_PEN)/gmp.dll
-
-$(DLL_PEN)/gmp.dll:
-       $(MAKE) -C gmp gmp.dll
-       $(MV) gmp/gmp.dll $(DLL_PEN)
-endif
-endif
+CLEAN_FILES += gmp/libgmp.a
 
-install :: gmp/libgmp.a
+# Need to get the GMP vars in through CPP to package.conf.in, and put
+# quotes around each element.
 
-ifeq "$(way)" ""
-clean distclean maintainer-clean ::
-       -$(MAKE) -C gmp MAKEFLAGS= $@
+empty =
+space = $(empty) $(empty)
+comma = ,
+PACKAGE_CPP_OPTS += -DGMP_INCLUDE_DIRS='$(subst $(space),$(comma),$(patsubst %,"%",$(strip $(GMP_INCLUDE_DIRS))))'
+PACKAGE_CPP_OPTS += -DGMP_LIB_DIRS='$(subst $(space),$(comma),$(patsubst %,"%",$(strip $(GMP_LIB_DIRS))))'
 
-INSTALL_LIBS += gmp/libgmp.a
-endif
+ifneq "$(GMP_INCLUDE_DIRS)" ""
+SRC_HC_OPTS += -I$(GMP_INCLUDE_DIRS)
+SRC_CC_OPTS += -I$(GMP_INCLUDE_DIRS)
+SRC_HSC2HS_OPTS += -I$(GMP_INCLUDE_DIRS)
 endif
-
-gmp/libgmp.a ::
-       $(MAKE) -C gmp MAKEFLAGS=
-       @$(CP) gmp/.libs/libgmp.a gmp
-       @$(RANLIB) gmp/libgmp.a
+ifneq "$(GMP_LIB_DIRS)" ""
+SRC_LD_OPTS += -L$(GMP_LIB_DIRS)
 endif
 
-CLEAN_FILES += gmp/libgmp.a
+SRC_CC_OPTS += -I../gmp/gmpbuild
+SRC_HC_OPTS += -I../gmp/gmpbuild
+SRC_HSC2HS_OPTS += -I../gmp/gmpbuild
+SRC_LD_OPTS += -L../gmp/gmpbuild
 
 #-----------------------------------------------------------------------------
 #
@@ -320,10 +371,10 @@ SRC_HC_OPTS += -optc-fno-stack-protector
 endif
 
 # -O3 helps unroll some loops (especially in copy() with a constant argument).
-GC_HC_OPTS += -optc-O3
+sm/Evac_HC_OPTS += -optc-funroll-loops
 
 # Without this, thread_obj will not be inlined (at least on x86 with GCC 4.1.0)
-GCCompact_HC_OPTS += -optc-finline-limit=2500
+sm/Compact_HC_OPTS += -optc-finline-limit=2500
 
 # -fno-strict-aliasing is required for the runtime, because we often
 # use a variety of types to represent closure pointers (StgPtr,
@@ -333,9 +384,6 @@ GCCompact_HC_OPTS += -optc-finline-limit=2500
 # upd_evacee() assigments get moved before the object copy.
 SRC_CC_OPTS += -fno-strict-aliasing
 
-# Cmm must be compiled via-C for now, because the NCG can't handle loops
-SRC_HC_OPTS += -fvia-C
-
 # We *want* type-checking of hand-written cmm.
 SRC_HC_OPTS += -dcmm-lint 
 
@@ -359,11 +407,29 @@ endif
 #
 # Just libHSrts is installed uniformly across ways
 #
-INSTALL_LIBS += $(LIBRARY)
-ifeq "$(DLLized)" "YES"
-INSTALL_PROGS += $(DLL_NAME) gmp/gmp.dll
-INSTALL_LIBS += $(patsubst %.a,%_imp.a,$(LIBARY))
-INSTALL_LIBS += gmp/libgmp_imp.a Main.dll_o
-endif
 
 include $(TOP)/mk/target.mk
+
+ifeq "$(DYNAMIC_RTS) $(HOSTPLATFORM)" "YES i386-unknown-mingw32"
+# $(LIBRARY).a is not the static library libHSrts.a but
+# libHSrts.dll.a, the import library for dynamic linking required for
+# linking the dynamic version of base
+buildbase: $(LIBRARY).a
+       $(MAKE) way="" -C ../libraries/ make.library.base
+endif
+
+#-----------------------------------------------------------------------------
+#
+# binary-dist
+
+binary-dist:
+       $(INSTALL_DIR)                         $(BIN_DIST_DIR)/rts
+       $(INSTALL_DIR)                         $(BIN_DIST_DIR)/rts/gmp
+       $(INSTALL_DATA)    Makefile            $(BIN_DIST_DIR)/rts/
+       $(INSTALL_DATA)    package.conf.in     $(BIN_DIST_DIR)/rts/
+ifneq "$(INSTALL_LIBS)" ""
+       $(INSTALL_DATA)    $(INSTALL_LIBS)     $(BIN_DIST_DIR)/rts/
+endif
+ifneq "$(INSTALL_LIBEXECS)" ""
+       $(INSTALL_PROGRAM) $(INSTALL_LIBEXECS) $(BIN_DIST_DIR)/rts/
+endif