Merge branch 'master' of http://darcs.haskell.org/ghc
[ghc.git] / ghc.mk
diff --git a/ghc.mk b/ghc.mk
index c1544ad..eaee872 100644 (file)
--- a/ghc.mk
+++ b/ghc.mk
@@ -1,7 +1,6 @@
-
 # -----------------------------------------------------------------------------
 #
-# (c) 2009 The University of Glasgow
+# (c) 2009-2013 The University of Glasgow
 #
 # This file is part of the GHC build system.
 #
@@ -52,7 +51,7 @@
 #     * For each package:
 #          o configure, generate package-data.mk and inplace-pkg-info
 #           o register each package into inplace/lib/package.conf
-#     * build libffi
+#     * build libffi (if not disabled by --with-system-libffi)
 #     * With bootstrapping compiler:
 #          o Build libraries/{filepath,hpc,Cabal}
 #           o Build compiler (stage 1)
@@ -129,13 +128,18 @@ include mk/ways.mk
 include mk/custom-settings.mk
 
 ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
-ifeq "$(DYNAMIC_BY_DEFAULT)" "YES"
+ifeq "$(DYNAMIC_GHC_PROGRAMS)" "YES"
 ifeq "$(findstring dyn,$(GhcLibWays))" ""
-$(error dyn is not in $$(GhcLibWays), but $$(DYNAMIC_BY_DEFAULT) is YES)
+$(error dyn is not in $$(GhcLibWays), but $$(DYNAMIC_GHC_PROGRAMS) is YES)
 endif
 else
 ifeq "$(findstring v,$(GhcLibWays))" ""
-$(error v is not in $$(GhcLibWays), and $$(DYNAMIC_BY_DEFAULT) is not YES)
+$(error v is not in $$(GhcLibWays), and $$(DYNAMIC_GHC_PROGRAMS) is not YES)
+endif
+endif
+ifeq "$(GhcProfiled)" "YES"
+ifeq "$(findstring p,$(GhcLibWays))" ""
+$(error p is not in $$(GhcLibWays), and $$(GhcProfiled) is YES)
 endif
 endif
 endif
@@ -150,6 +154,7 @@ endif
 include rules/prof.mk
 include rules/trace.mk
 include rules/library-path.mk
+include rules/add-dependency.mk
 include rules/make-command.mk
 include rules/pretty_commands.mk
 
@@ -162,7 +167,7 @@ include rules/clean-target.mk
 # -----------------------------------------------------------------------------
 # The inplace tree
 
-$(eval $(call clean-target,inplace,,inplace/bin inplace/lib))
+$(eval $(call clean-target,root,inplace,inplace/bin inplace/lib))
 
 # -----------------------------------------------------------------------------
 # Whether to build dependencies or not
@@ -191,7 +196,7 @@ include rules/way-prelims.mk
 $(foreach way,$(ALL_WAYS),\
   $(eval $(call way-prelims,$(way))))
 
-ifeq "$(DYNAMIC_BY_DEFAULT)" "YES"
+ifeq "$(DYNAMIC_GHC_PROGRAMS)" "YES"
 GHCI_WAY = dyn
 HADDOCK_WAY = dyn
 else
@@ -199,9 +204,19 @@ GHCI_WAY = v
 HADDOCK_WAY = v
 endif
 
+WINDOWS_DYN_PROG_RTS := rts
+ifeq "$(GhcThreaded)" "YES"
+WINDOWS_DYN_PROG_RTS := $(WINDOWS_DYN_PROG_RTS)_thr
+endif
+ifeq "$(GhcDebugged)" "YES"
+WINDOWS_DYN_PROG_RTS := $(WINDOWS_DYN_PROG_RTS)_debug
+endif
+WINDOWS_DYN_PROG_RTS := $(WINDOWS_DYN_PROG_RTS)_dyn_LIB_NAME
+
 # -----------------------------------------------------------------------------
 # Compilation Flags
 
+include rules/distdir-opts.mk
 include rules/distdir-way-opts.mk
 
 # -----------------------------------------------------------------------------
@@ -222,12 +237,9 @@ include rules/cmm-objs.mk
 ifneq "$(CLEANING)" "YES"
 
 include rules/hs-suffix-rules-srcdir.mk
-include rules/hs-suffix-rules.mk
+include rules/hs-suffix-way-rules-srcdir.mk
+include rules/hs-suffix-way-rules.mk
 include rules/hi-rule.mk
-
-$(foreach way,$(ALL_WAYS),\
-  $(eval $(call hi-rule,$(way))))
-
 include rules/c-suffix-rules.mk
 include rules/cmm-suffix-rules.mk
 
@@ -268,7 +280,7 @@ include rules/build-package.mk
 include rules/build-package-way.mk
 include rules/haddock.mk
 include rules/tags-package.mk
-include rules/extra-packages.mk
+include rules/foreachLibrary.mk
 
 # -----------------------------------------------------------------------------
 # Registering hand-written package descriptions (used in rts)
@@ -319,48 +331,6 @@ endif
        @:
 
 # -----------------------------------------------------------------------------
