# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Maintainer: Carl Smedstad <carsme@archlinux.org>
# Contributor: Pavel Borzenkov <pavel@voidptr.ru>
# Contributor: aksr <aksr at t-com dot me>

pkgname=criu
pkgver=4.0
pkgrel=2
pkgdesc='Utilities to checkpoint and restore processes in userspace'
arch=(x86_64 powerpc64le riscv64)
url='https://criu.org'
license=(
  'GPL-2.0-only'
  'LGPL-2.1-only'
)
depends=(
  'gcc-libs'
  'glibc'
  'gnutls'
  'libbpf'
  'libbsd'
  'libdrm'
  'libnet'
  'libnl'
  'nftables'
  'protobuf-c'
  'python'
  'python-protobuf'
)
makedepends=(
  'asciidoc'
  'git'
  'python-build'
  'python-installer'
  'python-setuptools'
  'python-wheel'
  'xmlto'
)
# Can't run tests (only unit tests) due to privilege escalation required.
# https://github.com/checkpoint-restore/criu/issues/434
# checkdepends=(
#   'libaio'
#   'python-yaml'
# )
provides=(
  'libcompel.so'
  'libcriu.so'
)
options=('!lto')
source=("git+https://github.com/checkpoint-restore/criu#tag=v$pkgver"
        'add-riscv64-support-2234.patch::https://github.com/checkpoint-restore/criu/pull/2234.patch'
        'add-riscv64-support-2518.patch::https://github.com/checkpoint-restore/criu/pull/2518.patch')
sha512sums=('cdfaebfe37c2e1111383444a0575499bb4a6b7618a944921d8c2bfa7c2a72831557bc568f8457c70ea0d94b0e70549ed5f4d43134e0c58baa5ffab3c1b3e40c6'
            '0b04aa6571b931a09cc9d7110394c7edbf9b829a8e416bc03c56b0e8c2e3db2283d802fb094b3bd09d849a431666deccfbf516863a16bdb5b1ccbbe3ae27a929'
            'e6828b767b35a5f70270f032de85b7d0bc18e07201898d79086ab1b965f9cc48ecc8575f5c783a0662b2a288013108609c1454f94cba3611bc0a16353a0b74f1')
b2sums=('4cf22e25a848007e5c7230aa1a60216f4ba98423be47442a00fd7d5c0079ac86901bfcd5c82d2ee7f4ab243bc1eb431328848317dc24c3c45d81250318bc9e21'
        '74cc6a6ce0ea6de2fcac6b56db6ba4e225c66762d323ec75e5b6ff0aaa7808aaec8c0ccc55a7e201318d0b65692d11cae51ad56f4bc841ada8a6716821ab44c9'
        'a8346ffc94288ada72633dbe4c6b2755eb6b94c9c1b08d2a22089c29592d30ae694f4f7c57d3a93b366eeb6ed13818a0eea386c00ee2b42d945c9f038715e810')

prepare() {
  cd "$pkgname"
  patch -Np1 < ${srcdir}/add-riscv64-support-2234.patch
  patch -Np1 < ${srcdir}/add-riscv64-support-2518.patch
}

build() {
  cd "$pkgname"

  # shellcheck disable=SC2001
  export CFLAGS=$(echo "$CFLAGS" | sed 's/-Wp,-D_FORTIFY_SOURCE=[0-9]//g')
  make criu crit amdgpu_plugin docs
  python -m build --wheel --no-isolation crit
  python -m build --wheel --no-isolation --skip-dependency-check lib
}

check() {
  cd "$pkgname"

  make unittest
  # make test
}

package() {
  cd "$pkgname"

  make \
    DESTDIR="$pkgdir" \
    PREFIX=/usr \
    SBINDIR=/usr/bin \
    LIBDIR=/usr/lib \
    LIBEXECDIR=/usr/lib \
    install
  python -m installer --destdir="$pkgdir" crit/dist/*.whl
  python -m installer --destdir="$pkgdir" lib/dist/*.whl
  # Remove empty directory to silence namcap warning.
  rm -r "$pkgdir/usr/include/compel/common/asm"
}
