#!/bin/sh
# GCC package build script (written by volkerdi@slackware.com)
#
# Copyright 2003, 2004  Slackware Linux, Inc., Concord, California, USA
# Copyright 2005, 2006  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.
#

# 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 notice, 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).

# -----------------------------------------------------------------------------
# Note by Eric Hameleers, Sun Mar 25 22:01:09 UTC 2007
# -----------------------------------------------------------------------------
# This is a gcc34 compatibility SlackBuild for use in Slackware > 11.0 ,
# where gcc4 is the default compiler suite.
# This gcc34 build installs to /usr/gcc34 and will not interfere with gcc4.
# *** Use gcc34 in your scripts, as follows ***
# * By using environment variables:
#   Most softwares support the CC and CXX environment variables.
#   First assign them, then run configure and/or make. Example:
#     CC=gcc34 CXX=g++34 ./configure
#
# * Using configure support:
#   If the software is using the standard GNU automake and configure,
#   then there is a chance it supports other compilers by passing in
#   a setting to the configure script.
#   First run configure --help to see if it mentions anything.
#   The following example is from MPlayer:
#     ./configure --help
#     ./configure --cc=gcc34
# -----------------------------------------------------------------------------

VERSION=3.4.6
ARCH=${ARCH:-i486}
TARGET=$ARCH-slackware-linux
BUILD=2compat

INSPREFIX=/usr/gcc34
PRGSUFFIX=34

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-gcc
# These are the directories to build other packages in:
PKG2=$TMP/package-gcc-g++

# Clear the build locations:
if [ -d $TMP ]; then
  rm -rf $TMP
fi
mkdir -p $PKG{1,2}/usr/doc/gcc-$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/gcc-$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/gcc-$VERSION
  mkdir -p $PKG1/usr/doc/gcc-${VERSION}/gcc
  ( cd gcc
    cp -a ABOUT* COPYING* LANG* NEWS README* SERVICE \
       $PKG1/usr/doc/gcc-$VERSION/gcc
  )
  mkdir -p $PKG2/usr/doc/gcc-${VERSION}/libstdc++-v3
  ( cd libstdc++-v3
    cp -a README $PKG2/usr/doc/gcc-${VERSION}/libstdc++-v3
    cp -a docs/html/faq/index.html $PKG2/usr/doc/gcc-${VERSION}/libstdc++-v3/faq.html
  )
)
# build gcc
( mkdir gcc.build.lnx;
  cd gcc.build.lnx;
  ../gcc-$VERSION/configure \
              --prefix=$INSPREFIX \
	      --program-suffix=$PRGSUFFIX \
	      --enable-languages=c,c++ \
              --enable-shared \
              --enable-threads=posix \
              --enable-__cxa_atexit \
              --disable-checking \
              --with-gnu-ld \
              --verbose \
              --target=${TARGET} \
              --host=${TARGET}

  # Start the build:
  make -j2 bootstrap
  make -j2 info

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

  make install DESTDIR=$PKG1
  make -i install-info DESTDIR=$PKG1

  chmod 755 $PKG1/$INSPREFIX/lib/libgcc_s.so.1

  # This is provided by binutils, so delete it here:
  rm -f $PKG1/$INSPREFIX/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 -name "*.a" | xargs file | grep "archive" | cut -f 1 -d : | xargs strip -g

  # Fix stuff up:
  ( cd $PKG1/$INSPREFIX/info ; rm dir ; gzip -9 * )
  ( cd $PKG1
    mkdir -p lib
    cd lib
    ln -sf ${INSPREFIX}/bin/cpp${PRGSUFFIX} .
  )
  ( cd $PKG1/$INSPREFIX/bin
    rm -f ${TARGET}-gcc-${VERSION}           # remove unnecessary hardlink
    ln -sf g++${PRGSUFFIX} c++${PRGSUFFIX}   # overwrite the hardlink
    ln -sf gcc${PRGSUFFIX} cc${PRGSUFFIX}
    ln -sf gcc${PRGSUFFIX} ${TARGET}-gcc${PRGSUFFIX}
    ln -sf gcc${PRGSUFFIX} ${TARGET}-gcc${PRGSUFFIX}-${VERSION}
    ln -sf g++${PRGSUFFIX} ${TARGET}-c++${PRGSUFFIX}
    ln -sf g++${PRGSUFFIX} ${TARGET}-g++${PRGSUFFIX}
  )
  ( cd $PKG1/$INSPREFIX/man
    gzip -9 */*
    cd man1
    mv g++.1.gz g++${PRGSUFFIX}.1.gz
    mv gcc.1.gz gcc${PRGSUFFIX}.1.gz
    ln -sf g++${PRGSUFFIX}.1.gz c++${PRGSUFFIX}.1.gz
    ln -sf gcc${PRGSUFFIX}.1.gz cc${PRGSUFFIX}.1.gz
  )  

  # Add profile scripts so that the gcc34 binaries will be found in the PATH
  mkdir -p $PKG1/etc/profile.d
  zcat $CWD/gcc34.sh.gz > $PKG1/etc/profile.d/gcc34.sh
  zcat $CWD/gcc34.csh.gz > $PKG1/etc/profile.d/gcc34.csh
  chmod 755 $PKG1/etc/profile.d/gcc34.sh
  chmod 755 $PKG1/etc/profile.d/gcc34.csh

  mkdir -p $PKG{1,2}/install
  # Install the descriptions:
  ( cd $CWD
    cat slack-desc.gcc > $PKG1/install/slack-desc
    cat slack-desc.gcc-g++ > $PKG2/install/slack-desc
  )

# keep a log
) 2>&1 | tee $TMP/gcc.build.log

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

# gcc-g++:
( cd $PKG2
  mkdir -p ./$INSPREFIX/bin
  mv $PKG1/$INSPREFIX/bin/*++* ./$INSPREFIX/bin
  mkdir -p ./$INSPREFIX/include
  mv $PKG1/$INSPREFIX/include/c++ ./$INSPREFIX/include
  mkdir -p ./$INSPREFIX/lib
  mv $PKG1/$INSPREFIX/lib/*++* ./$INSPREFIX/lib
  mkdir -p ./$INSPREFIX/libexec/gcc/$TARGET/$VERSION
  mv $PKG1/$INSPREFIX/libexec/gcc/$TARGET/$VERSION/cc1plus ./$INSPREFIX/libexec/gcc/$TARGET/$VERSION/cc1plus
  mkdir -p ./$INSPREFIX/man/man1
  mv $PKG1/$INSPREFIX/man/man1/*++* ./$INSPREFIX/man/man1
)

# 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/gcc$PRGSUFFIX-$VERSION-$ARCH-$BUILD.tgz )
( cd $PKG2
  makepkg -l y -c n $TMP/gcc$PRGSUFFIX-g++$PRGSUFFIX-$VERSION-$ARCH-$BUILD.tgz )

echo
echo "Slackware GCC$PRGSUFFIX compatibility package build complete!"
echo
