#!/bin/bash
# $Id: bld-tools-uclinux,v 1.20 2003/01/20 22:03:19 mrustad Exp $
# Script to build tool chain. Tools are built in the "build" directory,
# Built tools are placed into a tarball in the open-tools directory named
# mipsisa32-brecics-uclinux.<host>.<date>.tar.gz 

exec 2>&1

targetcpuarch=mips
target=mipsisa32-brecis-uclinux
tars=${archives:-.}
modules="binutils gcc gdb"

if test "x`uname`" = "xDarwin" ; then
	export CC="gcc3 -no-cpp-precomp"
fi

tmpbase=`cd ..;pwd`

if test ! -d "../uClinux" ; then
	echo "uClinux tree not found, install uClinux into the"
	echo "directory holding open-tools, ie. " ${tmpbase}
	exit 1
fi

tmpbase=`cd ../uClinux/brecis;pwd`

ucver=`grep UTS_RELEASE ${tmpbase}/../linux/include/linux/version.h | \
	awk '{ print $3 } ' | tr -d \"`
echo "uClinux version ${ucver}"
tool_build="(uClinux ${ucver} BRECIS Release 2.0)"
echo "tool_build=${tool_build}"

# Find source trees for modules.
source ./find-src ${modules}

hosttype=`./get-host-type`
hprefix=${dest:-/opt/BRECIS}/${hosttype}
tmppfx=tmp-$$
prefix=${dest:-${tmpbase}}/${tmppfx}/${target}

mkdir -p ${prefix} ${prefix}/bin ${prefix}/include ${prefix}/lib || exit 1
mkdir -p ${prefix}/${target} || exit 1

ln -sf `cd ..;pwd`/uClinux ${prefix}/../uClinux

export	PATH=${hprefix}/bin:${prefix}/bin:$PATH
echo PATH=$PATH

mkdir -p build-inc
cp -RL ../uClinux/uclibc/include build-inc

headers=`cd build-inc/include; pwd`
echo headers=${headers}

libs=`cd ../uClinux/uclibc/lib; pwd`
echo libs=${libs}

confopt=(--with-libs="${libs}" \
--enable-languages=c,c++ --with-cpu=4kc --enable-cstdio \
--disable-c-mbchar --enable-target-optspace)
makeopt=(CFLAGS=-O2 TCFLAGS=-Os LDFLAGS="-all-static -static")
if test "x`uname`" = "xDarwin" ; then
	makeopt=(CFLAGS=-O2 TCFLAGS=-Os)
fi

# build modules

for i in ${modules} ; do
	if test ! -f build-${!i%%-*}/.built ; then
		source ./bld-one ${!i}
		touch build-${!i%%-*}/.built
	fi
done

for i in gcc g++ c++ cpp ; do
	(cd ${prefix}/bin; ln -sf ${target}-${i} ${targetcpuarch}-${i})
done

(cd ${prefix}/${target}; rm -rf sys-include; \
	echo Linking sys-include to ../../../../uclibc/include; \
	ln -sf ../../../../uclibc/include sys-include)

(cd ${prefix}/${target}/lib; \
	echo Linking libraries to ../../../../../uclibc/lib; \
	ln -sf ../../../../../uclibc/lib/* .)

dir=`pwd`
echo dir=$dir
(cd ${prefix}/..; \
	echo tar czf ${dir}/${target}.${hosttype}.`date +%Y%m%d`.tar.gz \
		${target}; \
	tar czf ${dir}/${target}.${hosttype}.`date +%Y%m%d`.tar.gz \
		${target})

(cd ${tmpbase}; rm -rf ${tmppfx})

echo +=+=+= bld-tools-uclinux complete +=+=+=
