Move more flags from the Makefile into pragmas
[ghc-hetmet.git] / compiler / Makefile
index e881216..96057bc 100644 (file)
@@ -21,12 +21,18 @@ TOP = ..
 #
 UseGhcForCc = YES
 
+# We don't want to compile stage1 in multiple ways
+ifeq "$(findstring $(stage), 2 3)" ""
+WAYS=
+endif
+
 include $(TOP)/mk/boilerplate.mk
 
 ifeq "$(GhcThreaded)$(GhcProfiled)" "YESYES"
 $(error Cannot make GHC both threaded and profiled)
 endif
 
+
 #-----------------------------------------------------------------------------
 # Counting source code lines
 
@@ -46,9 +52,6 @@ count :
 #  - for stage N, object files and .hi files are placed inside 
 #    the directory stageN, in subdirectories as per the sources.
 #
-#  - .hi-boot files are *linked* into the stageN tree, because in GHC 5.05+
-#    the .hi-boot file must reside in the same place as the .hi file.
-#
 #  - we use explicit -o and -ohi options to direct the output from C & 
 #    Haskell compilations.
 #
@@ -74,8 +77,8 @@ count :
 #  There are other plausible designs that might work, but each has different
 #  problems:
 #
-#  - using -odir and -hidir: GHC <= 4.08 doesn't support -hidir, and
-#    anyway -odir puts all the objects in one directory (strips off the
+#  - using -odir and -hidir:
+#    -odir puts all the objects in one directory (strips off the
 #    subdirectory part), which eventually forces us to use VPATH to find
 #    the sources.  I have a really bad feeling about VPATH.
 #
@@ -96,7 +99,7 @@ stage_dirs :
            $(MKDIRHIER) stage$(stage)/$$i; \
        done
 
-ifeq "$(stage) $(ghc_ge_603)" "1 YES"
+ifeq "$(stage)" "1"
 UsingHsBoot = YES
 else
 ifneq "$(findstring $(stage), 2 3)" ""
@@ -107,27 +110,6 @@ endif
 endif
 
 boot :: stage_dirs
-# On Windows, we can't use symbolic links for the -hi-boot files
-# because GHC itself is a Mingw program and does not understand
-# symbolic links.  So we have to copy the files instead of link them.
-# That means that if you modify a .hi-boot file in Windows, you
-# have to to say 'make boot' again.
-#
-# PS: 'ln -s foo baz' takes 'foo' relative to the path to 'baz'
-#     whereas 'cp foo baz' treats the two paths independently.
-#     Hence the "../.." in the ln command line
-ifeq "$(UsingHsBoot)" "NO"
-ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
-       for i in */*hi-boot*; do \
-           cp -u -f $$i stage$(stage)/$$i; \
-       done
-else
-       for i in */*hi-boot*; do \
-           ($(RM) -f stage$(stage)/$$i \
-              && $(LN_S) ../../$$i stage$(stage)/$$i) || true ; \
-       done
-endif
-endif
 
 ifeq "$(stage)" "1"
 HC=$(GHC)
@@ -154,13 +136,15 @@ odir=stage$(stage)
 
 SRC_HC_OPTS += $(patsubst %, -i$(odir)/%, $(ALL_DIRS))
 
+SRC_HC_OPTS += -Wall -fno-warn-name-shadowing -fno-warn-orphans
+
 HS_OBJS = $(patsubst %, $(odir)/%, $(addsuffix .$(way_)o,$(basename $(HS_SRCS))))
 C_OBJS = $(patsubst %, $(odir)/%, $(addsuffix .$(way_)o,$(basename $(C_SRCS))))
 
 # Our standard cleaning rules don't know that we're doing our output
 # into $(odir), so we have to augment CLEAN_FILES appropriateliy.
 
-CLEAN_FILES += $(odir)/*/*.hi $(odir)/*/*.hi-boot $(odir)/*/*.o-boot
+CLEAN_FILES += $(odir)/*/*.$(way_)hi $(odir)/*/*.$(way_)hi-boot $(odir)/*/*.$(way_)o-boot
 
 ifeq "$(UsingHsBoot)" "YES"
 CLEAN_FILES += $(odir)/*/*.hi-boot $(odir)/*/*.o-boot
@@ -181,15 +165,11 @@ endif
 # be to do `env PATH=$(PATH) make ghc' to minimise the environment. (or the
 # equivalent of `env' if it doesn't exist locally).
 #
-ifneq "$(way)" "dll"
 ifneq "$(TARGETPLATFORM)" "i386-unknown-mingw32"
 GHC_PROG=$(odir)/ghc$(_way)-$(ProjectVersion)
 else
 GHC_PROG=$(odir)/ghc$(_way)
 endif
-else
-GHC_PROG=$(odir)/ghc-$(ProjectVersion)
-endif
 
 ifeq "$(stage)" "1"
 HS_PROG = $(GHC_PROG)
