#!/bin/sh -e

# Manuel M. T. Chakravarty <chak@acm.org>, June 2000
# Updated for GHC 5.00, Simon Marlow, March 2001
# Updated for GHC 5.04.3, Urban Boquist, March 2003
#
# Script to build GHC from .hc files (must be run in the fptools/ root
# directory into which the source and .hc files must already have been
# unpacked).  All options are passed through to ./configure (especially
# useful with --prefix).

configopts="$*"         # e.g., --enable-hc-boot-unregisterised
PWD=`pwd`

# check for GNU make
#
MAKENAMES="gmake make no-make"
for make in $MAKENAMES; do
  MAKE=$make
  $make --version 2>&1 | grep "GNU Make" >/dev/null && break
done
if [ $MAKE = no-make ]; then
  echo "Fatal error: Cannot find the GNU make utility"
  exit 1
fi

# build configuration
#
case "$configopts" in
*--enable-hc-boot-unregisterised*)
cat >mk/build.mk <<END
GhcWithInterpreter=NO
GhcWithNativeCodeGen=NO
SplitObjs=NO
GhcLibWays=
END
;;

*)
cat >mk/build.mk <<END
# empty
END
;;
esac

echo "*** Building compiler..."
./configure --enable-hc-boot $configopts

$MAKE -C glafp-utils boot all
$MAKE -C ghc boot
$MAKE -C libraries boot all
$MAKE -C ghc all

MAKEFLAGS=

echo "*** Building libraries..."

# Get rid of --enable-hc-boot-unregisterised in $configotps if we had it
configopts=`echo $configopts | sed s/--enable-hc-boot-unregisterised//`

# Reconfigure, using the newly-build ghc binary as our $(GHC), and
# with hc bootstrapping disabled.
HappyCmd="$PWD/distrib/fake-happy" ./configure --with-ghc="$PWD/ghc/compiler/ghc-inplace" $configopts

PRIMOP_BITS="primop-data-decl.hs-incl \
	      primop-tag.hs-incl  \
	      primop-list.hs-incl  \
	      primop-has-side-effects.hs-incl  \
	      primop-out-of-line.hs-incl  \
	      primop-commutable.hs-incl  \
	      primop-needs-wrapper.hs-incl  \
	      primop-can-fail.hs-incl  \
	      primop-strictness.hs-incl  \
	      primop-usage.hs-incl  \
	      primop-primop-info.hs-incl"

# Remove the old libraries.  Don't use make clean, because we don't
# want to delete the .hs files generated from the .hsc files, because
# we don't have hsc2hs built yet.
find libraries hslibs | grep '\.\(o\|a\)$' | xargs rm -f

# Do includes and RTS now
$MAKE -C ghc/includes boot && $MAKE -C ghc/includes all
$MAKE -C ghc/rts      boot && $MAKE -C ghc/rts      all

# Now build a new set of libraries
$MAKE -C libraries boot all

# Build all of ghc/utils
$MAKE -C ghc/utils clean && $MAKE -C ghc/utils boot all

# Now we can build hslibs (hsc2hs is required, so must be after ghc/utils)
$MAKE -C hslibs  boot all

# The reconfigure step updates a few files, which can lead to
# unnecessary recompilations.  Touch a bunch of things here to avoid
# having to recompile stuff that we've already built.
(cd ghc/compiler; touch $PRIMOP_BITS parser/hschooks.o prelude/PrimOp.o main/Config.hs main/Config.o ghc-*)

# At this point, the tree should be safe to do 'make install' in.
