# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ngtcp2
pkgver=1.9.1
pkgrel=0
pkgdesc="Implementation of IETF QUIC protocol"
url="https://nghttp2.org/ngtcp2/"
arch="all"
license="MIT"
makedepends="
	cmake
	gnutls-dev
	samurai
	"
checkdepends="cunit-dev"
subpackages="
	$pkgname-dev
	$pkgname-gnutls
	"
source="https://github.com/ngtcp2/ngtcp2/releases/download/v$pkgver/ngtcp2-$pkgver.tar.xz"

build() {
	# Build static library separately w/o LTO for compatibility with non-gcc
	# and minimal size.
	_build build-static -DENABLE_SHARED_LIB=OFF

	# NOTE: -DINTERPROCEDURAL_OPTIMIZATION=ON doesn't work here.
	export CFLAGS="$CFLAGS -flto=auto"
	_build build-shared -DENABLE_STATIC_LIB=OFF
}

_build() {
	local outdir="$1"; shift

	local crossopts=
	[ "$CBUILD" != "$CHOST" ] \
		&& crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"

	cmake -B "$outdir" -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DENABLE_GNUTLS=ON \
		-DENABLE_OPENSSL=OFF \
		$crossopts \
		"$@"
	cmake --build "$outdir"
}

check() {
	cmake --build build-static -t check
}

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

	rm -rf "$pkgdir"/usr/share/doc
}

gnutls() {
	pkgdesc="$pkgdesc - GnuTLS backend"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/lib/libngtcp2_crypto_gnutls.so*
}

sha512sums="
2f1ea7c97b248cbcfc2d616c2285e8d105176d94642649e4629fba279ebdd92424bcc6a96517e17e12005cb64bc2e9bbc0c54bb9da7f49337b6d8257e5be963b  ngtcp2-1.9.1.tar.xz
"