@@ -208,46 +188,94 @@ $(CONFIG_HS) : $(FPTOOLS_TOP)/mk/config.mk
        @$(RM) -f $(CONFIG_HS)
        @echo "Creating $(CONFIG_HS) ... "
        @echo "module Config where" >>$(CONFIG_HS)
+       @echo "cProjectName          :: String" >> $(CONFIG_HS)
        @echo "cProjectName          = \"$(ProjectName)\"" >> $(CONFIG_HS)
+       @echo "cProjectVersion       :: String" >> $(CONFIG_HS)
        @echo "cProjectVersion       = \"$(ProjectVersion)\"" >> $(CONFIG_HS)
+       @echo "cProjectVersionInt    :: String" >> $(CONFIG_HS)
        @echo "cProjectVersionInt    = \"$(ProjectVersionInt)\"" >> $(CONFIG_HS)
+       @echo "cProjectPatchLevel    :: String" >> $(CONFIG_HS)
        @echo "cProjectPatchLevel    = \"$(ProjectPatchLevel)\"" >> $(CONFIG_HS)
+       @echo "cBooterVersion        :: String" >> $(CONFIG_HS)
        @echo "cBooterVersion        = \"$(GhcVersion)\"" >> $(CONFIG_HS)
+       @echo "cStage                :: String" >> $(CONFIG_HS)
        @echo "cStage                = STAGE" >> $(CONFIG_HS)
+       @echo "cHscIfaceFileVersion  :: String" >> $(CONFIG_HS)
        @echo "cHscIfaceFileVersion  = \"$(HscIfaceFileVersion)\"" >> $(CONFIG_HS)
+       @echo "cSplitObjs            :: String" >> $(CONFIG_HS)
        @echo "cSplitObjs            = \"$(SplitObjs)\"" >> $(CONFIG_HS)
+       @echo "cGhcWithInterpreter   :: String" >> $(CONFIG_HS)
+       @echo "cGhcWithInterpreter   = \"$(GhcWithInterpreter)\"" >> $(CONFIG_HS)
+       @echo "cGhcWithNativeCodeGen :: String" >> $(CONFIG_HS)
        @echo "cGhcWithNativeCodeGen = \"$(GhcWithNativeCodeGen)\"" >> $(CONFIG_HS)
+       @echo "cGhcWithSMP           :: String" >> $(CONFIG_HS)
+       @echo "cGhcWithSMP           = \"$(GhcWithSMP)\"" >> $(CONFIG_HS)
+       @echo "cGhcRTSWays           :: String" >> $(CONFIG_HS)
+       @echo "cGhcRTSWays           = \"$(GhcRTSWays)\"" >> $(CONFIG_HS)
+       @echo "cGhcUnregisterised    :: String" >> $(CONFIG_HS)
        @echo "cGhcUnregisterised    = \"$(GhcUnregisterised)\"" >> $(CONFIG_HS)
+       @echo "cGhcEnableTablesNextToCode :: String" >> $(CONFIG_HS)
        @echo "cGhcEnableTablesNextToCode = \"$(GhcEnableTablesNextToCode)\"" >> $(CONFIG_HS)
+       @echo "cLeadingUnderscore    :: String" >> $(CONFIG_HS)
        @echo "cLeadingUnderscore    = \"$(LeadingUnderscore)\"" >> $(CONFIG_HS)
+       @echo "cRAWCPP_FLAGS         :: String" >> $(CONFIG_HS)
        @echo "cRAWCPP_FLAGS         = \"$(RAWCPP_FLAGS)\"" >> $(CONFIG_HS)
+       @echo "cGCC                  :: String" >> $(CONFIG_HS)
        @echo "cGCC                  = \"$(WhatGccIsCalled)\"" >> $(CONFIG_HS)
+       @echo "cMKDLL                :: String" >> $(CONFIG_HS)
        @echo "cMKDLL                = \"$(BLD_DLL)\"" >> $(CONFIG_HS)
+       @echo "cLdIsGNULd            :: String" >> $(CONFIG_HS)
        @echo "cLdIsGNULd            = \"$(LdIsGNULd)\"" >> $(CONFIG_HS)
+       @echo "cLD_X                 :: String" >> $(CONFIG_HS)
        @echo "cLD_X                 = \"$(LD_X)\"" >> $(CONFIG_HS)
-       @echo "cPROJECT_DIR          = \"$(PROJECT_DIR)\"" >> $(CONFIG_HS)
+       @echo "cGHC_DRIVER_DIR_REL   :: String" >> $(CONFIG_HS)
        @echo "cGHC_DRIVER_DIR_REL   = \"$(GHC_DRIVER_DIR_REL)\"" >> $(CONFIG_HS)
+       @echo "cGHC_TOUCHY_PGM       :: String" >> $(CONFIG_HS)
        @echo "cGHC_TOUCHY_PGM       = \"$(GHC_TOUCHY_PGM)\"" >> $(CONFIG_HS)
