#!/bin/sh
######################################################################
#
#    Copyright 2004 Sony Corporation 
#
#    This software is provided "as is" without warranty of any kind,
#    either expressed or implied, including but not limited to the
#    implied warranties of fitness for a particular purpose.
#
# This script builds the following tools so as to be used with OPEN-R
# SDK.
#
#      binutils-2.14
#      gcc-3.3.2
#      newlib-1.10.0
#
# Run the script in the directory where the source packages (.tar.gz
# files) of the above tools are placed.

######################################################################
# Constants

# Installation directory
PREFIX=/usr/local/OPEN_R_SDK

# Target name
#     Note: The target name `mipsel-linux' does not mean that Linux is
#           running on AIBO.  `linux' is specified just for enabling
#           PIC code generation.
TARGET=mipsel-linux

# source packages
BINUTILS=binutils-2.14
GCC=gcc-3.3.2
NEWLIB=newlib-1.10.0

BINUTILS_PKG=$BINUTILS.tar.gz
GCC_PKG=$GCC.tar.gz
NEWLIB_PKG=$NEWLIB.tar.gz

BINUTILS_BLD=bld-$BINUTILS
GCC_BLD=bld-$GCC
NEWLIB_BLD=bld-$NEWLIB

######################################################################
# Preparation
#

# Confirm the source packages exists
for src in $BINUTILS_PKG $GCC_PKG $NEWLIB_PKG; do
    if [ ! -e $src ]; then
	echo $src not found in the current directory, exiting. 1>&2
	exit 1
    fi
done

# Confirm the directory $PREFIX exists and is writable
if mkdir -p $PREFIX/tmp$$ > /dev/null 2>&1; then
    # OK; $PREFIX is writable directory
    rmdir $PREFIX/tmp$$
else
    echo Please create the writable directory $PREFIX.   Exiting. 1>&2
    exit 1
fi

# Unpack the source archives
for src in $BINUTILS_PKG $GCC_PKG $NEWLIB_PKG; do
    echo Unpacking $src
    tar xzf $src || exit
done

######################################################################
# Applying patches
#

# --------------------------------------------------------------------
# Make libstdc++-v3 use newlib
patch -p0 <<'EOF' || exit
--- gcc-3.3.2/libstdc++-v3/configure.orig	2003-09-11 12:08:35.000000000 +0900
+++ gcc-3.3.2/libstdc++-v3/configure	2003-11-18 15:12:29.552891100 +0900
@@ -4213,7 +4213,8 @@
   # GLIBCPP_CHECK_MATH_SUPPORT
 
   case "$target" in
-    *-linux*)
+#    *-linux*)
+    *-linux-nevermatch*)
       os_include_dir="os/gnu-linux"
       for ac_hdr in nan.h ieeefp.h endian.h sys/isa_defs.h \
         machine/endian.h machine/param.h sys/machine.h sys/types.h \
EOF

# --------------------------------------------------------------------
# BUFSIZ defined in newlib's stdio.h is too small for AIBO's file
# system.
patch -p0 <<'EOF' || exit
--- newlib-1.10.0/newlib/libc/include/stdio.h.orig	Mon Oct  7 17:19:42 2002
+++ newlib-1.10.0/newlib/libc/include/stdio.h	Mon Oct  7 17:20:00 2002
@@ -92,7 +92,7 @@
 #ifdef __BUFSIZ__
 #define	BUFSIZ		__BUFSIZ__
 #else
-#define	BUFSIZ		1024
+#define	BUFSIZ		16384
 #endif
 
 #ifdef __FOPEN_MAX__
EOF

# --------------------------------------------------------------------
# vfprintf modified not to use too much stack size
patch -p0 <<'EOF' || exit
--- newlib-1.10.0/newlib/libc/stdio/vfprintf.c.orig	Mon Oct  7 17:22:28 2002
+++ newlib-1.10.0/newlib/libc/stdio/vfprintf.c	Mon Oct  7 17:22:50 2002
@@ -429,9 +429,12 @@
 		return (EOF);
 
 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
+/* comment out: __sbprintf requires too large stack size (> BUFSIZ) */
+#if 0
 	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
 	    fp->_file >= 0)
 		return (__sbprintf(fp, fmt0, ap));
+#endif
 
 	fmt = (char *)fmt0;
 	uio.uio_iov = iovp = iov;
EOF

######################################################################
# Building binutils
#

mkdir $BINUTILS_BLD || exit
echo Configuring $BINUTILS
(cd $BINUTILS_BLD; ../$BINUTILS/configure --prefix=$PREFIX \
                                          --target=$TARGET) || exit

echo Making $BINUTILS
(cd $BINUTILS_BLD; make) || exit

echo Installing $BINUTILS
(cd $BINUTILS_BLD; make install) || exit

######################################################################
# Building gcc
#

# $TARGET-ar must be found via PATH
export PATH="$PREFIX/bin:$PATH"

mkdir $GCC_BLD || exit
echo Configuring $GCC
(cd $GCC_BLD; ../$GCC/configure --prefix=$PREFIX \
                                --target=$TARGET \
                                --with-gnu-as \
                                --with-gnu-ld \
                                --with-headers=../$NEWLIB/newlib/libc/include \
                                --with-as=$PREFIX/bin/$TARGET-as \
                                --with-ld=$PREFIX/bin/$TARGET-ld \
                                --disable-shared \
                                --enable-languages=c,c++ \
                                --disable-threads \
                                --with-newlib) || exit

echo Making $GCC
(cd $GCC_BLD; make) || exit

echo Installing $GCC
(cd $GCC_BLD; make install) || exit

######################################################################
# Building newlib
#

mkdir $NEWLIB_BLD || exit
echo Configuring $NEWLIB
(cd $NEWLIB_BLD; ../$NEWLIB/configure --prefix=$PREFIX \
                                      --target=$TARGET) || exit

echo Making $NEWLIB
(cd $NEWLIB_BLD; make) || exit

echo Installing $NEWLIB
(cd $NEWLIB_BLD; make install) || exit

######################################################################
# Creating libc-.a
#
# Some functions in libc.a have improper implementation.  Correct
# version of them are provided by libapsys.a.  To avoid mislinking, we
# use libc-.a, from which the duplicated functions are removed.

echo Creating $PREFIX/$TARGET/lib/libc-.a
cp $PREFIX/$TARGET/lib/libc.a $PREFIX/$TARGET/lib/libc-.a || exit
$PREFIX/bin/$TARGET-ar d $PREFIX/$TARGET/lib/libc-.a \
        mallocr.o freer.o reallocr.o callocr.o cfreer.o malignr.o \
	vallocr.o pvallocr.o mallinfor.o mallstatsr.o msizer.o malloptr.o \
        calloc.o malign.o msize.o mstats.o mtrim.o realloc.o valloc.o malloc.o \
	abort.o sbrkr.o exit.o rename.o || exit
$PREFIX/bin/$TARGET-ranlib $PREFIX/$TARGET/lib/libc-.a || exit
echo Done

