# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=pastix
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.2.1
pkgrel=1
pkgdesc='High performance parallel solver for very large sparse linear systems based on direct methods (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
depends=("${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-hwloc"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-scotch"
	 "${MINGW_PACKAGE_PREFIX}-starpu"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake")
provides=('spm')
license=('LGPL')
url="https://gitlab.inria.fr/solverstack/pastix"
source=("https://gitlab.inria.fr/solverstack/pastix//uploads/baa57033d98378e0f3affbf45900fb6e/${_realname}-${pkgver}.tar.gz")
sha256sums=('50742CC0E6E03728F7529FC607D5C65B9E14205F192947678D6103A872A6210C')

build() {
  local _arch_opt=""
  if [ "${CARCH}" = "i686" ]; then
    _arch_opt="-DBUILD_64bits=OFF"
  fi

  #Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DPASTIX_INT64=OFF \
      -DCMAKE_BUILD_TYPE=Release \
      -DPASTIX_WITH_PARSEC=OFF \
      -DPASTIX_WITH_STARPU=ON \
      -DPASTIX_ORDERING_METIS=ON \
      -DPASTIX_WITH_MPI=OFF \
      -DBUILD_DOCUMENTATION=OFF \
      -DBUILD_TESTING=OFF \
      ${_arch_opt} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  
  #Shared Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DPASTIX_INT64=OFF \
      -DCMAKE_BUILD_TYPE=Release \
      -DPASTIX_WITH_PARSEC=OFF \
      -DPASTIX_WITH_STARPU=ON \
      -DPASTIX_ORDERING_PTSCOTCH=ON \
      -DPASTIX_ORDERING_METIS=ON \
      -DPASTIX_WITH_MPI=OFF \
      -DBUILD_DOCUMENTATION=OFF \
      -DBUILD_TESTING=OFF \
      -DBUILD_SHARED_LIBS=ON \
      ${_arch_opt} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR=${pkgdir} cmake --build . --target install

  #Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR=${pkgdir} cmake --build . --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE  
  
  #Remove unwanted examples files
  #rm -rf ${pkgdir}${MINGW_PREFIX}/examples
}
