# Template file for 'python3-numpy'
pkgname=python3-numpy
version=2.2.3
revision=1
build_style=python3-pep517
build_helper="meson qemu"
make_build_args="-Csetup-args=-Ddisable-svml=true
 $(vopt_if openblas "" "-Csetup-args=-Dblas=blas -Csetup-args=-Dlapack=lapack")
"
hostmakedepends="python3-meson-python python3-pyproject-metadata
 python3-setuptools pkg-config python3-Cython gcc-fortran"
makedepends="python3-devel
 $(vopt_if openblas 'openblas-devel' 'lapack-devel cblas-devel')"
depends="python3"
checkdepends="python3-pytest-xdist python3-hypothesis python3-pytz meson"
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://github.com/numpy/numpy/releases"
distfiles="${PYPI_SITE}/n/numpy/numpy-${version}.tar.gz"
checksum=dbdc15f0c81611925f382dfa97b3bd0bc2c1ce19d4fe50482cb0ddc12ba30020
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

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
}

do_check() {
	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

	# The NumPy test helper wants to use setuptools to deploy a test
	# installation, which installs a broken f2py. Test against the wheel.
	local testdir="${wrksrc}/.xbps-testdir/$(date +%s)"
	python3 -m installer --destdir "${testdir}" dist/numpy*.whl

	cd "${testdir}"
	PATH="${testdir}/usr/bin:${PATH}" PYTHONPATH="${testdir}/${py3_sitelib}" \
		python3 <<-EOF
			import numpy
			numpy.test(
			    extra_argv=[
			        '-n', '${XBPS_MAKEJOBS}',
			        ${_skip+'-k', 'not(${_skip#or})'}
			    ],
			)
			EOF
}

post_install() {
	mv ${DESTDIR}/usr/bin/f2py{,3}
	vlicense LICENSE.txt LICENSE
}
