#!/bin/sh
# This script is from crossgcc FAQ-0.7.
# Before using this script it is a good idea to check with the most recent
# version of the FAQ to see if any changes have been made.  The FAQ can be
# obtained from ftp://ftp.cygnus.com/pub/embedded/crossgcc/FAQ.
#
# Note: This script provides support for cygwin32 hosts (the configuration
# of the gnu-win32 project), but current FSF releases of GCC,Binutils do not
# support cygwin32.  The support is present in case you pick up a copy of
# the cygwin32 source tree.

# 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.

[ -d $b2h ] || mkdir $b2h
(cd $b2h ; CC=gcc $src/configure --host=${build} --target=${host} --prefix=/tmp/junk -v)
[ $? = 0 ] || exit 1
(cd $b2h ; make all-gcc all-target-newlib CC=gcc CFLAGS=-g)
[ $? = 0 ] || exit 1

# 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.

[ -d $b2t ] || mkdir $b2t
(cd $b2t ; CC=gcc $src/configure --host=${build} --target=${target} --prefix=/tmp/junk -v)
[ $? = 0 ] || exit 1
(cd $b2t ; make all CC=gcc CFLAGS=-g)
[ $? = 0 ] || exit 1

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

# 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.
# libcygwin.a and libkernel32.a are only needed by cygwin32 hosted toolchains.

(
	cd $b2h/gcc
	rm -f crt0.o libc.a libm.a
	ln -s ../${host}/newlib/crt0.o .
	ln -s ../${host}/newlib/libc.a .
	ln -s ../${host}/newlib/libm.a .
	case $host in
	*cygwin32*)
		rm -f libcygwin.a libkernel32.a
		ln -s ../${host}/winsup/libcygwin.a .
		ln -s ../${host}/winsup/libkernel32.a .
		;;
	esac
)

# 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.
case $host in
*cygwin32*)
	CC="$b2h/gcc/xgcc -B$b2h/gcc/ -isystem $src/winsup/include -isystem $b2h/${host}/newlib/targ-include -isystem $src/newlib/libc/include" ;;
*)
	CC="$b2h/gcc/xgcc -B$b2h/gcc/ -isystem $b2h/${host}/newlib/targ-include -isystem $src/newlib/libc/include" ;;
esac
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/${target}/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
LD_FOR_TARGET=$b2t/ld/ld.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 cygwin32 hosted systems, but
# it doesn't hurt to always pass it.
DLLTOOL_FOR_TARGET=$b2t/binutils/dlltool

# Don't add -g here if hosting on i386-go32 and don't comment this out
# (that will cause -g to be used).  
# There's a fixed size to the line number table (or something like that).
CFLAGS=-O

# Ready.  Configure and build.

[ -d $h2t ] || mkdir $h2t

(cd $h2t ; CC="$CC" AR="$AR" RANLIB="$RANLIB" $src/configure --build=${build} --host=${host} --target=${target} --prefix=$rel -v)
[ $? = 0 ] || exit 1

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="$AS_FOR_TARGET" \
	LD_FOR_TARGET="$LD_FOR_TARGET" \
	AR_FOR_TARGET="$AR_FOR_TARGET" \
	NM_FOR_TARGET="$NM_FOR_TARGET" \
	RANLIB_FOR_TARGET="$RANLIB_FOR_TARGET" \
	DLLTOOL_FOR_TARGET="$DLLTOOL_FOR_TARGET"

[ $? = 0 ] || exit 1

# 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="$AS_FOR_TARGET" \
#	LD_FOR_TARGET="$LD_FOR_TARGET" \
#	AR_FOR_TARGET="$AR_FOR_TARGET" \
#	NM_FOR_TARGET="$NM_FOR_TARGET" \
#	RANLIB_FOR_TARGET="$RANLIB_FOR_TARGET" \
#	DLLTOOL_FOR_TARGET="$DLLTOOL_FOR_TARGET"

# Almost done.  Before the toolchain is usable we need to
# - convert the coff files to .exe's,
# - convert file names to follow MSDOS's 8.3 rules,
# - Change \n to \r\n in text files (like headres).
# The package dosrel-1.0 is set up to do all this.
# See ftp://ftp.cygnus.com/pub/embedded/crossgcc/dosrel-1.0.tar.gz

exit $?
