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

_realname=protobuf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
# Note: make sure to update python-protobuf to match this version
pkgver=33.4
pkgrel=1
pkgdesc="Protocol Buffers - Google's data interchange format (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://developers.google.com/protocol-buffers/'
msys2_repository_url="https://github.com/protocolbuffers/protobuf"
msys2_references=(
  'archlinux: protobuf'
  "cpe: cpe:/a:google:google-protobuf"
  "cpe: cpe:/a:google:protobuf"
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-abseil-cpp"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(https://github.com/protocolbuffers/${_realname}/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz
        0001-fix-build-on-clangarm64.patch::https://github.com/protocolbuffers/protobuf/commit/0e84323c.patch
        0002-windres-invocation.patch
        0003-demote-error-about-buildtime-runtime-version-difference-to-warning.patch
        0005-pkgconfig-that-understands-static-libraries.patch)
sha256sums=('bc670a4e34992c175137ddda24e76562bb928f849d712a0e3c2fb2e19249bea1'
            '7a584ecac368c360493da594440d0d3ee3f63d1152908ef74aad560709caa655'
            '174f714b842d5153c79c5fda1ae775ee002aea11d53cb5d5f51cb5c4b9e63d29'
            '2b680e1c642ccd5bb70b3dcf7217c27360bc0b74f63d8afb66dd668173d9b2dd'
            'd118346d092caa8a3d6c02d88207d7cd9318fe3077b6b36cf32d1da79fe4128c')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    0001-fix-build-on-clangarm64.patch \
    0002-windres-invocation.patch \
    0003-demote-error-about-buildtime-runtime-version-difference-to-warning.patch \
    0005-pkgconfig-that-understands-static-libraries.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"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DCMAKE_CXX_STANDARD=17 \
      -Dprotobuf_BUILD_SHARED_LIBS="OFF" \
      -Dprotobuf_BUILD_TESTS="OFF" \
      -Dprotobuf_WITH_ZLIB="ON" \
      -S ${_realname}-${pkgver} \
      -B build-${MSYSTEM}-static

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

  echo "Building shared library"
  CXXFLAGS+=" -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DCMAKE_CXX_STANDARD=17 \
      -Dprotobuf_BUILD_SHARED_LIBS="ON" \
      -Dprotobuf_BUILD_TESTS="OFF" \
      -Dprotobuf_WITH_ZLIB="ON" \
      -S ${_realname}-${pkgver} \
      -B build-${MSYSTEM}-shared

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

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

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}-shared

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