#!/bin/bash.exe

##############################################################################
# Building a GNU C/C++ Cross Compiler [Windows NT 4.0 and Cygwin 1.1]
# Modified: 26 Jan 2001
# Paolo Masetti
# paolo.masetti@itlug.org
#
# ----------------------------------------------------------------------------
# WARNING: This script is intended for making a cross compiler exclusively
#          for use with Lego(r) RCX and the legOS Firmware.
#
# See http://legos.sourceforge.net/ for more infos.
#
# ----------------------------------------------------------------------------
#
# original by
# Christopher Bahns
# Christopher Bahns Software
# chris@bahns.com
#
# Partial comments from original version follows.
#
# This document contains complete instructions on how to create a GNU-
# based cross compiler (C and C++) for use under Windows NT 4.0 and Cygnus
# Cygwin 1.0 and later (not the older Cygwin B20.1). These instructions are
# based on information provided by David Fiddes, who has a build of an
# earlier version of the GNU cross-compiler which seems to be intended for
# Cygwin B20.1. His build is available on his website (see below).
#
# ------------------------------ Notes ------------------------------
#
# 1. These instructions and script will not work with Cygwin B20.1
# (older free version). This version of Cygwin does not have all of the
# utilities or set up certain aspects of the Unix environment that are
# required. Essentially the same things can be accomplished with B20.1
# as with the commercial version 1.0, but the instructions would be a
# bit more complicated, and the resulting environment would not be very
# usable by a typical Unix programmer.
#
# 2. The most frustrating problems I had getting this build to work had
# to do with the addition of carriage returns by various software. This
# is the first thing I'd check if something goes wrong. The two things
# that got me were WinZip (which I was using at first to extract the
# archives), which adds CR's by default, and the Cygwin file system,
# which somehow got setup in text mode, even though the "mount" command
# was showing me "binmode". I solved the former by avoiding WinZip and
# just using the "tar" command from within the Cygwin environment. When
# using WinZip you can disable the "TAR file smart CR/LF conversion"
# option in "Options/Configuration/Miscellaneous". I solved the latter
# by completely removing Cygwin and reinstalling. You should make a
# simple program that can quickly tell you if a particular file has any
# CR's. I wasted a lot of time trying to diagnose such "simple" problems.
#
# The following program is called "showcr.c", which I use to determine
# whether a given text file contains carriage returns.
#
#	#include <stdio.h>
#	#include <stdlib.h>
#
#	int main(int argc, char *argv[])
#	{
#	 FILE *in;
#	 int i;
#	 if (argc != 2) {
#	   fputs("Usage: showcr <infile>\n", stderr);
#	   exit(1);
#	 }
#	 if (!(in = fopen(*++argv, "rb"))) {
#	   fprintf(stderr, "showcr: can't open %s\n", *argv);
#	   exit(1);
#	 }
#	 while((i = fgetc(in)) != EOF)
#	 {
#	   if (i == 13)
#	   {
#	      putchar('^');
#	      i = 'M';
#	   }
#	   putchar(i);
#	 }
#	 fclose(in);
#	 return 0;
#	}
#
##############################################################################

BINDIR=/bin
BUILDDIR=`pwd`
LOGFILE=$BUILDDIR/buildgcc.log

BINUTILS=binutils-2.10.1
GCC=gcc-2.95.2
NEWLIB=newlib-1.8.2

#PREFIXDIR=/h8300-hitachi-hms
TARGET=h8300-hitachi-hms

#***************** DO NOT CHANGE ANYTHING BELOW THIS LINE *****************#
#********************* (unless of course you want to) *********************#

echo ":-------------------- Begin `date` --------------------:" > $LOGFILE
echo Installing source code...
echo `date` Source code installation start 2>&1 | tee --append $LOGFILE
rm -rf $BUILDDIR/binutils
rm -rf $BUILDDIR/gcc
rm -rf $BUILDDIR/$BINUTILS
rm -rf $BUILDDIR/$GCC
rm -rf $BUILDDIR/$NEWLIB
tar -xzvf $BUILDDIR/$BINUTILS.tar.gz
tar -xzvf $BUILDDIR/$GCC.tar.gz
tar -xzvf $BUILDDIR/$NEWLIB.tar.gz
cd $BUILDDIR/$GCC
echo `date` Source code installation end 2>&1 | tee --append $LOGFILE

echo Building $BINUTILS...
cd $BUILDDIR
mkdir binutils
cd binutils
echo `date` Binutils configuration start  2>&1 | tee --append $LOGFILE
$BUILDDIR/$BINUTILS/configure --verbose --target=$TARGET 2>&1 | tee --append $LOGFILE
echo `date` Binutils configuration end 2>&1 | tee --append $LOGFILE
echo `date` Binutils build start 2>&1 | tee --append $LOGFILE
make 2>&1 | tee --append $LOGFILE
echo `date` Binutils build end 2>&1 | tee --append $LOGFILE
echo `date` Binutils install start 2>&1 | tee --append $LOGFILE
make install 2>&1 | tee --append $LOGFILE
echo `date` Binutils install 2>&1 | tee --append $LOGFILE

echo Building $GCC...
cd $BUILDDIR
echo `date` GCC patch start 2>&1 | tee --append $LOGFILE
cd $GCC
mv gcc/config/h8300/h8300.c gcc/config/h8300/h8300.c.orig
patch -u -i ../gcc-2.95.2-rcx-1.diff -o gcc/config/h8300/h8300.c
mv gcc/config/h8300/h8300.h gcc/config/h8300/h8300.h.orig
patch -u -i ../gcc-2.95.2-rcx-2.diff -o gcc/config/h8300/h8300.h
cd ..
echo `date` GCC patch end 2>&1 | tee --append $LOGFILE
mkdir gcc
cd gcc
echo `date` GCC configuration start 2>&1 | tee --append $LOGFILE
$BUILDDIR/$GCC/configure --verbose --target=$TARGET --enable-languages=c,c++ --enable-target-optspace --with-newlib --with-headers=$BUILDDIR/$NEWLIB/newlib/libc/include 2>&1 | tee --append $LOGFILE
echo `date` GCC configuration end 2>&1 | tee --append $LOGFILE
echo `date` GCC build start 2>&1 | tee --append $LOGFILE
make cross 2>&1 | tee --append $LOGFILE
echo `date` GCC build end 2>&1 | tee --append $LOGFILE

echo `date` GCC install start 2>&1 | tee --append $LOGFILE
make install 2>&1 | tee --append $LOGFILE
echo `date` GCC install end 2>&1 | tee --append $LOGFILE
echo Done.
echo Done. 2>&1 | tee --append $LOGFILE
echo ":-------------------- End `date` --------------------:" 2>&1 | tee --append $LOGFILE
cd $BUILDDIR

