#!/bin/sh
# The purpose of this script is to start with source, and useing the local
# (vendor) compiler, build the gcc suite and then rebuild it so it's fully
# self-hosted.  The last step is to run the regression tests.

# It does a fair amount of self-checking along the way, but the run time
# would be far too long if it did all the regressions every place it 
# made sense.

startdir=$(pwd)
PATH=$startdir/local_bin:$startdir/bin:$PATH
PATH="$PATH:/C/MSTOOLS.XP"   ## Put CL.EXE in the path (if it isn't already)


LN=ln  # could be ln -s if everything worked.

if [ $(basename $startdir) != gnu.intel ]
then
    echo "Wrong directory, bub."
    exit
fi

#-----------------------------------------------------------------------

# If everything isn't there yet, make it there.
if [ ! -f 0README.SSI ]
then
    sd sync -f

    # SD doesn't preserve executable bits, so put 'em back.
    xargs chmod u+x <executables
fi

#-----------------------------------------------------------------------

# Be sure to use the new .so files (once we install them in local_bin)
export LD_LIBRARY_PATH=$startdir/local_bin
export DEJAGNU_LD_LIBRARY_PATH=$startdir/local_bin

#-----------------------------------------------------------------------
# Figure out which host we are on, and convert the name from uname -m
# form to the form needed by the scripts.

m=$(uname -m | cut -f1 -d" " )
if [ $m = Pentium ]
then
   arch=i386
elif [ $m = x86 ]
then
   arch=i386
elif [ $m = Intel ]
then
   arch=i386
elif [ $m = Alpha ]
then
   arch=alpha
else
   echo "What machine am I on?"
   exit 1
fi

#-----------------------------------------------------------------------
# The value for CXX won't initially exist, but by the time we need it,
# it'd better!

CC_CONFIGURE="CC=\"cc -D__STDC__ -D_ALL_SOURCE\" \
	  CXX=\"$startdir/local_bin/gcc -B$startdir/local_bin/\" \
          CONFIG_SHELL=$INTERIX_ROOT/bin/sh \
	  RANLIB=: \
          ./configure  --verbose --host=$arch-pc-interix \
          --disable-shared \
          --target=$arch-pc-interix \
	  --with-stabs \
	  --prefix=/usr/contrib \
          --enable-targets=i386-pc-interix,alpha-pc-interix"

GCC_CONFIGURE="CC=\"$startdir/local_bin/gcc -B$startdir/local_bin/ -D_ALL_SOURCE\" \
	  CXX=\"$startdir/local_bin/gcc -B$startdir/local_bin/\" \
          CONFIG_SHELL=$INTERIX_ROOT/bin/sh \
	  RANLIB=: \
          ./configure  --verbose --host=$arch-pc-interix \
          --disable-shared \
          --target=$arch-pc-interix \
	  --prefix=/usr/contrib \
	  --with-stabs \
          --enable-targets=i386-pc-interix,alpha-pc-interix"

CONFIGURE="$CC_CONFIGURE"

#-----------------------------------------------------------------------
# Library version: we do this here to be sure it's done, and again below
# after sources are gotten, as this won't work right if sources haven't
# been gotten.

case $(fileinfo psxss.exe) in
*2.2*)
    LIBVERS=2.2
    ;;
*2.5*)
    LIBVERS=2.5
    ;;
*3.0*)
    LIBVERS=3.0
    ;;
*6.0*)
    LIBVERS=6.0
    ;;
esac

LIBVERS=$LIBVERS-$(date +%y%m%d)
export X=$startdir   # For libexec.
export W=$startdir   # For libexec.

#-----------------------------------------------------------------------
# A little sanity checking

if [ ! -f $INTERIX_ROOT/usr/include/alloca.h ]
then
   echo alloca.h missing
   exit 1
fi

# This is a dumb firebrand bug workaround (shows up in xterms)
TEMP=C:\\TEMP\\
TMP=C:\\TEMP\\
export TMP TEMP

#-----------------------------------------------------------------------
if false
then #########################
:
fi ################################

#-----------------------------------------------------------------------
if [ -f /bin/true ]
then
    # This is firebrand; apply the patches
    (
      # POSIXLY_CORRECT affects getopt_long (from libiberty) and we do
      # not want it on for the regressions.  Thus we do this in a subshell.
      export POSIXLY_CORRECT=0 
      /bin/patch -N -l -p 2 < firebrand.patch >log.patch.fb 2>&1
    )

    if grep -q "\\.rej" log.patch.fb
    then
	echo FB PATCH FAILED
	exit 1
    fi

    if grep -q malformed log.patch.fb
    then
	echo FB PATCH FAILED
	exit 1
    fi

    if grep -q "already exists" log.patch.fb
    then
	echo FB PATCH FAILED
	exit 1
    fi

