#!/bin/bash

set -e

echo "Require 2 days for compilation"
read -p "Currently broken, version 6.2.0 not compiled"

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

DEST=$OUTPUT/package-composable_kernel
PRGNAM=composable_kernel
# WARNING: numjob = 16 require 300 GB of RAM memory, numjob = 4 around 70GB
NUMJOBS=4
BUILD=1

pushd .

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

cmake \
    -Wno-dev \
    -G Ninja \
    -D CMAKE_HIP_COMPILER_ROCM_LIB=/opt/rocm/lib \
    -D HIP_LANG=/opt/rocm/lib64 \
    -D CMAKE_CXX_COMPILER=${ROCM_INSTALL_DIR}/bin/hipcc \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_INSTALL_PREFIX=${ROCM_INSTALL_DIR} \
    -D BUILD_DEV=OFF \
    -D INSTANCES_ONLY=ON \
    -D USE_BITINT_EXTENSION_INT4=ON \
    -D BUILD_TESTING=OFF \
    $ROCM_REL_DIR/composable_kernel-$LDIR

"${NINJA:=ninja}" -j $NUMJOBS || exit 1
DESTDIR=$DEST ${NINJA} install/strip || exit 1

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------------------------------------------------------|
composable_kernel: composable_kernel (High Performance Composable Kernel for AMD GPUs)
composable_kernel:
composable_kernel: The Composable Kernel (CK) library provides a programming model for
composable_kernel: writing performance-critical kernels for machine learning workloads
composable_kernel: across multiple architectures (GPUs, CPUs, etc.). The CK library
composable_kernel: uses general purpose kernel languages, such as HIP C++.
composable_kernel:
composable_kernel:
composable_kernel:
composable_kernel:
composable_kernel:
END

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

popd
