# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=protobuf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.16.0
pkgrel=1
pkgdesc="Protocol Buffers - Google's data interchange format (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://developers.google.com/protocol-buffers/'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('strip' '!debug' 'staticlibs')
source=(https://github.com/protocolbuffers/${_realname}/releases/download/v${pkgver}/${_realname}-all-${pkgver}.tar.gz
        0001-protobuf-3.1.0-gcc6.2.0-tests.patch
        0002-windres-invocation.patch)
sha256sums=('d7371dc2d46fddac1af8cb27c0394554b068768fc79ecaf5be1a1863e8ff3392'
            'cb64be6aadaaf3c13a92343827c07254124bcdbfebef2d42c4b54079ed204ed8'
            '21c4cddfc1010178f2e4cc25c8cd17cb916186379adcc1c20c49c93408b734ee')

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

  patch -p1 -i ${srcdir}/0001-protobuf-3.1.0-gcc6.2.0-tests.patch
  patch -p1 -i ${srcdir}/0002-windres-invocation.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 "Building static library"
  [[ -d "${srcdir}/build-${CARCH}-static" ]] && rm -rf "${srcdir}/build-${CARCH}-static"
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${CARCH}-static"
  cd "${srcdir}/build-${CARCH}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DCMAKE_SKIP_RPATH="ON" \
      -Dprotobuf_MSVC_STATIC_RUNTIME="ON" \
      -DBUILD_SHARED_LIBS="OFF" \
      -Dprotobuf_BUILD_TESTS="OFF" \
      -Dprotobuf_WITH_ZLIB="ON" \
      cmake

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

  echo "Building shared library"
  [[ -d "${srcdir}/build-${CARCH}-shared" ]] && rm -rf "${srcdir}/build-${CARCH}-shared"
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${CARCH}-shared"
  cd "${srcdir}/build-${CARCH}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DCMAKE_SKIP_RPATH="ON" \
      -Dprotobuf_MSVC_STATIC_RUNTIME="ON" \
      -DBUILD_SHARED_LIBS="ON" \
      -Dprotobuf_BUILD_TESTS="OFF" \
      -Dprotobuf_WITH_ZLIB="ON" \
      cmake

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

package() {
  cd "${srcdir}/build-${CARCH}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  cd "${srcdir}/build-${CARCH}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

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