fi

#-----------------------------------------------------------------------
# Report the version.
echo $LIBVERS
touch V.$LIBVERS

#-----------------------------------------------------------------------
echo Makeing Configure
if make -f Interix.mk total_configure ARCH=$arch CONFIGURE="$CONFIGURE" >log.configure 2>&1
then
   :
else
   echo CONFIGURE FAILED
   exit 1
fi


#-----------------------------------------------------------------------
# Since it takes a fair amount of time to build some of the stuff we
# don't need, we'll skip it during the early steps.
# We need tcl and expect to be able to build gdb.
#
# g77 is NOT built at this point, because it requires gcc.


echo Makeing Build
if make -f Interix.mk \
        	     BUILD_CFLAGS="-O2" \
	  	     build_libiberty build_bfd build_opcodes build_byacc \
                     build_gas build_ld build_gcc \
		     build_readline build_tcl build_tk >log.build 2>&1
then
   :
else
   echo INITIAL BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
# To successfully configure gdb, tcl and tk must already exist.
echo Configuring gdb
if make -f Interix.mk gdb_configure ARCH=$arch CONFIGURE="$CONFIGURE" >log.gdb.configure 2>&1
then
   :
else
   echo GDB CONFIGURE FAILED
   exit 1
fi

#-----------------------------------------------------------------------
# To successfully build libexec, gcc must work (requires gcc, not cc)
# and it must be configured to build with gcc.

echo Configuring libexec
if make -f Interix.mk libexec_configure ARCH=$arch CONFIGURE="$GCC_CONFIGURE" >log.libexec.configure 2>&1
then
   :
else
   echo LIBEXEC CONFIGURE FAILED
   exit 1
fi

#-----------------------------------------------------------------------
echo Makeing gdb Build

# VC6 falls over in blockframe.c (at least)
        	     BUILD_CFLAGS="-O2" \

if make -f Interix.mk \
        	     BUILD_CFLAGS="" \
		     build_gdb >log.gdb.build 2>&1
then
   :
else
   echo GDB BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
# Build binutils; we need it to build libexec.
echo Makeing Binutils
if make -f Interix.mk BUILD_CFLAGS="-O2" \
                     build_binutils >log.binutils 2>&1
then
   :
else
   echo INITIAL BINUTILS BUILD FAILED
   exit 1
fi


#-----------------------------------------------------------------------
# Build the gcc libraries; that way we can make a drop below.
# librx is the regular expression stuff, used by the other libs
# Note that libexec requires gcc and binutils.
export X=$startdir   # For libexec.
echo Makeing Libs
if make -f Interix.mk \
        	     BUILD_CFLAGS="-O2" \
		     build_librx build_libstdc++ build_libio \
                     build_libg++ >log.libs 2>&1
then
   :
else
   echo INITIAL LIB BUILD FAILED
   exit 1
fi


#-----------------------------------------------------------------------
# For libexec:
# We also need as, ld, ar, nm, objcopy, and objdump.  We have as, already.
cp $startdir/binutils/ar $startdir/local_bin/ar
cp $startdir/binutils/objdump $startdir/local_bin/objdump
cp $startdir/binutils/objcopy $startdir/local_bin/objcopy
cp $startdir/binutils/nm.new $startdir/local_bin/nm
cp $startdir/binutils/liblock $startdir/local_bin/liblock
cp $startdir/ld/ld.new $startdir/local_bin/ld


# We need libc.so to build libexec
# Actually, probably not, now.  Try deleteing this sometime.

if [ ! -f $INTERIX_ROOT/usr/lib/libc.so ]
then
    echo "Make initial libc.so"
    mkdir -p libcdir
    cd libcdir
    ../make.libc $startdir/local_bin 3

    if [ $? -ne 0 ]
    then
	echo make.libc #1 failed
	exit 1
    fi
else
    echo "Copy system's libc.so"
    cp $INTERIX_ROOT/usr/lib/libc.so $startdir/local_bin
fi


cd $startdir
export X=$startdir   # For libexec.
echo Makeing libexec
if make -f Interix.mk BUILD_CFLAGS="-O2" \
                     build_libexec >log.libexec 2>&1
