
_realname=gpr
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=26.0.0
pkgrel=1
pkgdesc="The framework for analyzing the GNAT Project (GPR) files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/gpr"
msys2_references=(
  'aur: gpr'
  'gentoo: dev-ada/gpr'
)
license=('spdx:(Apache-2.0 WITH LLVM-exception) AND GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-ada"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-core"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-iconv"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-gmp"
         "${MINGW_PACKAGE_PREFIX}-python-funcy"
         "${MINGW_PACKAGE_PREFIX}-python-mako"
         "${MINGW_PACKAGE_PREFIX}-python-docutils")
makedepends=("${MINGW_PACKAGE_PREFIX}-gprbuild"
             "${MINGW_PACKAGE_PREFIX}-python-e3-core")
source=("https://github.com/AdaCore/gpr/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-fix-build-with-gcc-ada-14.patch)
sha256sums=('146c37ede342cf4360d13bea90367d0860f23611bae71ed1c19598ffdd53a7ef'
            'df61949e11734267e0ccf8072612cc33abf26b92f12e40041c9b18e7e228eefa')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-with-gcc-ada-14.patch
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r "${srcdir}"/${_realname}-${pkgver} "${srcdir}"/build-${MSYSTEM}
  cd "${srcdir}"/build-${MSYSTEM}

  make setup prefix="${pkgdir}${MINGW_PREFIX}" GPR2KBDIR=${MINGW_PREFIX}/share/gprconfig

  make
}

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

  # Without -j1 concurrent gprinstall processes may crash:
  #
  #     raised ADA.IO_EXCEPTIONS.END_ERROR : a-tigeli.adb:96
  #     make: *** [Makefile:209: install-lib-static-pic] Error 1
  #
  # Inspired by: https://github.com/AdaCore/gprbuild/issues/41#issuecomment-396234433
  #
  make -j1 install prefix="${pkgdir}${MINGW_PREFIX}"

  cp "${pkgdir}"${MINGW_PREFIX}/include/gpr2.static/* \
    "${pkgdir}"${MINGW_PREFIX}/include/gpr2.relocatable

  # These conflict with the binaries from 'gprbuild'.
  #
  mv "${pkgdir}${MINGW_PREFIX}"/bin/gprconfig.exe "${pkgdir}${MINGW_PREFIX}"/bin/gpr2config.exe
  mv "${pkgdir}${MINGW_PREFIX}"/bin/gprclean.exe "${pkgdir}${MINGW_PREFIX}"/bin/gpr2clean.exe
  mv "${pkgdir}${MINGW_PREFIX}"/bin/gprbuild.exe "${pkgdir}${MINGW_PREFIX}"/bin/gpr2build.exe
  mv "${pkgdir}${MINGW_PREFIX}"/bin/gprinstall.exe "${pkgdir}${MINGW_PREFIX}"/bin/gpr2install.exe
  mv "${pkgdir}${MINGW_PREFIX}"/bin/gprinstall.exe.manifest "${pkgdir}${MINGW_PREFIX}"/bin/gpr2install.exe.manifest
  rm -rfv "${pkgdir}${MINGW_PREFIX}"/share/examples/gprbuild

  # Copy License Files
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}" \
    LICENSE-lib LICENSE-tool
}
