Test for misaligned sections, and fail if we find one
[ghc.git] / ghc.mk
diff --git a/ghc.mk b/ghc.mk
index bd3d5da..a8c8cee 100644 (file)
--- a/ghc.mk
+++ b/ghc.mk
@@ -16,7 +16,6 @@
 #   * remove old Makefiles, add new stubs for building in subdirs
 #     * docs/Makefile
 #     * docs/docbook-cheat-sheet/Makefile
-#     * docs/ext-core/Makefile
 #     * docs/man/Makefile
 #     * docs/storage-mgmt/Makefile
 #     * docs/vh/Makefile
@@ -33,7 +32,7 @@
 #     then we don't have to use -osuf/-hisuf.  We would have to install
 #     them in different places too, so we'd need ghc-pkg support for packages
 #     of different ways.
-#   * make PACKAGES generated by './configure' or './boot'?
+#   * make PACKAGES_STAGE1 generated by './configure' or './boot'?
 #   * we should use a directory of package.conf files rather than a single
 #     file for the inplace package database, so that we can express
 #     dependencies more accurately.  Otherwise it's possible to get into
@@ -55,7 +54,7 @@
 #           o register each package into inplace/lib/package.conf
 #     * build libffi
 #     * With bootstrapping compiler:
-#          o Build libraries/{filepath,hpc,extensible-exceptions,Cabal}
+#          o Build libraries/{filepath,hpc,Cabal}
 #           o Build compiler (stage 1)
 #     * With stage 1:
 #           o Build libraries/*
@@ -81,7 +80,6 @@ default : all
 # Catch make if it runs away into an infinite loop
 ifeq      "$(MAKE_RESTARTS)" ""
 else ifeq "$(MAKE_RESTARTS)" "1"
-else ifeq "$(MAKE_RESTARTS)" "2"
 else
 $(error Make has restarted itself $(MAKE_RESTARTS) times; is there a makefile bug?)
 endif
@@ -257,7 +255,7 @@ include rules/tags-package.mk
 include rules/extra-packages.mk
 
 # -----------------------------------------------------------------------------
-# Registering hand-written package descriptions (used in libffi and rts)
+# Registering hand-written package descriptions (used in rts)
 
 include rules/manual-package-config.mk
 
@@ -285,27 +283,49 @@ include rules/bindist.mk
        "$(MKDIRHIER)" $@
 
 # -----------------------------------------------------------------------------
-# Packages
+# Lax dependencies
 
-# --------------------------------
+ifeq "$(LAX_DEPENDENCIES)" "YES"
+LAX_DEPS_FOLLOW = |
+else
+LAX_DEPS_FOLLOW =
+endif
+
+# This is a bit of a hack. When LAX_DEPS_FOLLOW is | some rules end up
+# looking like
+#     target: a | b | c
+# The first | signals the start of the order-only dependencies, but make
+# treats the second | as a dependency. So we need to tell make how to
+# build that dependency.
+
+.PHONY: |
+| :
+       @:
+
+# -----------------------------------------------------------------------------
 # Properties of packages
+
 # These lists say "if this package is built, here's a property it has"
 # They do not say "this package will be built"; see $(PACKAGES_xx) for that
 
 # Packages that are built but not installed
-INTREE_ONLY_PACKAGES := haskeline mtl terminfo utf8-string xhtml
+PKGS_THAT_ARE_INTREE_ONLY := haskeline mtl terminfo utf8-string xhtml
 
-DPH_PACKAGES := dph/dph-base dph/dph-prim-interface dph/dph-prim-seq \
-                dph/dph-common dph/dph-prim-par dph/dph-par dph/dph-seq \
-                vector primitive
+PKGS_THAT_ARE_DPH := \
+    dph/dph-base \
+    dph/dph-prim-interface dph/dph-prim-seq dph/dph-prim-par \
+    dph/dph-lifted-base \
+    dph/dph-lifted-boxed dph/dph-lifted-copy dph/dph-lifted-vseg \
+    vector primitive random
 
 # Packages that, if present, must be built by the stage2 compiler,
 # because they use TH and/or annotations, or depend on other stage2
 # packages:
-STAGE2_PACKAGES := $(DPH_PACKAGES) haskell98 haskell2010 random
+PKGS_THAT_BUILD_WITH_STAGE2 := $(PKGS_THAT_ARE_DPH) haskell98 haskell2010
+
 # Packages that we shouldn't build if we don't have TH (e.g. because
 # we're building a profiled compiler):
-TH_PACKAGES := $(DPH_PACKAGES)
+PKGS_THAT_USE_TH := $(PKGS_THAT_ARE_DPH)
 
 # Packages that are built by stage0, in addition to stage1.  These
 # packages are dependencies of GHC, that we do not assume the stage0
@@ -313,11 +333,7 @@ TH_PACKAGES := $(DPH_PACKAGES)
 #
 # We assume that the stage0 compiler has a suitable bytestring package,
 # so we don't have to include it below.
-PACKAGES_STAGE0 = Cabal/cabal hpc extensible-exceptions binary bin-package-db hoopl
-
-# These packages are installed, but are installed hidden
-# Why install them at all?  Because the 'ghc' package depends on them
-HIDDEN_PACKAGES = binary
+PKGS_THAT_BUILD_WITH_STAGE0 = Cabal/Cabal hpc binary bin-package-db hoopl
 
 # $(EXTRA_PACKAGES)  is another classification, of packages built but
 #                    not installed
@@ -326,19 +342,24 @@ HIDDEN_PACKAGES = binary
 #                    this ghc.mk 
 
 
-# --------------------------------
+
+# ----------------------------------------------------------------------------
 # Packages to build
 # The lists of packages that we *actually* going to build in each stage:
 #
-#  $(PACKAGES_STAGE0)   does double duty; it really is the list of packages
-#                       we build the bootstrap compiler in stage 0
-#
-#  $(PACKAGES)          A list of directories relative to libraries/ containing
-#                       packages that will be built by stage1, in dependency
-#                       order.
-#
-#  $(PACKAGES_STAGE2)   Ditto, for stage2.
+#  $(PACKAGES_STAGE0) 
+#  $(PACKAGES_STAGE1)
+#  $(PACKAGES_STAGE2)
 #
