# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=dbcsr
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.9.0
pkgrel=2
pkgdesc="Distributed Block Compressed Sparse Row matrix library (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64')
url='https://www.cp2k.org/dbcsr'
msys2_repository_url="https://github.com/cp2k/dbcsr"
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-omp"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-libxsmm")
           #"${MINGW_PACKAGE_PREFIX}-opencl-icd")
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             #"${MINGW_PACKAGE_PREFIX}-opencl-headers"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
             #"${MINGW_PACKAGE_PREFIX}-python-fypp")
source=("https://github.com/cp2k/dbcsr/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-on-mingw.patch")
sha256sums=('a04cacd2203bd97a31ac993f9ab84237a48191140bba29efadbc27db544bbcd6'
            '3a3620ab94d7f0abc6ab5fcb32671e4489e1fbf973d7e0680ee76374f9faf897')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/001-fix-build-on-mingw.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} = x86_64 ]]; then
    _extra_config=("-DUSE_SMM=libxsmm") # "-DUSE_ACCEL=opencl")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DUSE_OPENMP=ON \
      -DUSE_MPI=OFF \
      -DWITH_C_API=OFF \
      -DBLA_VENDOR=OpenBLAS \
      -DBUILD_TESTING=OFF \
      -DWITH_EXAMPLES=OFF \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