-# 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
-PKGS_THAT_ARE_INTREE_ONLY := haskeline transformers terminfo xhtml
-
-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:
-PKGS_THAT_BUILD_WITH_STAGE2 := \
-    $(PKGS_THAT_ARE_DPH) old-time haskell98 haskell2010
-
-# Packages that we shouldn't build if we don't have TH (e.g. because
-# we're building a profiled compiler):
-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
-# compiler already has installed (or up-to-date enough).
-#
-# We assume that the stage0 compiler has a suitable bytestring package,
-# so we don't have to include it below.
-PKGS_THAT_BUILD_WITH_STAGE0 = Cabal/Cabal hpc binary bin-package-db hoopl
-
-# $(EXTRA_PACKAGES)  is another classification, of packages built but
-#                    not installed
-#                    It is set in rules/extra-package.mk, 
-#                    by $(call extra-packages) a little further down 
-#                    this ghc.mk 
-
-
-
-# ----------------------------------------------------------------------------
 # Packages to build
 # The lists of packages that we *actually* going to build in each stage:
 #
@@ -368,83 +338,123 @@ PKGS_THAT_BUILD_WITH_STAGE0 = Cabal/Cabal hpc binary bin-package-db hoopl
 #  $(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
-    ifeq "$3" ""
-        $1 += $2
-    else
-        ifeq "$$(CLEANING)" "YES"
-            $1 += $2
-        else
-            ifeq $3
-                $1 += $2
-            endif
-        endif
-    endif
+# Note that we need to add them to these variables in dependency
+# order, as this is the order that they get configured in.
+
+ifeq "$(CLEANING)" "YES"
+
+define addLibraryForCleaning
+# We just add all packages to both the stage 0 and stage 1 lists.
+# Stage 2 gets cleaned in the same way as stage 1, so no need to
+# add it there.
+PACKAGES_STAGE0 += $1
+PACKAGES_STAGE1 += $1
 endef
+$(eval $(call foreachLibrary,addLibraryForCleaning))
 
-define addPackage # args: $1 = package, $2 = condition
-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_STAGE1,$1,$2)
-endif
+
+# Packages that are built by stage0. These packages are dependencies of
+# programs such as GHC and ghc-pkg, that we do not assume the stage0
+# compiler already has installed (or up-to-date enough).
+
+PACKAGES_STAGE0 = Cabal/Cabal hpc bin-package-db hoopl transformers
+ifeq "$(Windows_Host)" "NO"
+ifneq "$(HostOS_CPP)" "ios"
+PACKAGES_STAGE0 += terminfo
+endif
+endif
+
+PACKAGES_STAGE1 += ghc-prim
+PACKAGES_STAGE1 += $(INTEGER_LIBRARY)
+PACKAGES_STAGE1 += base
+PACKAGES_STAGE1 += filepath
+PACKAGES_STAGE1 += array
+PACKAGES_STAGE1 += deepseq
+PACKAGES_STAGE1 += bytestring
+PACKAGES_STAGE1 += containers
+PACKAGES_STAGE1 += old-locale
+
+ifeq "$(Windows_Host)" "YES"
+PACKAGES_STAGE1 += Win32
+endif
+PACKAGES_STAGE1 += time
+ifeq "$(Windows_Host)" "NO"
+PACKAGES_STAGE1 += unix
+endif
+
+PACKAGES_STAGE1 += directory
+PACKAGES_STAGE1 += process
+PACKAGES_STAGE1 += hpc
+PACKAGES_STAGE1 += pretty
+PACKAGES_STAGE1 += template-haskell
+PACKAGES_STAGE1 += Cabal/Cabal
+PACKAGES_STAGE1 += binary
+PACKAGES_STAGE1 += bin-package-db
+PACKAGES_STAGE1 += hoopl
+PACKAGES_STAGE1 += transformers
+
+ifneq "$(CrossCompiling)" "YES"
+PACKAGES_STAGE2 += old-time
+PACKAGES_STAGE2 += haskell98
+PACKAGES_STAGE2 += haskell2010
+endif
+
+# We normally install only the packages down to this point
+REGULAR_INSTALL_PACKAGES := $(addprefix libraries/,$(PACKAGES_STAGE1))
+ifeq "$(Stage1Only)" "NO"
+REGULAR_INSTALL_PACKAGES += compiler
+endif
+REGULAR_INSTALL_PACKAGES += $(addprefix libraries/,$(PACKAGES_STAGE2))
+
+PACKAGES_STAGE1 += xhtml
+ifeq "$(Windows_Target)" "NO"
+ifneq "$(TargetOS_CPP)" "ios"
+PACKAGES_STAGE1 += terminfo
+endif
+endif
+PACKAGES_STAGE1 += haskeline
+
+# If we have built the programs with dynamic libraries, then
+# ghc will be dynamically linked against haskeline.so etc, so
+# we need the dynamic libraries of everything down to here
+REGULAR_INSTALL_DYNLIBS := $(addprefix libraries/,$(PACKAGES_STAGE1))
+REGULAR_INSTALL_DYNLIBS += $(addprefix libraries/,$(PACKAGES_STAGE2))
+REGULAR_INSTALL_DYNLIBS := $(filter-out $(REGULAR_INSTALL_PACKAGES),\
+                                        $(REGULAR_INSTALL_DYNLIBS))
+
+ifneq "$(CrossCompiling)" "YES"
+define addExtraPackage
+ifeq "$2" "-"
+# Do nothing; this package is already handled above
+else ifeq "$2 $$(GhcProfiled)" "dph YES"
+# Ignore the package: These packages need TH, which is incompatible
+# with a profiled GHC
+else
+PACKAGES_STAGE2 += $1
 endif
 endef
