#!/bin/sh
# 
# This script builds a Xtoolchain crosscompiler package
#
# useage: build.sh processor languages gcc-version libc-version [ headers-version ]
#
# To use it you must install Darwinports-1.1 (or later)
#
# Copyright by H. N. Schaller, Jan 2006
#

export HERE=$PWD

if [ "$5" = "" ]
  then HDRS=""
  else HDRS="-hdrs-$5"
fi
export BUILD_VERSION="$1-gcc-$3-glibc-$4$HDRS"
export XBUILD_NAME="Xtoolchain-$BUILD_VERSION"

[ -r "$HERE/GNUTOOLS" ] && echo "*** Warning: This file system is not distinguishing upper and lower case filenames. This may cause trouble! ***"

echo Building $XBUILD_NAME.
date

set -ex

export PATH=$HERE/gnutools:/opt/local/bin:/Developer/Xtoolchain/tools:$PATH	# make us find wget etc.

# allow to fetch files from the online-repositories

[ -r /opt/local/bin/wget ] || /opt/local/bin/port install wget || exit 1

# install GNU tools and override through $PATH

mkdir -p gnutools

[ -r gnutools/awk ] || ( /opt/local/bin/port install gawk && ln -s /opt/local/bin/gawk gnutools/awk )
[ -r gnutools/sed ] || ( /opt/local/bin/port install gsed && ln -s /opt/local/bin/gnused gnutools/sed )
[ -r gnutools/make ] || ( /opt/local/bin/port install gmake && ln -s /opt/local/bin/gmake gnutools/make ) # /usr/bin/make has builtin-rule for .m.c: which conflicts with the sources of gprof
[ -r /opt/local/bin/msgfmt ] || /opt/local/bin/port install gettext

echo awk is: $(which awk)
echo sed is: $(which sed)
echo make is: $(which make)

# wrap Xcode as & ld so that they pretend to be the minimum required binutils

echo '[ "$1" = -v ] && echo GNU assembler 2.13 || /usr/bin/as $*' >gnutools/as
echo '[ "$1" = --version ] && echo GNU ld 2.13 || /usr/bin/ld $*' >gnutools/ld
chmod a+x gnutools/as
chmod a+x gnutools/ld

[ -r crosstool-0.38.tar.gz ] || wget http://kegel.com/crosstool/crosstool-0.38.tar.gz
[ -r crosstool-0.38 ] || tar -xzvf crosstool-0.38.tar.gz

[ -r /Developer/Xtoolchain/gcc-$3-glibc-$4 ] || (
cd crosstool-0.38

export GCC_LANGUAGES="$2"

export TARBALLS_DIR="$PWD/downloads"
export RESULT_TOP="/Developer/Xtoolchain"

# Really, you should do the mkdir before running this,
# and chown /opt/crosstool to yourself so you don't need to run as root.

mkdir -p $RESULT_TOP

# Build the toolchain.  Takes a couple hours and a couple gigabytes - no not really that long and large.

# gcc-3.3 doesn't support this, need gcc-3.4
#eval `cat arm-xscale.dat gcc-3.4.0-glibc-2.3.2.dat` sh all.sh --notest
#eval `cat arm-xscale.dat gcc-3.4.1-glibc-2.3.3.dat` sh all.sh --notest
#eval `cat arm-xscale.dat gcc-3.4.1-glibc-20040827.dat` sh all.sh --notest
#eval `cat arm-xscale.dat gcc-3.4.2-glibc-20040827.dat` sh all.sh --notest

eval `cat $1.dat gcc-$3-glibc-$4$HDRS.dat` sh all.sh --notest

file /Developer/Xtoolchain/gcc-$3-glibc-$4/$TARGET/tmp/*

printf '#include <stdio.h>\nint main() { printf("Hello World! %lf", 3.0*5.0); }\n' >test.c
/Developer/Xtoolchain/gcc-$3-glibc-$4/$TARGET/$TARGET/bin/gcc test.c
ls -l a.out
file a.out

printf '#include <iostream>\nusing namespace std; int main() { cout << "Hello World!\\n"; return 0; }\n' >test.cpp
/Developer/Xtoolchain/gcc-$3-glibc-$4/$TARGET/$TARGET/bin/gcc test.cpp -lstdc++
ls -l a.out
file a.out

rm -f "$HERE/packages/$XBUILD_NAME.pkg"

)

[ -r "packages/$XBUILD_NAME.pkg" ] || (
cd packages
setplist Description.plist IFPkgDescriptionVersion "$BUILD_VERSION"
setplist Info.plist CFBundleShortVersionString "gcc-$3-glibc-$4"
mkpkg "Xtoolchain" "$BUILD_VERSION" "/Developer/Xtoolchain/gcc-$3-glibc-$4" Info.plist Description.plist installer-resources
rm -f "$HERE/$XBUILD_NAME.dmg"
) || exit 1

[ -r "$XBUILD_NAME.dmg" ] || (
mkdir /tmp/dmg$$
cp -R README.rtf build.sh Makefile tools "packages/$XBUILD_NAME.pkg" /tmp/dmg$$
echo >"/tmp/dmg$$/target=$1"
echo >"/tmp/dmg$$/languages=$2"
echo >"/tmp/dmg$$/gcc=$3"
echo >"/tmp/dmg$$/glibc=$4"
echo >"/tmp/dmg$$/headers=$5"
mkdmg Xtoolchain "$BUILD_VERSION" /tmp/dmg$$
rm -rf /tmp/dmg$$
) || exit 1

echo Done with $XBUILD_NAME.
date

