#!/bin/sh
# This script is build-3way.sh from crossgcc FAQ-0.8.
# 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.
#
# This patch assumes:
# - the source tree contains binutils, gcc, and newlib build with the
#   one-tree script
# - that crossgcc-gcc-2.x.y.patch has been applied
#
# 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.
#
# Syntax: sh build-3way.sh [install]
#
# The default is to configure and build, but not install.
#
# The recommended way to use this script is to modify the variables
# build,host,target,src,rel,relexec as necessary, then run:
#
# build-3way.sh
# build-3way.sh install
#
# The process is rather involved as there are a lot of steps.
# On the other hand, it is really rather straightforward.
# The goal is to build a $host cross $target toolchain.  Some hosts aren't
# well suited to program development (eg: msdos) and other hosts may not have
# complete GNU support yet.  Both of these cases are ideallyhandled by a
# script like this where we build everything in a more familiar and
# comfortable environment (eg: unix).
#
# The toolchain we are building is composed of basically two pieces:
#
# 1) programs that run in the host environment.
#    These include gcc, cpp, cc1, as, ld, objdump, etc.  These critters
#    are built with a $build cross $host cross-compiler.
#
# 2) libraries of functions that run in the target environment.
#    These include libgcc.a, libc.a, libm.a, etc.  These critters are
#    built with a $build cross $target cross-compiler.
#
# We end up building 3 complete toolchains: $build cross $host,
# $build cross $target, and ultimately $host cross $target.
# Remember, the only environment in which we can run programs is $build:
# that is the reason for the complexity.
#
# The cool thing is that this can be done at all!

set -e

here=`pwd`

action=$1

build=sparc-sun-solaris2
host=i386-go32
target=m68k-coff
src=$here/src
rel=/tmp/test
relexec=$rel/H-${host}

# 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 $build cross $host cross-compiler.

if [ ! -f $b2h/configure.done ] ; then
    [ -d $b2h ] || mkdir $b2h
    (cd $b2h ; CC=gcc $src/configure --host=${build} --target=${host} --prefix=/tmp/junk -v)
    touch $b2h/configure.done
fi
# For cygwin32 so mount,umount get built.
(
	cd $b2h/gcc
	rm -f crt0.o libc.a libg.a libm.a libcygwin.a libkernel32.a
	ln -s ../${host}/newlib/crt0.o .
	ln -s ../${host}/newlib/libc.a .
	ln -s ../${host}/newlib/libg.a .
	ln -s ../${host}/newlib/libm.a .
	ln -s ../${host}/winsup/libcygwin.a .
	ln -s ../${host}/winsup/libkernel32.a .
)
(cd $b2h ; make -w all-target-newlib all-target-winsup CC=gcc)

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

if [ ! -f $b2t/configure.done ] ; then
    [ -d $b2t ] || mkdir $b2t
    (cd $b2t ; CC=gcc $src/configure --host=${build} --target=${target} --prefix=/tmp/junk -v)
    touch $b2t/configure.done
fi
#(cd $b2t ; make -w all-gcc all-target-newlib CC=gcc CFLAGS=-g)
(cd $b2t ; make -w all-gcc CC=gcc CFLAGS=-g)

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

(
	cd $b2h/gcc
	rm -f crt0.o libc.a libg.a libm.a libcygwin.a libkernel32.a
	ln -s ../${host}/newlib/crt0.o .
	ln -s ../${host}/newlib/libc.a .
	ln -s ../${host}/newlib/libg.a .
	ln -s ../${host}/newlib/libm.a .
	ln -s ../${host}/winsup/libcygwin.a .
	ln -s ../${host}/winsup/libkernel32.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 $src/winsup/include -isystem $b2h/${host}/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 $src/winsup/include -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 win32 hosted systems, but
# it doesn't hurt to always pass it.
DLLTOOL_FOR_TARGET=$b2t/binutils/dlltool
#BISON=$b2h/byacc/byacc

# For go32 cannot use -g because it can overflow coff debug info tables.
CFLAGS=-O
CXXFLAGS=-O

# Ready.  Configure and build.

if [ ! -f $h2t/configure.done ] ; then
    [ -d $h2t ] || mkdir $h2t
    (cd $h2t ; CC="$CC" AR="$AR" RANLIB="$RANLIB" $src/configure --build=${build} --host=${host} --target=${target} --prefix=$rel --exec-prefix=$relexec -v)
    touch $h2t/configure.done
fi

cd $h2t

# `info' already made for FSF releases.
#maketargets="all info"
maketargets=all

make -w ${maketargets} \
	CFLAGS="$CFLAGS" \
	CXXFLAGS="$CXXFLAGS" \
	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"

# All done, install if asked to.

if [ x"$action" = xinstall ] ; then
    make -w install install-info \
	CFLAGS="$CFLAGS" \
	CXXFLAGS="$CXXFLAGS" \
	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"

    # Install cygwin.dll [if built].
    if [ -f $b2h/$host/winsup/new-cygwin.dll ] ; then
	cp $b2h/$host/winsup/new-cygwin.dll $relexec/bin/cygwin.dll
    fi
fi

# 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 $?
