+# Building ghc different ways (default is just `normal' sequential)
+
+WAYS=$(GhcCompilerWays)
+
+# -----------------------------------------------------------------------------
+# Bootstrapping
+
+# The stage1/stage2/stage3 business is quite delicate. Here's how it works:
+#
+# - the variable $(stage) holds the current stage number. To build a
+# particular stage, you say 'make stage=N' where N is 1, 2, or 3.
+# N defaults to 1.
+#
+# - 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.
+#
+# - we generate a different .depend file for each build. They need to be
+# different, because each stage might include different files: stage1
+# might not include GHCi, for example. For each stage, a normal .depend
+# file is generated, and then post-processed to add the correct stageN/
+# prefix to each object and .hi filename. The resulting .depend file
+# is named .depend-$(stage). See the end of this Makefile for details.
+#
+# - normal implicit rules don't work any more, because they're of the form
+#
+# %.o : %.hs
+#
+# whereas we really need
+#
+# stageN/%.o : %.hs
+#
+# so suffix.mk now defines the appropriate suffix rules when
+# $(odir) is set to a non-empty value. Here we set $(odir) to
+# stage1, stage2, or stage3.
+#
+# 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
+# subdirectory part), which eventually forces us to use VPATH to find
+# the sources. I have a really bad feeling about VPATH.
+#
+# - invoke make in the stageN subdirectory. This probably requires VPATH
+# too.
+#
+# - create a link tree. The problem with requiring link trees is that
+# Windows doesn't support symbolic links.
+
+boot ::
+ $(MKDIRHIER) stage$(stage)
+ for i in $(ALL_DIRS); do \
+ $(MKDIRHIER) stage$(stage)/$$i; \
+ done
+# 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 "$(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 \
+ $(LN_S) -f ../../$$i stage$(stage)/$$i || true ; \
+ done
+endif
+
+ifeq "$(stage)" ""
+stage=1
+endif
+
+ifeq "$(stage)" "1"
+HC=$(GHC)
+endif
+
+ifeq "$(stage)" "2"
+HC=$(GHC_STAGE1)
+endif
+
+ifeq "$(stage)" "3"
+HC=$(GHC_STAGE2)
+endif
+
+stage1 ::
+ $(MAKE) stage=1
+
+stage2 ::
+ $(MAKE) stage=2
+
+stage3 ::
+ $(MAKE) stage=3
+
+odir=stage$(stage)
+
+SRC_HC_OPTS += $(patsubst %, -i$(odir)/%, $(ALL_DIRS))