then
   :
else
   echo LIBEXEC BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
# At this point we've built everything we need to deliver a product,
# except g77; we can't build it until later because it requires gcc..

echo Makeing image part 1
./make.image $startdir $startdir/image $arch $startdir/drop.$arch.tgz 1 >log.drop.1 2>&1

# log.drop should be empty, but to make a point if it isn't:
cat log.drop.1

#-----------------------------------------------------------------------
# The below is optional... for now we do it until we never need the tools
# until the full build at the end is completed.
echo Makeing Build of testtools
if make -f Interix.mk \
        	     BUILD_CFLAGS="-O2" \
		     build_tools build_binutils >log.tool.build 2>&1
then
   :
else
   echo TOOL BUILD FAILED
   exit 1
fi

# The section below is a "tell me 3 times" rebuild that assures that
# the pieces of the compiler are all stable.  Because it's the whole
# compiler suite we're rebuilding, it's more than three times thru
# most stages to be sure, for example, that a subtle change to the 
# assembler doesn't end up destabilizing the compiler.
#-----------------------------------------------------------------------
# Make sure we have known working copies saved away.
cp $startdir/gcc/xgcc $startdir/local_bin/xgcc.msvc
cp $startdir/gcc/cc1 $startdir/local_bin/cc1.msvc
cp $startdir/gcc/cc1plus $startdir/local_bin/cc1plus.msvc
cp $startdir/gcc/cpp $startdir/local_bin/cpp.msvc

#-----------------------------------------------------------------------
# For this pass, we only need to make C, not C++ or objective C.
cd $startdir/gcc
echo Makeing gcc stage 1
make stage1 >$startdir/log.stage1 2>&1
if make CC="stage1/xgcc -Bstage1/ -B$startdir/local_bin/" \
        BUILD_CFLAGS="-g -O2" \
	GCC_FOR_TARGET="./xgcc -B./ -B$startdir/local_bin/" \
	LANGUAGES=c >>$startdir/log.stage1 2>&1
then
   :
else
   echo STAGE1 BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
# This time we make C and C++; they're the only tools we build;
# All other parts of gcc are simply targets.

cd $startdir/gcc
echo Makeing gcc stage 2
make stage2 >$startdir/log.stage2 2>&1
if make CC="stage2/xgcc -Bstage2/ -B$startdir/local_bin/" \
        BUILD_CFLAGS="-g -O2" \
	GCC_FOR_TARGET="./xgcc -B./ -B$startdir/local_bin/" \
	LANGUAGES="c c++" \
	>>$startdir/log.stage2 2>&1
then
   :
else
   echo STAGE2 BUILD FAILED
   exit 1
fi
#-----------------------------------------------------------------------

cd $startdir/gcc
if make compare >$startdir/log.compare 2>&1
then
   :
else
   echo GCC STAGE 2 COMPARE FAILED
   exit 1
fi

#-----------------------------------------------------------------------
# This needs some careful work yet... maybe on make; nonexistent files
# may be OK. ???

## Do we want to remove back copies?  Yet????

# bootstrap the assembler
# the gas Makefile knows how to do this better, but it relies on ln -s
# and it's not worth trying to fix right now.

cd $startdir/gas

# The as that's in local_bin is the current as.new
rm -rf stage1
mkdir stage1
mv *.o stage1
mv as.new stage1
mv gasp.new stage1

#-----------------------------------------------------------------------
echo Makeing gas stage1
if make CC="$startdir/gcc/xgcc -B$startdir/gcc/ -B$startdir/local_bin/" \
	BUILD_CFLAGS="-g -O2" \
        ALLOCA= as.new gasp.new >$startdir/log.gas.stage1 2>&1
then
   :
else
   echo GAS STAGE1 BUILD FAILED
   exit 1
fi


#-----------------------------------------------------------------------
# Now the assembler built with itself.
mv $startdir/local_bin/as $startdir/local_bin/as.msvc
cp as.new $startdir/local_bin/as
rm -rf stage2
mkdir stage2
mv *.o stage2
mv as.new stage2
mv gasp.new stage2

#-----------------------------------------------------------------------
echo Makeing gas stage2
if make CC="$startdir/gcc/xgcc -B$startdir/gcc/ -B$startdir/local_bin/" \
	BUILD_CFLAGS="-g -O2" \
        ALLOCA= as.new gasp.new >$startdir/log.gas.stage2 2>&1