+$(eval $(call foreachLibrary,addExtraPackage))
+endif
 
-# Add all the packages. Note that we need to add them in dependency
-# order, as this is the order that they get configured in.
+# If we want to just install everything, then we want all the packages
+SUPERSIZE_INSTALL_PACKAGES := $(addprefix libraries/,$(PACKAGES_STAGE1))
+ifeq "$(Stage1Only)" "NO"
+SUPERSIZE_INSTALL_PACKAGES += compiler
+endif
+SUPERSIZE_INSTALL_PACKAGES += $(addprefix libraries/,$(PACKAGES_STAGE2))
 
-$(eval $(call addPackage,ghc-prim))
-ifeq "$(CLEANING)" "YES"
-$(eval $(call addPackage,integer-gmp))
-$(eval $(call addPackage,integer-simple))
+INSTALL_DYNLIBS  :=
+ifeq "$(InstallExtraPackages)" "NO"
+INSTALL_PACKAGES := $(REGULAR_INSTALL_PACKAGES)
+ifeq "$(DYNAMIC_GHC_PROGRAMS)" "YES"
+INSTALL_DYNLIBS := $(REGULAR_INSTALL_DYNLIBS)
+endif
 else
-$(eval $(call addPackage,$(INTEGER_LIBRARY)))
-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))
-$(eval $(call addPackage,old-locale))
-$(eval $(call addPackage,old-time))
-
-$(eval $(call addPackage,Win32,($$(Windows),YES)))
-$(eval $(call addPackage,time))
-$(eval $(call addPackage,unix,($$(Windows),NO)))
-
-$(eval $(call addPackage,directory))
-$(eval $(call addPackage,process))
-$(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,binary))
-$(eval $(call addPackage,bin-package-db))
-$(eval $(call addPackage,hoopl))
-$(eval $(call addPackage,transformers))
-$(eval $(call addPackage,xhtml))
-$(eval $(call addPackage,terminfo,($$(Windows),NO)))
-$(eval $(call addPackage,haskeline))
-
-$(eval $(call extra-packages))
+INSTALL_PACKAGES := $(SUPERSIZE_INSTALL_PACKAGES)
+endif
+
+endif
 
 # -------------------------------------------
 # Dependencies between package-data.mk files
@@ -459,7 +469,7 @@ $(eval $(call extra-packages))
 # parallelism, but we don't know the dependencies until we've
 # generated the package-data.mk files.
 define fixed_pkg_dep
-libraries/$1/$2/package-data.mk : $$(GHC_PKG_INPLACE) $$(fixed_pkg_prev)
+libraries/$1/$2/package-data.mk : $$(fixed_pkg_prev)
 fixed_pkg_prev:=libraries/$1/$2/package-data.mk
 endef
 
@@ -484,6 +494,7 @@ utils/ghc-pwd/dist-install/package-data.mk: compiler/stage2/package-data.mk
 utils/ghc-cabal/dist-install/package-data.mk: compiler/stage2/package-data.mk
 
 utils/ghctags/dist-install/package-data.mk: compiler/stage2/package-data.mk
+utils/dll-split/dist-install/package-data.mk: compiler/stage2/package-data.mk
 utils/hpc/dist-install/package-data.mk: compiler/stage2/package-data.mk
 utils/ghc-pkg/dist-install/package-data.mk: compiler/stage2/package-data.mk
 utils/hsc2hs/dist-install/package-data.mk: compiler/stage2/package-data.mk
@@ -498,6 +509,11 @@ endif
 # --------------------------------
 # Misc package-related settings
 
