# Contributor: Holger Jaekel <holger.jaekel@gmx.de>
# Maintainer: Holger Jaekel <holger.jaekel@gmx.de>
pkgname=basisu
pkgver=1.16.4
pkgrel=1
pkgdesc="Basis Universal GPU Texture Codec"
url="https://github.com/BinomialLLC/basis_universal"
arch="all"
license="Apache-2.0"
makedepends="
	cmake
	opencl-dev
	samurai
	zstd-dev
	"
subpackages="$pkgname-libs $pkgname-dev"
source="basisu-$pkgver.tar.gz::https://github.com/BinomialLLC/basis_universal/archive/refs/tags/$pkgver.tar.gz
	10_library.patch
	20_soversion.patch"
builddir="$srcdir"/basis_universal-$pkgver

build() {
	case "$CARCH" in
	x86*) local sse=ON ;;
	*) local sse=OFF ;;
	esac

	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=ON \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DSSE=$sse \
		$CMAKE_CROSSOPTS
	cmake --build build
}

check() {
	for _filename in test_files/*.png; do
		# To compress a sRGB PNG/BMP/TGA/JPEG image to an ETC1S .KTX2 file:
		build/basisu -ktx2 $_filename

		# To compress a sRGB PNG/BMP/TGA/JPEG image to an UASTC .KTX2 file:
		build/basisu -ktx2 -uastc $_filename

		# To compress a sRGB PNG/BMP/TGA/JPEG image to an RDO UASTC .KTX2 file with mipmaps:
		build/basisu -ktx2 -uastc -uastc_rdo_l 1.0 -mipmap $_filename

		# To compress a sRGB PNG/BMP/TGA/JPEG image to an ETC1S .basis file:
		build/basisu $_filename

		# To compress a image to a higher quality UASTC .basis file:
		build/basisu -uastc -uastc_level 2 $_filename

		# To compress a image to a higher quality UASTC .basis file with RDO post processing,
		# so the .basis file is more compressible:
		build/basisu -uastc -uastc_level 2 -uastc_rdo_l .75 $_filename
	done
}

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

sha512sums="
7f7dd62741b4a3e13050233a2ed751e6108cde9eab7b05ea5882ded6ab49fe181cc30e795cf73f8fa625a71e77ae891fda5ea84e20b632b1397844d6539715b3  basisu-1.16.4.tar.gz
a30d327ad86d723af625613f9e0c8ab81a4ef1cb5d9425f150a7b13845f51cb997a91d5e18cd605759c493a77f6291a516b412f8bf37f774b109bbf3a8d0d550  10_library.patch
139355e155e008ba903bb483cedcca36642f636feafaf72a38e59dd1c871ab004fc1eae50768e0ac267b74c1f083dee24d71b25668818f659532055b2cb16138  20_soversion.patch
"