then
   :
else
   echo GAS STAGE2 BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
cd $startdir/gas
echo Makeing Gas compare
if make comparison against=stage2 >$startdir/log.gas.compare 2>&1
then
   :
else
   echo GAS COMPARISON FAILED.
   exit 1
fi

#-----------------------------------------------------------------------
# We don't have any way of directly checking the three libraries, so
# now that we're pretty confident the basic tools are working (unless
# one of the 3 below break them), just go for it.

cd $startdir/bfd
make clean >$startdir/log.bfd 2>&1
echo Makeing bfd
if make CC="$startdir/gcc/xgcc -B$startdir/gcc/ -B$startdir/local_bin/ \
           -D_ALL_SOURCE" \
           BUILD_CFLAGS="-g -O2" >>$startdir/log.bfd 2>&1
then
   :
else
   echo BFD BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
cd $startdir/libiberty
echo Makeing libiberty
make clean >$startdir/log.libiberty 2>&1
if make CC="$startdir/gcc/xgcc -B$startdir/gcc/ -B$startdir/local_bin/" \
           BUILD_CFLAGS="-g -O2" >>$startdir/log.libiberty 2>&1
then
   :
else
   echo LIBIBERTY BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
cd $startdir/opcodes
echo Makeing opcodes
make clean >$startdir/log.opcodes 2>&1
if make CC="$startdir/gcc/xgcc -B$startdir/gcc/ -B$startdir/local_bin/" \
           BUILD_CFLAGS="-g -O2" >>$startdir/log.opcodes 2>&1
then
   :
else
   echo OPCODES BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
cd $startdir/gas
# Now the assembler built with itself, and the new libraries.
mkdir stage3
mv *.o stage3
mv as.new stage3
mv gasp.new stage3

echo Makeing gas stage3
if make CC="$startdir/gcc/xgcc -B$startdir/gcc/ -B$startdir/local_bin/" ALLOCA= \
	BUILD_CFLAGS="-g -O2" \
        as.new gasp.new >$startdir/log.gas.stage3 2>&1
then
   :
else
   echo GAS STAGE3 BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
# In theory, it we should repeat the above and compare the results, but
# exactly how paranoid to we need to be?

# Now that the assember is made, we have to generate the compiler's
# objects with it.  (Presumably, the output from gas compiled for bootstrap
# and the one compiled with gcc is the same, and the comparison above would
# indicate that, but let's take no chances.)
# Again we only need C and C++.

cd $startdir/gcc

echo Makeing gcc stage2 again
make stage2 >$startdir/log.gcc.stage2.2 2>&1
if make CC="stage2/xgcc -Bstage2/ -B$startdir/local_bin/"  \
	GCC_FOR_TARGET="./xgcc -B./ -B$startdir/local_bin/" \
	LANGUAGES="c c++" \
        BUILD_CFLAGS="-g -O2" >>$startdir/log.gcc.stage2.2 2>&1
then
   :
else
   echo STAGE2 BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
cd $startdir/gcc
echo Makeing gcc stage2 again compare
if make compare >$startdir/log.gcc.compare.2 2>&1
then
   :
else
   echo GCC MAKE COMPARE for GAS FAILED.
fi

#-----------------------------------------------------------------------
#if this failed, we have assembler problems.  If it's OK, we can go on
#about our business for the moment.

# Now make ld with all new pieces (except itself).
cd $startdir/ld
echo Makeing ld
make clean >$startdir/log.ld 2>&1
if make CC="$startdir/gcc/xgcc -B$startdir/gcc/ -B$startdir/local_bin/" \
        BUILD_CFLAGS="-g -O2" >>$startdir/log.ld 2>&1
then
   :
else
   echo LD BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
# Now set up to make ld with itself
mv $startdir/local_bin/ld $startdir/local_bin/ld.msvc
mv ./ld.new $startdir/local_bin/ld

# and do so.
echo Makeing ld stage 2
if make CC="$startdir/gcc/xgcc -B$startdir/gcc/ -B$startdir/local_bin/" \
        BUILD_CFLAGS="-g -O2" >$startdir/log.ld.2 2>&1
then
   :
else
   echo LD STAGE 2 BUILD FAILED
   exit 1
fi

# Check that the results are the same.
# Nuke dates
tail +140c $startdir/local_bin/ld >t1
tail +140c ld.new >t2

if cmp t1 t2
then
   :
else
   echo LD COMPARISON FAILED
   exit 1
