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

_realname=gmp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.2.1
pkgrel=2
pkgdesc="A free library for arbitrary precision arithmetic (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://gmplib.org/"
license=('LGPL3' 'GPL')
source=(https://gmplib.org/download/gmp-${pkgver}/${_realname}-${pkgver}.tar.xz{,.sig})
options=('staticlibs')
validpgpkeys=('343C2FF0FBEE5EC2EDBEF399F3599FF828C67298') # Niels Möller <nisse@lysator.liu.se>"
sha256sums=('fd4829912cddd12f84181c3451cc752be224643e87fac497b69edddadc49b4f2'
            'SKIP')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  [[ -d ../stash ]] && rm -rf ../stash
  mkdir ../stash
  cp config.{guess,sub} ../stash
  autoreconf -fiv
  cp -f ../stash/config.{guess,sub} .
}

build() {
  local -a extra_config
  case "$CARCH" in
    i?86|x86_64)
      ;;
    *)
      extra_config+=(--disable-assembly)
      ;;
  esac
  # Build static version
  mkdir -p "${srcdir}/static-${MINGW_CHOST}" && cd "${srcdir}/static-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-cxx \
    "${extra_config[@]}" \
    --enable-static \
    --disable-shared
  make

  # Build shared version
  mkdir -p "${srcdir}/shared-${MINGW_CHOST}" && cd "${srcdir}/shared-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-cxx \
    "${extra_config[@]}" \
    --disable-static \
    --enable-shared
  make
}

check() {
  cd "${srcdir}/static-${MINGW_CHOST}"
  make check
}

package() {
  cd "${srcdir}/static-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  cd ${srcdir}/shared-${MINGW_CHOST}
  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib}
  cp .libs/libgmp*.dll ${pkgdir}${MINGW_PREFIX}/bin/
  cp .libs/libgmp*.dll.a ${pkgdir}${MINGW_PREFIX}/lib/
}
