# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Maintainer: Chih-Hsuan Yen <yan12125@archlinux.org>
# Contributor: Daichi Shinozaki <dsdseg@gmail.com>

pkgbase=flatbuffers
pkgname=(flatbuffers python-flatbuffers)
# https://github.com/google/flatbuffers/blob/master/CHANGELOG.md
pkgver=25.12.19
pkgrel=3
pkgdesc='An efficient cross platform serialization library for C++, with support for Java, C# and Go'
arch=(x86_64 aarch64)
url='https://google.github.io/flatbuffers/'
license=(Apache)
depends=(gcc-libs)
makedepends=(cmake
             git
             python-build
             python-installer
             python-numpy
             python-setuptools)
source=(git+https://github.com/google/flatbuffers#tag=v$pkgver-2026-02-06-03fffb2)
sha256sums=('2efba40d9c63ad93f64c86a9d7e58f94bd70f5eb687210e5fe0d26d6eebca299')

prepare() {
  sed -i 's/-Werror=/-W/g;s/-Werror//g' $pkgname/CMakeLists.txt

  cd $pkgbase
  # Work-around missing out-of-source build support in Python tests
  sed -i 's#${test_dir}/../flatc#${test_dir}/../../build/flatc#' tests/PythonTest.sh
}

build() {
  cmake -B build -S $pkgbase \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DFLATBUFFERS_BUILD_FLATLIB=OFF \
    -DFLATBUFFERS_BUILD_SHAREDLIB=ON
  cmake --build build

# Python bindings
  cd $pkgbase/python
  VERSION=$pkgver python -m build --wheel --no-isolation
}

check() {
  # tests are broken with out-of-source builds
  # https://github.com/google/flatbuffers/issues/7282
  cmake --build build --target test || true

  ./$pkgbase/tests/PythonTest.sh
}

package_flatbuffers() {
  DESTDIR="$pkgdir" cmake --install build
}

package_python-flatbuffers() {
  pkgdesc='An efficient cross platform serialization library for Python'
  depends=(python)
  optdepends=(
    'python-numpy: support for Numpy arrays'
  )

  cd $pkgbase/python
  VERSION=$pkgver python -m installer --destdir="$pkgdir" dist/*.whl
}
