# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=lapack
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.9.0
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
pkgdesc="Linear Algebra PACKage (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
options=('strip' 'staticlibs')
license=('LGPL')
url="https://www.netlib.org/lapack"
source=(${_realname}-${pkgver}.tar.gz::https://github.com/Reference-LAPACK/lapack/archive/v${pkgver}.tar.gz
        '370-fix-xgesvd-build-error.patch'
        '367-lapacke-missing-symbols.patch')
sha256sums=('106087f1bb5f46afdfba7f569d0cbe23dacb9a07cd24733765a0e89dbe1ad573'
            'f758f505ea709c4b7dec0ca9870454feabf3e95222a427794c2225207181717c'
            '327d62ee39eeb967ce48b4ed0b95e78c6210f688b5c3f944810aafe68d2dadfa')

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

  # https://github.com/Reference-LAPACK/lapack/pull/370
  patch -Np1 -i "${srcdir}"/370-fix-xgesvd-build-error.patch
  # https://github.com/Reference-LAPACK/lapack/pull/367
  patch -Np1 -i "${srcdir}"/367-lapacke-missing-symbols.patch
}

build() {
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi

  cd "${srcdir}"
  mkdir -p build-${CARCH} && cd build-${CARCH}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_Fortran_COMPILER=${MINGW_PREFIX}/bin/gfortran.exe \
      -DCMAKE_BUILD_TYPE=${_btype} \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_NEED_RESPONSE=ON \
      -DBUILD_TESTING=OFF \
      -DLAPACKE_WITH_TMG=ON \
      -DCBLAS=ON \
      -DBUILD_DEPRECATED=ON \
      ../${_realname}-${pkgver}
  make

  cd "${srcdir}"
  mkdir -p build-${CARCH}s && cd build-${CARCH}s
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_Fortran_COMPILER=${MINGW_PREFIX}/bin/gfortran.exe \
      -DCMAKE_BUILD_TYPE=${_btype} \
      -DBUILD_SHARED_LIBS=OFF \
      -DBUILD_TESTING=OFF \
      -DLAPACKE_WITH_TMG=ON \
      -DCBLAS=ON \
      -DBUILD_DEPRECATED=ON \
      ../${_realname}-${pkgver}
  make
}

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