+       @echo "cGHC_TOUCHY_DIR_REL   :: String" >> $(CONFIG_HS)
        @echo "cGHC_TOUCHY_DIR_REL   = \"$(GHC_TOUCHY_DIR_REL)\"" >> $(CONFIG_HS)
+       @echo "cGHC_UNLIT_PGM        :: String" >> $(CONFIG_HS)
        @echo "cGHC_UNLIT_PGM        = \"$(GHC_UNLIT_PGM)\"" >> $(CONFIG_HS)
+       @echo "cGHC_UNLIT_DIR_REL    :: String" >> $(CONFIG_HS)
        @echo "cGHC_UNLIT_DIR_REL    = \"$(GHC_UNLIT_DIR_REL)\"" >> $(CONFIG_HS)
+       @echo "cGHC_MANGLER_PGM      :: String" >> $(CONFIG_HS)
        @echo "cGHC_MANGLER_PGM      = \"$(GHC_MANGLER_PGM)\"" >> $(CONFIG_HS)
+       @echo "cGHC_MANGLER_DIR_REL  :: String" >> $(CONFIG_HS)
        @echo "cGHC_MANGLER_DIR_REL  = \"$(GHC_MANGLER_DIR_REL)\"" >> $(CONFIG_HS)
+       @echo "cGHC_SPLIT_PGM        :: String" >> $(CONFIG_HS)
        @echo "cGHC_SPLIT_PGM        = \"$(GHC_SPLIT_PGM)\"" >> $(CONFIG_HS)
+       @echo "cGHC_SPLIT_DIR_REL    :: String" >> $(CONFIG_HS)
        @echo "cGHC_SPLIT_DIR_REL    = \"$(GHC_SPLIT_DIR_REL)\"" >> $(CONFIG_HS)
+       @echo "cGHC_SYSMAN_PGM       :: String" >> $(CONFIG_HS)
        @echo "cGHC_SYSMAN_PGM       = \"$(GHC_SYSMAN)\"" >> $(CONFIG_HS)
+       @echo "cGHC_SYSMAN_DIR_REL   :: String" >> $(CONFIG_HS)
        @echo "cGHC_SYSMAN_DIR_REL   = \"$(GHC_SYSMAN_DIR)\"" >> $(CONFIG_HS)
+       @echo "cGHC_CP               :: String" >> $(CONFIG_HS)
        @echo "cGHC_CP               = \"$(GHC_CP)\"" >> $(CONFIG_HS)
+       @echo "cGHC_PERL             :: String" >> $(CONFIG_HS)
        @echo "cGHC_PERL             = \"$(GHC_PERL)\"" >> $(CONFIG_HS)
-ifeq ($(GhcWithIlx),YES)
-       @echo "cILX2IL               = \"$(ILX2IL)\"" >> $(CONFIG_HS)
-       @echo "cILASM                = \"$(ILASM)\"" >> $(CONFIG_HS)
-endif
+       @echo "cEnableWin32DLLs      :: String" >> $(CONFIG_HS)
        @echo "cEnableWin32DLLs      = \"$(EnableWin32DLLs)\"" >> $(CONFIG_HS)
+       @echo "cCONTEXT_DIFF         :: String" >> $(CONFIG_HS)
        @echo "cCONTEXT_DIFF         = \"$(CONTEXT_DIFF)\"" >> $(CONFIG_HS)
+       @echo "cUSER_WAY_NAMES       :: String" >> $(CONFIG_HS)
        @echo "cUSER_WAY_NAMES       = \"$(USER_WAY_NAMES)\"" >> $(CONFIG_HS)
+       @echo "cUSER_WAY_OPTS        :: String" >> $(CONFIG_HS)
        @echo "cUSER_WAY_OPTS        = \"$(USER_WAY_OPTS)\"" >> $(CONFIG_HS)
+       @echo "cDEFAULT_TMPDIR       :: String" >> $(CONFIG_HS)
        @echo "cDEFAULT_TMPDIR       = \"$(DEFAULT_TMPDIR)\"" >> $(CONFIG_HS)
+       @echo "cRelocatableBuild     :: Bool"                 >> $(CONFIG_HS)
+ifeq "$(RelocatableBuild)" "YES"
+       @echo "cRelocatableBuild     = True"                  >> $(CONFIG_HS)
+else
+       @echo "cRelocatableBuild     = False"                 >> $(CONFIG_HS)
+endif
+       @echo "cLibFFI               :: Bool"                 >> $(CONFIG_HS)
+ifeq "$(UseLibFFIForAdjustors)" "YES"
+       @echo "cLibFFI               = True"                  >> $(CONFIG_HS)
+else
+       @echo "cLibFFI               = False"                 >> $(CONFIG_HS)
+endif
        @echo done.
 
 CLEAN_FILES += $(CONFIG_HS)