+# Run Haddock for the packages that will be installed. We need to handle
+# compiler specially due to the different dist directory name.
+$(foreach p,$(INSTALL_PACKAGES),$(eval $p_dist-install_DO_HADDOCK = YES))
+compiler_stage2_DO_HADDOCK = YES
+
 BOOT_PKG_CONSTRAINTS := \
     $(foreach d,$(PACKAGES_STAGE0),\
         $(foreach p,$(basename $(notdir $(wildcard libraries/$d/*.cabal))),\
@@ -530,8 +546,8 @@ endef
 
 PRIMOPS_TXT_STAGE1 = compiler/stage1/build/primops.txt
 
-libraries/ghc-prim/dist-install/build/GHC/PrimopWrappers.hs : $(GENPRIMOP_INPLACE) $(PRIMOPS_TXT_STAGE1) | $$(dir $$@)/.
-       "$(GENPRIMOP_INPLACE)" --make-haskell-wrappers < $(PRIMOPS_TXT_STAGE1) >$@
+libraries/ghc-prim/dist-install/build/GHC/PrimopWrappers.hs : $$(genprimopcode_INPLACE) $(PRIMOPS_TXT_STAGE1) | $$(dir $$@)/.
+       "$(genprimopcode_INPLACE)" --make-haskell-wrappers < $(PRIMOPS_TXT_STAGE1) >$@
 
 # Required so that Haddock documents the primops.
 libraries/ghc-prim_dist-install_EXTRA_HADDOCK_SRCS = libraries/ghc-prim/dist-install/build/autogen/GHC/Prim.hs
@@ -561,136 +577,92 @@ libraries/dph/dph-lifted-copy_dist-install_EXCLUDED_WAYS := dyn
 libraries/dph/dph-lifted-vseg_dist-install_EXCLUDED_WAYS := dyn
 endif
 
-# ----------------------------------------------
-# Checking packages with 'cabal check'
-
-ifeq "$(phase)" "final"
-ifeq "$(CHECK_PACKAGES)" "YES"
-all: check_packages
-endif
-endif
-
-# These packages don't pass the Cabal checks because hs-source-dirs
-# points outside the source directory. This isn't a real problem in
-# 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.
-# In compiler's case, include-dirs points outside of the source tree
-CHECKED_compiler = YES
-
 # -----------------------------------------------------------------------------
 # Include build instructions from all subdirs
 
 ifneq "$(BINDIST)" "YES"
-BUILD_DIRS += \
-   $(GHC_MKDIRHIER_DIR)
+BUILD_DIRS += utils/mkdirhier
 endif
 
-ifeq "$(Windows)" "YES"
-BUILD_DIRS += \
-   $(GHC_TOUCHY_DIR)
+ifeq "$(Windows_Host)" "YES"
+BUILD_DIRS += utils/touchy
 endif
 
-BUILD_DIRS += \
-   docs/users_guide \
-   docs/man \
-   $(GHC_UNLIT_DIR) \
-   $(GHC_HP2PS_DIR)
+BUILD_DIRS += utils/unlit
+BUILD_DIRS += utils/hp2ps
 
 ifneq "$(GhcUnregisterised)" "YES"
-BUILD_DIRS += \
-   $(GHC_SPLIT_DIR)
+BUILD_DIRS += driver/split
 endif
 
 ifneq "$(BINDIST)" "YES"
-BUILD_DIRS += \
-   $(GHC_GENPRIMOP_DIR)
+BUILD_DIRS += utils/genprimopcode
 endif
 
-ifeq "$(BuildingCrossCompiler)-$(phase)" "YES-final"
-MAYBE_GHCI=
-else
-MAYBE_GHCI=driver/ghci
-endif
-
-BUILD_DIRS += \
-   driver \
-   $(MAYBE_GHCI) \
-   driver/ghc \
-   driver/haddock \
-   libffi \
-   includes \
-   rts
+BUILD_DIRS += driver
+BUILD_DIRS += driver/ghci
+BUILD_DIRS += driver/ghc
+BUILD_DIRS += driver/haddock
+BUILD_DIRS += libffi
+BUILD_DIRS += utils/deriveConstants
+BUILD_DIRS += includes
+BUILD_DIRS += rts
 
 ifneq "$(BINDIST)" "YES"
-BUILD_DIRS += \
-   bindisttest \
-   $(GHC_GENAPPLY_DIR)
+BUILD_DIRS += bindisttest 
+BUILD_DIRS += utils/genapply
 endif
 
 ifneq "$(CLEANING)" "YES"
-BUILD_DIRS += \
-   $(patsubst %, libraries/%, $(PACKAGES_STAGE1))
+# These are deliberately in reverse order, so as to ensure that
+# there is no need to have them in dependency order. That's important
+# because it's tricky to ensure that they are in dependency order when
+# cross-compiling, as some packages may only be in PACKAGES_STAGE0
+# or PACKAGES_STAGE1.
+BUILD_DIRS += $(patsubst %, libraries/%, $(PACKAGES_STAGE2))
+BUILD_DIRS += $(patsubst %, libraries/%, $(PACKAGES_STAGE1))
+BUILD_DIRS += $(patsubst %, libraries/%, $(filter-out $(PACKAGES_STAGE1),$(PACKAGES_STAGE0)))
+BUILD_DIRS += $(wildcard libraries/dph)
 endif
 
+
 ifeq "$(INTEGER_LIBRARY)" "integer-gmp"
 BUILD_DIRS += libraries/integer-gmp/gmp
+BUILD_DIRS += libraries/integer-gmp/mkGmpDerivedConstants
 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 \
-   $(MAYBE_COMPILER) \
-   $(GHC_HSC2HS_DIR) \
-   $(GHC_PKG_DIR) \
-   utils/testremove \
-   $(MAYBE_GHCTAGS) \
-   utils/ghc-pwd \
-   $(GHC_CABAL_DIR) \
-   $(MAYBE_HPC) \
-   $(MAYBE_RUNGHC) \
-   ghc
+BUILD_DIRS += libraries/integer-gmp/mkGmpDerivedConstants
+endif
+
+BUILD_DIRS += utils/haddock
+BUILD_DIRS += utils/haddock/doc
+BUILD_DIRS += compiler
+BUILD_DIRS += utils/hsc2hs
+BUILD_DIRS += utils/ghc-pkg
+BUILD_DIRS += utils/testremove
+BUILD_DIRS += utils/ghctags
+BUILD_DIRS += utils/dll-split
+BUILD_DIRS += utils/ghc-pwd
+BUILD_DIRS += utils/ghc-cabal
+BUILD_DIRS += utils/hpc
+BUILD_DIRS += utils/runghc
+BUILD_DIRS += ghc
 
 ifneq "$(BINDIST)" "YES"
-ifneq "$(BuildingCrossCompiler)-$(phase)" "YES-final"
-BUILD_DIRS += \
-   utils/mkUserGuidePart
+ifneq "$(CrossCompiling)-$(phase)" "YES-final"
+BUILD_DIRS += utils/mkUserGuidePart
 endif
 endif
 
+BUILD_DIRS += docs/users_guide
+BUILD_DIRS += docs/man
 BUILD_DIRS += utils/count_lines
 BUILD_DIRS += utils/compare_sizes
 
-ifneq "$(CLEANING)" "YES"
-# After compiler/, because these packages depend on it
-BUILD_DIRS += \
-   $(patsubst %, libraries/%, $(PACKAGES_STAGE2))
-endif
-
 # ----------------------------------------------
 # Actually include all the sub-ghc.mk's
 
-# 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)
-ifeq "$(GhcProfiled)" "YES"
-BUILD_DIRS_EXTRA := $(filter-out libraries/dph,$(BUILD_DIRS_EXTRA))
-endif
-include $(patsubst %, %/ghc.mk, $(BUILD_DIRS) $(BUILD_DIRS_EXTRA))
+include $(patsubst %, %/ghc.mk, $(BUILD_DIRS))
 
 # A useful pseudo-target (must be after the include above, because it needs
 # the value of things like $(libraries/base_dist-install_v_LIB).
@@ -712,24 +684,6 @@ $(foreach pkg,$(PACKAGES_STAGE1) $(PACKAGES_STAGE2),$(eval libraries/$(pkg)_dist
 # Add $(GhcBootLibHcOpts) to all stage0 package builds
 $(foreach pkg,$(PACKAGES_STAGE0),$(eval libraries/$(pkg)_dist-boot_HC_OPTS += $$(GhcBootLibHcOpts)))
 
-# -----------------------------------------------
-# Haddock-related bits
-
-# Don't run Haddock for the package that will not be installed
-$(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/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/dist-haddock/index.html))
-endif
-INSTALL_LIBRARY_DOCS += libraries/dist-haddock/*
-endif
-
 # -----------------------------------------------------------------------------
 # Bootstrapping libraries
 
@@ -744,21 +698,21 @@ $(shell echo "[]" >$(BOOTSTRAPPING_CONF))
 endif
 endif
 
-$(eval $(call clean-target,$(BOOTSTRAPPING_CONF),,$(BOOTSTRAPPING_CONF)))
+$(eval $(call clean-target,root,bootstrapping_conf,$(BOOTSTRAPPING_CONF)))
 
 # register the boot packages in strict sequence, because running
 # multiple ghc-pkgs in parallel doesn't work (registrations may get
 # lost).
 fixed_pkg_prev=
 $(foreach pkg,$(PACKAGES_STAGE0),$(eval $(call fixed_pkg_dep,$(pkg),dist-boot)))
-
-compiler/stage1/package-data.mk : $(fixed_pkg_prev)
+utils/ghc-pkg/dist/package-data.mk: $(fixed_pkg_prev)
+compiler/stage1/package-data.mk:    $(fixed_pkg_prev)
 endif
 
 ifneq "$(BINDIST)" "YES"
 # Make sure we have all the GHCi libs by the time we've built
 # 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, and we don't have a better way to express that dependency.
 #
 GHCI_LIBS = $(foreach lib,$(PACKAGES_STAGE1),$(libraries/$(lib)_dist-install_GHCI_LIB)) \
            $(compiler_stage2_GHCI_LIB)
@@ -780,10 +734,23 @@ libraries/vector/dist-install/build/Data/Vector/Fusion/Stream/Monadic.$($(way)_o
   ))
 endif
 
+# -----------------------------------------------
+# Haddock-related bits
+
+# Build the Haddock contents and index
+ifeq "$(HADDOCK_DOCS)" "YES"
+libraries/dist-haddock/index.html: $(haddock_INPLACE) $(ALL_HADDOCK_FILES)
+       cd libraries && sh gen_contents_index --intree
+ifeq "$(phase)" "final"
+$(eval $(call all-target,library_doc_index,libraries/dist-haddock/index.html))
+endif
+INSTALL_LIBRARY_DOCS += libraries/dist-haddock/*
+endif
+
 # -----------------------------------------------------------------------------
 # Creating a local mingw copy on Windows
 
-ifeq "$(Windows)" "YES"
+ifeq "$(Windows_Host)" "YES"
 
 install : install_mingw
 .PHONY: install_mingw
@@ -795,7 +762,7 @@ install : install_perl
 install_perl : $(INPLACE_PERL)
        "$(CP)" -Rp $(INPLACE_PERL) $(prefix)
 
-endif # Windows
+endif # Windows_Host
 
 ifneq "$(BINDIST)" "YES"
 $(ghc-prim-$(libraries/ghc-prim_dist-install_VERSION)_HADDOCK_FILE): \
@@ -803,9 +770,9 @@ $(ghc-prim-$(libraries/ghc-prim_dist-install_VERSION)_HADDOCK_FILE): \
 endif # BINDIST
 
 libraries/ghc-prim/dist-install/build/autogen/GHC/Prim.hs: \
-                            $(PRIMOPS_TXT_STAGE1) $(GENPRIMOP_INPLACE) \
+                            $(PRIMOPS_TXT_STAGE1) $$(genprimopcode_INPLACE) \
                           | $$(dir $$@)/.
-       "$(GENPRIMOP_INPLACE)" --make-haskell-source < $< > $@
+       "$(genprimopcode_INPLACE)" --make-haskell-source < $< > $@
 
 .PHONY: tags
 tags: tags_compiler
@@ -833,7 +800,7 @@ define installLibsTo
                    $(RANLIB) $2/`basename $$i` ;; \
                  *.dll) \
                    $(call INSTALL_PROGRAM,$(INSTALL_OPTS),$$i,$2) ; \
-                   $(STRIP_CMD) $2/$$i ;; \
+                   $(STRIP_CMD) $2/`basename $$i` ;; \
                  *.so) \
                    $(call INSTALL_SHLIB,$(INSTALL_OPTS),$$i,$2) ;; \
                  *.dylib) \
@@ -900,7 +867,7 @@ INSTALLED_PACKAGE_CONF=$(DESTDIR)$(topdir)/package.conf.d
 
 # Install packages in the right order, so that ghc-pkg doesn't complain.
 # Also, install ghc-pkg first.
-ifeq "$(Windows)" "NO"
+ifeq "$(Windows_Host)" "NO"
 INSTALLED_GHC_REAL=$(DESTDIR)$(ghclibexecdir)/bin/ghc
 INSTALLED_GHC_PKG_REAL=$(DESTDIR)$(ghclibexecdir)/bin/ghc-pkg
 else
@@ -908,20 +875,8 @@ INSTALLED_GHC_REAL=$(DESTDIR)$(bindir)/ghc.exe
 INSTALLED_GHC_PKG_REAL=$(DESTDIR)$(bindir)/ghc-pkg.exe
 endif
 
-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/,$(PKGS_THAT_ARE_INTREE_ONLY)),\
-                                   $(INSTALLED_PKG_DIRS))
-
 # Set the INSTALL_DISTDIR_p for each package; compiler is special
-$(foreach p,$(filter-out compiler,$(INSTALLED_PKG_DIRS)),\
+$(foreach p,$(filter-out compiler,$(INSTALL_PACKAGES)),\
    $(eval INSTALL_DISTDIR_$p = dist-install))
 INSTALL_DISTDIR_compiler = stage2
 
@@ -933,14 +888,11 @@ install_packages: rts/package.conf.install
        $(call INSTALL_DIR,"$(INSTALLED_PACKAGE_CONF)")
        $(call INSTALL_DIR,"$(DESTDIR)$(topdir)/rts-1.0")
        $(call installLibsTo, $(RTS_INSTALL_LIBS), "$(DESTDIR)$(topdir)/rts-1.0")
-ifeq "$(DYNAMIC_BY_DEFAULT)" "YES"
-       $(foreach p, $(PKGS_THAT_ARE_INTREE_ONLY), \
-           $(call installLibsTo, $(wildcard libraries/$p/dist-install/build/*.so libraries/$p/dist-install/build/*.dll libraries/$p/dist-install/build/*.dylib), "$(DESTDIR)$(topdir)/$p-$(libraries/$p_dist-install_VERSION)"))
-endif
-       $(foreach p, $(INSTALLED_PKG_DIRS),                           \
+       $(foreach p, $(INSTALL_DYNLIBS), \
+           $(call installLibsTo, $(wildcard $p/dist-install/build/*.so $p/dist-install/build/*.dll $p/dist-install/build/*.dylib), "$(DESTDIR)$(topdir)/$($p_PACKAGE)-$($p_dist-install_VERSION)"))
+       $(foreach p, $(INSTALL_PACKAGES),                             \
            $(call make-command,                                      \
-                  CROSS_COMPILE="$(CrossCompilePrefix)"              \
-                  "$(GHC_CABAL_INPLACE)" copy                        \
+                  "$(ghc-cabal_INPLACE)" copy                        \
                                          "$(STRIP_CMD)"              \
                                          $p $(INSTALL_DISTDIR_$p)    \
                                          '$(DESTDIR)'                \
@@ -948,10 +900,9 @@ endif
                                          '$(ghclibdir)'              \
                                          '$(docdir)/html/libraries'))
        "$(INSTALLED_GHC_PKG_REAL)" --force --global-package-db "$(INSTALLED_PACKAGE_CONF)" update rts/package.conf.install
-       $(foreach p, $(INSTALLED_PKG_DIRS),                           \
+       $(foreach p, $(INSTALL_PACKAGES),                             \
            $(call make-command,                                      \
-                  CROSS_COMPILE="$(CrossCompilePrefix)"              \
-                  "$(GHC_CABAL_INPLACE)" register                    \
+                  "$(ghc-cabal_INPLACE)" register                    \
                                          "$(INSTALLED_GHC_REAL)"     \
                                          "$(INSTALLED_GHC_PKG_REAL)" \
                                          "$(DESTDIR)$(topdir)"       \
@@ -1032,12 +983,12 @@ unix-binary-dist-prep:
        "$(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)
-       echo "LATEX_DOCS         = $(LATEX_DOCS)"         >> $(BIN_DIST_MK)
        echo "BUILD_DOCBOOK_HTML = $(BUILD_DOCBOOK_HTML)" >> $(BIN_DIST_MK)
        echo "BUILD_DOCBOOK_PS   = $(BUILD_DOCBOOK_PS)"   >> $(BIN_DIST_MK)
        echo "BUILD_DOCBOOK_PDF  = $(BUILD_DOCBOOK_PDF)"  >> $(BIN_DIST_MK)
        echo "BUILD_MAN          = $(BUILD_MAN)"          >> $(BIN_DIST_MK)
-       echo "GHC_CABAL_INPLACE  = utils/ghc-cabal/dist-install/build/tmp/ghc-cabal-bindist" >> $(BIN_DIST_MK)
+       echo "override ghc-cabal_INPLACE = utils/ghc-cabal/dist-install/build/tmp/ghc-cabal-bindist" >> $(BIN_DIST_MK)
+       echo "UseSystemLibFFI    = $(UseSystemLibFFI)"    >> $(BIN_DIST_MK)
        cd $(BIN_DIST_PREP_DIR) && autoreconf
        $(call removeFiles,$(BIN_DIST_PREP_TAR))
 # h means "follow symlinks", e.g. if aclocal.m4 is a symlink to a source
@@ -1049,13 +1000,6 @@ windows-binary-dist-prep:
        $(MAKE) prefix=$(TOP)/$(BIN_DIST_PREP_DIR) install
        cd bindistprep && "$(TAR_CMD)" cf - $(BIN_DIST_NAME) | bzip2 -c > ../$(BIN_DIST_PREP_TAR_BZ2)
 
-windows-installer:
-ifeq "$(ISCC_CMD)" ""
-       @echo No ISCC_CMD, so not making installer
-else
-       "$(ISCC_CMD)" /O. /Fbindistprep/$(WINDOWS_INSTALLER_BASE) - < distrib/ghc.iss
-endif
-
 # tryTimes tries to run its third argument multiple times, until it
 # succeeds. Don't call it directly; call try10Times instead.
 # The first and second argument to tryTimes are lists of values.
@@ -1073,9 +1017,6 @@ try10Times = $(call tryTimes,,x x x x x x x x x x,$1) { echo Failed; false; }
 .PHONY: publish-binary-dist
 publish-binary-dist:
        $(call try10Times,$(PublishCp) $(BIN_DIST_TAR_BZ2) $(PublishLocation)/dist)
-ifeq "$(mingw32_TARGET_OS)" "1"
-       $(call try10Times,$(PublishCp) $(WINDOWS_INSTALLER) $(PublishLocation)/dist)
-endif
 
 ifeq "$(mingw32_TARGET_OS)" "1"
 DOCDIR_TO_PUBLISH = $(BIN_DIST_INST_DIR)/doc
@@ -1097,7 +1038,7 @@ publish-docs:
 #
 
 # A source dist is built from a complete build tree, because we
-# require some extra files not contained in a darcs checkout: the
+# require some extra files not contained in a git checkout: the
 # output from Happy and Alex, for example.
 #
 # The steps performed by 'make dist' are as follows:
@@ -1131,7 +1072,7 @@ 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
+    boot packages ghc.mk
 
 VERSION :
        echo $(ProjectVersion) >VERSION
@@ -1201,16 +1142,6 @@ publish-sdist :
        $(call try10Times,$(PublishCp) $(SRC_DIST_TESTSUITE_TARBALL) $(PublishLocation)/dist)
 endif
 
-ifeq "$(BootingFromHc)" "YES"
-# In a normal build we use GHC to compile C files (see
-# rules/c-suffix-rules.mk), which passes a number of its own options
-# to the C compiler.  So when bootstrapping we have to provide these
-# flags explicitly to C compilations.
-SRC_CC_OPTS += -DNO_REGS -DUSE_MINIINTERPRETER
-SRC_CC_OPTS += -D__GLASGOW_HASKELL__=$(ProjectVersionInt)
-SRC_CC_OPTS += $(addprefix -I,$(GHC_INCLUDE_DIRS))
-endif
-
 # -----------------------------------------------------------------------------
 # sdisting libraries
 
@@ -1227,7 +1158,6 @@ sdist_%:
 
 CLEAN_FILES += libraries/bootstrapping.conf
 CLEAN_FILES += libraries/integer-gmp/cbits/GmpDerivedConstants.h
-CLEAN_FILES += libraries/integer-gmp/cbits/mkGmpDerivedConstants
 
 # These are no longer generated, but we still clean them for a while
 # as they may still be in old GHC trees:
@@ -1289,6 +1219,7 @@ distclean : clean
        $(call removeFiles,libraries/unix/include/HsUnixConfig.h)
        $(call removeFiles,libraries/old-time/include/HsTimeConfig.h)
        $(call removeTrees,utils/ghc-pwd/dist-boot)
+       $(call removeTrees,includes/dist-derivedconstants)
        $(call removeTrees,inplace)
        $(call removeTrees,$(patsubst %, libraries/%/autom4te.cache, $(PACKAGES_STAGE1) $(PACKAGES_STAGE2)))
 
@@ -1322,33 +1253,48 @@ bootstrapping-files: $(libffi_HEADERS)
 ifeq "$(HADDOCK_DOCS)" "YES"
 BINDIST_HADDOCK_FLAG = --with-haddock="$(BINDIST_PREFIX)/bin/haddock"
 endif
-ifeq "$(DYNAMIC_BY_DEFAULT)" "YES"
+ifeq "$(DYNAMIC_GHC_PROGRAMS)" "YES"
 BINDIST_LIBRARY_FLAGS = --enable-shared --disable-library-vanilla
 else
 BINDIST_LIBRARY_FLAGS = --enable-library-vanilla --disable-shared
 endif
 BINDIST_LIBRARY_FLAGS += --disable-library-prof
 
-.PHONY: validate_build_transformers
-validate_build_transformers:
-       cd libraries/transformers && "$(BINDIST_PREFIX)/bin/ghc" --make Setup
-       cd libraries/transformers && ./Setup configure --with-ghc="$(BINDIST_PREFIX)/bin/ghc" $(BINDIST_HADDOCK_FLAG) $(BINDIST_LIBRARY_FLAGS) --global --builddir=dist-bindist --prefix="$(BINDIST_PREFIX)"
-       cd libraries/transformers && ./Setup build   --builddir=dist-bindist
+.PHONY: validate_build_xhtml
+validate_build_xhtml:
+       cd libraries/xhtml && "$(BINDIST_PREFIX)/bin/ghc" --make Setup
+       cd libraries/xhtml && ./Setup configure --with-ghc="$(BINDIST_PREFIX)/bin/ghc" $(BINDIST_HADDOCK_FLAG) $(BINDIST_LIBRARY_FLAGS) --global --builddir=dist-bindist --prefix="$(BINDIST_PREFIX)"
+       cd libraries/xhtml && ./Setup build   --builddir=dist-bindist
 ifeq "$(HADDOCK_DOCS)" "YES"
-       cd libraries/transformers && ./Setup haddock --builddir=dist-bindist
+       cd libraries/xhtml && ./Setup haddock --builddir=dist-bindist
 endif
-       cd libraries/transformers && ./Setup install --builddir=dist-bindist
-       cd libraries/transformers && ./Setup clean   --builddir=dist-bindist
-       cd libraries/transformers && rm -f Setup Setup.exe Setup.hi Setup.o
+       cd libraries/xhtml && ./Setup install --builddir=dist-bindist
+       cd libraries/xhtml && ./Setup clean   --builddir=dist-bindist
+       cd libraries/xhtml && rm -f Setup Setup.exe Setup.hi Setup.o
 
 # -----------------------------------------------------------------------------
 # Numbered phase targets
 
+# In phase 1, we'll be building dependency files for most things
+# built by the bootstrapping compiler while make is 'include'ing
+# makefiles. But in order to build dependency files, we'll need to
+# build any automatically generated .hs files, which means that
+# we'll need to be able to build any tools that generate .hs files
+# etc. But in order to do that, we need to already know the
+# dependencies for those tools, so we build their dependency files
+# here.
 .PHONY: phase_0_builds
+# hsc2hs is needed, e.g. to make the .hs files for hpc.
 phase_0_builds: $(utils/hsc2hs_dist_depfile_haskell)
 phase_0_builds: $(utils/hsc2hs_dist_depfile_c_asm)
+# genprimopcode is needed to make the .hs-incl files that are in the
+# ghc package.
 phase_0_builds: $(utils/genprimopcode_dist_depfile_haskell)
 phase_0_builds: $(utils/genprimopcode_dist_depfile_c_asm)
+# deriveConstants is used to create header files included in the
+# ghc package.
+phase_0_builds: $(utils/deriveConstants_dist_depfile_haskell)
+phase_0_builds: $(utils/deriveConstants_dist_depfile_c_asm)
 
 .PHONY: phase_1_builds
 phase_1_builds: $(PACKAGE_DATA_MKS)