# Template file for 'python3-scipy'
pkgname=python3-scipy
version=1.11.2
revision=1
build_style=meson
build_helper="python3"
configure_args="$(vopt_if openblas "" "-Dblas=blas -Dlapack=lapack")"
hostmakedepends="python3-build python3-installer python3-meson-python
 python3-wheel python3-Cython python3-pybind11 pythran python3-numpy
 gcc-fortran pkg-config"
makedepends="python3-devel python3-pybind11 python3-numpy pythran
 $(vopt_if openblas openblas-devel "lapack-devel cblas-devel")"
depends="python3-numpy"
short_desc="Scientific library for Python3"
maintainer="Andrew J. Hesford <ajh@sideband.org>"
license="BSD-3-Clause"
homepage="https://scipy.org/"
distfiles="${PYPI_SITE}/s/scipy/scipy-${version}.tar.gz"
checksum=b29318a5e39bd200ca4381d80b065cdf3076c7d7281c5e36569e99273867f61d
make_check="no" # Tests need an installed copy to run and meson makes this tough

build_options="openblas"

if [ "$CROSS_BUILD" ]; then
	_pybind11_dir="${py3_sitelib}/pybind11"
	configure_args+=" --cross-file=python.cross"
	export PKG_CONFIG_PATH="${XBPS_CROSS_BASE}/${_pybind11_dir}/share/pkgconfig"
	# pybind11 uses a path relative to the pkgconfig file to set $prefix,
	# which causes the wrapper to double-include $XBPS_CROSS_BASE; override
	# so that the wrapper properly points to the right location
	export PKG_CONFIG_PYBIND11_PREFIX="/${_pybind11_dir}"
fi

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="numpy can't be built with openblas";;
	esac
fi

pre_patch() {
	if [ "${CROSS_BUILD}" ]; then
		# Meson can't tolerate $CC with arguments as set by the build helper
		CC="${XBPS_CROSS_TRIPLET}-gcc"
		# CXX needs to know where to find Python headers
		CXXFLAGS+=" -I${XBPS_CROSS_BASE}/${py3_inc}"
	fi
}

post_patch() {
	if [ "$CROSS_BUILD" ]; then
		local _xpy="${XBPS_CROSS_BASE}/${py3_sitelib}"
		cat > python.cross <<-EOF
		[properties]
		numpy-include-dir = '${_xpy}/numpy/core/include'
		pythran-include-dir = '${_xpy}/pythran'
		EOF
	fi
}

do_build() {
	# Use the build directory already configured by xbps-src for meson
	python3 -m build --no-isolation --wheel \
		-Cbuilddir="./build" -Ccompile-args="${makejobs}" .
}

do_install() {
	python3 -m installer --destdir "${DESTDIR}" \
		--no-compile-bytecode dist/*.whl
	vlicense LICENSE.txt
}
