# Maintainer: Peter Budai <peterbud@hotmail.com>

_realname=openlibm
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.8.7
pkgrel=1
pkgdesc="High quality system independent, portable, open source libm implementation (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://openlibm.org'
msys2_repository_url='https://github.com/JuliaMath/openlibm'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/JuliaMath/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
	"0001-openlibm-fix-pkgconfig-file.patch")
sha256sums=('e328a1d59b94748b111e022bca6a9d2fc0481fb57d23c87d90f394b559d4f062'
            'e3f2974f26d1ea5b548b153408959f3f53ab5d920dec6b94fb69e75654a635f7')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

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

  apply_patch_with_msg \
    0001-openlibm-fix-pkgconfig-file.patch
}

build() {
  cp -rf "${_realname}-${pkgver}" "build-${MSYSTEM}"

  cd "${srcdir}/build-${MSYSTEM}"
  local _compilersetting="USEGCC=1"
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _compilersetting="USECLANG=1 USEGCC=0"
  fi

  make prefix=${MINGW_PREFIX} ${_compilersetting}
}

check() {
  local _compilersetting="USEGCC=1"
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _compilersetting="USECLANG=1 USEGCC=0"
  fi

  cd "${srcdir}"/build-${MSYSTEM}
  make test ${_compilersetting}
}

package() {
  local _compilersetting="USEGCC=1"
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _compilersetting="USECLANG=1 USEGCC=0"
  fi

  cd "${srcdir}"/build-${MSYSTEM}
  make prefix=${MINGW_PREFIX} DESTDIR="${pkgdir}" install ${_compilersetting}

  install -Dm644 "${srcdir}"/build-${MSYSTEM}/LICENSE.md \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
