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

_realname=cereal
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.2
pkgrel=2
pkgdesc="A C++11 library for serialization (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://uscilab.github.io/cereal/"
msys2_repository_url="https://github.com/USCiLab/cereal"
msys2_references=(
  "cpe: cpe:2.3:a:usc:cereal"
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost")
depends=()
checkdepends=("${MINGW_PACKAGE_PREFIX}-gflags")
source=("https://github.com/USCiLab/cereal/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "${_realname}-1.2.2.patch"
        "002-fix-build-with-libc++-15.patch::https://github.com/USCiLab/cereal/commit/f3e31f32.patch")
sha256sums=('16a7ad9b31ba5880dac55d62b5d6f243c3ebc8d46a3514149e56b5e7ea81f85f'
            '1f82ffd1cac9b171ef4a60612492fef2216c851960f84543742ac4206fcc5a94'
            '57bbac101e78c2830307f30073fd6046a9bbd8907bd698eee1afe764f623d691')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/${_realname}-1.2.2.patch"
  patch -Np1 -i "${srcdir}/002-fix-build-with-libc++-15.patch"
}

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

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

  CFLAGS+=" -Wno-unused-variable"
  CXXFLAGS+=" -Wno-unused-variable"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DWITH_WERROR=OFF \
    -DBUILD_TESTS=OFF \
    "${extra_config[@]}" \
    "../${_realname}-${pkgver}"

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/cmake -DBUILD_TESTS=ON ../${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/ctest.exe || true
}

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

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