# Template file for 'tbb'
pkgname=tbb
reverts="2021.5.0_1"
version=2020.U3
revision=2
# version rewrite: 2017_U7 (upstream) => 2017.7 (xbps)
_ver=${version/./_}
build_style=gnu-makefile
hostmakedepends="cmake"
short_desc="Intel Threading Building Blocks"
maintainer="Andrea Brancaleoni <abc@pompel.me>"
license="Apache-2.0"
homepage="https://www.threadingbuildingblocks.org"
distfiles="https://github.com/oneapi-src/oneTBB/archive/${_ver}.tar.gz"
checksum=2103cc6238c935664f87680618f6684d57501d4a2fa8ea8f6c97ad6ff7dc722a

case "$XBPS_TARGET_MACHINE" in
	x86_64*)
		make_build_args="arch=intel64"
		;;
	i686*)
		make_build_args="arch=ia32"
		;;
	armv7l*)
		make_build_args="arch=armv7"
		;;
	aarch64*)
		make_build_args="arch=arm64"
		;;
	ppc64*)
		make_build_args="arch=ppc64"
		;;
	ppc*)
		make_build_args="arch=ppc32"
		;;
	*)
		# make sure to fall back instead of letting host uname control it
		make_build_args="arch=generic"
		;;
esac

post_extract() {
	sed -i 's|CPLUS = g..|CPLUS = $(CXX)|g' build/linux.gcc.inc
	sed -i 's|CONLY = gcc|CONLY = $(CC)|g' build/linux.gcc.inc

	# alternative might be:
	# https://git.alpinelinux.org/cgit/aports/tree/testing/libtbb/glibc-struct-mallinfo.patch
	if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
		vsed -e "s@#define MALLOC_UNIXLIKE_OVERLOAD_ENABLED __linux__@@" \
		  -i src/tbbmalloc/proxy.h
	fi
}

do_install() {
	vmkdir usr/lib
	vcopy build/linux_*/*.so* usr/lib
	vmkdir usr/include/tbb
	vcopy include/tbb/* usr/include/tbb

	cmake \
		-DINSTALL_DIR=$DESTDIR/usr/lib/cmake/TBB \
		-DSYSTEM_NAME=Linux \
		-DLIB_REL_PATH=../.. \
		-DINC_REL_PATH=../../../include \
		-P cmake/tbb_config_installer.cmake
}

tbb-devel_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.so"
		vmove usr/lib/cmake
	}
}
