# Contributor: prspkt <prspkt@protonmail.com>
# Maintainer: prspkt <prspkt@protonmail.com>
pkgname=brotli
pkgver=1.2.0
pkgrel=1
pkgdesc="Generic lossless compressor"
url="https://github.com/google/brotli"
arch="all"
license="MIT"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends_build="cmake samurai"
subpackages="
	$pkgname-doc
	$pkgname-static
	$pkgname-dev
	$pkgname-libs
	"
if [ -z "$BOOTSTRAP" ]; then
	makedepends_host="python3-dev py3-gpep517 py3-setuptools py3-wheel"
	subpackages="$subpackages py3-$pkgname-pyc py3-$pkgname:py3"
fi
source="$pkgname-$pkgver.tar.gz::https://github.com/google/brotli/archive/refs/tags/v$pkgver.tar.gz
	optimize-mips.patch
	fix-lp64.patch
	"

# secfixes:
#   1.0.9-r0:
#     - CVE-2020-8927

prepare() {
	default_prepare
	sed -i 's,/usr/bin/env bash,/bin/sh,' tests/*.sh
}

build() {
	export CFLAGS="$CFLAGS -flto=auto -O2"

	# static libs, see https://github.com/google/brotli/issues/795
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DBUILD_SHARED_LIBS=OFF
	cmake --build build

	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DBUILD_SHARED_LIBS=ON
	cmake --build build

	if [ -z "$BOOTSTRAP" ]; then
		gpep517 build-wheel \
			--wheel-dir .dist \
			--output-fd 3 3>&1 >&2
	fi
}

check() {
	ctest --test-dir build

	python3 -m venv --clear --without-pip --system-site-packages .testenv
	.testenv/bin/python3 -m installer .dist/*.whl
	cd python; ../.testenv/bin/python3 -m unittest discover -v -p '*_test.py'
}

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

	local file; for file in common dec enc; do
		install -D -m 755 "$builddir"/build/libbrotli$file.a \
			"$pkgdir"/usr/lib/
	done

	local man; for man in docs/*.?; do
		install -D -m644 $man "$pkgdir"/usr/share/man/man${man##*.}/${man##*/}
	done

	if [ -z "$BOOTSTRAP" ]; then
		python3 -m installer -d "$pkgdir" .dist/*.whl
	fi
}

py3() {
	pkgdesc="$pkgdesc (python bindings)"

	amove usr/lib/python3*
}

sha512sums="
f94542afd2ecd96cc41fd21a805a3da314281ae558c10650f3e6d9ca732b8425bba8fde312823f0a564c7de3993bdaab5b43378edab65ebb798cefb6fd702256  brotli-1.2.0.tar.gz
73b1fc67d7680d9841d90be020d224b158bab047e0641d129ce3f9cafe0d39a375b803ace3b3e0e0dc94b982dc566367b2c1407c120edaa32ab8f3bdf83740d7  optimize-mips.patch
6fa19d521a50ba473be2b2cd6b13c3f9b9b988420b51a515823bd61db1b5142b51d2043c394feb815ba3ddab6e6cdc7b9c49acfe6efab59a58714cf29532fe6d  fix-lp64.patch
"
