# Maintainer: David Grayson <davidegrayson@gmail.com>
# Contributor: dragon-archer <dragon-archer@outlook.com>

_realname=catch
_projname=Catch2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.15.3
pkgrel=1
pkgdesc="A modern, C++-native, header-only, test framework for unit-tests, TDD and BDD using C++11 and later (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: catch2'
  'gentoo: dev-cpp/catch'
)
url='https://github.com/catchorg/Catch2'
msys2_repository_url='https://github.com/catchorg/Catch2'
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python")
license=('spdx:BSL-1.0')
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        '0001-fix-clang-regression.patch')
sha256sums=('b0299ae552918220a7a6e21e7de5b714777f4e8c883fb70c4bb23fe01df8c6e3'
            '5c795cb09098554f687212108a4f756a96756698a2fa4d3f7b89927ab5e46cb7')

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

prepare() {
  cd "${srcdir}/${_projname}-${pkgver}"

  apply_patch_with_msg \
    0001-fix-clang-regression.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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      ${_extra_config[@]} \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -S ${_projname}-${pkgver} \
      -B build-${MSYSTEM}

  cmake --build build-${MSYSTEM}
}

check() {
  # Disable -Werror due to another problem since Clang 20 from https://github.com/catchorg/Catch2/issues/2991
  cmake -DCATCH_DEVELOPMENT_BUILD=ON -DCATCH_ENABLE_WERROR=OFF -S ${_projname}-${pkgver} -B build-${MSYSTEM}
  cmake --build build-${MSYSTEM}

  # Exclude ApprovalTests as 0001-fix-clang-regression.patch reduced the total
  # number of tests, causing ApprovalTests to fail.
  ctest --test-dir build-${MSYSTEM} --output-on-failure -E ApprovalTests
}

package() {
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}

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