# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=antlr4
pkgbase=mingw-w64-${_realname}-runtime-cpp
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-runtime-cpp"
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git"
          "${MINGW_PACKAGE_PREFIX}-${_realname}-runtime-cpp-git")
pkgver=4.9.2
pkgrel=1
pkgdesc="ANother Tool for Language Recognition v4 (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.antlr.org/"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/antlr/antlr4/archive/${pkgver}.tar.gz"
        "0001-install-dll-to-bin.patch"
        "0002-cmake-remove-error-msg.patch")
sha256sums=('6c86ebe2f3583ac19b199e704bdff9d70379f12347f7f2f1efa38051cd9a18cf'
            '8e2663dce2b1824335c050f1627feb3a5e99f9505383e3f661cde9b800b05563'
            '571404ede0904f69189c3b3f7ccf1f062259c21f1686682caf793857f5ca4e00')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-install-dll-to-bin.patch"
  patch -Np1 -i "${srcdir}/0002-cmake-remove-error-msg.patch"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    "../${_realname}-${pkgver}/runtime/Cpp"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install
}