@@ -368,7 +396,7 @@ SRC_HC_OPTS += -Istage$(stage)
 ALL_DIRS = \
   utils basicTypes types hsSyn prelude rename typecheck deSugar coreSyn \
   vectorise specialise simplCore stranal stgSyn simplStg codeGen main \
-  profiling parser cprAnalysis ndpFlatten iface cmm
+  profiling parser cprAnalysis iface cmm
 
 # Make sure we include Config.hs even if it doesn't exist yet...
 ALL_SRCS += $(CONFIG_HS)
@@ -382,16 +410,6 @@ else
 SRC_HC_OPTS += -DOMIT_NATIVE_CODEGEN
 endif
 
-ifeq ($(GhcWithIlx),YES)
-ALL_DIRS += ilxGen
-SRC_HC_OPTS += -DILX
-endif
-
-ifeq ($(GhcWithJavaGen),YES)
-ALL_DIRS += javaGen
-SRC_HC_OPTS += -DJAVA
-endif
-
 ifeq "$(BootingFromHc)" "YES"
 # HC files are always from a self-booted compiler
 bootstrapped = YES
@@ -403,6 +421,15 @@ bootstrapped = NO
 endif
 endif
 
+# Beyond stage 1, honour any Mac OS X depolyment target options.  If we use 
+# these options in stage 1 we get a linker error if the bootstrap compiler is
+#  for a more recent OS version
+ifneq "$(findstring $(stage), 2 3)" ""
+SRC_CC_OPTS += $(MACOSX_DEPLOYMENT_CC_OPTS)
+SRC_HC_OPTS += $(addprefix -optc, $(MACOSX_DEPLOYMENT_CC_OPTS))
+SRC_LD_OPTS += $(addprefix -optl, $(MACOSX_DEPLOYMENT_LD_OPTS))
+endif
+
 # -----------------------------------------------------------------------------
 # Building a compiler with interpreter support
 #
@@ -411,13 +438,16 @@ endif
 # the interpreter is supported on this platform.
 
 ifeq "$(bootstrapped)" "YES"
-SRC_HC_OPTS += -package hpc -package bytestring
-PKG_DEPENDS += hpc bytestring
+SRC_HC_OPTS += -package bytestring
+PKG_DEPENDS += bytestring
 endif
 
+SRC_HC_OPTS += -package hpc
+PKG_DEPENDS += hpc
+
 ifeq "$(GhcWithInterpreter) $(bootstrapped)" "YES YES"
 
-# Yes, include the interepreter, readline, and Template Haskell extensions
+# Yes, include the interepreter and Template Haskell extensions
 SRC_HC_OPTS += -DGHCI -package template-haskell
 PKG_DEPENDS += template-haskell
 
@@ -438,6 +468,9 @@ ifeq "$(GhcDebugged)" "YES"
 SRC_LD_OPTS += -debug
 endif
 
+SRC_HC_OPTS     += -I$(FPTOOLS_TOP)/libffi/build/include
+SRC_HSC2HS_OPTS += -I$(FPTOOLS_TOP)/libffi/build/include
+
 ALL_DIRS += ghci
 
 # If we are going to use dynamic libraries instead of .o files for ghci,
@@ -450,26 +483,23 @@ else
 EXCLUDED_SRCS += ghci/keepCAFsForGHCi.c
 endif
 
-# Enable readline if either:
-#   - we're building stage 1 and $(GhcHasReadline)="YES"
-#   - we're building stage 2/3, and we have built the readline package
+# Enable editline if either:
+#   - we're building stage 1 and $(GhcHasEditline)="YES"
+#   - we're building stage 2/3, and we have built the editline package
 #
-# But we don't enable readline on Windows, because readline is fairly
-# broken there.
+# But we don't enable editline on Windows, as Windows terminals have
+# editline-like support builtin.
 #
 ifneq "$(TARGETPLATFORM)" "i386-unknown-mingw32"
 ifeq "$(stage)" "1"
-ifeq "$(GhcHasReadline)" "YES"
-SRC_HC_OPTS += -package readline -DUSE_READLINE
-PKG_DEPENDS += readline
+ifeq "$(GhcHasEditline)" "YES"
+SRC_HC_OPTS += -package editline -DUSE_EDITLINE
+PKG_DEPENDS += editline
 endif
 else
--include $(FPTOOLS_TOP)/libraries/readline/config.mk
-# readline's config.mk sets PACKAGE, which we don't want here
-PACKAGE=
-ifeq "$(READLINE_BUILD_PACKAGE)" "yes"
-SRC_HC_OPTS += -package readline -DUSE_READLINE
-PKG_DEPENDS += readline
+ifeq "$(wildcard $(FPTOOLS_TOP_ABS)/libraries/editline/unbuildable)" ""
+SRC_HC_OPTS += -package editline -DUSE_EDITLINE
+PKG_DEPENDS += editline
 endif
 endif # stage=1
 endif # not windows
