# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=benchmark
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_gtest_ver=1.11.0
pkgver=1.5.5
pkgrel=1
pkgdesc="A microbenchmark support library, by Google (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/google/benchmark"
license=('APACHE')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gtest")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/google/benchmark/archive/v${pkgver}.tar.gz"
        googletest-release-${_gtest_ver}.tar.gz::https://github.com/google/googletest/archive/release-${_gtest_ver}.tar.gz
        "0001-cmake-disable-librt.patch"
        "0002-cmake-remove-werror.patch")
sha256sums=('3bff5f237c317ddfd8d5a9b96b3eede7c0802e799db520d38ce756a2a46a18a0'
            'b4870bf121ff7795ba20d20bcdd8627b8e088f2d1dab299a031c1034eddc93d5'
            '1d85c2b4b1679b0ba6b9d299c89de318b6f8284d426f44ae689c76b2917fffc4'
            'd0182ff9e327106e697bcf98f52bb3ddc2d31b9ae24cc577cbe43267d392893e')

prepare() {
  cp -r googletest-release-${_gtest_ver} ${_realname}-${pkgver}/googletest

  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-cmake-disable-librt.patch"
  patch -Np1 -i "${srcdir}/0002-cmake-remove-werror.patch"
  sed -i "s|v0.0.0|v${pkgver}|g" "cmake/GetGitVersion.cmake"
}

build() {
  # Shared Build
  [[ -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.exe \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DBUILD_SHARED_LIBS="ON" \
      ../${_realname}-${pkgver}

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

  # Static Build
  [[ -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.exe \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DBUILD_SHARED_LIBS="OFF" \
      ../${_realname}-${pkgver}

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

check() {
  # Shared Check
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true

  # Static Check
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

package() {
  # Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  # Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

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