# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ngtcp2
pkgver=1.12.0
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-static
	$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="
9d387df7d80665a346b170cf2adc0cc1163ae8d1d921355c1c07ec4e788feb76b9fd681dd606b139b3a2c27f3f0cf3e0a61014b9a3623c4bc06e86d32da0a90b  ngtcp2-1.12.0.tar.xz
"