@@ -496,9 +526,13 @@ SRC_MKDEPENDC_OPTS += -I$(GHC_INCLUDE_DIR)
 # -----------------------------------------------------------------------------
 #              Haskell compilations
 
-SRC_HC_OPTS += \
-  -cpp -fglasgow-exts -fno-generics -Rghc-timing \
-  -I. -Iparser
+SRC_HC_OPTS += -cpp -fglasgow-exts -Rghc-timing -I. -Iparser -Iutil
+
+ifeq "$(ghc_ge_609)" "NO"
+SRC_HC_OPTS += -fno-generics
+else
+SRC_HC_OPTS += -XNoGenerics
+endif
 
 # Omitted:     -I$(GHC_INCLUDE_DIR)
 # We should have -I$(GHC_INCLUDE_DIR) in SRC_HC_OPTS, 
@@ -509,11 +543,6 @@ SRC_HC_OPTS += \
 # Disaster!  Then the hc file sees the GHC 5.02 (or whatever)
 # include files.   For the moment we've reverted to using
 # an explicit path in the .hs sources
-#
-# For the benefit of <5.00 compilers, do include GHC_INCLUDE_DIR
-# when generating dependencies. (=> it gets passed onto mkdependHS,
-# which needs it).
-SRC_MKDEPENDHS_OPTS += -I$(GHC_INCLUDE_DIR)
 
 # We need System.Posix (or Posix when ghc < 6.2)
 ifeq "$(TARGETPLATFORM)" "i386-unknown-mingw32"
@@ -522,81 +551,31 @@ SRC_HC_OPTS += -package Win32
 PKG_DEPENDS += Win32
 endif
 else
-ifeq "$(bootstrapped) $(ghc_ge_601)" "NO NO"
-SRC_HC_OPTS += -package posix
-else
 SRC_HC_OPTS += -package unix
 PKG_DEPENDS += unix
 endif
-endif
 
-# We use the Cabal package in stages 2/3 only; in stage 1 we're using
-# the libcompat library which provides the Cabal modules.
-ifneq "$(stage)" "1"
 SRC_HC_OPTS += -package Cabal
 PKG_DEPENDS += Cabal
-endif
 
-ifeq "$(ghc_ge_603)" "YES"
 # Ignore lang, to avoid potential clash with the Generics module if
 # lang happens to be a dependency of some exposed package in the local
 # GHC installation (eg. wxHaskell did this around 6.4).
 SRC_HC_OPTS += -ignore-package lang
-endif
 
 SRC_CC_OPTS += -Iparser -I. -O
-SRC_HC_OPTS += -recomp $(GhcHcOpts) $(GhcStage$(stage)HcOpts)
+SRC_HC_OPTS += $(GhcHcOpts) $(GhcStage$(stage)HcOpts)
 SRC_HC_OPTS += -H16M
 
 ifeq "$(BootingFromHc)" "YES"
 SRC_CC_OPTS += -D__GLASGOW_HASKELL__=$(ProjectVersionInt)
 endif
 
-#      Special flags for particular modules
-#      The standard suffix rule for compiling a Haskell file
-#      adds these flags to the command line
-
-# There used to be a -no-recomp flag on PrimOp, but why?
-# It's an expensive module to recompile!
-prelude/PrimOp_HC_OPTS                 = -H80m
-
-
-main/ParsePkgConf_HC_OPTS      += -fno-warn-incomplete-patterns
-parser/Parser_HC_OPTS          += -fno-warn-incomplete-patterns
-
-ifeq "$(ghc_ge_603)" "NO"
-# Use -fvia-C since the NCG can't handle the narrow16Int# (and intToInt16#?)
-# primops on all platforms.
-parser/Parser_HC_OPTS          += -fvia-C
-# because the NCG can't handle the 64-bit math in here
-prelude/PrelRules_HC_OPTS      += -fvia-C
-# ByteCodeItbls uses primops that the NCG doesn't support.
-ghci/ByteCodeItbls_HC_OPTS     += -fvia-C
-ghci/ByteCodeLink_HC_OPTS      += -fvia-C -monly-3-regs
-endif
-
-# Careful optimisation of the parser: we don't want to throw everything
-# at it, because that takes too long and doesn't buy much, but we do want
-# to inline certain key external functions, so we instruct GHC not to
-# throw away inlinings as it would normally do in -Onot mode:
-parser/Parser_HC_OPTS          += -Onot -fno-ignore-interface-pragmas
-
 ifeq "$(HOSTPLATFORM)" "hppa1.1-hp-hpux9"
 rename/RnMonad_HC_OPTS                 =  -O2 -O2-for-C
 endif
 