+# These are automatically derived from
+#    (a) the set of packages in this source tree
+#    (b) the predicates above, e.g. $(PKGS_THAT_BUILD_WITH_STAGE2)
+#    (c) which platform we're on, and a few other things
+
+
+# no processing to do on this one: it really is the list of packages
+# to build with stage 0.
+PACKAGES_STAGE0 = $(PKGS_THAT_BUILD_WITH_STAGE0)
 
 define addPackageGeneral
 # args: $1 = PACKAGES variable, $2 = package, $3 = condition
@@ -356,11 +377,13 @@ define addPackageGeneral
 endef
 
 define addPackage # args: $1 = package, $2 = condition
-ifneq "$(filter $1,$(TH_PACKAGES)) $(GhcProfiled)" "$1 YES"
-ifeq "$(filter $1,$(STAGE2_PACKAGES))" "$1"
+ifneq "$(filter $1,$(PKGS_THAT_USE_TH)) $(GhcProfiled)" "$1 YES"
+ifeq "$(filter $1,$(PKGS_THAT_BUILD_WITH_STAGE2))" "$1"
+ifneq "$(BuildingCrossCompiler)" "YES"
 $(call addPackageGeneral,PACKAGES_STAGE2,$1,$2)
+endif
 else
-$(call addPackageGeneral,PACKAGES,$1,$2)
+$(call addPackageGeneral,PACKAGES_STAGE1,$1,$2)
 endif
 endif
 endef
@@ -375,6 +398,7 @@ endif
 $(eval $(call addPackage,base))
 $(eval $(call addPackage,filepath))
 $(eval $(call addPackage,array))
+$(eval $(call addPackage,deepseq))
 $(eval $(call addPackage,bytestring))
 $(eval $(call addPackage,containers))
 
@@ -386,14 +410,12 @@ $(eval $(call addPackage,old-time))
 $(eval $(call addPackage,time))
 $(eval $(call addPackage,directory))
 $(eval $(call addPackage,process))
-$(eval $(call addPackage,random))
-$(eval $(call addPackage,extensible-exceptions))
 $(eval $(call addPackage,haskell98))
 $(eval $(call addPackage,haskell2010))
 $(eval $(call addPackage,hpc))
 $(eval $(call addPackage,pretty))
 $(eval $(call addPackage,template-haskell))
-$(eval $(call addPackage,Cabal/cabal))
+$(eval $(call addPackage,Cabal/Cabal))
 $(eval $(call addPackage,binary))
 $(eval $(call addPackage,bin-package-db))
 $(eval $(call addPackage,hoopl))
@@ -412,11 +434,11 @@ $(eval $(call extra-packages))
 # configured and registered all of its dependencies.  So the following
 # hack forces all the configure steps to happen in exactly the following order:
 #
-#  $(PACKAGES) ghc(stage2) $(PACKAGES_STAGE2)
+#  $(PACKAGES_STAGE1) ghc(stage2) $(PACKAGES_STAGE2)
 #
 # Ideally we should use the correct dependencies here to allow more
 # parallelism, but we don't know the dependencies until we've
-# generated the pacakge-data.mk files.
+# generated the package-data.mk files.
 define fixed_pkg_dep
 libraries/$1/$2/package-data.mk : $$(GHC_PKG_INPLACE) $$(fixed_pkg_prev)
 fixed_pkg_prev:=libraries/$1/$2/package-data.mk
@@ -424,7 +446,7 @@ endef
 
 ifneq "$(BINDIST)" "YES"
 fixed_pkg_prev=
-$(foreach pkg,$(PACKAGES),$(eval $(call fixed_pkg_dep,$(pkg),dist-install)))
+$(foreach pkg,$(PACKAGES_STAGE1),$(eval $(call fixed_pkg_dep,$(pkg),dist-install)))
 
 # the GHC package doesn't live in libraries/, so we add its dependency manually:
 compiler/stage2/package-data.mk: $(fixed_pkg_prev)
@@ -447,10 +469,8 @@ utils/hsc2hs/dist-install/package-data.mk: compiler/stage2/package-data.mk
 utils/compare_sizes/dist-install/package-data.mk: compiler/stage2/package-data.mk
 utils/runghc/dist-install/package-data.mk: compiler/stage2/package-data.mk
 
-# add the final two package.conf dependencies: ghc-prim depends on RTS,
-# and RTS depends on libffi.
+# add the final package.conf dependency: ghc-prim depends on RTS
 libraries/ghc-prim/dist-install/package-data.mk : rts/package.conf.inplace
-rts/package.conf.inplace : libffi/package.conf.inplace
 endif
 
 # --------------------------------
@@ -462,17 +482,12 @@ BOOT_PKG_CONSTRAINTS := \
             --constraint "$p == $(shell grep -i "^Version:" libraries/$d/$p.cabal | sed "s/[^0-9.]//g")"))
 
 # The actual .a and .so/.dll files: needed for dependencies.
-ALL_STAGE1_LIBS  = $(foreach lib,$(PACKAGES),$(libraries/$(lib)_dist-install_v_LIB))
+ALL_STAGE1_LIBS  = $(foreach lib,$(PACKAGES_STAGE1),$(libraries/$(lib)_dist-install_v_LIB))
 ifeq "$(BuildSharedLibs)" "YES"
-ALL_STAGE1_LIBS += $(foreach lib,$(PACKAGES),$(libraries/$(lib)_dist-install_dyn_LIB))
+ALL_STAGE1_LIBS += $(foreach lib,$(PACKAGES_STAGE1),$(libraries/$(lib)_dist-install_dyn_LIB))
 endif
 BOOT_LIBS = $(foreach lib,$(PACKAGES_STAGE0),$(libraries/$(lib)_dist-boot_v_LIB))
 
-OTHER_LIBS = libffi/dist-install/build/libHSffi$(v_libsuf) libffi/dist-install/build/HSffi.o
-ifeq "$(BuildSharedLibs)" "YES"
-OTHER_LIBS  += libffi/dist-install/build/libHSffi$(dyn_libsuf)
-endif
-
 # ----------------------------------------
 # Special magic for the ghc-prim package
 
@@ -526,8 +541,6 @@ endif
 # these cases, so we just skip checking them.
 # NB. these must come before we include the ghc.mk files below, because
 # they disable the relevant rules.
-CHECKED_libraries/dph/dph-seq = YES
-CHECKED_libraries/dph/dph-par = YES
 # In compiler's case, include-dirs points outside of the source tree
 CHECKED_compiler = YES
 
