# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=liblktlang
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=26.0.0
pkgrel=1
pkgdesc="A Python framework to generate language parsers (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/langkit"
license=('spdx:Apache-2.0 WITH LLVM-exception')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-gcc-ada"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-core"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-gmp"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-iconv"
         "${MINGW_PACKAGE_PREFIX}-prettier-ada"
         "${MINGW_PACKAGE_PREFIX}-vss-text")
makedepends=("${MINGW_PACKAGE_PREFIX}-gprbuild"
             "${MINGW_PACKAGE_PREFIX}-langkit"
             "${MINGW_PACKAGE_PREFIX}-langkit_support"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=("!strip")
source=("https://github.com/AdaCore/langkit/archive/v${pkgver}/langkit-${pkgver}.tar.gz"
        "https://github.com/AdaCore/AdaSat/archive/v${pkgver}/AdaSat-${pkgver}.tar.gz")
sha256sums=('9cef59480c9d927433ab4a74fbdfe118192b65c869b66bd59539325d966f7cf9'
            'de4fa5a74fd4b27cf3fead6e8c9491f22b0ab4b7b591b076073c6ba2a870162c')

prepare() {
  [[ -d langkit-${pkgver}/langkit/adasat ]] && rm -rf langkit-${pkgver}/langkit/adasat
  cp -r "${srcdir}"/AdaSat-${pkgver} langkit-${pkgver}/langkit/adasat
}

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

  GPR_PROJECT_PATH="${srcdir}"/build-${MSYSTEM}/langkit/adasat \
  ${MINGW_PREFIX}/bin/python manage.py \
    make \
    --build-mode prod \
    --lib lkt \
    --no-langkit-support \
    --no-mypy \
    --library-types=static,static-pic,relocatable

  cd lkt/bootstrap/python
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

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

  ${MINGW_PREFIX}/bin/python -m langkit.scripts.lkm \
    install \
    -c lkt/langkit.yaml \
    "${pkgdir}"${MINGW_PREFIX} \
    --build-mode prod \
    --disable-all-mains \
    --library-types=static,static-pic,relocatable

  cd lkt/bootstrap/python
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

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