#! /bin/bash

# This script can be used to generate some unregisterised .hc files
# for bootstrapping GHC on a new/unsupported platform.  It involves a
# two-stage bootstrap: the first stage builds an unregisterised set of
# libraries & RTS, and the second stage builds an unregisterised
# compiler.  

# Take the .hc files from the libraries of stage 1, and the compiler
# of stage 2, to the target system and bootstrap from these to get a
# working (unregisterised) compiler.

set -e

long='-optc-DNO_REGS -optc-DUSE_MINIINTERPRETER -fno-asm-mangling -funregisterised -fvia-C'

base=`pwd`

if [ ! -f b1-stamp ]; then
  mkdir b1
  cd b1
  lndir ../fptools
  cd ..

  mkdir b1-obj

  cd b1
   ./configure --prefix=$base/b1-obj
   echo "GhcLibHcOpts = -O -H24m -keep-hc-files-too $long" >> mk/config.mk
   echo "GhcRtsHcOpts = -O $long" >> mk/config.mk
   make boot
   make all
   make install
  cd ..

  touch b1-stamp
fi

# exit 0

if [ ! -f b2-stamp ]; then
  mkdir b2
  cd b2
  lndir ../fptools
  cd ..

  mkdir b2-obj
  cd b2
   ./configure --prefix=$base/b2-obj --with-hc=$base/b1-obj/bin/ghc
   echo "WithGhcHc = $base/b1-obj/bin/ghc" >> mk/config.mk
   echo "GhcHcOpts = -O -H24m -keep-hc-files-too $long" >> mk/config.mk
   echo "GhcWithNativeCodeGen=NO" >> mk/config.mk
   make boot
   make all
   make install
  cd ..

  touch b2-stamp
fi
