#!/usr/bin/make -f

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

# Large file support actually works, but some buildds and Salsa do not like us
# creating 5 GB files.
SKIP_TESTS = \
	test_big_arrays \
	test_large_file_support \
	test_configtool
# Some tests have architecture-specific issues. Generally, these are related to
# unexpected behavior of floating point operations (unexpected for the unit
# tests, that is).
ifneq ($(filter armel armhf,$(DEB_HOST_ARCH)),)
SKIP_TESTS += \
	FPClass \
	SpecialFloats \
	TestBoolCmp \
	error \
	except \
	overflow \
	test_denormal_numbers \
	test_divide_err \
	test_empty \
	test_exotic \
	test_features \
	test_half_fpe \
	test_linear_interpolation_formula_symmetric \
	test_log2_special \
	test_nonarray_assignment \
	test_special_values \
	test_where \
	test_zero_power_nonzero
endif
# The huge vector exceeds the available address space on 32 bit architectures
ifeq ($(DEB_HOST_ARCH_BITS),32)
SKIP_TESTS += \
	test_huge_vectordot
endif
ifneq ($(filter armel,$(DEB_HOST_ARCH)),)
SKIP_TESTS += \
	test_dot_errstate
endif
ifneq ($(filter mips64el,$(DEB_HOST_ARCH)),)
SKIP_TESTS += \
	NanFunctions \
	SpecialComplex \
	SpecialFloats \
	error \
	test_reduce
endif
ifneq ($(filter alpha mips64el powerpc ppc64 sparc64,$(DEB_HOST_ARCH)),)
SKIP_TESTS += \
	sq_cases
endif
ifneq ($(filter hppa powerpc,$(DEB_HOST_ARCH)),)
SKIP_TESTS += \
	ctypeslib \
	test_dtype
endif
ifneq ($(filter powerpc,$(DEB_HOST_ARCH)),)
SKIP_TESTS += \
	test_linalg
endif
ifneq ($(filter ppc64el,$(DEB_HOST_ARCH)),)
SKIP_TESTS += \
    test_cpu_features \
    test_ppc64_ibm_double_double128 \
    test_linear_interpolation_formula_symmetric
endif
ifneq ($(filter s390x,$(DEB_HOST_ARCH)),)
SKIP_TESTS += \
    test_cpu_features
endif
ifeq ($(PYBUILD_AUTOPKGTEST),1)
SKIP_TESTS += test_dict_mode
endif

export PYBUILD_BUILD_ARGS = --config-setting build-dir={dir}/.mesonpy
export PYBUILD_TEST_ARGS = $(if $(SKIP_TESTS),-k 'not ($(call concat_with,$(space)or$(space),$(SKIP_TESTS)))')
ifeq ($(PYBUILD_AUTOPKGTEST),1)
PYBUILD_TEST_ARGS += /usr/lib/python3/dist-packages/numpy
endif

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

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

%:
	dh $@ --buildsystem=pybuild

auto-build-for-default-python:
	PYBUILD_DISABLE="$(filter-out $(PY3DEF),$(PY3VERS))" dh_auto_build
	touch $@

auto-build-for-other-python:
	PYBUILD_DISABLE="$(PY3DEF)" dh_auto_build
	touch $@

override_dh_auto_build-arch: auto-build-for-default-python auto-build-for-other-python
	true

override_dh_auto_build-indep: auto-build-for-default-python
	$(MAKE) -C doc html \
		GITVER=Unknown \
		MPLCONFIGDIR=. \
		SPHINXOPTS= \
		PYTHON=python3 \
		PYTHONPATH=$(shell pybuild --print {build_dir} --interpreter python3)

# Nothing to install for -indep
override_dh_auto_install-indep:

# No tests to run for the -doc package
override_dh_auto_test-indep:

override_dh_auto_test-arch:
	dh_auto_test --arch -O--buildsystem=pybuild

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

MULTIARCH_TREE_DIR = $(STAGING_DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/python3-numpy/numpy
execute_before_dh_install-arch:
	# 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
	# Remove various LICENSE files from inconvienient locations
	find $(STAGING_DIR)/usr/lib -name "LICENSE*" -delete
	# Move NumPy headers to Multi-Arch friendly location
	mkdir -p $(MULTIARCH_TREE_DIR)/_core $(MULTIARCH_TREE_DIR)/random $(MULTIARCH_TREE_DIR)/f2py
	mv $(STAGING_DIR)/usr/lib/python$(PY3DEF)/dist-packages/numpy/_core/include $(MULTIARCH_TREE_DIR)/_core
	mv $(STAGING_DIR)/usr/lib/python$(PY3DEF)/dist-packages/numpy/_core/lib $(MULTIARCH_TREE_DIR)/_core
	mv $(STAGING_DIR)/usr/lib/python$(PY3DEF)/dist-packages/numpy/random/lib $(MULTIARCH_TREE_DIR)/random
	mv $(STAGING_DIR)/usr/lib/python$(PY3DEF)/dist-packages/numpy/f2py/src $(MULTIARCH_TREE_DIR)/f2py
	rm -rf $(STAGING_DIR)/usr/lib/python3.*/dist-packages/numpy/_core/include
	rm -rf $(STAGING_DIR)/usr/lib/python3.*/dist-packages/numpy/_core/lib
	rm -rf $(STAGING_DIR)/usr/lib/python3.*/dist-packages/numpy/random/lib
	rm -rf $(STAGING_DIR)/usr/lib/python3.*/dist-packages/numpy/f2py/src
	# Remove build path from __config__.py
	sed -i -e "s;$(CURDIR);\$$BUILDDIR;g" $(STAGING_DIR)/usr/lib/python3*/dist-packages/numpy/__config__.py
	# Remove Python version information from __config__.py
	sed -i -e '/"Python Information": {/,/},/d' $(STAGING_DIR)/usr/lib/python3*/dist-packages/numpy/__config__.py
	# Install numpy.pc for global discovery (without numpy-config shenanigans)
	mkdir -p $(STAGING_DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	sed \
		-e "s,@VERSION@,$(shell echo $(DEB_VERSION_UPSTREAM) | cut -d+ -f1),g" \
		-e "s,@MULTIARCH@,$(DEB_HOST_MULTIARCH),g" \
		debian/numpy.pc.in > $(STAGING_DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/numpy.pc

execute_before_dh_gencontrol:
	python3 debian/helpers/numpy_substvars.py >> debian/python3-numpy.substvars

download_remote_intersphinx_inventories:
	curl https://matplotlib.org/stable/objects.inv -o debian/intersphinx/matplotlib_objects.inv
	curl https://numpydoc.readthedocs.io/en/latest/objects.inv -o debian/intersphinx/numpydoc_objects.inv
	curl https://numpy.org/neps/objects.inv -o debian/intersphinx/neps_objects.inv
	curl https://numpy.org/numpy-tutorials/objects.inv -o debian/intersphinx/numpy-tutorials_objects.inv
	curl https://dmlc.github.io/dlpack/latest/objects.inv -o debian/intersphinx/dlpack_objects.inv
	curl https://scipy-lectures.org/objects.inv -o debian/intersphinx/scipy-lectures_objects.inv
