# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>
# Contributor: Biswapriyo Nath <nathbappai@gmail.com>

_realname=grpc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.35.0
_abseil_ver=20200923.3
pkgrel=4
pkgdesc="gRPC - Google's high performance, open source, general RPC framework (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.grpc.io/'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-protobuf>=3.5.0"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-re2")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-re2"
             "automake" "autoconf")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/grpc/grpc/archive/v${pkgver}.tar.gz"
        "abseil-cpp-${_abseil_ver}.tar.gz::https://github.com/abseil/abseil-cpp/archive/${_abseil_ver}.tar.gz"
        "0001-abseil-Fix-compiler-warnings.patch"
        "0002-abseil-Remove-librt-library.patch"
        "0003-abseil-Include-pthread-header.patch")
sha256sums=('27dd2fc5c9809ddcde8eb6fa1fa278a3486566dfc28335fca13eb8df8bd3b958'
            'ebe2ad1480d27383e4bf4211e2ca2ef312d5e6a09eba869fd2e8a5c5d553ded2'
            '9a8d939b5a0978a4b6658aa9061423111ede3d33a330e654e4a977b1eda63c92'
            'd3ecabcebf64c2211b40e07f376de028ad1b101051cba7e69d77f6074217238d'
            'a948c1be67e752c2e50ea220c38e767d4138e2534d57d863182739d46d5e4e7b')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true

  cd "${srcdir}/${_realname}-${pkgver}"
  #patch -p1 -i ${srcdir}/001-fix-linking.patch
  #patch -Np1 -i ${srcdir}/002-fix-verify-_WIN32_WINNT.patch

  cd ${srcdir}

  rmdir ${srcdir}/${_realname}-${pkgver}/third_party/abseil-cpp
  mv abseil-cpp-${_abseil_ver} ${srcdir}/${_realname}-${pkgver}/third_party/abseil-cpp

  # https://github.com/abseil/abseil-cpp/pull/790
  cd "${srcdir}/${_realname}-${pkgver}/third_party/abseil-cpp"
  patch -Np1 -i "${srcdir}/0001-abseil-Fix-compiler-warnings.patch"
  patch -Np1 -i "${srcdir}/0002-abseil-Remove-librt-library.patch"

  patch -Np1 -i "${srcdir}/0003-abseil-Include-pthread-header.patch"
}

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

  # remove __USE_MINGW_ANSI_STDIO=1 define, it breaks grpc string handling for
  # cases like 'gpr_log(GPR_DEBUG, "Failed to free %" PRIuPTR ...'
  CPPFLAGS="${CPPFLAGS//-D__USE_MINGW_ANSI_STDIO=1/}"
  # add STRSAFE_NO_DEPRECATE define, otherwise strsafe breaks libc++ headers
  CXXFLAGS+=" -DSTRSAFE_NO_DEPRECATE"
  case "${MINGW_PACKAGE_PREFIX}" in
    *-clang-x86_64*)
    ;;
    *-x86_64*)
      # https://github.com/msys2/MINGW-packages/issues/8984
      LDFLAGS+=" -Wl,--disable-dynamicbase,--default-image-base-low"
    ;;
  esac

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DCMAKE_SKIP_RPATH="ON" \
      -DBUILD_SHARED_LIBS="$( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] && echo "OFF" || echo "ON" )" \
      -DgRPC_ZLIB_PROVIDER="package" \
      -DgRPC_CARES_PROVIDER="package" \
      -DgRPC_PROTOBUF_PROVIDER="package" \
      -DgRPC_RE2_PROVIDER="package" \
      -DgRPC_SSL_PROVIDER="package" \
      -DgRPC_BENCHMARK_PROVIDER="package" \
      ../${_realname}-${pkgver}

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

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  #make test
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
