# Maintainer: Dirk Stolle

_realname=ppl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2
pkgrel=2
pkgdesc="Parma Polyhedra Library - convex polyhedra + numerical abstractions (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://bugseng.com/products/ppl'
msys2_documentation_url='https://support.bugseng.com/ppl/'
msys2_repository_url='https://github.com/BUGSENG/PPL'
msys2_references=(
  'anitya: 17287'
  'archlinux: ppl'
  'gentoo: dev-libs/ppl'
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-glpk"
         "${MINGW_PACKAGE_PREFIX}-gmp")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
# Documentation, including HTML and PDF variants, is ca. 1520 files, while the whole package with
# documentation is ca. 1560 files. Therefore, documentation is disabled. Maybe it should be split
# into a separate docs package.
options=(!docs)
source=("https://support.bugseng.com/ppl/download/ftp/releases/${pkgver}/ppl-${pkgver}.tar.gz"
        "0001-make-test-immune-from-LTO.patch"
        "0002-use-frounding-math-on-clang.patch")
sha256sums=('6bc36dd4a87abc429d8f9c00c53e334e5041a9b0857cfc00dbad6ef14294aac8'
            '1703c76fac3375652b0f7aaa64c825eb9cf7aac6264ddc045e1ddbee8f96cd39'
            '0ed0507823562ab986bb414f09de5af7df5eadf17428f4f5028e3b4ebde29b71')

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

  # Fix configure check broken by LTO (Fedora).
  # See <https://github.com/BUGSENG/PPL/commit/d7cd42677f9c59590127badb4c0972df5a65f6bc>.
  patch -Np1 -i "${srcdir}/0001-make-test-immune-from-LTO.patch"

  patch -Np1 -i "${srcdir}/0002-use-frounding-math-on-clang.patch"

  # Fix detection of C++11 features (Fedora)
  sed -i 's,== 201103L,>= 201103L,g' m4/ac_check_cxx11.m4

  autoreconf -fiv
}

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

  if [[ ${CC} == clang ]]; then
    CFLAGS="${CFLAGS/-Wp,-D_FORTIFY_SOURCE=2 -fstack-protector-strong/}"
    CXXFLAGS="${CXXFLAGS/-Wp,-D_FORTIFY_SOURCE=2 -fstack-protector-strong/}"
  fi

  CXXFLAGS+=" -std=c++11 -w" \
  ../"${_realname}-${pkgver}"/configure \
    --prefix="${MINGW_PREFIX}" \
    --enable-interfaces="c,cxx" \
    --enable-shared \
    --disable-static \
    --disable-documentation

  make
}

check() {
  cd "build-${MSYSTEM}"

  make check || echo "Some tests failed."
}

package() {
  cd "build-${MSYSTEM}"

  make install DESTDIR="${pkgdir}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