-utils/Digraph_HC_OPTS          = -fglasgow-exts 
-
-basicTypes/SrcLoc_HC_OPTS      = -funbox-strict-fields
-
-# We always optimise some low-level modules, otherwise performance of
-# a non-optimised compiler is severely affected.
-main/BinIface_HC_OPTS          += -O
-utils/Binary_HC_OPTS           += -O -funbox-strict-fields
-utils/FastMutInt_HC_OPTS       += -O
-utils/Encoding_HC_OPTS         += -O
-utils/StringBuffer_HC_OPTS     += -O -funbox-strict-fields
-utils/FastString_HC_OPTS       += -O -funbox-strict-fields
+# basicTypes/SrcLoc_HC_OPTS    = -funbox-strict-fields
 
 # ---- Profiling ----
 #simplCore/Simplify_HC_OPTS = -auto-all
@@ -622,12 +601,14 @@ ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
 main/SysTools_HC_OPTS          += '-\#include <windows.h>' '-\#include <process.h>'
 endif
 
-parser/Lexer_HC_OPTS += -funbox-strict-fields
-
 # ghc_strlen percolates through so many modules that it is easier to get its
 # prototype via a global option instead of a myriad of per-file OPTIONS
 SRC_HC_OPTS += '-\#include "cutils.h"'
 
+ifeq "$(ghc_ge_605)" "NO"
+utils/LazyUniqFM_HC_OPTS += -fallow-undecidable-instances
+endif
+
 # ----------------------------------------------------------------------------
 #              Generate supporting stuff for prelude/PrimOp.lhs 
 #              from prelude/primops.txt
@@ -699,31 +680,30 @@ primop-usage.hs-incl: prelude/primops.txt
 #-----------------------------------------------------------------------------
 #              Linking
 
-# Include libghccompat in stage1 only.  In stage2 onwards, all these
-# libraries will be available from the main libraries.
-
-ifeq "$(stage)" "1"
-include $(GHC_COMPAT_DIR)/compat.mk
+ifeq "$(GhcUnregisterised)" "NO"
+ifeq "$(HOSTPLATFORM)" "ia64-unknown-linux"
+# needed for generating proper relocation in large binaries: trac #856
+SRC_LD_OPTS += -optl-Wl,--relax
+endif
 endif
-
-SRC_LD_OPTS += -no-link-chk
 
 # -----------------------------------------------------------------------------
 # create ghc-inplace, a convenient way to run ghc from the build tree...
 # See comments in $(FPTOOLS_TOP)/utils/ghc-pkg/Makefile for why we use
 # a real binary here rather than a shell script.
 
-INPLACE_PROG = $(odir)/ghc-inplace$(exeext)
+INPLACE_SRC  = $(odir)/ghc-inplace.c
+INPLACE_PROG = $(odir)/ghc-inplace$(_way)$(exeext)
+INPLACE_EXTRA_FLAGS = -I$(TOP)/includes
 EXCLUDED_C_SRCS += ghc-inplace.c
 
-# FPTOOLS_TOP_ABS platform uses backslashes, at least on Cygwin, but that
-# will go wrong when we use it in a Haskell string below.
-TOP_ABS=$(subst \\,/,$(FPTOOLS_TOP_ABS_PLATFORM))
+CLEAN_FILES += $(INPLACE_SRC)
 
-GHC_PATH=$(TOP_ABS)/$(GHC_COMPILER_DIR_REL)/$(GHC_PROG)
+GHC_PATH=$(FPTOOLS_TOP_ABS)/$(GHC_COMPILER_DIR_REL)/$(GHC_PROG)$(exeext)
 
 $(INPLACE_PROG): ghc-inplace.c
-       $(HC) -cpp -optc-DGHC_PATH=\"$(GHC_PATH)\" -optc-DTOP_ABS=\"$(TOP_ABS)\" $< -o $@
+       $(SED) -e "s@GHC_PATH@$(GHC_PATH)@g" -e "s@TOP_ABS@$(FPTOOLS_TOP_ABS)@g" < $< > $(INPLACE_SRC)
+       $(HC) -cpp $(INPLACE_EXTRA_FLAGS) $(INPLACE_SRC) -o $@
 
 all :: $(INPLACE_PROG)
 
@@ -794,8 +774,7 @@ endif
 # typecheck/TcType_HC_OPTS += -auto-all
 # typecheck/TcUnify_HC_OPTS += -auto-all
 
-coreSyn/CorePrep_HC_OPTS += -auto-all
-# parser/Parser_HC_OPTS += -fasm
+# coreSyn/CorePrep_HC_OPTS += -auto-all
 
 #-----------------------------------------------------------------------------
 # Building the GHC package
@@ -813,13 +792,16 @@ PACKAGE = ghc
 HIERARCHICAL_LIB = NO
 VERSION = $(ProjectVersion)
 PKG_DEPENDS += base haskell98
