#!/bin/sh
# Slackware build script for oneDNN 
# Written by Condor <condor _AT_ stz-bg.com>
# build with AMD support: https://github.com/intel/llvm/blob/sycl/sycl/doc/GetStartedGuide.md#build-dpc-toolchain-with-support-for-hip-amd
# https://github.com/oneapi-src/oneDNN/blob/main/src/gpu/amd/README.md


PRGNAM=oneDNN
VERSION=${VERSION:-3.6.2}
BUILD=${BUILD:-1}
TAG=${TAG:-condor}

CWD=$(pwd)
TMP=${TMP:-/tmp/condor}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}
NUMJOBS=${NUMJOBS:-" -j$(expr $(nproc) + 1) "}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;


cmake \
    -B build \
    -Wno-dev \
    -D CMAKE_INSTALL_PREFIX=/usr \
    -D DNNL_BUILD_EXAMPLES=OFF \
    -D DNNL_BUILD_TESTS=OFF \
    -D TBBROOT=/usr \
    ..

#    -D DNNL_AMD_SYCL_KERNELS_TARGET_ARCH="gfx908:xnack-;gfx90a;gfx942;gfx1010;gfx1012;gfx1030;gfx1100;gfx1101;gfx1102;gfx1151;gfx1200;gfx1201" \
#    -D DNNL_GPU_VENDOR=AMD \

cmake --build build $NUMJOBS
DESTDIR=$DEST cmake --install build --strip

mkdir -p $PKG/usr/doc
cp -a ../doc $PKG/usr/doc/$PRGNAM-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-AMD-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}
