# Maintainer: ZUO Zhihua <zuo.zhihua@qq.com>

_realname=fortran-stdlib
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.7.0
pkgrel=1
pkgdesc="Fortran Standard Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/fortran-lang/stdlib"
msys2_references=(
  'aur: fortran_stdlib'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-fypp")
source=("https://github.com/fortran-lang/stdlib/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-using-openblas.patch")
sha256sums=('07615b1fd0d9c78f04ec5a26234d091cb7e359933ba2caee311dcd6f58d87af0'
            '1ec6c36684c2c37bcb3adb0d8c13f1182377365643e575e399e7076d6a79e59d')

prepare() {
  cd "${srcdir}/stdlib-${pkgver}"
  patch -p1 -i "${srcdir}"/001-using-openblas.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -G Ninja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_TESTING=off \
      -DCMAKE_MAXIMUM_RANK=4 \
      -DBUILD_SHARED_LIBS=ON \
      ../stdlib-${pkgver}

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

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