# Maintainer: Peter Budai <peterbudai at hotmail.com>

_realname=clblast
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.5.2
pkgrel=1
pkgdesc="Tuned OpenCL BLAS (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://github.com/CNugteren/CLBlast'
license=('Apache 2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
            "${MINGW_PACKAGE_PREFIX}-opencl-headers"
            "${MINGW_PACKAGE_PREFIX}-opencl-icd")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/CNugteren/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('ad804ec228100bd5a329a9dcd88d987e70b93c9fd714bb2b3dce6cf016b8c8e9')

build() {
  cd "${srcdir}"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DTUNERS=OFF \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  make
}


check() {
  cd "${srcdir}"/build-${CARCH}
  #make check
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"

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

  # move the dll to bin
  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mv "${pkgdir}${MINGW_PREFIX}/lib/libclblast.dll" "${pkgdir}${MINGW_PREFIX}/bin"

  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  # Fix paths
  sed -i "s|${WINPREFIX}|${MINGW_PREFIX}|g" \
    ${pkgdir}${MINGW_PREFIX}/lib/cmake/CLBlast/CLBlastConfig.cmake \
    ${pkgdir}${MINGW_PREFIX}/lib/cmake/CLBlast/CLBlastConfig-release.cmake
}