-PACKAGE_CPP_OPTS += -DPKG_DEPENDS='$(PKG_DEPENDS)'
+LIB_LD_OPTS += $(foreach pkg,$(PKG_DEPENDS),-package $(pkg))
+# We have to expand each package dependency with its version, which we
+# can do by calling "ghc-pkg list $pkg --simple-output".
+PACKAGE_CPP_OPTS += -DPKG_DEPENDS='$(foreach pkg,$(PKG_DEPENDS),$(shell $(GHC_PKG_INPLACE) latest --global $(pkg)))'
+# We want to define STAGE to be like "2" in the Haskell code, so we need
+# to quote the "s so that they don't get interpreted by the shell.
 PACKAGE_CPP_OPTS += -DSTAGE='"$(stage)"'
 
 # Omit Main from the library, the client will want to plug their own Main in
-LIBOBJS = $(filter-out $(odir)/main/Main.o   $(odir)/parser/hschooks.o     \
-                       $(odir)/main/Main.p_o $(odir)/parser/hschooks.p_o,  \
-                       $(OBJS))
+LIBOBJS = $(filter-out $(odir)/main/Main.$(way_)o $(odir)/parser/hschooks.$(way_)o, $(OBJS))
 
 # disable splitting: it won't really help with GHC, and the specialised
 # build system for compiler/ isn't set up to handle it.
@@ -833,11 +815,17 @@ GhcLibWays = $(GhcCompilerWays)
 # override $(GhcLibHcOpts): we want GhcStage2HcOpts to take precedence
 GhcLibHcOpts =
 
+ifeq "$(DOING_BIN_DIST)" "YES"
+# This is derived from the sources when we are in a source tree, but we
+# don't have any sources in a bindist, so we have to shortcut it
+HS_IFACES := $(wildcard stage$(stage)/*/*.hi)
+else
 # override default definition of HS_IFACES so we can add $(odir)
 HS_IFACES   = $(addsuffix .$(way_)hi,$(basename $(HS_OBJS)))
+endif
 
 # Haddock can't handle recursive modules currently, so we disable it for now.
-HADDOCK_DOCS = NO
+override HADDOCK_DOCS = NO
 
 # Tell package.mk not to set $(HC)
 NO_SET_HC = YES
@@ -855,24 +843,6 @@ NO_SET_HC = YES
 # configuration.  Yeuch... maybe one day this will all be done more cleanly.
 STAMP_PKG_CONF = $(GHC_DRIVER_DIR)/stamp-pkg-conf-$(PACKAGE)
 
-ifeq "$(GhcBuildDylibs)" "YES"
-ifeq "$(darwin_TARGET_OS)" "1"
-GhcLibraryName=libHS$(PACKAGE)$(_way)_dyn.dylib
-else
-GhcLibraryName=libHS$(PACKAGE)$(_way)_dyn.so
-endif
-else
-GhcLibraryName=libHS$(PACKAGE)$(_way).a
-endif
-
-ifneq "$(DOING_BIN_DIST)" "YES"
-$(GHC_PROG) : $(GhcLibraryName) main/Main.hs
-       $(RM) package.conf.inplace
-       $(RM) $(STAMP_PKG_CONF)
-       $(MAKE) $(STAMP_PKG_CONF)
-       $(MAKE) -f Makefile.ghcbin $(MFLAGS) HS_PROG=$(GHC_PROG) $@
-endif
-
 # Propagate standard targets to Makefile.ghcbin
 docs runtests $(BOOT_TARGET) TAGS clean distclean mostlyclean maintainer-clean $(INSTALL_TARGET) $(INSTALL_DOCS_TARGET) html chm HxS ps dvi txt::
        $(MAKE) -f Makefile.ghcbin $(MFLAGS) $@
@@ -903,25 +873,59 @@ EXTRA_SRCS += parser/Parser.y
 
 TAGS_HS_SRCS = parser/Parser.y.pp $(filter-out $(DERIVED_SRCS) main/Config.hs parser/Parser.y, $(sort $(SRCS)))
 
+WRONG_GHCTAGS_HS_SRCS = $(filter-out $(DERIVED_SRCS) main/Config.hs parser/Parser.y, $(sort $(SRCS)))
+# above is wrong because of the following problem:
+#      module `main:DataCon' is defined in multiple files: basicTypes/DataCon.lhs
+#                                                        basicTypes/DataCon.lhs-boot
+
+GHCTAGS_HS_SRCS = $(HS_SRCS)
+GHCTAGS_HC_OPTS = $(patsubst -i$(odir)/%, -i%, $(HC_OPTS))
+GHCTAGS_HC_OPTS += -DSTAGE='"$(stage)"'
+
+#------------------------------------------------------------
+#                      Tags
+
+.PHONY: ghctags
+
+ghctags :: $(GHCTAGS_HS_SRCS) $(TAGS_C_SRCS)
+       @if [ "$(stage)" != 2 ]; then echo "Must use 'make stage=2 ghctags'"; exit 1; fi
+       @$(RM) TAGS
+       @touch TAGS
+       @echo SOURCES ARE "$(GHCTAGS_HS_SRCS)"
+       : ifneq "$(GHCTAGS_HS_SRCS)" ""
+       @echo TIME TO ROCK AND ROLL
+       # $(GHCTAGS_INPLACE) -- $(MKDEPENDHS_OPTS) $(filter-out -split-objs, $(MKDEPENDHS_HC_OPTS)) -- $(GHCTAGS_HS_SRCS)
+       $(GHCTAGS_INPLACE) -- $(GHCTAGS_HC_OPTS) -- $(GHCTAGS_HS_SRCS)
+       : endif
+ifneq "$(TAGS_C_SRCS)" ""
+       etags -a $(TAGS_C_SRCS)
+endif
+       @( DEREFFED=`ls -l Makefile | sed -e 's/.*-> \(.*\)/\1/g'` && $(RM) `dirname $$DEREFFED`/TAGS && $(CP) TAGS `dirname $$DEREFFED` ) 2>/dev/null || echo TAGS file generated, perhaps copy over to source tree?
+
 include $(TOP)/mk/target.mk
 
 ifeq "$(BUILD_GHC_PACKAGE)" "YES"
-ifeq "$(way)" ""
 all :: $(GHC_PROG)
 endif
-endif
 
 $(odir)/main/Config.$(way_)o: SRC_HC_OPTS+=-DSTAGE='"$(stage)"'
 
-#-----------------------------------------------------------------------------
-# binary-dist
+ifneq "$(findstring $(stage), 2 3)" ""
+$(warning LIBRARY is $(LIBRARY))
 
-ifeq "$(DOING_BIN_DIST)" "YES"
-# This is derived from the sources when we are in a source tree, but we
-# don't have any sources in a bindist, so we have to shortcut it
-HS_IFACES := $(wildcard stage$(stage)/*/*.hi)
+ifneq "$(DOING_BIN_DIST)" "YES"
+$(GHC_PROG) : $(LIBRARY) main/Main.hs
+       $(RM) package.conf.inplace
+       $(RM) $(STAMP_PKG_CONF)
+       $(MAKE) way="" $(STAMP_PKG_CONF)
+       $(MAKE) -f Makefile.ghcbin $(MFLAGS) HS_PROG=$(GHC_PROG) $@
+endif
 endif
 
