# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Geoffroy Carrier <geoffroy.carrier@koon.fr>
# Contributor: congyiwu <congyiwu AT gmail DOT com>

pkgname='json-c'
pkgver=0.17
pkgrel=1.1
pkgdesc="A JSON implementation in C"
url="https://github.com/json-c/json-c/wiki"
license=(MIT)
ncxfab_makedepends=(
  base-devel
  cmake
  git
  ninja
  sh
  coreutils
)
ncxfat_makedepends=(binutils glibc gcc)
provides=(libjson-c.so)
_commit=b4c371fa0cbc4dcbaccc359ce9e957a22988fb34  # tags/json-c-0.17-20230812^0
source=("json-c::git+https://github.com/json-c/json-c#commit=$_commit" aarch64.cmake)
b2sums=('SKIP' 'SKIP')

pkgver() {
  cd json-c
  local tag="$(git describe --tags --abbrev=0)"
  local ver="$(git describe --tags)"
  echo "${tag%-*}${ver#$tag}" | sed 's/^json-c-//;s/[^-]*-g/r&/;s/-/+/g'
}

prepare() {
  cd json-c
}

build() {
  if (( MPKG_CROSSCOMPILED )); then
    CROSS_OPT="-DCMAKE_TOOLCHAIN_FILE=$(realpath $MPKG_TARGETARCH.cmake)"
  fi
  
  local cmake_options=(
    -DCMAKE_BUILD_TYPE=None
    $CROSS_OPT
    -DCMAKE_INSTALL_PREFIX=$MPKG_USRPATH
    -DCMAKE_INSTALL_LIBDIR=$MPKG_USRPATH/lib
    -DBUILD_STATIC_LIBS=OFF
    -DENABLE_THREADING=ON
    -DENABLE_RDRAND=OFF
  )

  cmake -S json-c -B build -G Ninja "${cmake_options[@]}"
  cmake --build build
}

check() {
  cd build
  ctest --output-on-failure --stop-on-failure -j$(nproc)
}

package() {
  ncxfat_depends=(glibc)

  DESTDIR="$pkgdir" cmake --install build
  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 json-c/COPYING
}

# vim:set sw=2 sts=-1 et:
