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

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

# 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 \
    0002-fix-aarch64-build.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
}
