#!/bin/bash
# $Id: bld-tools-newlib,v 1.12 2003/01/20 22:03:19 mrustad Exp $
# Script to build tool chain. Tools are built in the "build" directory,
# Built tools are installed in the "/opt/BRECIS/<host>-x-mipsisa32-brecis-elf"
# directory - specifically in /opt/BRECIS/<host>-x-mipsisa32-brecis-elf/bin

exec 2>&1

tool_build="(BRECIS Standalone newlib)"
targetcpuarch=mips
target=mipsisa32-brecis-elf
tars=${archives:-.}
modules="binutils gcc gdb"

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

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

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

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

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

headers=${newlib}/newlib/libc/include

confopt=(--nfp --with-newlib --enable-languages=c --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 cpp ; do
	(cd ${prefix}/bin; ln -sf ${target}-${i} ${targetcpuarch}-${i})
done

rm -rf build-${gcc%%-*}
headers=${prefix}/${target}/include

# build newlib and then gcc with installed newlib

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

echo +=+=+= bld-tools-newlib tool build complete +=+=+=
