# Maintainer: Lukas Fleischer <lfleischer@archlinux.org>
# Maintainer: Christian Heusel <gromit@archlinux.org>
# Contributor: David Runge <dvzrv@archlinux.org>
# Contributor: Aleksey Filippov <sarum9in@gmail.com>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Contributor: Thomas S Hatch <thatch45@gmail.com>
# Contributor: Geoffroy Carrier <geoffroy@archlinux.org>
# Contributor: Daniel J Griffiths <ghost1227@archlinux.us>

pkgbase='protobuf'
pkgname=('protobuf' 'python-protobuf' 'ruby-google-protobuf')
pkgver=33.1
pkgrel=1
pkgdesc="Protocol Buffers - Google's data interchange format"
arch=('x86_64' 'aarch64')
url='https://developers.google.com/protocol-buffers/'
license=('BSD-3-Clause')
depends=(
  'gcc-libs'
  'glibc'
  'zlib'
  'abseil-cpp'
)
makedepends=(
  'bazelisk'
  'cmake'
  'git'
  'gtest'
  'python-build'
  'python-installer'
  'python-setuptools'
  'python-wheel'
  'ruby'
  'ruby-erb'
  'ruby-ffi-compiler'
  'ruby-rake'
  'ruby-rake-compiler'
)
checkdepends=(
  python-pytest
  python-numpy
)
source=(
  git+https://github.com/protocolbuffers/protobuf.git#tag=v$pkgver
  protobuf-fix-build-type-none.patch
  protobuf-33-patch-enum-traits-impl.patch
)
sha512sums=('2dba565a0101090b5ab173f1343eed5b1fbe46d94590b88425a9687224fe496aef1eddf4b322c0d265a1bee492d644c5ffe317ecba798f0bda28247bfc756600'
            '18bc71031bbcbc3810a9985fa670465040f06a6c104ab8079b56bdfc499bb6cec40805a0cefd455031142490a576dc60aa8000523877ac0353b93558e9beabbd'
            '8f7b8cb3829490962668bbb0eb8104f565a16bb336669e03afdc355c7ad50efb094851f7f78eba0d0a9704290b1c45ec56c9e6d6677cbf96ccfbfb659c014756')

options=(!lto)

_gemname=google-protobuf

prepare() {
  cd "$pkgname"
  bazel --version

  patch -p1 < ../protobuf-fix-build-type-none.patch # Fix cmake config compatibility mode
  patch -p1 < ../protobuf-33-patch-enum-traits-impl.patch
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_C_FLAGS="$CFLAGS -ffat-lto-objects"
    -D CMAKE_CXX_FLAGS="$CXXFLAGS -ffat-lto-objects"
    -D protobuf_BUILD_SHARED_LIBS=ON
    -D protobuf_USE_EXTERNAL_GTEST=ON
    -D protobuf_ABSL_PROVIDER=package
    -S "$pkgbase"
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose

  cd "$pkgbase"
  local bazel_options=(
    --noenable_bzlmod
    --enable_workspace
  )

  # protobuf has both osx-aarch_64 and linux-aarch_64 platforms
  # so we need to explicitly specify the target CPU when building
  # for aarch64
  [[ $CARCH == "aarch64" ]] && bazel_options+=(--cpu=linux-aarch_64)

  bazel build //python/dist:binary_wheel "${bazel_options[@]}"

  cd ruby
  local _gemdir="$(gem env gemdir)"
  local _gemver=4.$pkgver

  PROTOC="$srcdir"/build/protoc \
    rake
  rake gem

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install${_gemdir}" \
    --bindir "tmp_install/usr/bin" \
    "pkg/${_gemname}-${_gemver}.gem"

  install -D lib/google/protobuf_c.so \
    "tmp_install${_gemdir}/gems/${_gemname}-${_gemver}"/lib/google/$(ruby -e 'require "rbconfig"; puts RbConfig::CONFIG["RUBY_API_VERSION"]')/protobuf_c.so

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "tmp_install${_gemdir}/cache/" \
    "tmp_install${_gemdir}/gems/${_gemname}-${_gemver}/vendor/" \
    "tmp_install${_gemdir}/doc/${_gemname}-${_gemver}/ri/ext/"

  find "tmp_install${_gemdir}/gems/" \
    -type f \
    \( \
      -iname "*.o" -o \
      -iname "*.c" -o \
      -iname "*.time" -o \
      -iname "gem.build_complete" -o \
      -iname "Makefile" \
    \) \
    -delete

  find "tmp_install${_gemdir}/extensions/" \
    -type f \
    \( \
      -iname "mkmf.log" -o \
      -iname "gem_make.out" \
    \) \
    -delete
}

check() {
  ctest --test-dir build
}

package_protobuf() {
  replaces=('protobuf-cpp')
  provides=('libprotoc.so' 'libprotobuf.so' 'libprotobuf-lite.so')

  DESTDIR="$pkgdir" cmake --install build

  cd $pkgbase
  install -vDm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname"
  install -vDm 644 editors/protobuf-mode.el \
    -t "$pkgdir/usr/share/emacs/site-lisp/"
  install -vDm 644 editors/proto.vim \
    -t "${pkgdir}/usr/share/vim/vimfiles/syntax"
}

package_python-protobuf() {
  pkgdesc='Python 3 bindings for Google Protocol Buffers'
  depends=(
    "protobuf=$pkgver"
    'python'
  )

  python -m installer --destdir="$pkgdir" "$pkgbase"/bazel-bin/python/dist/*.whl
  install -vDm 644 $pkgbase/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}

package_ruby-google-protobuf() {
  pkgdesc='Ruby bindings for Google Protocol Buffers'
  depends=(
    'ruby'
    'ruby-bigdecimal'
    'ruby-rake'
  )

  cp -a $pkgbase/ruby/tmp_install/* "$pkgdir"

  install -vDm 644 $pkgbase/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -Dm 0644 $pkgbase/ruby/*.md -t "$pkgdir/usr/share/doc/${pkgname}"
}
