#!/bin/bash

set -e

cd $ROCM_REL_DIR
wget https://github.com/ROCm/ROCT-Thunk-Interface/archive/rocm-$PKGVER.tar.gz
tar xf ROCT-Thunk-Interface-rocm-$PKGVER.tar.gz
cd ROCT-Thunk-Interface-rocm-$PKGVER
#patch -Np1 -i $ROCM_REL_DIR/hsakmt-global-visibility.patch

rm -rf $ROCM_BUILD_DIR/hsakmt-roct
mkdir -p $ROCM_BUILD_DIR/roct-thunk-interface
cd $ROCM_BUILD_DIR/roct-thunk-interface

DEST=$OUTPUT/package-hsakmt-roct
PRGNAM=hsakmt-roct
NUMJOBS=${NUMJOBS:-" -j$(expr $(nproc) + 1) "}
BUILD=1

rm -rf $DEST

pushd .

cmake \
    -Wno-dev \
    -D CMAKE_BUILD_TYPE=Release \
    -D BUILD_SHARED_LIBS=ON \
    -D CMAKE_INSTALL_PREFIX=${ROCM_INSTALL_DIR} \
    $ROCM_REL_DIR/ROCT-Thunk-Interface-$LDIR/

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

mkdir -p $DEST/usr/lib64
ln -s /opt/rocm/lib64/libhsa-runtime64.so $DEST/usr/lib64/libhsa-runtime64.so
ln -s /opt/rocm/lib64/libhsa-runtime64.so.1 $DEST/usr/lib64/libhsa-runtime64.so.1
ln -s /opt/rocm/lib64/libhsa-runtime64.so.1.14.0 $DEST/usr/lib64/libhsa-runtime64.so.1.14.0

mkdir -p $DEST/install
cat >> $DEST/install/slack-desc << 'END'
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.
# Line up the first '|' above the ':' following the base package name, and
# the '|' on the right side marks the last column you can put a character in.
# You must make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':' except on otherwise blank lines.

           |-----handy-ruler------------------------------------------------------|
hsakmt-roct: hsakmt-roct (Radeon Open Compute Thunk Interface)
hsakmt-roct:
hsakmt-roct: This repository includes the user-mode API interfaces used to
hsakmt-roct: interact with the ROCk driver.
hsakmt-roct:
hsakmt-roct:
hsakmt-roct:
hsakmt-roct:
hsakmt-roct:
hsakmt-roct:
hsakmt-roct:
END

cd $DEST
makepkg -l y -c n $OUTPUT/$PRGNAM-$PKGVER-$ARCH-${BUILD}$TAG.txz

popd
