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

# Some notes, Fri May 16 12:31:32 PDT 2003:
#
# Why i486 and not i386?  Because the shared C++ libraries in gcc-3.2.x will
# require 486 opcodes even when a 386 target is used (so we already weren't
# compatible with the i386 for Slackware 9.0, didn't notive, and nobody
# complained :-).  gcc-3.3 fixes this issue and allows you to build a 386
# compiler, but the fix is done in a way that produces binaries that are not
# compatible with gcc-3.2.x compiled binaries.  To retain compatibility with
# Slackware 9.0, we'll have to use i486 (or better) as the compiler target
# for gcc-3.3.
#
# It's time to say goodbye to i386 support in Slackware.  I've surveyed 386
# usage online, and the most common thing I see people say when someone asks
# about running Linux on a 386 is to "run Slackware", but then they also 
# usually go on to say "be sure to get an OLD version, like 4.0, before glibc,
# because it'll be more efficient."  Now, if that's the general advice, then
# I see no reason to continue 386 support in the latest Slackware (and indeed
# it's no longer easily possible).

VERSION=3.3.4
ARCH=${ARCH:-sparc64}
TARGET=${TARGET:-sparc64}
BUILD=${BUILD:-1}

CWD=`pwd`
# Temporary build location.  This should *NOT* be a directory
# path a non-root user could create later...
TMP=/var/gcc64-`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 $PKG1/usr/doc/gcc64-$VERSION

cd $TMP
tar xjvf $CWD/gcc-$VERSION.tar.bz2
# install docs
( cd gcc-$VERSION
  # Fix perms/owners
  chown -R root.root .
  find . -perm 777 -exec chmod 755 {} \;
  find . -perm 775 -exec chmod 755 {} \;
  find . -perm 754 -exec chmod 755 {} \;
  find . -perm 664 -exec chmod 644 {} \;
  mkdir -p $PKG1/usr/doc/gcc64-$VERSION
  # Only the most recent ChangeLog... shouldn't be too big. :)
  cp -a \
    BUGS COPYING COPYING.LIB ChangeLog FAQ INSTALL MAINTAINERS README* *.html \
    $PKG1/usr/doc/gcc64-$VERSION
  mkdir -p $PKG1/usr/doc/gcc64-${VERSION}/gcc
  ( cd gcc
    cp -a ABOUT* COPYING* LANG* NEWS README* SERVICE \
       $PKG1/usr/doc/gcc64-$VERSION/gcc
  )
)
# build gcc
( mkdir gcc.build.lnx;
  cd gcc.build.lnx;
  # CFLAGS="-Wall -g1 -O2"
  # CFLAGS="-Wall -O2" LDFLAGS=-s
  # CXXFLAGS="-O2"
  ../gcc-$VERSION/configure --prefix=/usr \
              --disable-shared \
	      --disable-multilib \
              --enable-threads=single \
	      --enable-languages=c \
              --disable-checking \
              --with-gnu-ld \
              --verbose \
              --target=${TARGET}-slackware-linux 

  # Include all debugging info (for now):
  make bootstrap || exit 1;
  # Include less debugging info:
  #make CFLAGS='-O2' LIBCFLAGS='-g -O2' LIBCXXFLAGS='-g -O2 -fno-implicit-templates' bootstrap
  # Include even less debugging info:
  #make BOOT_CFLAGS='-O2 -mcpu=v9' STAGE1_CFLAGS='-O2 -mcpu=v9' CFLAGS='-O2 -mcpu=v9' CXXFLAGS='-O2 -mcpu=v9' LIBCFLAGS='-O2 -g -mcpu=v9' LIBCXXFLAGS='-O2 -g -mcpu=v9 -fno-implicit-templates' GCJFLAGS='-O2 -mcpu=v9' bootstrap || exit 1

  make info || exit 1
  make check || exit 1;
  make install DESTDIR=$PKG1
  make -i install-info DESTDIR=$PKG1

  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

  # Strip out unneeded stuff from the libraries and binaries:
  ( cd $PKG1
    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 $PKG1/usr/lib/gcc-lib -name "*.a" | xargs strip -g

  # 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 gcc gcc64-$VERSION || mv sparc64-slackware-linux-gcc gcc64-$VERSION
    ln -sf gcc64-$VERSION gcc64
    ln -sf gcc64 cc64
    ln -sf gcc64-$VERSION sparc64-linux-gcc
    ln -sf gcc64-$VERSION gcc
    ln -sf gcc64 cc
    ln -sf gcc64-$VERSION ${TARGET}-slackware-linux-gcc
    ln -sf gcc64-$VERSION ${TARGET}-slackware-linux-gcc-$VERSION
    chown root.bin * .
  )

  # Some stuff is already in the 32 bit gcc package
  rm -r $PKG1/usr/{info,man,share/locale}

  mkdir -p $PKG1/install
  # Install the descriptions:
  ( cd $CWD
    cat slack-desc.gcc > $PKG1/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 $PKG1
  makepkg -l y -c n $TMP/gcc64-$VERSION-$ARCH-$BUILD.tgz )

echo
echo "Slackware GCC package build complete!"
echo
