#!/bin/sh
# This is taken from the crossgcc FAQ, version 0.4.

# Create some variables that will be useful.
build=sparc-sun-sunos4.1.3
host=i386-go32
target=m68k-coff
here=`pwd`
# `src' is presumed to have been built with the "one-tree" script.
src=$here/src
rel=/bar # Install the toolchain in /bar again.

# Build directory for the $build cross $host toolchain.
b2h=$here/b-${build}-x-${host}
# Build directory for the $build cross $target toolchain.
b2t=$here/b-${build}-x-${target}
# Build directory for the $host cross $target toolchain.
h2t=$here/b-${host}-x-${target}

# The first step is to build a sunos4 cross go32 cross-compiler.
# The value for --prefix we give here is /tmp/junk as we don't intend
# to install this toolchain.

mkdir $b2h
(cd $b2h ; CC=gcc $src/configure --host=${build} --target=${host} --prefix=/tmp/junk -v)
(cd $b2h ; make all-gcc all-newlib CC=gcc CFLAGS=-g)

# Now build a $build cross $target toolchain.
# The value for --prefix we give here is /tmp/junk as we don't intend
# to install this toolchain.

mkdir $b2t
(cd $b2t ; CC=gcc $src/configure --host=${build} --target=${target} --prefix=/tmp/junk -v)
(cd $b2t ; make all CC=gcc CFLAGS=-g)

# Now that we've built the tools that we need, we can finally build
# our $host cross $target toolchain.

mkdir $h2t

# Unfortunately, autoconf doesn't like "path prefix unused" messages, so
# we can't use -B../newlib/.  This works around that.  The alternative
# is to install the $build cross $host toolchain and use that.

(
	cd $b2h/gcc
	rm -f crt0.o libc.a libm.a
	ln -s ../newlib/crt0.o .
	ln -s ../newlib/libc.a .
	ln -s ../newlib/libm.a .
)

# Both configure and make need to be told where to find the various pieces.
# Define several variables of the things we need to pass to configure and make.

# These are for building programs that run on $build.
CC_FOR_BUILD=gcc
CXX_FOR_BUILD=gcc

# These are for building programs and libraries that run on $host.
CC="$b2h/gcc/xgcc -B$b2h/gcc/ -isystem $b2h/newlib/targ-include -isystem $src/newlib/libc/include"
AR=$b2h/binutils/ar
RANLIB=$b2h/binutils/ranlib

# These are for building libraries that run on $target.
CC_FOR_TARGET="$b2t/gcc/xgcc -B$b2t/gcc/ -isystem $b2t/newlib/targ-include -isystem $src/newlib/libc/include"
GCC_FOR_TARGET="$CC_FOR_TARGET"
CC_FOR_TARGET="$CC_FOR_TARGET"
CXX_FOR_TARGET="$CC_FOR_TARGET"
AS_FOR_TARGET=$b2t/gas/as.new
AR_FOR_TARGET=$b2t/binutils/ar
NM_FOR_TARGET=$b2t/binutils/nm.new
RANLIB_FOR_TARGET=$b2t/binutils/ranlib
# $DLLTOOL_FOR_TARGET is only needed for win32 hosted systems, but
# it doesn't hurt to always pass it.
DLLTOOL_FOR_TARGET=$b2t/binutils/dlltool

CFLAGS=-O2

# Ready.  Configure and build.

(cd $h2t ; CC="$CC" AR="$AR" RANLIB="$RANLIB" $src/configure --build=${build} --host=${host} --target=${target} --prefix=$rel -v)

cd $h2t

make all \
	CC_FOR_BUILD="$CC_FOR_BUILD" \
	CXX_FOR_BUILD="$CXX_FOR_BUILD" \
	CC="$CC" \
	AR="$AR" \
	RANLIB="$RANLIB" \
	GCC_FOR_TARGET="$CC_FOR_TARGET" \
	CC_FOR_TARGET="$CC_FOR_TARGET" \
	CXX_FOR_TARGET="$CC_FOR_TARGET" \
	AS_FOR_TARGET=$b2t/gas/as.new \
	AR_FOR_TARGET=$b2t/binutils/ar \
	NM_FOR_TARGET=$b2t/binutils/nm.new \
	RANLIB_FOR_TARGET=$b2t/binutils/ranlib \
	DLLTOOL_FOR_TARGET="$DLLTOOL_FOR_TARGET"

# All we have to do now is install it.
# This piece is separated from the previous make as sometimes
# one wants to install things differently.

make install \
	CC_FOR_BUILD="$CC_FOR_BUILD" \
	CXX_FOR_BUILD="$CXX_FOR_BUILD" \
	CC="$CC" \
	AR="$AR" \
	RANLIB="$RANLIB" \
	GCC_FOR_TARGET="$CC_FOR_TARGET" \
	CC_FOR_TARGET="$CC_FOR_TARGET" \
	CXX_FOR_TARGET="$CC_FOR_TARGET" \
	AS_FOR_TARGET=$b2t/gas/as.new \
	AR_FOR_TARGET=$b2t/binutils/ar \
	NM_FOR_TARGET=$b2t/binutils/nm.new \
	RANLIB_FOR_TARGET=$b2t/binutils/ranlib \
	DLLTOOL_FOR_TARGET="$DLLTOOL_FOR_TARGET"

exit $?