fi

rm t1 t2

#-----------------------------------------------------------------------
# Now remake gas with the new linker and libs.
cd $startdir/gas

mkdir stage4
mv as.new stage4
mv gasp.new stage4

## recompile of bfd changed bfd.h, so it's not just a relink.
echo Makeing gas relink... stage 4
if make CC="$startdir/gcc/xgcc -B$startdir/gcc/ -B$startdir/local_bin/" ALLOCA= \
   BUILD_CFLAGS="-g -O2" \
   as.new gasp.new >$startdir/log.gas.stage4 2>&1
then
   :
else
   echo GAS RELINK FAILED
   exit 1
fi

# Not useful to compare because the libs changed.
cp as.new $startdir/local_bin/as

#-----------------------------------------------------------------------
# Now remake gcc with all new pieces/parts.
# A new gas implies that we need to redo stage 2 completely.
# We'll build EVERYTHING (except g77) this time.

cd $startdir/gcc
echo Makeing gcc with new linker and as.
make stage2 >$startdir/log.gcc.stage2.3 2>&1
if make CC="stage2/xgcc -Bstage2/ -B$startdir/local_bin/" \
   LANGUAGES="c objective-c proto gcov c++" \
   GCC_FOR_TARGET="./xgcc -B./ -B$startdir/local_bin/" \
   BUILD_CFLAGS="-g -O2" >>$startdir/log.gcc.stage2.3 2>&1
then
   :
else
   echo STAGE2 BUILD \#3 FAILED
   exit 1
fi

#the .o files should compare, but not the linked results.
echo Makeing gcc stage2 \#3 compare
if make compare >$startdir/log.gcc.compare.3 2>&1
then
   :
else
   echo GCC MAKE COMPARE \#3 FAILED
   # Some compairison failures are OK, so don't fail here.
fi

#-----------------------------------------------------------------------
# Assuming the .o's compared, we're done bootstrapping.  If they didn't that
# doesn't necessarily mean somethings wrong, but it's time to be VERRRRY 
# suspicious.

#-----------------------------------------------------------------------
# Put the results where we can find them.

# Make sure the latest and greatest are where we can find them.
cp -f $startdir/gcc/xgcc $startdir/local_bin/gcc
cp -f $startdir/gcc/cc1 $startdir/local_bin
cp -f $startdir/gcc/cc1plus $startdir/local_bin
cp -f $startdir/gcc/cpp $startdir/local_bin
cp -f $startdir/gcc/g++ $startdir/local_bin
cp -f $startdir/gcc/libgcc.a $startdir/local_bin
cp -f $startdir/ld/ld.new $startdir/local_bin/ld
cp -f $startdir/gas/as.new $startdir/local_bin/as

cp -f $startdir/libg++/libg++.a $startdir/local_bin
cp -f $startdir/libstdc++/libstdc++.a $startdir/local_bin
#-----------------------------------------------------------------------
#Now build fortran (it's only a target).
cd $startdir
echo Makeing g77 Build 
if make -f Interix.mk build_g77 \
		 CC="$startdir/local_bin/gcc -B$startdir/local_bin/" \
		 BUILD_CFLAGS="-g -O2" \
		 >log.g77.build 2>&1
then
   :
else
   echo G77 BUILD FAILED
   exit 1
fi
#-----------------------------------------------------------------------
# Fortran is finally built; put it where regressions can find find them.
cp -f $startdir/gcc/g77 $startdir/local_bin
cp -f $startdir/gcc/f771 $startdir/local_bin
cp -f $startdir/gcc/libf2c.a $startdir/local_bin

#-----------------------------------------------------------------------
# libc is trickier, as we want to use the system one
# if it's present, but make one otherwise.

echo "Makeing libc.so (and other libraries)"
mkdir -p libcdir
cd libcdir
../make.libc $startdir/local_bin 3

if [ $? -ne 0 ]
then
    echo make.libc #2 failed
    exit 1
fi

if [ -f $INTERIX_ROOT/usr/lib/libc.so ]
then
    echo "... use system's copy."
    rm -f $startdir/local_bin/libc.so*
    rm -f $startdir/local_bin/libm.so*

    cp $INTERIX_ROOT/usr/lib/libc.so $startdir/local_bin/libc.so
    cp $INTERIX_ROOT/usr/lib/libm.so $startdir/local_bin/libm.so
else
    echo "... freshly built."
