#!/usr/tgcware/bin/bash

#--[ User configurable bits start here ]------------------------
#
BLD_REL=4.4.2
BLD_PATCHES="gcc-4.4.2-irix5.3.diff"
BLD_PKGVERS="CoolMix/4.4.2-r2"
#
#--[ No user configurable bits beyond this ]--------------------

echo -n "Started at "
date

export CONFIG_SHELL=/usr/tgcware/bin/bash
export PATH=/usr/local/gnu/bin:/usr/tgcware/bin:/usr/sbin:/usr/bsd:/sbin:/usr/bin:/etc:/usr/etc:/usr/bin/X11
export CC=gcc
export CFLAGS=-O2

BLD_OS=`/sbin/uname -r`
BLD_OS_MAJ=`echo $BLD_OS | cut -d '.' -f1`
BLD_OS_MIN=`echo $BLD_OS | cut -d '.' -f2`
BLD_PFX=/usr/local/gnu

BLD_HOST=`/sbin/uname -a`

echo "GCC Patches applied:"
for p in $BLD_PATCHES;
do
  echo $p
done
echo

echo -n "Using: "
$BLD_PFX/mips-sgi-irix$BLD_OS/bin/as --version | head -1
echo -n "Using: native linker"
/usr/bin/ld -V 2>&1 | head -1 | cut -d ':' -f3
echo

echo "Build host: $BLD_HOST"
if [ $BLD_OS == 6.5 ];
then
  BLD_OS_FULL=`/sbin/uname -R`
  echo "Operating system: IRIX $BLD_OS_FULL"
else
  echo "Operating system: IRIX $BLD_OS"
fi
echo

echo "IRIX Patches installed:"
versions -b patchSG* | grep patchSG | cut -c37-200
echo

if [ $BLD_OS_MAJ == 6 ];
then
  # IRIX 6.x triple ABI build, compiler binaries are N32
  BLD_LIBDIR=$BLD_PFX/lib32
else
  # IRIX 5.x sinble ABI build
  BLD_LIBDIR=$BLD_PFX/lib
fi

# Pull in the right GMP/MPFR bits
export GMPINC="-I$BLD_PFX/include"
export GMPLIBS="-L$BLD_LIBDIR -lmpfr -lgmp"

# Host dependent GCC configuration bits
BLD_CONFARGS="-v"

# IRIX 6.5 has Java, the rest doesn't.
if [ $BLD_OS == 6.5 ];
then
  BLD_CONFARGS="$BLD_CONFARGS --enable-languages=c,ada,c++,fortran,java,objc,obj-c++"
else
  BLD_CONFARGS="$BLD_CONFARGS --enable-languages=c,ada,c++,fortran,objc,obj-c++"
fi

BLD_CONFARGS="$BLD_CONFARGS --prefix=$BLD_PFX"
BLD_CONFARGS="$BLD_CONFARGS --enable-shared"

if [ $BLD_OS == 6.2 ];
then
  BLD_CONFARGS="$BLD_CONFARGS --enable-threads=posix95"
fi

if [ $BLD_OS == 6.5 ];
then
  BLD_CONFARGS="$BLD_CONFARGS --enable-threads=posix"
  BLD_CONFARGS="$BLD_CONFARGS --enable-libgcj --enable-java-awt=xlib"
fi

BLD_CONFARGS="$BLD_CONFARGS --enable-objc-gc"

BLD_CONFARGS="$BLD_CONFARGS --disable-nls"

# We always use GNU as and native ld.
BLD_CONFARGS="$BLD_CONFARGS --with-gnu-as --with-as=$BLD_PFX/mips-sgi-irix$BLD_OS/bin/as"
BLD_CONFARGS="$BLD_CONFARGS --without-gnu-ld --with-ld=/usr/bin/ld"

# GMP/MPFR directories are different for IRIX 5.x and 6.x
BLD_CONFARGS="$BLD_CONFARGS --with-mpfr-include=$BLD_PFX/include"
BLD_CONFARGS="$BLD_CONFARGS --with-mpfr-lib=$BLD_LIBDIR"

BLD_CONFARGS="$BLD_CONFARGS --enable-checking=release"

if [ ! -f gcc-$BLD_REL.tar.bz2 ];
then
  echo "Not found: gcc-$BLD_REL.tar.bz2"
  exit 1;
fi

bzip2 -dc gcc-$BLD_REL.tar.bz2 | tar xf -

#
# Always apply all patches.
# They should never result in IRIX version dependent conflicts anyway...
#
for p in $BLD_PATCHES;
do
  echo Apply : $p
  (cd gcc-$BLD_REL && patch -s -p1 < ../$p)
done
echo

rm -rf gcc-$BLD_REL/obj
mkdir gcc-$BLD_REL/obj
cd gcc-$BLD_REL/obj
../configure $BLD_CONFARGS

# Bootstrap
make

if [ $BLD_OS_MAJ == 6 ];
then
  # Run 3 ABI checks in parallel
  make -j3 -k check RUNTESTFLAGS="--target_board=unix\{-mabi=32,-mabi=n32,-mabi=64\}"
else
  make -k check
fi

# Report
echo -n "Using: "											>  ../../gcc-$BLD_REL.mips-sgi-irix$BLD_OS-sum.txt
$BLD_PFX/mips-sgi-irix$BLD_OS/bin/as --version | head -1	>> ../../gcc-$BLD_REL.mips-sgi-irix$BLD_OS-sum.txt
echo -n "Using: native linker"								>> ../../gcc-$BLD_REL.mips-sgi-irix$BLD_OS-sum.txt
/usr/bin/ld -V 2>&1 | head -1 | cut -d ':' -f3				>> ../../gcc-$BLD_REL.mips-sgi-irix$BLD_OS-sum.txt
echo

echo "Build host: $BLD_HOST"								>> ../../gcc-$BLD_REL.mips-sgi-irix$BLD_OS-sum.txt
if [ $BLD_OS == 6.5 ];
then
  echo "Operating system: IRIX $BLD_OS_FULL"				>> ../../gcc-$BLD_REL.mips-sgi-irix$BLD_OS-sum.txt
else
  echo "Operating system: IRIX $BLD_OS"						>> ../../gcc-$BLD_REL.mips-sgi-irix$BLD_OS-sum.txt
fi
echo
../contrib/test_summary										>> ../../gcc-$BLD_REL.mips-sgi-irix$BLD_OS-sum.txt

# Install
make install DESTDIR=`pwd`/root

# Tidy up

# Strip executables
(cd root && find usr/local/gnu/bin -type f -print | xargs -n1 /usr/bin/strip)
(cd root && find usr/local/gnu/libexec/gcc -type f -print | xargs -n1 /usr/bin/strip)

# Remove static libgcj
if [ $BLD_OS == 6.5 ];
then
  (cd root && find  `pwd` -type f -name "libgcj.a" -print | xargs rm -f)
fi

# Strip debug info from static & shared libs
(cd root && find  `pwd` -type f -name "*.a" -print | grep -v libgcc.a | grep -v libgcc_eh.a | xargs -n1 objcopy --strip-debug --preserve-dates)
(cd root && find  `pwd` -type f -name "*.so*" -print | grep -v libgcc_s.so | xargs -n1 objcopy --strip-debug --preserve-dates)


(cd root && /sbin/tar cf - usr/ | gzip -9 > ../../../gcc-$BLD_REL.mips-sgi-irix$BLD_OS.tar.gz)
cd ../../..

echo -n "Finished at "
date

# Clean up
#rm -rf gcc-$BLD_REL
