#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

# The following tests are problematic and skipped:
# - Meson's python module always returns the default interpreter, which makes
#   Cython related tests fail on non-default python versions
# - numpy.distutils is still shipped because of Python 3.11, which makes the
#   test_all_modules_are_expected test fail with Python 3.12+ (where it is
#   supposed to be dropped).
# - Some f2py tests have architecture-specific failures. Further investigation
#   is needed.
# - There seems to be an issue with floating point exceptions on armhf/armel
SKIP_TESTS = \
	cython \
	test_all_modules_are_expected
ifeq ($(filter amd64 arm64 ppc64el,$(DEB_HOST_ARCH)),)
SKIP_TESTS += f2py
endif
ifneq ($(filter riscv64 armhf armel,$(DEB_HOST_ARCH)),)
SKIP_TESTS += FPClass test_float
endif
ifneq ($(filter armhf armel,$(DEB_HOST_ARCH)),)
SKIP_TESTS += error exception warning fpe overflow \
	SpecialFloats \
	test_divide_err \
	test_errobj \
	test_nonarray_assignment \
	test_zero_power_nonzero \
	test_log2_special \
	test_special_values \
	test_empty \
	test_where \
	test_features
endif
ifneq ($(filter ppc64 s390x,$(DEB_HOST_ARCH)),)
SKIP_TESTS += test_impossible_feature_enable
endif

export PYBUILD_TEST_CUSTOM = 1
export PYBUILD_TEST_ARGS = {interpreter} {dir}/runtests.py --no-build --verbose --pythonpath {build_dir} \
	-- $(if $(SKIP_TESTS),-k 'not ($(call concat_with,$(space)or$(space),$(SKIP_TESTS)))')

ifneq ($(DEB_HOST_ARCH),$(DEB_BUILD_ARCH))
export PYBUILD_BEFORE_BUILD = meson env2mfile --debarch $(DEB_HOST_ARCH) --cross -o {dir}/debian/meson-cross.txt \
	&& {dir}/debian/add_longdouble_format_property.py {dir}/debian/meson-cross.txt $(DEB_HOST_ARCH)
export PYBUILD_BUILD_ARGS = --config-setting setup-args=--cross-file={dir}/debian/meson-cross.txt
export PYBUILD_AFTER_BUILD = rm {dir}/debian/meson-cross.txt
endif

# Helper variables
PY3VERS = $(shell py3versions -vr)
STAGING_DIR = $(CURDIR)/debian/python3-numpy
space = $(eval) $(eval)
concat_with = $(subst $(space),$1,$2)

%:
	dh $@ --buildsystem=pybuild

execute_after_dh_python3:
	# tweak the entry_points console_scripts list to include all supported versions
	if [ -f $(STAGING_DIR)/usr/lib/python3/dist-packages/numpy-*.dist-info/entry_points.txt ] ; then \
		ENTRYPOINT=$(shell ls $(STAGING_DIR)/usr/lib/python3/dist-packages/numpy-*.dist-info/entry_points.txt) ; \
		python3 debian/edit_console_scripts.py $$ENTRYPOINT "f2py3" "numpy.f2py.f2py2e:main" ; \
		for v in $(PY3VERS); do \
			python3 debian/edit_console_scripts.py $$ENTRYPOINT "f2py$$v" "numpy.f2py.f2py2e:main" ; \
		done \
	fi
	rm -rf $(STAGING_DIR)/usr/lib/python3.*

execute_before_dh_install:
	# add versioned f2pyX.Y scripts
	cp -a $(STAGING_DIR)/usr/bin/f2py $(STAGING_DIR)/usr/bin/f2py3
	# Create wrappers to run python$$i without dh-python generating a
	# hard dependency on all of those interpreters (#945824)
	set -e; for v in $(PY3VERS); do \
		sed -e "s,@VERSION@,$$v,g" debian/f2py.in > $(STAGING_DIR)/usr/bin/f2py$$v; \
		chmod 0755 $(STAGING_DIR)/usr/bin/f2py$$v; \
	done
	# add numpy.pc backported from NumPy 2.0 (cf bug #1067398)
	mkdir -p $(STAGING_DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	sed -e  "s,@VERSION@,$(DEB_VERSION_UPSTREAM),g" debian/numpy.pc.in > $(STAGING_DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/numpy.pc

execute_before_dh_gencontrol:
	python3 debian/versions3.helper >> debian/python3-numpy.substvars

