#!/bin/bash

set -e

cd $ROCM_REL_DIR
wget https://github.com/ROCm/rocALUTION/archive/rocm-$PKGVER.tar.gz
tar xf rocALUTION-$LDIR.tar.gz
rm -rf $ROCM_BUILD_DIR/rocalution
mkdir -p $ROCM_BUILD_DIR/rocalution
cd $ROCM_BUILD_DIR/rocalution

DEST=$OUTPUT/package-rocalution
PRGNAM=rocALUTION
NUMJOBS=${NUMJOBS:-" -j$(expr $(nproc) + 1) "}
BUILD=1
rm -rf $DEST

pushd .

export HIPCC_COMPILE_FLAGS_APPEND="-parallel-jobs=$(nproc)"
export HIPCC_LINK_FLAGS_APPEND="-parallel-jobs=$(nproc)"

cmake \
    -Wno-dev \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_TOOLCHAIN_FILE="$ROCM_REL_DIR/rocALUTION-$LDIR/toolchain-linux.cmake" \
    -D CMAKE_CXX_FLAGS="${CXXLFLAGS} -fcf-protection=none" \
    -D CMAKE_INSTALL_PREFIX=${ROCM_INSTALL_DIR} \
    -D ROCM_PATH=${ROCM_INSTALL_DIR} \
    -D SUPPORT_HIP=ON \
    -D BUILD_SHARED_LIBS=ON \
    $ROCM_REL_DIR/rocALUTION-$LDIR

#    -D SUPPORT_OMP=ON \
#    -D SUPPORT_MPI=ON \
cmake --build . $NUMJOBS
DESTDIR=$DEST cmake --install . --strip

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------------------------------------------------------|
rocALUTION: rocALUTION (Next generation library for iterative sparse solvers)
rocALUTION:
rocALUTION: rocALUTION is a sparse linear algebra library that can be used to
rocALUTION: explore fine-grained parallelism on top of the ROCm platform runtime
rocALUTION: and toolchains. Based on C++ and HIP, rocALUTION provides a portable,
rocALUTION: generic, and flexible design that allows seamless integration with
rocALUTION: other scientific software packages.
rocALUTION:
rocALUTION:
rocALUTION:
END

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

popd

