#!/bin/bash

set -e

cd $ROCM_REL_DIR
wget https://github.com/ROCm/llvm-project/archive/rocm-$PKGVER.tar.gz
#wget https://www.ixip.net/rocm/rocm-llvm-fix-segfault.patch
tar xf llvm-project-rocm-$PKGVER.tar.gz
cd llvm-project-rocm-$PKGVER

#patch -Np1 -i $ROCM_REL_DIR/rocm-llvm-fix-segfault.patch
rm -rf $ROCM_BUILD_DIR/llvm-amdgpu
mkdir -p $ROCM_BUILD_DIR/llvm-amdgpu
cd $ROCM_BUILD_DIR/llvm-amdgpu
DEST=$OUTPUT/package-rocm-llvm
DESTDEV=$OUTPUT/package-device-libs
DESTCOMGR=$OUTPUT/package-comgr
PRGNAM=llvm-project
PRGNAMDEV=rocm-device-libs
PRGNAMCOMGR=rocm-comgr
NUMJOBS=${NUMJOBS:-" -j$(expr $(nproc) + 1) "}
BUILD=1

rm -rf $DEST
rm -rf $DESTDEV
rm -rf $DESTCOMGR

pushd .

CFLAGS+=' -g1' \
CXXFLAGS+=' -g1' \
cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX='/opt/rocm/lib/llvm' \
    -DLLVM_ENABLE_PROJECTS='llvm;clang;lld;compiler-rt;clang-tools-extra;mlir' \
    -DCLANG_ENABLE_AMDCLANG=ON \
    -DLLVM_ENABLE_RUNTIMES='libcxx;libcxxabi;libunwind' \
    -DLIBCXX_ENABLE_STATIC=ON \
    -DLIBCXXABI_ENABLE_STATIC=ON \
    -DLLVM_TARGETS_TO_BUILD='AMDGPU;NVPTX;X86' \
    -DCLANG_DEFAULT_LINKER=lld \
    -DFFI_INCLUDE_DIR='/usr/include' \
    -DFFI_LIBRARY_DIR='/usr/lib64' \
    -DLLVM_INSTALL_UTILS=ON \
    -DLLVM_ENABLE_BINDINGS=OFF \
    -DLLVM_LINK_LLVM_DYLIB=OFF \
    -DLLVM_BUILD_LLVM_DYLIB=OFF \
    -DLLVM_ENABLE_ASSERTIONS=ON \
    -DOCAMLFIND=NO \
    -DLLVM_ENABLE_OCAMLDOC=OFF \
    -DLLVM_INCLUDE_BENCHMARKS=OFF \
    -DLLVM_BUILD_TESTS=OFF \
    -DLLVM_INCLUDE_TESTS=OFF \
    -DCLANG_INCLUDE_TESTS=OFF \
    -DLLVM_BINUTILS_INCDIR=/usr/include \
    -DMLIR_ENABLE_VULKAN_RUNNER=ON \
    -DMLIR_ENABLE_ROCM_RUNNER=ON \
    $ROCM_REL_DIR/$PRGNAM-$LDIR/llvm

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

mkdir -p $DEST/install

echo "Building ROCm Device Libraries"
mkdir -p $ROCM_BUILD_DIR/device-libs
cd $ROCM_BUILD_DIR/device-libs
cmake \
    -Wno-dev \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_INSTALL_PREFIX=$ROCM_INSTALL_DIR \
    -D CMAKE_PREFIX_PATH="$ROCM_BUILD_DIR/build" \
    $ROCM_REL_DIR/$PRGNAM-$LDIR/amd/device-libs
cmake --build . $NUMJOBS
DESTDIR=$DESTDEV cmake --install . --strip

mkdir -p $DESTDEV/install
cat >> $DESTDEV/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------------------------------------------------------|
rocm-device-libs: ROCm Device Libraries
rocm-device-libs:
rocm-device-libs: rocm-device-libs is a collection of CMake modules for common build and
rocm-device-libs: development tasks within the ROCm project. It is therefore a build
rocm-device-libs: dependency for many of the libraries that comprise the ROCm platform.
rocm-device-libs:
rocm-device-libs: rocm-device-libs is not required for building libraries or programs that
rocm-device-libs: use ROCm; it is required for building some of the libraries that are
rocm-device-libs: a part of ROCm.
rocm-device-libs:
rocm-device-libs:
END

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

echo "Building comgr"
mkdir -p $ROCM_BUILD_DIR/rocm-comgr
cd $ROCM_BUILD_DIR/rocm-comgr
cmake \
    -Wno-dev \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_INSTALL_PREFIX=$ROCM_INSTALL_DIR \
    -D CPACK_PACKAGING_INSTALL_PREFIX=$ROCM_INSTALL_DIR \
    -D FILE_REORG_BACKWARD_COMPATIBIL=ON \
    -D BUILD_TESTING=OFF \
    -D CMAKE_PREFIX_PATH="$ROCM_BUILD_DIR/build;$ROCM_BUILD_DIR/device-libs" \
    $ROCM_REL_DIR/llvm-project-$LDIR/amd/comgr

cmake --build . $NUMJOBS
DESTDIR=$DESTCOMGR cmake --install . --strip

mkdir -p $DESTCOMGR/usr/lib64
ln -s /opt/rocm/lib64/libamd_comgr.so $DESTCOMGR/usr/lib64/libamd_comgr.so
ln -s /opt/rocm/lib64/libamd_comgr.so.2 $DESTCOMGR/usr/lib64/libamd_comgr.so.2
ln -s /opt/rocm/lib64/libamd_comgr.so.2.$ROCM_VERSION $DESTCOMGR/usr/lib64/libamd_comgr.so.2.8.$ROCM_VERSION


mkdir -p $DESTCOMGR/install
cat >> $DESTCOMGR/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------------------------------------------------------|
comgr: Code Object Manager (comgr)
comgr:
comgr: The Comgr library provides APIs for compiling and inspecting AMDGPU
comgr: code objects.
comgr:
comgr:
comgr:
comgr:
comgr:
comgr:
comgr:
END

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

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------------------------------------------------------|
rocm-llvm: rocm-llvm (Radeon Open Compute - LLVM toolchain (llvm, clang, lld))
rocm-llvm:
rocm-llvm: AMD Fork of The LLVM Compiler Infrastructure
rocm-llvm:
rocm-llvm: The AMD fork aims to contain all of upstream LLVM, and also includes
rocm-llvm: several AMD-specific additions in the llvm-project/amd directory:
rocm-llvm:
rocm-llvm: amd/comgr
rocm-llvm: amd/device-libs
rocm-llvm:
rocm-llvm:
END

cd $DEST
mkdir -p opt/rocm/lib/llvm/lib/bfd-plugins
ln -s /opt/rocm/lib/llvm/lib/LLVMgold.so $DEST/opt/rocm/lib/llvm/lib/bfd-plugins/LLVMgold.so
ln -s /opt/rocm/lib/llvm $DEST/opt/rocm/llvm
# Fix rpath to avoid error when running amdclang and friends
# (error while loading shared libraries: libunwind.so.1: cannot open shared object file: No such file or directory)
patchelf --set-rpath '$ORIGIN' "$DEST/opt/rocm/lib/llvm/lib/libc++abi.so"

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

popd