fi
#-----------------------------------------------------------------------
# Now that fortran and the .so libs are built, make a delivery image.
cd $startdir
echo Makeing image part 2
./make.image $startdir $startdir/image $arch $startdir/drop.$arch.tgz 23t >log.drop.2 2>&1

# log.drop should be empty, but to make a point if it isn't:
cat log.drop.2

#-----------------------------------------------------------------------
# Rebuild EVERYTHING with our new tools (one last time), and configure
# for the new compilers.

#-----------------------------------------------------------------------
# When we build anything further, let's do it with shared libraries.
# It requires a warm body to make the decision that the just-built
# ld.so and libc.so are any good, so we won't override any existing
# ones.  However, let's try to jam something in place if there's nothing.
cd $startdir
#
# ld.so is easy.
if [ ! -f $INTERIX_ROOT/usr/lib/ld.so ]
then
  cp libexec/libdl/ld.so.* $INTERIX_ROOT/usr/lib
  (cd $INTERIX_ROOT/usr/lib; $LN ld.so.* ld.so)
fi

# libc is trickier, as we want to use the system one
# if it's present, but make one otherwise.

echo re-Makeing libc.so
mkdir -p libcdir
cd libcdir
../make.libc $startdir/local_bin 3

if [ $? -ne 0 ]
then
    echo make.libc #3 failed
    exit 1
fi

if [ -f $INTERIX_ROOT/usr/lib/libc.so ]
then
    echo "... use system's copy."
    rm -f $startdir/local_bin/libc.so*
    rm -f $startdir/local_bin/libm.so*

    cp $INTERIX_ROOT/usr/lib/libc.so $startdir/local_bin/libc.so
    cp $INTERIX_ROOT/usr/lib/libm.so $startdir/local_bin/libm.so
else
    echo "... freshly built."
fi

cd $startdir

# Tell the linker to default to shared.
cat <<-EOF >ld/LINKER_DEFAULT_OVERRIDE
	# Override the normal build default where it's
	# easy to change and visible.
	DYNAMIC_LINK=true
EOF

#-----------------------------------------------------------------------
cd $startdir
echo Makeing Final Clean 
if make -f Interix.mk clean >log.final.clean 2>&1
then
   :
else
   echo FINAL CLEAN FAILED
   exit 1
