#!/bin/sh
# GCC package build script (written by volkerdi@slackware.com)
# Alterations (and errors) for sparc by greg lim - sunsmoke at gmail
#
# Copyright 2003, 2004  Slackware Linux, Inc., Concord, California, USA
# Copyright 2005, 2006, 2007, 2008  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

VERSION=4.2.3
ARCH=${ARCH:-sparc}
TARGET=sparc64-bobware-linux
HOST=sparc-bobware-linux
GCCBUILD=sparc-bobware-linux
BUILD=1

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mtune=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "sparc" ]; then
  SLKCFLAGS="-O2 -mcpu=v9"
fi

CWD=$(pwd)
# Temporary build location.  This should *NOT* be a directory
# path a non-root user could create later...
TMP=/gcc-$(mcookie)

# This is the main DESTDIR target:
PKG1=$TMP/package-gcc64

# Clear the build locations:
if [ -d $TMP ]; then
  rm -rf $TMP
fi
mkdir -p $TMP
mkdir -p $PKG1/usr/doc/gcc-$VERSION

# Insert package descriptions:
mkdir -p $PKG1/install
cat $CWD/slack-desc.gcc > $PKG1/install/slack-desc

cd $TMP
tar xjvf $CWD/gcc-$VERSION.tar.bz2

# install docs

# build gcc
( mkdir gcc.build.lnx;
  cd gcc.build.lnx;

  #  --with-cpu=$ARCH 
  #  --with-arch=$ARCH
  #  --host=${HOST}
  
  AR_FOR_TARGET=/usr/bin/ar \
  AS_FOR_TARGET=/usr/bin/as \
  LD_FOR_TARGET=/usr/bin/ld \
  NM_FOR_TARGET=/usr/bin/nm \
  OBJDUMP_FOR_TARGET=/usr/bin/objdump \
  RANLIB_FOR_TARGET=/usr/bin/ranlib \
  STRIP_FOR_TARGET=/usr/bin/strip \
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  ../gcc-$VERSION/configure --prefix=/usr \
     --disable-shared \
     --disable-multilib \
     --enable-languages=c \
     --enable-threads=single \
     --disable-checking \
     --with-gnu-ld \
     --verbose \
     --host=${HOST} \
     --target=${TARGET}

  # Start the build:

  # Include all debugging info (for now):
  make -j4;

  # Set GCCCHECK=something to run the tests
  if [ ! -z $GCCCHECK ]; then
    make -j4 check
  fi

  make install DESTDIR=$PKG1

# Be sure the "specs" file is installed.
#if [ ! -r $PKG1/usr/lib/gcc/${ARCH}-bobware-linux/${VERSION}/specs ]; then
#  cat stage1-gcc/specs > $PKG1/usr/lib/gcc/${ARCH}-bobware-linux/${VERSION}/specs
#fi

#  chmod 755 $PKG1/usr/lib/libgcc_s.so.1

  # This is provided by binutils, so delete it here:
  rm -f $PKG1/usr/lib/libiberty.a

  # Fix stuff up:
#  ( cd $PKG1/usr/info ; rm dir ; gzip -9 * )
#  ( cd $PKG1
#    mkdir -p lib
#    cd lib
#    ln -sf /usr/bin/cpp .
#  )
  ( cd $PKG1/usr/bin
    mv sparc64-bobware-linux-gcc gcc64-$VERSION
    ln -sf gcc64-$VERSION gcc64
    ln -sf gcc64 cc64
    ln -sf gcc64-$VERSION ${TARGET}-gcc
    ln -sf gcc64-$VERSION sparc64-linux-gcc
    ln -sf gcc64-$VERSION ${TARGET}-gcc-$VERSION
  )
#  ( cd $PKG1/usr/man
#    gzip -9 */*
#    cd man1
#    ln -sf g++.1.gz c++.1.gz
#    ln -sf gcc.1.gz cc.1.gz
#  )  
 # Some stuff is already in the 32 bit gcc package
  rm -r $PKG1/usr/{info,man,share/locale}

  ## build the tgz package
  #(
  #  cd $PKG1;
  #  makepkg -l y -c n $TMP/gcc-$VERSION-$ARCH-$BUILD.tgz
  #)
# keep a log
) 2>&1 | tee $TMP/gcc.build.log

# OK, time to split the big package where needed:

# Filter all .la files (thanks much to Mark Post for the sed script):
#( cd $TMP
#  for file in $(find . -type f -name "*.la") ; do
#    cat $file | sed -e 's%-L/gcc-[[:graph:]]* % %g' > $TMP/tmp-la-file
#    cat $TMP/tmp-la-file > $file
#  done
#  rm $TMP/tmp-la-file
#)

## Strip bloated binaries and libraries:
for dir in $PKG{1,2,3,4,5,6}; do
  ( cd $dir
    find . -name "lib*so*" -exec strip --strip-unneeded "{}" \;
    find . -name "lib*a" -exec strip -g "{}" \;
    strip --strip-unneeded usr/bin/* 2> /dev/null
    find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
    find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  )
done

( cd $PKG1
  makepkg -l y -c n $TMP/gcc64-$VERSION-$ARCH-$BUILD.tgz )

echo
echo "Bobware GCC package build complete!"
echo