@@ -539,9 +552,13 @@ BUILD_DIRS += \
    $(GHC_MKDIRHIER_DIR)
 endif
 
+ifeq "$(Windows)" "YES"
+BUILD_DIRS += \
+   $(GHC_TOUCHY_DIR)
+endif
+
 BUILD_DIRS += \
    docs/users_guide \
-   docs/ext-core \
    docs/man \
    $(GHC_UNLIT_DIR) \
    $(GHC_HP2PS_DIR)
@@ -556,9 +573,15 @@ BUILD_DIRS += \
    $(GHC_GENPRIMOP_DIR)
 endif
 
+ifeq "$(BuildingCrossCompiler)-$(phase)" "YES-final"
+MAYBE_GHCI=
+else
+MAYBE_GHCI=driver/ghci
+endif
+
 BUILD_DIRS += \
    driver \
-   driver/ghci \
+   $(MAYBE_GHCI) \
    driver/ghc \
    driver/haddock \
    libffi \
@@ -573,7 +596,7 @@ endif
 
 ifneq "$(CLEANING)" "YES"
 BUILD_DIRS += \
-   $(patsubst %, libraries/%, $(PACKAGES))
+   $(patsubst %, libraries/%, $(PACKAGES_STAGE1))
 endif
 
 ifeq "$(INTEGER_LIBRARY)" "integer-gmp"
@@ -582,28 +605,38 @@ else ifneq "$(findstring clean,$(MAKECMDGOALS))" ""
 BUILD_DIRS += libraries/integer-gmp/gmp
 endif
 
+ifeq "$(BuildingCrossCompiler)-$(phase)" "YES-final"
+MAYBE_COMPILER=
+MAYBE_GHCTAGS=
+MAYBE_HPC=
+MAYBE_RUNGHC=
+else
+MAYBE_COMPILER=compiler
+MAYBE_GHCTAGS=utils/ghctags
+MAYBE_HPC=utils/hpc
+MAYBE_RUNGHC=utils/runghc
+endif
+
 BUILD_DIRS += \
    utils/haddock \
    utils/haddock/doc \
-   compiler \
+   $(MAYBE_COMPILER) \
    $(GHC_HSC2HS_DIR) \
    $(GHC_PKG_DIR) \
    utils/testremove \
-   utils/ghctags \
+   $(MAYBE_GHCTAGS) \
    utils/ghc-pwd \
    $(GHC_CABAL_DIR) \
-   utils/hpc \
-   utils/runghc \
+   $(MAYBE_HPC) \
+   $(MAYBE_RUNGHC) \
    ghc
-ifeq "$(Windows)" "YES"
-BUILD_DIRS += \
-   $(GHC_TOUCHY_DIR)
-endif
 
 ifneq "$(BINDIST)" "YES"
+ifneq "$(BuildingCrossCompiler)-$(phase)" "YES-final"
 BUILD_DIRS += \
    utils/mkUserGuidePart
 endif
+endif
 
 BUILD_DIRS += utils/count_lines
 BUILD_DIRS += utils/compare_sizes
@@ -617,7 +650,10 @@ endif
 # ----------------------------------------------
 # Actually include all the sub-ghc.mk's
 
-include $(patsubst %, %/ghc.mk, $(BUILD_DIRS))
+# BUILD_DIRS_EXTRA needs to come after BUILD_DIRS, because stuff in
+# libraries/dph/ghc.mk refers to stuff defined earlier, in particular
+# things like $(libraries/dph/dph-base_dist-install_GHCI_LIB)
+include $(patsubst %, %/ghc.mk, $(BUILD_DIRS) $(BUILD_DIRS_EXTRA))
 
 # A useful pseudo-target (must be after the include above, because it needs
 # the value of things like $(libraries/base_dist-install_v_LIB).
@@ -634,7 +670,7 @@ stage1_libs : $(ALL_STAGE1_LIBS)
 #   libraries/<pkg>_dist-install_HC_OPTS += -Wwarn
 
 # Add $(GhcLibHcOpts) to all package builds
-$(foreach pkg,$(PACKAGES) $(PACKAGES_STAGE2),$(eval libraries/$(pkg)_dist-install_HC_OPTS += $$(GhcLibHcOpts)))
+$(foreach pkg,$(PACKAGES_STAGE1) $(PACKAGES_STAGE2),$(eval libraries/$(pkg)_dist-install_HC_OPTS += $$(GhcLibHcOpts)))
 
 # Add $(GhcBootLibHcOpts) to all stage0 package builds
 $(foreach pkg,$(PACKAGES_STAGE0),$(eval libraries/$(pkg)_dist-boot_HC_OPTS += $$(GhcBootLibHcOpts)))
