# Maintainer: Yusuke Sasaki <y_sasaki@nuem.nagoya-u.ac.jp>

_realname=googletest
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-gtest")
pkgver=1.11.0
pkgrel=1
pkgdesc="Googletest (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/google/googletest"
license=('custom:BSD3')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/googletest/archive/release-${pkgver}.tar.gz"
        01-fix-pkgconfig-files.patch)
sha256sums=('b4870bf121ff7795ba20d20bcdd8627b8e088f2d1dab299a031c1034eddc93d5'
            '7112f4bebfa4bd6658c5c886242d0dff820d00ff9204edb667ff64618107f84a')

prepare() {
  cd googletest-release-${pkgver}
  patch -p1 -i ${srcdir}/01-fix-pkgconfig-files.patch
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  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" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_SKIP_RPATH=ON \
    ../googletest-release-${pkgver}

  ninja
  ${MINGW_PREFIX}/bin/python ../googletest-release-${pkgver}/googlemock/scripts/fuse_gmock_files.py ../googletest-release-${pkgver}/googlemock fused-gmock-src
  ${MINGW_PREFIX}/bin/python ../googletest-release-${pkgver}/googletest/scripts/fuse_gtest_files.py ../googletest-release-${pkgver}/googletest fused-gtest-src
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ninja install

  mkdir -p "${pkgdir}"/${MINGW_PREFIX}/src/{gmock,gtest}/{cmake,src}
  install -m 0644 fused-gtest-src/gtest/* "${pkgdir}"${MINGW_PREFIX}/src/gtest/src/
  install -m 0644 fused-gmock-src/gmock/* "${pkgdir}"${MINGW_PREFIX}/src/gmock/src/
  install -m 0644 fused-gmock-src/*.cc "${pkgdir}"${MINGW_PREFIX}/src/gmock/src/
  install -m 0644 ${srcdir}/googletest-release-${pkgver}/googletest/cmake/* "${pkgdir}"${MINGW_PREFIX}/src/gtest/cmake/
  
  install -Dm 644 ${srcdir}/googletest-release-${pkgver}/googlemock/scripts/generator/{*.py,LICENSE,README*} -t "${pkgdir}${MINGW_PREFIX}/share/gmock/generator"
  install -Dm 644 ${srcdir}/googletest-release-${pkgver}/googlemock/scripts/generator/cpp/* -t "${pkgdir}${MINGW_PREFIX}/share/gmock/generator/cpp"
  
  install -Dm 0644 ${srcdir}/googletest-release-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/gtest/LICENSE
  install -Dm 0644 ${srcdir}/googletest-release-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/gmock/LICENSE
}
