#!/bin/sh
# GCC package build script (written by volkerdi@slackware.com)
#
# Copyright 2003  Slackware Linux, Inc., Concord, California, 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=3.2.3
ARCH=sparc64
TARGET=sparc64-slackware-linux
BUILD=1

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:
PKG=$TMP/package-gcc64

# Clear the build locations:
if [ -d $TMP ]; then
  rm -rf $TMP
fi

mkdir -p $TMP
cd $TMP
tar xjvf $CWD/gcc-$VERSION.tar.bz2
# build gcc
( mkdir gcc.build.lnx;
  cd gcc.build.lnx;
../gcc-$VERSION/configure --prefix=/usr \
              --disable-shared \
	      --disable-multilib \
	      --target=${TARGET} \
	      --enable-languages=c \
	      --enable-threads=single \
	      --verbose \
	      --with-gnu-ld \
	      --disable-checking
  # Include all debugging info (for now):
  make
  make check
  make install DESTDIR=$PKG

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

  # Strip out unneeded stuff from the libraries and binaries:
( cd $PKG
  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
)  
  # Most people debug their own code (not the libraries), so we'll strip these.
  # It cuts the size down quite a bit.
  find $PKG/usr/lib/gcc-lib -name "*.a" | xargs strip -g

  # Fix stuff up:
  ( cd $PKG/usr/bin
    mv sparc64-slackware-linux-gcc gcc64-$VERSION
    ln -sf gcc64-$VERSION gcc64
    ln -sf gcc64 cc64
    ln -sf gcc64-$VERSION sparc64-linux-gcc 
    chown root.bin * .
  )

  # No idea why it's turning out with this name
  ( cd $PKG/usr/bin
    rm sparc64-slackware-linux-sparc64-slackware-linux-gcc
  )

  # Some stuff is already in the 32 bit gcc package
  rm -r $PKG/usr/{info,man,share/locale}
  mkdir -p $PKG/install
  # Install the descriptions:
  ( cd $CWD
    cat slack-desc.gcc > $PKG/install/slack-desc
  )

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

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

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

echo
echo "Slackware GCC package build complete!"
echo

