#!/bin/bash

# binutils.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 30-May-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t" 
            WERROR="--enable-werror=no" # Needed for i486 but doesn't harm on ARM
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# The --enable-initfini-array option was added in binutils-2.21.51.0.3.
# This option currently causes a world of hurt trying to compile glibc,
# and might break static libraries or cause other ill effects.  There
# is an upstream patch for glibc but it does not avoid all of the known
# problems (and there may be some unknown ones, too), so we will avoid
# introducing this feature for now.
# References:
# http://sourceware.org/bugzilla/show_bug.cgi?id=12343
# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46770
NO_INITFINI=" --disable-initfini-array "

# Uncomment this to include the experimental gold linker:
GOLD=" --enable-gold=yes --enable-ld=default "

# Set to ld.gold or ld.bfd:
DEFAULT_LD=ld.bfd

# Extract source:
# It's easier to stick with what Debian are doing.  I've been bitten too many
# times in the past by weirdness with binutils.
tar xvvf $PORTCWD/sources/$PKGNAM*$VERSION*tar*z*
#tar xvvf $CWD/$PKGNAM*$VERSION*?z*
cd $PKGNAM* || exit 1
slackhousekeeping

# Apply patches:
if [ -f $PORTCWD/sources/$PKGNAM*diff* ]; then
   zcat $PORTCWD/sources/$PKGNAM*diff* | patch -p1 || exit 1
   egrep -v '^#' debian/patches/series | while read line ; do
       auto_apply_patch debian/patches/$line || exit 1
   done
fi

# Configure:
CFLAGS="$SLKCFLAGS" \
CPPFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure $WERROR $GOLD $NO_INITFINI \
   --prefix=/usr \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --with-docdir=/usr/doc/binutils-$VERSION \
   --enable-targets=$ARCH-slackware-linux-gnueabi \
   --enable-shared \
   --enable-plugins \
   --enable-threads \
   --build=$ARCH-slackware-linux-gnueabi || failconfig
# Not for ARM:
#   --enable-multilib \
#   --enable-64-bit-bfd \

# Build:
make clean
make $NUMJOBS || failmake
make info

# Install into package:
make install DESTDIR=$PKG || exit 1

# "make install" skips this, but binutils.spec doesn't.  Sneaky, huh?
cp -a include/libiberty.h $PKG/usr/include/libiberty.h

# Differentiate between BSD strings and GNU strings
( cd $PKG/usr/bin ; mv strings strings-GNU )
( cd $PKG/usr/man/man1 ; mv strings.1 strings-GNU.1 )

# Move ldscripts to /usr/lib${LIBDIRSUFFIX}, and then put symlinks in place
mv $PKG/usr/${ARCH}-slackware-linux-gnueabi/lib/ldscripts $PKG/usr/lib${LIBDIRSUFFIX}
( cd $PKG/usr/${ARCH}-slackware-linux-gnueabi
  ln -s /usr/lib${LIBDIRSUFFIX}/ldscripts lib/ldscripts
  for FILE in ar as ld ld.bfd ld.gold nm objcopy objdump ranlib strip ; do
    if [ -r "/usr/bin/$FILE" ]; then
      rm -f bin/$FILE
      ln -s /usr/bin/$FILE bin/$FILE
    fi
  done
)

# If the requested default linker is present, make it the default:
# Set the link differently on the system to change the default at runtime.
if [ -r $PKG/usr/bin/$DEFAULT_LD ]; then
  ( cd $PKG/usr/bin ; rm -f ld ; ln -sf $DEFAULT_LD ld )
fi

# Remove some unneeded man pages, and then compress the rest
rm -f $PKG/usr/man/man1/{dlltool,windres}.1
( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $(find . -type l) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

# Copy docs:
mkdir -p $PKG/usr/doc/binutils-$VERSION
cp \
  $CWD/release.binutils-* \
  COPYING* ChangeLog.linux MAI* README* \
  $PKG/usr/doc/binutils-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n
 
# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links

cat << EOF

######################################################################
Now rebuild oprofile because it links to libbfd (part of this package)
######################################################################

EOF