fi
# config caches could mislead us.
rm -f */config.cache */*/config.cache

#-----------------------------------------------------------------------
# tcl and tk are already built, so gdb's configure will find them
# and do the right thing, this time.


CONFIGURE="$GCC_CONFIGURE"
echo $CONFIGURE
cd $startdir
echo Makeing Final Configure 
if make -f Interix.mk total_configure ARCH=$arch \
           CONFIGURE="$CONFIGURE" >log.final.configure 2>&1
then
   :
else
   echo FINAL CONFIGURE FAILED
   exit 1
fi

#-----------------------------------------------------------------------
cd $startdir
echo Makeing Final Build 
if make -f Interix.mk total_build \
        	     BUILD_CFLAGS="-g -O2" \
		     >log.final.build 2>&1
then
   :
else
   echo FINAL BUILD FAILED
   exit 1
fi

#-----------------------------------------------------------------------
#Now build fortran (it's only a target).
# (This time it'll be shared).
cd $startdir
echo Makeing g77 Build  2
if make -f Interix.mk build_g77 \
		 CC="$startdir/local_bin/gcc -B$startdir/local_bin/" \
		 BUILD_CFLAGS="-g -O2" \
		 >log.g77.build.2 2>&1
then
   :
else
   echo G77 BUILD  2 FAILED
   exit 1
fi

#-----------------------------------------------------------------------
# Fortran is built again; put it where regressions can find find them.
cp -f $startdir/gcc/g77 $startdir/local_bin
cp -f $startdir/gcc/f771 $startdir/local_bin
cp -f $startdir/gcc/libf2c.a $startdir/local_bin

#-----------------------------------------------------------------------
# Make a complete set of shared libs with the latest and greatest
# (pic will confuse this, but for now...).

echo re-Makeing libc.so
mkdir -p libcdir
cd libcdir
../make.libc $startdir/local_bin 3

if [ $? -ne 0 ]
then
    echo make.libc #4 failed
    exit 1
fi

# But we want to use the official libc and libm, if present.
if [ -f $INTERIX_ROOT/usr/lib/libc.so ]
then
    echo "... use system's copy."
    rm -f $startdir/local_bin/libc.so*
    rm -f $startdir/local_bin/libm.so*

    cp $INTERIX_ROOT/usr/lib/libc.so $startdir/local_bin/libc.so
    cp $INTERIX_ROOT/usr/lib/libm.so $startdir/local_bin/libm.so
else
    echo "... freshly built."
fi

#-----------------------------------------------------------------------
# Make a shared library image.
cd $startdir
echo Makeing shared lib image.
rm -rf $startdir/image.so
./make.image $startdir $startdir/image.so $arch $startdir/drop.so.$arch.tgz 123t >log.so.drop 2>&1

# log.so.drop should be empty, but to make a point if it isn't:
cat log.so.drop
#-----------------------------------------------------------------------
# Now we regress (to childhood)

# First check the tools.  (There are "expected" failures.)

### Tcl moved to last in case it hangs
### Ditto expect


cd $startdir/dejagnu
echo Makeing dejagnu check
if make check  >$startdir/log.dejagnu.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of " $startdir/log.dejagnu.check
echo "                          Expect P=61"

#-----------------------------------------------------------------------
cd $startdir/gas
echo Makeing gas/gasp check
if make check  >$startdir/log.gas.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of " $startdir/log.gas.check
echo "                          Expect (intel)P=62, E=1"
echo "                          Expect (alpha)P=57, E=1"

#-----------------------------------------------------------------------
cd $startdir/ld
echo Makeing ld check
if make check \
      CC_FOR_TARGET="$startdir/local_bin/gcc -B $startdir/local_bin/" \
      CXX_FOR_TARGET="$startdir/local_bin/gcc -B $startdir/local_bin/" \
	>$startdir/log.ld.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of " $startdir/log.ld.check
echo "                          Expect P=15, U=2 (S-records)"

#-----------------------------------------------------------------------
cd $startdir/binutils
echo Makeing binutils check
if make check CC_FOR_TARGET="$startdir/local_bin/gcc -B $startdir/local_bin/" >$startdir/log.binutils.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of " $startdir/log.binutils.check
echo "                          Expect P=24, E=1"

# there is no bfd test
# there is no libiberty test
# there is no opcodes test
# there is no librx test
# there is no texinfo test
# there is no byacc test

#-----------------------------------------------------------------------
cd $startdir/libg++
echo Makeing libg++ check
if make check CXXFLAGS="-g -O -I $startdir/local_inc" >$startdir/log.libg++.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
echo "                          Expect no output"

#-----------------------------------------------------------------------
cd $startdir/libio
echo Makeing libio check
if make check  >$startdir/log.libio.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
# This doesn't follow the usual pattern.
if grep ": success!" $startdir/log.libio.check
then
   : nothing
else
   echo "failed"
fi
echo "                          Expect : success!"

#-----------------------------------------------------------------------
cd $startdir/libexec/libdl
export X=$startdir   # For libexec.
echo Makeing libexec check
if make check  >$startdir/log.libexec.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of" $startdir/log.libexec.check
echo "                          Expect # of LOD 0"
echo "   LOD = 10 means 'unexpected relocation'"

#-----------------------------------------------------------------------
cd $startdir/libstdc++
echo Makeing libstdc++ check
if make check \
      CFLAGS="-g -I $startdir/local_inc" \
      CXXFLAGS="-g -O -I $startdir/local_inc" \
         >$startdir/log.libstdc++.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of " $startdir/log.libstdc++.check
echo "                          Expect no output"

#-----------------------------------------------------------------------
cd $startdir/gcc
echo Makeing protoize check
if make test-protoize-simple >$startdir/log.protoize.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "^Test passed" $startdir/log.protoize.check
echo "                          Expect Test passed"

#-----------------------------------------------------------------------
# # before we can test gdb, we gotta build a usable (for gdb) tcl.
# cd $startdir/dejagnu.8
# if (eval $GCC_CONFIGURE) > $startdir/log.dejagnu.8.configure 2>&1
# then
#    :
# else
#    echo DEJAGNU.8 CONFIGURE FAILED
#    exit 1
# fi
# 
# cd $startdir/dejagnu.8
# if make > $startdir/log.dejagnu.8.build 2>&1 
# then
#    :
# else
#    echo DEJAGNU.8 BUILD FAILED
#    exit 1
# fi
# 
# 
# cd $startdir
# # Move the old ones out of the way, and set up to use the new one
# # (The scripts actually look for these specific directories.)
# mv expect expect.prev
# mv dejagnu dejagnu.prev
# mv tcl tcl.prev

#-----------------------------------------------------------------------
cd $startdir/gdb
echo Makeing gdb check
(
  # export DEJAGNULIBS=$startdir/dejagnu.8/dejagnu
  # export TCL_LIBRARY=$startdir/dejagnu.8/tcl/library 

  # Now use that one we copied above.
  export DEJAGNULIBS=$startdir/dejagnu.8

  # PATH=$PATH:$startdir/dejagnu.8/dejagnu:$startdir/dejagnu.8/expect
  if make check \
	  RUNTESTFLAGS="COMPILER=\\\"$startdir/local_bin/gcc -B$startdir/local_bin/\\\"" \
	  >$startdir/log.gdb.check 2>&1
  then
       : nothing
  else
       echo Test exited with $?
  fi
)

grep "# of " $startdir/log.gdb.check
# echo "                          Expect (intel) P=4366, X=44, US=1"
echo "                          Expect (intel) P=4377, F=3, X=45, US=1"
echo "                          Expect (alpha) P=4369, F=1, X=43, US=1"
#  Unresolved/Unreported are for core files and pthreads.
#  There is a WARNING about "all subsequent tests" which can be ignored;
#  it's used intentionally, with the message as a side-effect.
#
#  signals/bt test near end fails on alpha because signal hits before
#  function prologue can complete; livable indefinitely, and may not
#  be fixable.
#
#  3 failures specific to longjmp: it's getting the wrong entry
#  point in the case of shared libs.
#
#  If stack tests fail, see 0README.SSI about UseDebugPort.

# # Restore the old ones, because the gcc tests need them.
# cd $startdir
# mv expect.prev expect
# mv dejagnu.prev dejagnu
# mv tcl.prev tcl
#-----------------------------------------------------------------------
# Do gcc/g++ last because they take so long.
cd $startdir/gcc
echo Makeing gcc check
if make check-gcc >$startdir/log.gcc.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of " $startdir/log.gcc.check
echo "                          Expect (intel)P=4919, F=2, E=4, U=7"
echo "                          Expect (alpha)P=4918, F=3, E=4, U=7"

# 2 U's due to pack-3 -- pending EGCS

#-----------------------------------------------------------------------
cd $startdir/gcc
echo Makeing g++ check
if make check-g++ >$startdir/log.g++.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of " $startdir/log.g++.check
echo "                          Expect (intel)P=3203, F=2, E=107, U=6"
echo "                          Expect (alpha)P=3202, F=3, E=107, U=6"

#-----------------------------------------------------------------------
cd $startdir/gcc/testsuite/fcvs
echo Makeing g77 check
if driver >$startdir/log.g77.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of " $startdir/log.g77.check
echo "                          Expect P=3344, F=3, I=189, LOD=38"


#-----------------------------------------------------------------------
### Temporarily moved here because it's prone to hanging.
cd $startdir/tcl
echo Makeing tcl check
if make check  >$startdir/log.tcl.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of " $startdir/log.tcl.check
# Tcl tests are prone to leaving bodies around (one sort-of intentionally)
sleep 5
TCLTESTS=$(ps -ew | grep "tcltest" | cut -c1-7)
if [ "$TCLTESTS" ]; then
   kill $TCLTESTS
fi
echo "                          Expect P=3609, F=13"

# P=3208, F=10 indicates that it hung and was killed at the top level
# (killing all subsequent tests) in sockets tests.

#-----------------------------------------------------------------------
# Some additional tests
echo Makeing other tests
cd $startdir/othertests
driver "$startdir/local_bin/gcc -B$startdir/local_bin/ -D_ALL_SOURCE" \
       "$startdir/local_bin/g++ -B$startdir/local_bin/" \
       "$startdir/local_bin/g77 --driver=$startdir/local_bin/gcc -B $startdir/local_bin/ -B $startdir/local_bin/" \
       "$startdir/local_bin"
echo "                          Expect T=3, F=0"

#-----------------------------------------------------------------------
### Temporarily moved here because it's prone to hanging.
cd $startdir/expect
if [ -d /tmp ]
then
    :
else
    echo +++++++++++++++++++++Expect test requires /tmp
    echo                      Will fail some tests.
fi

echo Makeing expect check
if make check  >$startdir/log.expect.check 2>&1
then
   : nothing
else
   echo Test exited with $?
fi
grep "# of " $startdir/log.expect.check
echo "                          Expect P=19"

