# Maintainer: gym603 <gui_yuan_miao@163.com>

_realname=re2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=2024-07-02
pkgver=${_pkgver//-/}
pkgrel=4
pkgdesc="Fast, safe, thread-friendly regular expression engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: re2'
)
url="https://github.com/google/re2"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-abseil-cpp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
checkdepends=("${MINGW_PACKAGE_PREFIX}-gtest")
source=("https://github.com/google/re2/releases/download/${_pkgver}/re2-${_pkgver}.tar.gz"
        001-fix-pkgconfig-file.patch)
sha256sums=('eb2df807c781601c14a260a507a5bb4509be1ee626024cb45acbd57cb9d4032b'
            '97d1aedfa763b162219d71144a8806e841c5e502048ad823e8bddc7fdad3dbcf')

prepare() {
  cd "${srcdir}"/${_realname}-${_pkgver}
  patch -p1 -i "${srcdir}"/001-fix-pkgconfig-file.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

  echo "Build static for ${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

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

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

  echo "Build shared for ${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

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

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

check() {
  echo "Tests static for ${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}-static"
  ${MINGW_PREFIX}/bin/cmake -DRE2_BUILD_TESTING=ON ../${_realname}-${_pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/ctest ./ || true

  echo "Tests shared for ${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}-shared"
  ${MINGW_PREFIX}/bin/cmake -DRE2_BUILD_TESTING=ON ../${_realname}-${_pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/ctest ./ || true
}

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

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

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