# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Nicolas Lorin <androw95220@gmail.com>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=mimalloc1
_projname=mimalloc
pkgver=1.9.3
pkgrel=1
_pkgdesc="A compact general purpose allocator with excellent performance"
pkgdesc="$_pkgdesc (in secure mode)"
url="https://github.com/microsoft/mimalloc"
# ppc64le: tests segfault in secure mode
arch="all !ppc64le"
license="MIT"
makedepends="cmake samurai"
provides="mimalloc=$pkgver-r$pkgrel"
subpackages="
	$pkgname-dev
	$pkgname-insecure
	$pkgname-debug
	"
source="https://github.com/microsoft/mimalloc/archive/v$pkgver/$_projname-$pkgver.tar.gz
	remove-include-linux-prctl-h.patch
	cmake-add-insecure-suffix.patch
	fix-format-specifier.patch
	"
builddir="$srcdir/$_projname-$pkgver"

_buildtypes='debug insecure secure'

build() {
	# https://github.com/microsoft/mimalloc/issues/597
	export CFLAGS="$CFLAGS -D__USE_ISOC11"
	_build out/debug -DCMAKE_BUILD_TYPE=Debug -DMI_DEBUG_FULL=ON

	# This is called "release" build by upstream.
	_build out/insecure -DCMAKE_BUILD_TYPE=Release -DMI_SECURE=OFF

	_build out/secure -DCMAKE_BUILD_TYPE=Release -DMI_SECURE=ON
}

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

	local crossopts=
	[ "$CBUILD" = "$CHOST" ] \
		|| crossopts='-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux'

	CFLAGS=${CFLAGS/-Os/} cmake -B "$outdir" -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DMI_INSTALL_TOPLEVEL=ON \
		$crossopts \
		"$@" .
	cmake --build "$outdir"
}

check() {
	local btype; for btype in $_buildtypes; do
		msg "Testing $btype build"

		cd "$builddir"/out/$btype
		ctest --timeout 300
	done
}

package() {
	local btype; for btype in $_buildtypes; do
		DESTDIR="$pkgdir" cmake --install out/$btype
	done

	cd "$pkgdir"
	ln -s libmimalloc-secure.so usr/lib/libmimalloc.so
	ln -s libmimalloc-secure.so.${pkgver%%.*} usr/lib/libmimalloc.so.${pkgver%%.*}
	ln -s libmimalloc-secure.so.${pkgver%.*} usr/lib/libmimalloc.so.${pkgver%.*}
	ln -s libmimalloc-secure.a usr/lib/libmimalloc.a

	sed 's/-secure/-insecure/g' usr/lib/cmake/mimalloc/mimalloc-release.cmake \
		> usr/lib/cmake/mimalloc/mimalloc-insecure.cmake
	mv usr/lib/cmake/mimalloc/mimalloc-release.cmake usr/lib/cmake/mimalloc/mimalloc-secure.cmake
	ln -s mimalloc-secure.cmake usr/lib/cmake/mimalloc/mimalloc-release.cmake

	rm -f usr/lib/*.o
}

dev() {
	provides="mimalloc-dev=$pkgver-r$pkgrel"

	default_dev
}

insecure() {
	pkgdesc="$_pkgdesc (in insecure mode)"
	provides="mimalloc-insecure=$pkgver-r$pkgrel"

	amove usr/lib/libmimalloc-insecure.*
}

debug() {
	pkgdesc="$_pkgdesc (debug build)"
	provides="mimalloc-debug=$pkgver-r$pkgrel"
	options="!strip"

	amove usr/lib/libmimalloc-debug.*
}

sha512sums="
d4a4aaaedf842582e5e6e8091d2d81ceeb29f3dfc9bf0a44e33a9f96c11f01a81dfaf8913d0b48dbf439ac595e37dee61b61cc70a89425347c0fe06bf1548dad  mimalloc-1.9.3.tar.gz
7015d5123280639f18b3a07b007de43bf3cb60009d144dcb6f9d027ba14b35a18ece5f8c21b3c63723e2f05b86c3ea5b64e521d61b54b1e5c885934904cbd63a  remove-include-linux-prctl-h.patch
8b0542f0b5992cdc1423345ba980ead00ba89784b6608716bbc67587560a5782325af871d4084abf2b8f38abe3ac008ae733dbde0f5d84344689739cb49a1aff  cmake-add-insecure-suffix.patch
7d0944915a9b6c2fb85f80e73bc7a01e9125238e0aba7458d69550d77e23a4734fb447673d63dc6b59e17bdc4618280ed9af1c0603bd34394f0ad2b3cfc64876  fix-format-specifier.patch
"
