# Maintainer: gym603 <gui_yuan_miao@163.com>

_realname=re2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=2021-04-01
pkgver=${_pkgver//-/}
pkgrel=1
pkgdesc="Fast, safe, thread-friendly regular expression engine (mingw-w64)."
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/google/re2"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(${_realname}-${_pkgver}.tar.gz::https://github.com/google/re2/archive/${_pkgver}.tar.gz)
sha256sums=('358aedf71dbf26506848905f5d4417b7adba5cf44d3bbcf70bf4ef68ccb0871e')

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"
}

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

  echo "Build static for ${MINGW_CHOST}"
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DRE2_BUILD_TESTING=ON \
    -DBUILD_SHARED_LIBS=OFF \
    ../${_realname}-${_pkgver}

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

  echo "Build shared for ${MINGW_CHOST}"
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DRE2_BUILD_TESTING=ON \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-${_pkgver}

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

check() {
  echo "Tests static for ${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  ${MINGW_PREFIX}/bin/ctest ./ || true

  echo "Tests shared for ${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  ${MINGW_PREFIX}/bin/ctest ./ || true
}

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

  echo "Install shared for ${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  cd "${srcdir}/${_realname}-${_pkgver}"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
