# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Gaetan Bisson <bisson@archlinux.org>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: Simone Sclavi 'Ito' <darkhado@gmail.com>

pkgname=libjpeg-turbo
pkgver=3.0.4
pkgrel=1.1
pkgdesc="JPEG image codec with accelerated baseline compression and decompression"
url="https://libjpeg-turbo.org/"
_url="https://github.com/libjpeg-turbo/libjpeg-turbo/"
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
license=(
  BSD-3-Clause
  IJG
)
depends=(glibc)
makedepends=(
  cmake
  ninja
  nasm
  strip-nondeterminism
)
makedepends_powerpc64=(
  'java-environment=17'
)
makedepends_powerpc64le=(
  'java-environment>=19'
)
makedepends_riscv64=(
  'java-environment>=19'
)
makedepends_x86_64=(
  'java-environment>=19'
)
optdepends_powerpc64=('java-runtime>=17: for TurboJPEG Java wrapper')
optdepends_powerpc64le=('java-runtime>=19: for TurboJPEG Java wrapper')
optdepends_riscv64=('java-runtime>=19: for TurboJPEG Java wrapper')
optdepends_x86_64=('java-runtime>=19: for TurboJPEG Java wrapper')
provides=(
  libjpeg
  libjpeg.so
  libturbojpeg.so
)
source=(
  $_url/releases/download/$pkgver/$pkgname-$pkgver.tar.gz{,.sig}
)
sha512sums=('04d37d65131f7ab8586507fe6520452e791e9b8d72a08cf52f81279450b4c6b8a5b49ff3e14e358d3c32cb5666b6d4576d1e16895af8213a1761464de5dc9d31'
            'SKIP')
b2sums=('cf951582ce7cdf0dce39075bb1cc58f6a61fa0bdaca4874a4f06a03d2cd11775bc626c8d391d4fc76574d302a781ba47e5c90e2aa2d6a21a8b8d06712437e241'
        'SKIP')
validpgpkeys=('0338C8D8D9FDA62CF9C421BD7EC2DBB6F4DBF434') # The libjpeg-turbo Project (Signing key for official binaries) <information@libjpeg-turbo.org>

build() {
  local cmake_options=(
    -B build
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_INSTALL_LIBDIR=/usr/lib
    -D CMAKE_BUILD_TYPE=None
    -D ENABLE_STATIC=OFF
    -D WITH_JPEG8=ON
    -G Ninja
    -S $pkgname-$pkgver
    -W no-dev
  )

  case "${CARCH}" in
    powerpc)
      cmake_options+=(
        -DWITH_SIMD=0
        -DWITH_JAVA=OFF
      )
    ;;
    powerpc64|riscv64)
      cmake_options+=(
        -DWITH_SIMD=1
        -DWITH_JAVA=ON
      )
    ;;
    *)
      cmake_options+=(
        -DWITH_SIMD=1
        -DWITH_JAVA=ON
      )
    ;;
  esac

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

  case "${CARCH}" in
    powerpc) ;;
    *)
      # cmake creates non-reproducible jars: https://github.com/libjpeg-turbo/libjpeg-turbo/issues/745
      strip-nondeterminism --timestamp "$SOURCE_DATE_EPOCH" build/java/turbojpeg.jar
    ;;
  esac
}

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

package() {
  DESTDIR="$pkgdir" cmake --install build -v

  # header required by some dependents
  # https://bugs.archlinux.org/task/24787
  install -vDm 644 $pkgname-$pkgver/jpegint.h "$pkgdir/usr/include"

  install -vDm 644 $pkgname-$pkgver/LICENSE.md -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $pkgname-$pkgver/README.ijg -t "$pkgdir/usr/share/licenses/$pkgname/"
}