+
+#-----------------------------------------------------------------------------
+# binary-dist
+
 binary-dist:
        $(INSTALL_DIR)                         $(BIN_DIST_DIR)/compiler
        $(INSTALL_DIR)                         $(BIN_DIST_DIR)/compiler/stage$(stage)
@@ -952,7 +956,7 @@ endif
 # careful to recompile stuff you need if you reconfigure or change HsVersions.h.
 
 # Aargh, these don't work properly anyway, because GHC's recompilation checker
-# just reports "compilation NOT required".  Do we have to add -no-recomp for each
+# just reports "compilation NOT required".  Do we have to add -fforce-recomp for each
 # of these .hs files?  I haven't done anything about this yet.
 
 $(odir)/codeGen/Bitmap.$(way_)o                :  ../includes/MachDeps.h
@@ -1001,12 +1005,8 @@ ifneq "$(BootingFromHc)" "YES"
        $(MKDEPENDHS) -M -optdep-f -optdep.depend-BASE $(foreach way,$(WAYS),-optdep-s -optdep$(way)) $(foreach obj,$(MKDEPENDHS_OBJ_SUFFICES),-osuf $(obj)) $(MKDEPENDHS_OPTS) $(filter-out -split-objs, $(MKDEPENDHS_HC_OPTS)) $(HS_SRCS)
 endif
        $(MKDEPENDC) -f .depend-BASE $(MKDEPENDC_OPTS) $(foreach way,$(WAYS),-s $(way)) -- $(CC_OPTS) -- $(C_SRCS) 
-       $(PERL) -pe 'binmode(stdin); binmode(stdout); s@(\S*[._]o)@stage$(stage)/$$1@g; s@(\S*[._]hi)@stage$(stage)/$$1@g; s@^.*/compat.*$$@@g;' <.depend-BASE >.depend-$(stage)
+       $(PERL) -pe 'binmode(stdin); binmode(stdout); s@(\S*[._]o)@stage$(stage)/$$1@g; s@(\S*[._]hi)@stage$(stage)/$$1@g;' <.depend-BASE >.depend-$(stage)
 # The binmode stuff tells perl not to add stupid ^M's to the output
-#
-# The /lib/compat replacement is to workaround a bug in the
-# -optdep--exclude-module flag in GHC 6.4.  It is not required for any
-# other version of GHC, but doesn't do any harm.
 
 ifeq "$(MakefileDeps)" "YES"
 $(CONFIG_HS) : Makefile