# Template file for 'python3-numpy'
pkgname=python3-numpy
version=1.25.2
revision=1
build_style=python3-module
hostmakedepends="python3-setuptools python3-Cython gcc-fortran"
makedepends="python3-devel
 $(vopt_if openblas 'openblas-devel' 'lapack-devel cblas-devel')"
depends="python3-setuptools"
checkdepends="python3-pytest-xdist python3-hypothesis python3-pytz"
short_desc="Fast and sophisticated array facility to Python3"
maintainer="Andrew J. Hesford <ajh@sideband.org>"
license="BSD-3-Clause"
homepage="https://www.numpy.org/"
changelog="https://numpy.org/doc/stable/release.html"
distfiles="${PYPI_SITE}/n/numpy/numpy-${version}.tar.gz"
checksum=fd608e19c8d7c55021dffd43bfe5492fab8cc105cc8986f813f8c3c048b38760
alternatives="numpy:f2py:/usr/bin/f2py3"

build_options="openblas"

case "$XBPS_TARGET_MACHINE" in
	x86_64*|i686*|aarch64*|armv[67]*|ppc64*)
		# Prefer accelerated routines where available
		build_options_default="openblas"
		;;
	*) ;;
esac

if [ "$build_option_openblas" ]; then
	case "$XBPS_TARGET_MACHINE" in
		ppc64*) ;;
		ppc*) broken="segfaults";;
	esac
fi

# SVML AVX-512 functions have very limited support; disble for now
export NPY_DISABLE_SVML=1

post_patch() {
	case "${XBPS_TARGET_MACHINE}" in
		armv5tel-musl)
			cp "${FILESDIR}/fenv-constants.h" numpy/core/src/npymath/
			patch -Np0 -i "${FILESDIR}/fenv-constants.patch"
			;;
	esac

	if [ "$XBPS_TARGET_LIBC" = musl ] ; then
		vsed -i 's|"backtrace",||' numpy/core/setup_common.py
	fi
}

pre_build() {
	# Build numpy in parallel
	make_build_args+=" ${makejobs}"

	# Find the right linear algebra subroutines on the target arch
	: > site.cfg
	for _blaslib in $(vopt_if openblas openblas "lapack blas"); do
		cat >> site.cfg <<-EOF
		[$_blaslib]
		libraries = ${_blaslib}
		include_dirs = ${XBPS_CROSS_BASE}/usr/include
		library_dirs = ${XBPS_CROSS_BASE}/usr/lib
		runtime_library_dirs = ${XBPS_CROSS_BASE}/usr/lib
		EOF
	done
}

do_check() {
	local testjobs="-n $XBPS_MAKEJOBS"
	local _skip

	case $XBPS_TARGET_MACHINE in
		# this triggers a known bug in musl libm, fixed in:
		# https://git.musl-libc.org/cgit/musl/commit/?id=aa2d23e57c9c95f0ffeb80cb035e5a5be52d8ef0
		*-musl) _skip+="or(test_against_cmath)" ;;
	esac

	if [ ${XBPS_TARGET_MACHINE%-musl} = "i686" ]; then
		# see: https://github.com/numpy/numpy/issues/18388
		_skip+="or(test_float_remainder_overflow)"
		_skip+="or(test_identityless_reduction_huge_array)"
		_skip+="or((TestKind)and(test_int))"
		# these 3 fail in CI but not locally
		_skip+="or(test_einsum_sums_int8)"
		_skip+="or(test_einsum_sums_uint8)"
		_skip+="or(test_einsum_sums_int16)"
	fi

	echo python3 runtests.py -- ${testjobs} ${_skip+-k "not(${_skip#or})"}
	python3 runtests.py -- ${testjobs} ${_skip+-k "not(${_skip#or})"}
}

post_install() {
	rm ${DESTDIR}/usr/bin/f2py

	rm ${DESTDIR}/${py3_sitelib}/numpy/LICENSE.txt
	vlicense LICENSE.txt LICENSE
}
