# Maintainer: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>

_realname=cvise
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.12.0
pkgrel=1
pkgdesc="Super-parallel Python port of the C-Reduce (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/marxin/cvise"
msys2_references=(
  "aur: cvise"
)
license=('spdx:BSD-3-Clause-Clear')
depends=(
  "${MINGW_PACKAGE_PREFIX}-cc-libs"
  "${MINGW_PACKAGE_PREFIX}-clang-libs"
  "${MINGW_PACKAGE_PREFIX}-clang"
  "${MINGW_PACKAGE_PREFIX}-llvm-libs"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-python-chardet"
  "${MINGW_PACKAGE_PREFIX}-python-pebble"
  "${MINGW_PACKAGE_PREFIX}-python-psutil"
  "${MINGW_PACKAGE_PREFIX}-unifdef"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
  "${MINGW_PACKAGE_PREFIX}-llvm"
)
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
optdepends=("colordiff")
source=("https://github.com/marxin/cvise/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "002-make-relocatable.patch"
        "003-fix-cvise-delta.patch"
        "004-add-basic-bash-support.patch"
        "005-fix-build-with-llvm-21.patch::https://github.com/marxin/cvise/commit/68262f7d.patch")
sha256sums=('7eb3b2ec4e64e3033cb47e35363336f50b1313a34a106609ad1110c532389779'
            'b37883a30063e45f29b41425a660014aaf302578202df6bf0b293ec9afe1ffb0'
            '2544e65b5100f72961066e314d0ee0ccded1241e2c48c0d85e8157c932a6966b'
            'f2ad4266b9d3d43c44320725f97f4b6f94172a77dd2e38917ada20ffc25dc637'
            'd12cde7a8e7327df06db7adfb4c52f623c758048ede8ff77b9ec504c41ce1e4a')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    002-make-relocatable.patch \
    003-fix-cvise-delta.patch \
    004-add-basic-bash-support.patch \
    005-fix-build-with-llvm-21.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  ${MINGW_PREFIX}/bin/cmake \
    -Wno-dev \
    -G Ninja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_INSTALL_LIBEXECDIR=lib \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    "${_extra_config[@]}" \
    -B build-${MSYSTEM} \
    -S "${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}
}

check() {
    ${MINGW_PREFIX}/bin/ctest --test-dir build-${MSYSTEM} --output-on-failure
}

package() {
  ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM} --prefix "${pkgdir}"${MINGW_PREFIX}

  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q "${pkgdir}"${MINGW_PREFIX}/share/cvise

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