@@ -643,20 +679,18 @@ $(foreach pkg,$(PACKAGES_STAGE0),$(eval libraries/$(pkg)_dist-boot_HC_OPTS += $$
 # Haddock-related bits
 
 # Don't run Haddock for the package that will not be installed
-$(foreach p,$(INTREE_ONLY_PACKAGES),$(eval libraries/$p_dist-install_DO_HADDOCK = NO))
+$(foreach p,$(PKGS_THAT_ARE_INTREE_ONLY),$(eval libraries/$p_dist-install_DO_HADDOCK = NO))
 # We don't haddock the bootstrapping libraries
 $(foreach p,$(PACKAGES_STAGE0),$(eval libraries/$p_dist-boot_DO_HADDOCK = NO))
 
 # Build the Haddock contents and index
 ifeq "$(HADDOCK_DOCS)" "YES"
-libraries/index.html: inplace/bin/haddock$(exeext) $(ALL_HADDOCK_FILES)
+libraries/dist-haddock/index.html: inplace/bin/haddock$(exeext) $(ALL_HADDOCK_FILES)
        cd libraries && sh gen_contents_index --inplace
 ifeq "$(phase)" "final"
-$(eval $(call all-target,library_doc_index,libraries/index.html))
+$(eval $(call all-target,library_doc_index,libraries/dist-haddock/index.html))
 endif
-INSTALL_LIBRARY_DOCS += libraries/*.html libraries/*.gif libraries/*.css libraries/*.js
-CLEAN_FILES += libraries/doc-index* libraries/haddock*.css \
-              libraries/haddock*.js libraries/index*.html libraries/*.gif
+INSTALL_LIBRARY_DOCS += libraries/dist-haddock/*
 endif
 
 # -----------------------------------------------------------------------------
@@ -689,7 +723,7 @@ ifneq "$(BINDIST)" "YES"
 # ghc-stage2.  DPH includes a bit of Template Haskell which needs the
 # GHCI libs, and we don't have a better way to express that dependency.
 #
-GHCI_LIBS = $(foreach lib,$(PACKAGES),$(libraries/$(lib)_dist-install_GHCI_LIB)) \
+GHCI_LIBS = $(foreach lib,$(PACKAGES_STAGE1),$(libraries/$(lib)_dist-install_GHCI_LIB)) \
            $(compiler_stage2_GHCI_LIB)
 
 ifeq "$(UseArchivesForGhci)" "NO"
@@ -745,7 +779,7 @@ TAGS: TAGS_compiler
 # -----------------------------------------------------------------------------
 # Installation
 
-install: install_libs install_packages install_libexecs install_headers \
+install: install_libs install_packages install_libexecs \
          install_libexec_scripts install_bins install_topdirs
 ifeq "$(HADDOCK_DOCS)" "YES"
 install: install_docs
@@ -765,7 +799,8 @@ install_libs: $(INSTALL_LIBS)
                    $(call INSTALL_DATA,$(INSTALL_OPTS),$$i,"$(DESTDIR)$(ghclibdir)"); \
                    $(RANLIB) $(DESTDIR)$(ghclibdir)/`basename $$i` ;; \
                  *.dll) \
-                   $(call INSTALL_DATA,-s $(INSTALL_OPTS),$$i,"$(DESTDIR)$(ghclibdir)") ;; \
+                   $(call INSTALL_PROGRAM,$(INSTALL_OPTS),$$i,"$(DESTDIR)$(ghclibdir)") ; \
+                   $(STRIP_CMD) "$(DESTDIR)$(ghclibdir)"/$$i ;; \
                  *.so) \
                    $(call INSTALL_SHLIB,$(INSTALL_OPTS),$$i,"$(DESTDIR)$(ghclibdir)") ;; \
                  *.dylib) \
@@ -795,7 +830,7 @@ else
        done
 # We rename ghc-stage2, so that the right program name is used in error
 # messages etc.
-       "$(MV)" "$(DESTDIR)$(ghclibexecdir)/ghc-stage2" "$(DESTDIR)$(ghclibexecdir)/ghc"
+       "$(MV)" "$(DESTDIR)$(ghclibexecdir)/ghc-stage$(INSTALL_GHC_STAGE)" "$(DESTDIR)$(ghclibexecdir)/ghc"
 endif
 
 install_topdirs: $(INSTALL_TOPDIRS)
@@ -804,12 +839,6 @@ install_topdirs: $(INSTALL_TOPDIRS)
                $(call INSTALL_PROGRAM,$(INSTALL_BIN_OPTS),$$i,"$(DESTDIR)$(topdir)"); \
        done
 
-install_headers: $(INSTALL_HEADERS)
-       $(call INSTALL_DIR,"$(DESTDIR)$(ghcheaderdir)")
-       for i in $(INSTALL_HEADERS); do \
-               $(call INSTALL_HEADER,$(INSTALL_OPTS),$$i,"$(DESTDIR)$(ghcheaderdir)"); \
-       done
-
 install_docs: $(INSTALL_DOCS)
        $(call INSTALL_DIR,"$(DESTDIR)$(docdir)")
 ifneq "$(INSTALL_DOCS)" ""
@@ -846,14 +875,16 @@ INSTALLED_GHC_REAL=$(DESTDIR)$(bindir)/ghc.exe
 INSTALLED_GHC_PKG_REAL=$(DESTDIR)$(bindir)/ghc-pkg.exe
 endif
 
-INSTALLED_PKG_DIRS := $(addprefix libraries/,$(PACKAGES)) \
-                      compiler \
-                      $(addprefix libraries/,$(PACKAGES_STAGE2))
+INSTALLED_PKG_DIRS := $(addprefix libraries/,$(PACKAGES_STAGE1))
+ifeq "$(BuildingCrossCompiler)" "NO"
+INSTALLED_PKG_DIRS := $(INSTALLED_PKG_DIRS) compiler
+endif
+INSTALLED_PKG_DIRS := $(INSTALLED_PKG_DIRS) $(addprefix libraries/,$(PACKAGES_STAGE2))
 ifeq "$(InstallExtraPackages)" "NO"
 INSTALLED_PKG_DIRS := $(filter-out $(addprefix libraries/,$(EXTRA_PACKAGES)),\
                                    $(INSTALLED_PKG_DIRS))
 endif
-INSTALLED_PKG_DIRS := $(filter-out $(addprefix libraries/,$(INTREE_ONLY_PACKAGES)),\
+INSTALLED_PKG_DIRS := $(filter-out $(addprefix libraries/,$(PKGS_THAT_ARE_INTREE_ONLY)),\
                                    $(INSTALLED_PKG_DIRS))
 
 # Set the INSTALL_DISTDIR_p for each package; compiler is special
@@ -863,14 +894,14 @@ INSTALL_DISTDIR_compiler = stage2
 
 # Now we can do the installation
 install_packages: install_libexecs
-install_packages: libffi/package.conf.install rts/package.conf.install
+install_packages: rts/package.conf.install
        $(call INSTALL_DIR,"$(DESTDIR)$(topdir)")
-       "$(RM)" $(RM_OPTS_REC) "$(INSTALLED_PACKAGE_CONF)"
+       $(call removeTrees,"$(INSTALLED_PACKAGE_CONF)")
        $(call INSTALL_DIR,"$(INSTALLED_PACKAGE_CONF)")
-       "$(INSTALLED_GHC_PKG_REAL)" --force --global-conf "$(INSTALLED_PACKAGE_CONF)" update libffi/package.conf.install
        "$(INSTALLED_GHC_PKG_REAL)" --force --global-conf "$(INSTALLED_PACKAGE_CONF)" update rts/package.conf.install
        $(foreach p, $(INSTALLED_PKG_DIRS),                           \
            $(call make-command,                                      \
+                   CROSS_COMPILE="$(CrossCompilePrefix)"              \
                   "$(GHC_CABAL_INPLACE)" install                     \
                                          "$(INSTALLED_GHC_REAL)"     \
                                          "$(INSTALLED_GHC_PKG_REAL)" \
@@ -882,10 +913,12 @@ install_packages: libffi/package.conf.install rts/package.conf.install
                                          '$(ghclibdir)'              \
                                          '$(docdir)/html/libraries'  \
                                          $(RelocatableBuild)))
-       $(foreach p, $(HIDDEN_PACKAGES),                                   \
-           $(call make-command,                                           \
-                  "$(INSTALLED_GHC_PKG_REAL)"                             \
-                      --global-conf "$(INSTALLED_PACKAGE_CONF)" hide $p))
+# when we install the packages above, ghc-pkg obeys umask when creating
+# the package.conf files, but for everything else we specify the
+# permissions. We therefore now fix the permissions of package.cache.
+# This means "sudo make install" does the right thing even if it runs
+# with an 077 umask.
+       for f in '$(INSTALLED_PACKAGE_CONF)'/*; do $(CREATE_DATA) "$$f"; done
 
 # -----------------------------------------------------------------------------
 # Binary distributions
@@ -911,11 +944,10 @@ $(eval $(call bindist,.,\
     $(BINDIST_LIBS) \
     $(BINDIST_HI) \
     $(BINDIST_EXTRAS) \
-    $(includes_H_CONFIG) \
-    $(includes_H_PLATFORM) \
     $(includes_H_FILES) \
-    includes/ghcconfig.h \
-    $(INSTALL_HEADERS) \
+    $(includes_DERIVEDCONSTANTS) \
+    $(includes_GHCCONSTANTS) \
+    $(libffi_HEADERS) \
     $(INSTALL_LIBEXECS) \
     $(INSTALL_LIBEXEC_SCRIPTS) \
     $(INSTALL_TOPDIRS) \
@@ -947,7 +979,7 @@ endif
 BIN_DIST_MK = $(BIN_DIST_PREP_DIR)/bindist.mk
 
 unix-binary-dist-prep:
-       "$(RM)" $(RM_OPTS_REC) bindistprep/
+       $(call removeTrees,bindistprep/)
        "$(MKDIRHIER)" $(BIN_DIST_PREP_DIR)
        set -e; for i in packages LICENSE compiler ghc rts libraries utils docs libffi includes driver mk rules Makefile aclocal.m4 config.sub config.guess install-sh settings.in ghc.mk inplace distrib/configure.ac distrib/README distrib/INSTALL; do ln -s ../../$$i $(BIN_DIST_PREP_DIR)/; done
        echo "HADDOCK_DOCS       = $(HADDOCK_DOCS)"       >> $(BIN_DIST_MK)
@@ -958,13 +990,13 @@ unix-binary-dist-prep:
        echo "BUILD_MAN          = $(BUILD_MAN)"          >> $(BIN_DIST_MK)
        echo "GHC_CABAL_INPLACE  = utils/ghc-cabal/dist-install/build/tmp/ghc-cabal" >> $(BIN_DIST_MK)
        cd $(BIN_DIST_PREP_DIR) && autoreconf
-       "$(RM)" $(RM_OPTS) $(BIN_DIST_PREP_TAR)
+       $(call removeFiles,$(BIN_DIST_PREP_TAR))
 # h means "follow symlinks", e.g. if aclocal.m4 is a symlink to a source
 # tree then we want to include the real file, not a symlink to it
        cd bindistprep && "$(TAR_CMD)" hcf - -T ../$(BIN_DIST_LIST) | bzip2 -c > ../$(BIN_DIST_PREP_TAR_BZ2)
 
 windows-binary-dist-prep:
-       "$(RM)" $(RM_OPTS_REC) bindistprep/
+       $(call removeTrees,bindistprep/)
        $(MAKE) prefix=$(TOP)/$(BIN_DIST_PREP_DIR) install
        cd bindistprep && "$(TAR_CMD)" cf - $(BIN_DIST_NAME) | bzip2 -c > ../$(BIN_DIST_PREP_TAR_BZ2)
 
@@ -1028,20 +1060,29 @@ publish-docs:
 #
 # Directory in which we're going to build the src dist
 #
-SRC_DIST_NAME=ghc-$(ProjectVersion)
-SRC_DIST_DIR=$(TOP)/$(SRC_DIST_NAME)
+SRC_DIST_ROOT      = sdistprep
+SRC_DIST_BASE_NAME = ghc-$(ProjectVersion)
+
+SRC_DIST_GHC_NAME          = ghc-$(ProjectVersion)-src
+SRC_DIST_GHC_ROOT          = $(SRC_DIST_ROOT)/ghc
+SRC_DIST_GHC_DIR           = $(SRC_DIST_GHC_ROOT)/$(SRC_DIST_BASE_NAME)
+SRC_DIST_GHC_TARBALL       = $(SRC_DIST_ROOT)/$(SRC_DIST_GHC_NAME).tar.bz2
+
+SRC_DIST_TESTSUITE_NAME    = ghc-$(ProjectVersion)-testsuite
+SRC_DIST_TESTSUITE_ROOT    = $(SRC_DIST_ROOT)/testsuite-ghc
+SRC_DIST_TESTSUITE_DIR     = $(SRC_DIST_TESTSUITE_ROOT)/$(SRC_DIST_BASE_NAME)
+SRC_DIST_TESTSUITE_TARBALL = $(SRC_DIST_ROOT)/$(SRC_DIST_TESTSUITE_NAME).tar.bz2
 
 #
 # Files to include in source distributions
 #
-SRC_DIST_DIRS = mk rules docs distrib bindisttest libffi includes utils docs rts compiler ghc driver libraries ghc-tarballs
-SRC_DIST_FILES += \
-       configure.ac config.guess config.sub configure \
-       aclocal.m4 README ANNOUNCE HACKING LICENSE Makefile install-sh \
-       ghc.spec.in ghc.spec settings.in VERSION \
-       boot boot-pkgs packages ghc.mk
-
-SRC_DIST_TARBALL = $(SRC_DIST_NAME)-src.tar.bz2
+SRC_DIST_GHC_DIRS = mk rules docs distrib bindisttest libffi includes \
+    utils docs rts compiler ghc driver libraries ghc-tarballs
+SRC_DIST_GHC_FILES += \
+    configure.ac config.guess config.sub configure \
+    aclocal.m4 README ANNOUNCE HACKING LICENSE Makefile install-sh \
+    ghc.spec.in ghc.spec settings.in VERSION \
+    boot boot-pkgs packages ghc.mk
 
 VERSION :
        echo $(ProjectVersion) >VERSION
@@ -1049,50 +1090,66 @@ VERSION :
 sdist : VERSION
 
 # Use:
-#     $(call sdist_file,compiler,stage2,cmm,Foo/Bar,CmmLex,x)
+#     $(call sdist_ghc_file,compiler,stage2,cmm,Foo/Bar,CmmLex,x)
 # to copy the generated file that replaces compiler/cmm/Foo/Bar/CmmLex.x, where
 # "stage2" is the dist dir.
-define sdist_file
-       "$(CP)" $1/$2/build/$4/$5.hs $(SRC_DIST_DIR)/$1/$3/$4
-       mv $(SRC_DIST_DIR)/$1/$3/$4/$5.$6 $(SRC_DIST_DIR)/$1/$3/$4/$5.$6.source
+define sdist_ghc_file
+       "$(CP)" $1/$2/build/$4/$5.hs $(SRC_DIST_GHC_DIR)/$1/$3/$4
+       mv $(SRC_DIST_GHC_DIR)/$1/$3/$4/$5.$6 $(SRC_DIST_GHC_DIR)/$1/$3/$4/$5.$6.source
 endef
 
-.PHONY: sdist-prep
-sdist-prep :
-       "$(RM)" $(RM_OPTS_REC) $(SRC_DIST_DIR)
-       "$(RM)" $(RM_OPTS) $(SRC_DIST_TARBALL)
-       mkdir $(SRC_DIST_DIR)
-       cd $(SRC_DIST_DIR) && for i in $(SRC_DIST_DIRS); do mkdir $$i; ( cd $$i && lndir $(TOP)/$$i ); done
-       cd $(SRC_DIST_DIR) && for i in $(SRC_DIST_FILES); do $(LN_S) $(TOP)/$$i .; done
-       cd $(SRC_DIST_DIR) && $(MAKE) distclean
-       rm -rf $(SRC_DIST_DIR)/libraries/tarballs/
-       rm -rf $(SRC_DIST_DIR)/libraries/stamp/
-       $(call sdist_file,compiler,stage2,cmm,,CmmLex,x)
-       $(call sdist_file,compiler,stage2,cmm,,CmmParse,y)
-       $(call sdist_file,compiler,stage2,parser,,Lexer,x)
-       $(call sdist_file,compiler,stage2,parser,,Parser,y.pp)
-       $(call sdist_file,compiler,stage2,parser,,ParserCore,y)
-       $(call sdist_file,utils/hpc,dist,,,HpcParser,y)
-       $(call sdist_file,utils/genprimopcode,dist,,,Lexer,x)
-       $(call sdist_file,utils/genprimopcode,dist,,,Parser,y)
-       $(call sdist_file,utils/haddock,dist,src,Haddock,Lex,x)
-       $(call sdist_file,utils/haddock,dist,src,Haddock,Parse,y)
-       cd $(SRC_DIST_DIR) && "$(RM)" $(RM_OPTS_REC) compiler/stage[123] mk/build.mk
-       cd $(SRC_DIST_DIR) && "$(FIND)" $(SRC_DIST_DIRS) \( -name _darcs -o -name SRC -o -name "autom4te*" -o -name "*~" -o -name ".cvsignore" -o -name "\#*" -o -name ".\#*" -o -name "log" -o -name "*-SAVE" -o -name "*.orig" -o -name "*.rej" -o -name "*-darcs-backup*" \) -print | "$(XARGS)" $(XARGS_OPTS) "$(RM)" $(RM_OPTS_REC)
+.PHONY: sdist-ghc-prep
+sdist-ghc-prep :
+       $(call removeTrees,$(SRC_DIST_GHC_ROOT))
+       $(call removeFiles,$(SRC_DIST_GHC_TARBALL))
+       -mkdir $(SRC_DIST_ROOT)
+       mkdir $(SRC_DIST_GHC_ROOT)
+       mkdir $(SRC_DIST_GHC_DIR)
+       cd $(SRC_DIST_GHC_DIR) && for i in $(SRC_DIST_GHC_DIRS); do mkdir $$i; ( cd $$i && lndir $(TOP)/$$i ); done
+       cd $(SRC_DIST_GHC_DIR) && for i in $(SRC_DIST_GHC_FILES); do $(LN_S) $(TOP)/$$i .; done
+       cd $(SRC_DIST_GHC_DIR) && $(MAKE) distclean
+       $(call removeTrees,$(SRC_DIST_GHC_DIR)/libraries/tarballs/)
+       $(call removeTrees,$(SRC_DIST_GHC_DIR)/libraries/stamp/)
+       $(call removeTrees,$(SRC_DIST_GHC_DIR)/compiler/stage[123])
+       $(call removeFiles,$(SRC_DIST_GHC_DIR)/mk/build.mk)
+       $(call sdist_ghc_file,compiler,stage2,cmm,,CmmLex,x)
+       $(call sdist_ghc_file,compiler,stage2,cmm,,CmmParse,y)
+       $(call sdist_ghc_file,compiler,stage2,parser,,Lexer,x)
+       $(call sdist_ghc_file,compiler,stage2,parser,,Parser,y.pp)
+       $(call sdist_ghc_file,compiler,stage2,parser,,ParserCore,y)
+       $(call sdist_ghc_file,utils/hpc,dist-install,,,HpcParser,y)
+       $(call sdist_ghc_file,utils/genprimopcode,dist,,,Lexer,x)
+       $(call sdist_ghc_file,utils/genprimopcode,dist,,,Parser,y)
+       $(call sdist_ghc_file,utils/haddock,dist,src,Haddock,Lex,x)
+       $(call sdist_ghc_file,utils/haddock,dist,src,Haddock,Parse,y)
+       cd $(SRC_DIST_GHC_DIR) && "$(FIND)" $(SRC_DIST_GHC_DIRS) \( -name .git -o -name "autom4te*" -o -name "*~" -o -name "\#*" -o -name ".\#*" -o -name "log" -o -name "*-SAVE" -o -name "*.orig" -o -name "*.rej" \) -print | "$(XARGS)" $(XARGS_OPTS) "$(RM)" $(RM_OPTS_REC)
+
+.PHONY: sdist-testsuite-prep
+sdist-testsuite-prep :
+       $(call removeTrees,$(SRC_DIST_TESTSUITE_ROOT))
+       $(call removeFiles,$(SRC_DIST_TESTSUITE_TARBALL))
+       -mkdir $(SRC_DIST_ROOT)
+       mkdir $(SRC_DIST_TESTSUITE_ROOT)
+       mkdir $(SRC_DIST_TESTSUITE_DIR)
+       mkdir $(SRC_DIST_TESTSUITE_DIR)/testsuite
+       cd $(SRC_DIST_TESTSUITE_DIR)/testsuite && lndir $(TOP)/testsuite
+       $(call removeTrees,$(SRC_DIST_TESTSUITE_DIR)/testsuite/.git)
 
 .PHONY: sdist
-sdist : sdist-prep
-       "$(TAR_CMD)" chf - $(SRC_DIST_NAME) 2>$src_log | bzip2 >$(TOP)/$(SRC_DIST_TARBALL)
+sdist : sdist-ghc-prep sdist-testsuite-prep
+       cd $(SRC_DIST_GHC_ROOT) && "$(TAR_CMD)" chf - $(SRC_DIST_BASE_NAME) 2> src_ghc_log | bzip2 > $(TOP)/$(SRC_DIST_GHC_TARBALL)
+       cd $(SRC_DIST_TESTSUITE_ROOT) && "$(TAR_CMD)" chf - $(SRC_DIST_BASE_NAME) 2> src_ghc_log | bzip2 > $(TOP)/$(SRC_DIST_TESTSUITE_TARBALL)
 
-sdist-manifest : $(SRC_DIST_TARBALL)
-       tar tjf $(SRC_DIST_TARBALL) | sed "s|^ghc-$(ProjectVersion)/||" | sort >sdist-manifest
+sdist-manifest : $(SRC_DIST_GHC_TARBALL)
+       tar tjf $(SRC_DIST_GHC_TARBALL) | sed "s|^ghc-$(ProjectVersion)/||" | sort >sdist-manifest
 
 # Upload the distribution(s)
 # Retrying is to work around buggy firewalls that corrupt large file transfers
 # over SSH.
 ifneq "$(PublishLocation)" ""
 publish-sdist :
-       $(call try10Times,$(PublishCp) $(SRC_DIST_TARBALL) $(PublishLocation)/dist)
+       $(call try10Times,$(PublishCp) $(SRC_DIST_GHC_TARBALL) $(PublishLocation)/dist)
+       $(call try10Times,$(PublishCp) $(SRC_DIST_TESTSUITE_TARBALL) $(PublishLocation)/dist)
 endif
 
 ifeq "$(BootingFromHc)" "YES"
@@ -1102,7 +1159,7 @@ ifeq "$(BootingFromHc)" "YES"
 # flags explicitly to C compilations.
 SRC_CC_OPTS += -DNO_REGS -DUSE_MINIINTERPRETER
 SRC_CC_OPTS += -D__GLASGOW_HASKELL__=$(ProjectVersionInt)
-SRC_CC_OPTS += -I$(GHC_INCLUDE_DIR)
+SRC_CC_OPTS += $(addprefix -I,$(GHC_INCLUDE_DIRS))
 endif
 
 # -----------------------------------------------------------------------------
@@ -1123,76 +1180,87 @@ CLEAN_FILES += libraries/bootstrapping.conf
 CLEAN_FILES += libraries/integer-gmp/cbits/GmpDerivedConstants.h
 CLEAN_FILES += libraries/integer-gmp/cbits/mkGmpDerivedConstants
 
+# These four are no longer generated, but we still clean them for a while
+# as they may still be in old GHC trees:
+CLEAN_FILES += includes/GHCConstants.h
+CLEAN_FILES += includes/DerivedConstants.h
+CLEAN_FILES += includes/ghcautoconf.h
+CLEAN_FILES += includes/ghcplatform.h
+
 clean : clean_files clean_libraries
 
 .PHONY: clean_files
 clean_files :
-       "$(RM)" $(RM_OPTS) $(CLEAN_FILES)
+       $(call removeFiles,$(CLEAN_FILES))
 
 .PHONY: clean_libraries
-clean_libraries: $(patsubst %,clean_libraries/%_dist-install,$(PACKAGES) $(PACKAGES_STAGE2))
+clean_libraries: $(patsubst %,clean_libraries/%_dist-install,$(PACKAGES_STAGE1) $(PACKAGES_STAGE2))
 clean_libraries: $(patsubst %,clean_libraries/%_dist-boot,$(PACKAGES_STAGE0))
 
 clean_libraries:
-       "$(RM)" $(RM_OPTS_REC) $(patsubst %, libraries/%/dist, $(PACKAGES) $(PACKAGES_STAGE2))
-       "$(RM)" $(RM_OPTS) $(patsubst %, libraries/%/*.buildinfo, $(PACKAGES) $(PACKAGES_STAGE2))
+       $(call removeTrees,$(patsubst %, libraries/%/dist, $(PACKAGES_STAGE1) $(PACKAGES_STAGE2)))
+       $(call removeFiles,$(wildcard $(patsubst %, libraries/%/*.buildinfo, $(PACKAGES_STAGE1) $(PACKAGES_STAGE2))))
+       $(call removeFiles,$(patsubst %, libraries/%/config.log, $(PACKAGES_STAGE1) $(PACKAGES_STAGE2)))
+       $(call removeFiles,$(patsubst %, libraries/%/config.status, $(PACKAGES_STAGE1) $(PACKAGES_STAGE2)))
+       $(call removeFiles,$(wildcard $(patsubst %, libraries/%/include/Hs*Config.h, $(PACKAGES_STAGE1) $(PACKAGES_STAGE2))))
 
 # We have to define a clean target for each library manually, because the
 # libraries/*/ghc.mk files are not included when we're cleaning.
 ifeq "$(CLEANING)" "YES"
 $(foreach lib,$(PACKAGES_STAGE0),\
   $(eval $(call clean-target,libraries/$(lib),dist-boot,libraries/$(lib)/dist-boot)))
-$(foreach lib,$(PACKAGES) $(PACKAGES_STAGE2),\
+$(foreach lib,$(PACKAGES_STAGE1) $(PACKAGES_STAGE2),\
   $(eval $(call clean-target,libraries/$(lib),dist-install,libraries/$(lib)/dist-install)))
 endif
 
+clean : clean_haddock_index
+.PHONY: clean_haddock_index
+clean_haddock_index:
+       $(call removeTrees,libraries/dist-haddock)
+
 clean : clean_bindistprep
 .PHONY: clean_bindistprep
 clean_bindistprep:
-       "$(RM)" $(RM_OPTS_REC) bindistprep/
+       $(call removeTrees,bindistprep/)
 
 distclean : clean
-       "$(RM)" $(RM_OPTS) config.cache config.status config.log mk/config.h mk/stamp-h
-       "$(RM)" $(RM_OPTS) mk/config.mk mk/are-validating.mk mk/project.mk
-       "$(RM)" $(RM_OPTS) mk/config.mk.old mk/project.mk.old
-       "$(RM)" $(RM_OPTS) settings docs/users_guide/ug-book.xml
-       "$(RM)" $(RM_OPTS) compiler/ghc.cabal compiler/ghc.cabal.old
-       "$(RM)" $(RM_OPTS) ghc/ghc-bin.cabal
-       "$(RM)" $(RM_OPTS) libraries/base/include/HsBaseConfig.h
-       "$(RM)" $(RM_OPTS) libraries/directory/include/HsDirectoryConfig.h
-       "$(RM)" $(RM_OPTS) libraries/process/include/HsProcessConfig.h
-       "$(RM)" $(RM_OPTS) libraries/unix/include/HsUnixConfig.h
-       "$(RM)" $(RM_OPTS) libraries/old-time/include/HsTimeConfig.h
-       "$(RM)" $(RM_OPTS_REC) utils/ghc-pwd/dist
-       "$(RM)" $(RM_OPTS_REC) inplace
-
-       "$(RM)" $(RM_OPTS) $(patsubst %, libraries/%/config.log, $(PACKAGES) $(PACKAGES_STAGE2))
-       "$(RM)" $(RM_OPTS) $(patsubst %, libraries/%/config.status, $(PACKAGES) $(PACKAGES_STAGE2))
-       "$(RM)" $(RM_OPTS) $(patsubst %, libraries/%/include/Hs*Config.h, $(PACKAGES) $(PACKAGES_STAGE2))
-       "$(RM)" $(RM_OPTS_REC) $(patsubst %, libraries/%/autom4te.cache, $(PACKAGES) $(PACKAGES_STAGE2))
+       $(call removeFiles,config.cache config.status config.log mk/config.h mk/stamp-h)
+       $(call removeFiles,mk/config.mk mk/are-validating.mk mk/project.mk)
+       $(call removeFiles,mk/config.mk.old mk/project.mk.old)
+       $(call removeFiles,settings docs/users_guide/ug-book.xml)
+       $(call removeFiles,compiler/ghc.cabal compiler/ghc.cabal.old)
+       $(call removeFiles,ghc/ghc-bin.cabal)
+       $(call removeFiles,libraries/base/include/HsBaseConfig.h)
+       $(call removeFiles,libraries/directory/include/HsDirectoryConfig.h)
+       $(call removeFiles,libraries/process/include/HsProcessConfig.h)
+       $(call removeFiles,libraries/unix/include/HsUnixConfig.h)
+       $(call removeFiles,libraries/old-time/include/HsTimeConfig.h)
+       $(call removeTrees,utils/ghc-pwd/dist-boot)
+       $(call removeTrees,inplace)
+       $(call removeTrees,$(patsubst %, libraries/%/autom4te.cache, $(PACKAGES_STAGE1) $(PACKAGES_STAGE2)))
 
 maintainer-clean : distclean
-       "$(RM)" $(RM_OPTS) configure mk/config.h.in
-       "$(RM)" $(RM_OPTS_REC) autom4te.cache libraries/*/autom4te.cache
-       "$(RM)" $(RM_OPTS) ghc.spec
-       "$(RM)" $(RM_OPTS) $(patsubst %, libraries/%/GNUmakefile, \
-               $(PACKAGES) $(PACKAGES_STAGE2))
-       "$(RM)" $(RM_OPTS) $(patsubst %, libraries/%/ghc.mk, $(PACKAGES) $(PACKAGES_STAGE2))
-       "$(RM)" $(RM_OPTS) $(patsubst %, libraries/%/configure, \
-               $(PACKAGES) $(PACKAGES_STAGE2))
-       "$(RM)" $(RM_OPTS) libraries/base/include/HsBaseConfig.h.in
-       "$(RM)" $(RM_OPTS) libraries/directory/include/HsDirectoryConfig.h.in
-       "$(RM)" $(RM_OPTS) libraries/process/include/HsProcessConfig.h.in
-       "$(RM)" $(RM_OPTS) libraries/unix/include/HsUnixConfig.h.in
-       "$(RM)" $(RM_OPTS) libraries/old-time/include/HsTimeConfig.h.in
+       $(call removeFiles,configure mk/config.h.in)
+       $(call removeTrees,autom4te.cache $(wildcard libraries/*/autom4te.cache))
+       $(call removeFiles,ghc.spec)
+       $(call removeFiles,$(patsubst %, libraries/%/GNUmakefile, \
+               $(PACKAGES_STAGE1) $(PACKAGES_STAGE2)))
+       $(call removeFiles,$(patsubst %, libraries/%/ghc.mk, $(PACKAGES_STAGE1) $(PACKAGES_STAGE2)))
+       $(call removeFiles,$(patsubst %, libraries/%/configure, \
+               $(PACKAGES_STAGE1) $(PACKAGES_STAGE2)))
+       $(call removeFiles,libraries/base/include/HsBaseConfig.h.in)
+       $(call removeFiles,libraries/directory/include/HsDirectoryConfig.h.in)
+       $(call removeFiles,libraries/process/include/HsProcessConfig.h.in)
+       $(call removeFiles,libraries/unix/include/HsUnixConfig.h.in)
+       $(call removeFiles,libraries/old-time/include/HsTimeConfig.h.in)
 
 .PHONY: all_libraries
 
 .PHONY: bootstrapping-files
-bootstrapping-files: $(OTHER_LIBS)
-bootstrapping-files: includes/ghcautoconf.h
-bootstrapping-files: includes/DerivedConstants.h
-bootstrapping-files: includes/GHCConstants.h
+bootstrapping-files: $(includes_H_CONFIG)
+bootstrapping-files: $(includes_DERIVEDCONSTANTS)
+bootstrapping-files: $(includes_GHCCONSTANTS)
+bootstrapping-files: $(libffi_HEADERS)
 
 .DELETE_ON_ERROR: