# Contributor: Leo <thinkabit.ukim@gmail.com>
# Contributor: stef <l0ls0fo2i@ctrlc.hu>
# Contributor: André Klitzing <aklitzing@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=zstd
pkgver=1.5.7
pkgrel=0
pkgdesc="Zstandard - Fast real-time compression algorithm"
url="https://facebook.github.io/zstd/"
arch="all"
license="BSD-3-Clause OR GPL-2.0-or-later"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="grep meson"
checkdepends="file"
subpackages="
	$pkgname-libs
	$pkgname-static
	$pkgname-dev
	$pkgname-doc
	$pkgname-frugal
	"
source="zstd-$pkgver.tar.gz::https://github.com/facebook/zstd/archive/v$pkgver.tar.gz"
# we run the tests in build() for pgo, so it's tested from there.
# options="!check"

# secfixes:
#   1.4.9-r0:
#     - CVE-2021-24032
#   1.4.1-r0:
#     - CVE-2021-24031
#   1.3.8-r0:
#     - CVE-2019-11922

build() {
	export CFLAGS="${CFLAGS/-Os/-O3} -flto=auto -ffat-lto-objects"
	export CXXFLAGS="${CXXFLAGS/-Os/-O3} -flto=auto -ffat-lto-objects"
	export CPPFLAGS="${CPPFLAGS/-Os/-O3} -flto=auto -ffat-lto-objects"
	local pgo=false
	if [ -z "$BOOTSTRAP" ]; then
		pgo=true
	fi
	# 2-stage pgo+lto build (non-bootstrap), standard meson usage.
	# note that with clang,
	#  llvm-profdata merge --output=output/somefilename(?) output/*.profraw
	# is needed.
	# believe it or not, this is +30% faster on x86_64 than the default makefile build (same params)..
	# maybe needs more testing
	# shellcheck disable=2046
	abuild-meson \
		--default-library=both \
		-Db_ndebug=true \
		$($pgo && echo -Db_pgo=generate) \
		-Dbin_contrib=true \
		-Dbin_programs=true \
		-Dbin_tests="$(want_check && echo true || echo false)" \
		-Dbacktrace=disabled \
		-Dmulti_thread=enabled \
		-Dlz4=disabled \
		-Dlzma=disabled \
		-Dzlib=disabled \
		build/meson output

	meson compile -C output

	if $pgo; then
		meson test -t 4 --print-errorlogs -C output
		meson configure -Dbin_tests=false -Db_pgo=use output
		meson compile -C output
	fi
}

check() {
	# ran in build above, via pgo
	return 0
	#meson test -t 4 --print-errorlogs -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output
}

frugal() {
	pkgdesc="$pkgdesc (frugal cli)"

	amove usr/bin/zstd-frugal
}

sha512sums="
26e441267305f6e58080460f96ab98645219a90d290a533410b1b0b1d2f870721c95f8384e342ee647c5e968385a5b7e30c2d04340c37f59b3e6d86762c3260c  zstd-1.5.7.tar.gz
"
