#!/bin/bash

# arm/build
# Check package dependencies, set metadata and launch
# package build script.
# by Stuart Winter <mozes@slackware.com>
#
source /usr/share/slackdev/buildkit.sh

# Package metadata:
export PKGNAM=llvm
export VERSION=${VERSION:-13.0.0}
export BUILD=${BUILD:-3}
#export BUILD=${BUILD:-1_slack14.2}
export PKGSERIES=${PKGSERIES:-d}
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz

## ******************************************************************* ##
# For test packages - best to store in another location rather than
# overwriting the working copy in the main tree:
# export PKGSTORE=/tmp/
# mkdir -vpm755 $PKGSTORE/$PKGSERIES
## ******************************************************************* ##

# Ensure base ARM packages are installed first:
slackbasedeps

# Ensure $PKGNAM isn't already installed.
# We want to build with gcc since we have a cross toolchain for it.  Building
# llvm with llvm takes >1 day, although this is what we do on x86 Slackware.
# Perhaps in the future.
#
# We need llvm to build itself now, since libcxxabi does not build with gcc.
#
#slackfailpkgdeps $PKGNAM || removepkg $PKGNAM

# Some package builds remove llvm, so let's make sure it's
# present.  There is a bootstrap option to build llvm with GCC but
# we use a self-hosted llvm to build itself in Slackware.
{ slackcheckpkgdeps llvm || installpkg $PKGSTORE/d/llvm-[0-9]*.t?z      || exit 99; }

# Ensure specific build dependencies (mainly libraries):
{ slackcheckpkgdeps xz              || installpkg $PKGSTORE/a/xz-[0-9]*.t?z      || exit 99; }
{ slackcheckpkgdeps python3         || installpkg $PKGSTORE/d/python3-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps libedit         || installpkg $PKGSTORE/l/libedit-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps libffi          || installpkg $PKGSTORE/l/libffi-[0-9]*.t?z  || exit 99; }
{ slackcheckpkgdeps libxml2         || installpkg $PKGSTORE/l/libxml2-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps ncurses         || installpkg $PKGSTORE/l/ncurses-[0-9]*.t?z || exit 99; }

# There's a problem with gcc-go on arm at the moment in that it seems to stall
# when the system is under load, which causes the Go test on LLVM to fail; so
# let's remove it util it works again.
#removepkg gcc-go

# Launch the package build script:
BUILDLOG=$SLKPORTARCH-$( basename $SLACKPACKAGE .t?z ).build.log
( ./$PKGNAM.SlackBuild ) >& /dev/stdout | tee $BUILDLOG

# Compress the build log:
bzip2 -9fvz $BUILDLOG
