#!/usr/bin/make -f

export PYBUILD_NAME=scipy

include /usr/share/dpkg/architecture.mk

unexport LDFLAGS
export FFLAGS="-fPIC"
export ATLAS=None
export PATH := $(CURDIR)/debian/extra_bin:$(PATH)
export SCIPY_USE_PYTHRAN=0

PY3VERS:= $(shell py3versions -v -s)
PY3_DEFAULT:= $(shell py3versions -dv)
TMPDIR := $(CURDIR)/build/tmp
BASE=$(shell pwd)/debian

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  export NPY_NUM_BUILD_JOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

export SCIPY_USE_PYTHRAN = $(shell if dpkg-query -s python3-pythran >/dev/null 2>/dev/null; then echo 1; else echo 0; fi )

# specify build against generic BLAS/LAPACK. Preferred optimised implementation should be installed
# by the end user, not by the package build.
export PYBUILD_BUILD_ARGS := -Csetup-args=-Dblas=blas -Csetup-args=-Dlapack=lapack

# stats/tests/test_continuous_basic.py::test_cont_basic[500-200-ncf-arg74] test fails with IntegrationWarning
# https://github.com/scipy/scipy/issues/14519
# test_data (pooch) needs internet or cached data files
SKIP_TEST_LIST += test_cont_basic[500-200-ncf-arg74] test_existence_all test_ascent test_face test_electrocardiogram

ifneq (,$(findstring $(DEB_HOST_ARCH), arm64))
    SKIP_TEST_LIST += test_maxiter_worsening[lgmres]
endif

# linalg/tests/test_decomp.py started segfaulting on armel
ifneq (,$(findstring $(DEB_HOST_ARCH), armel))
    SKIP_TEST_LIST += test_decomp test_random_exact test_brunnermunzel_normal_dist
endif

# some tests broken on i386, see https://github.com/scipy/scipy/issues/17839
ifneq (,$(findstring $(DEB_HOST_ARCH), i386))
    SKIP_TEST_LIST += test_find_peaks_exact test_find_peaks_withnoise test_x0_equals_Mb[bicgstab] test_rv_sample
    SKIP_TEST_LIST += test_location_scale[pdf test_examples[True-float64] test_pdf_nolan_samples[pct_range0-alpha_range0-beta_range0]
endif

# Bug#1017864 test_kolmogorov.py segfaults on i386 (including hurd)
SKIP_KOLMOGOROV_ARCH_LIST := i386 hurd-i386
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(SKIP_KOLMOGOROV_ARCH_LIST)$(space)))
  SKIP_TEST_LIST += TestSmirnovp
endif

# integrate/_ivp/tests/test_ivp.py fail on mips64el
# see https://github.com/scipy/scipy/issues/16883
# https://github.com/scipy/scipy/issues/17839#issuecomment-1400636840
ifneq (,$(findstring $(DEB_HOST_ARCH), mips64el))
    SKIP_TEST_LIST += test_first_step test_classes test_integrality test_integrator test_double_integrator
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), mipsel))
    SKIP_TEST_LIST += test_NaN_warnings test_moments_warnings test_fht_special_cases test_integrality test_integrator test_double_integrator
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), hppa))
    SKIP_TEST_LIST += test_nodata test_cont_basic[500-200-semicircular-arg89]
endif

# powerpc: failures in stats/tests/test_stats.py: overflow encountered in _hypergeom_*
ifneq (,$(findstring $(DEB_HOST_ARCH), powerpc))
    SKIP_TEST_LIST += test_hypergeom_cdf test_hypergeom_sf test_nch_hypergeom test_precision test_precise test_less_greater test_maxiter_worsening[lgmres]
endif

# test_atol[bicg] only just misses tolerance on s390x, but the design of the test makes it difficult to workaround,
# so skip. See https://github.com/scipy/scipy/issues/17839#issuecomment-1399477182
ifneq (,$(findstring $(DEB_HOST_ARCH), s390x))
    SKIP_TEST_LIST += test_maxiter_worsening test_atol[bicg]
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), hurd-i386))
    SKIP_TEST_LIST += test_mip1 test_find_peaks_exact test_find_peaks_withnoise test_examples[True-float64] test_rv_sample test_location_scale[pdf
    SKIP_TEST_LIST += test_pdf_nolan_samples[pct_range0-alpha_range0-beta_range0]
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), ia64))
    SKIP_TEST_LIST += test_maxiter_worsening[lgmres]
endif

# https://github.com/scipy/scipy/issues/17839#issuecomment-1399478853
ifneq (,$(findstring $(DEB_HOST_ARCH), ppc64))
    SKIP_TEST_LIST += test_precond_dummy test_maxiter_worsening[lgmres]
endif

# https://github.com/scipy/scipy/issues/17839#issuecomment-1401780853
ifneq (,$(findstring $(DEB_HOST_ARCH), riscv64))
    SKIP_TEST_LIST += test_maxiter_worsening[lgmres]
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), x32))
    SKIP_TEST_LIST += test_input_validation test_basic test_sign test_flexible_input \
      test_perm_discrepancy test_integers_nd test_0dim test_0sample
endif

%:
	dh $@ --buildsystem=pybuild

execute_after_dh_auto_clean:
	rm -rf build .pybuild
	-find $(CURDIR) -name __pycache__ -type d -exec rm -rf {} +
	-rm -rf doc/source/fontList*
	-rm -rf doc/source/reference/generated
	-rm -rf doc/build

execute_before_dh_clean:
	# make sure symlinks to submodules are cleaned up or empty directories from tarball are removed
	rm -rf doc/source/_static/scipy-mathjax
	rm -rf scipy/_lib/boost
	rm -rf scipy/_lib/highs
	rm -rf scipy/_lib/unuran
	rm -rf scipy/sparse/linalg/_propack/PROPACK

execute_before_dh_auto_configure:
	echo "Symlink to submodules"
	ln -sf ../../../submodules/doc/source/_static/scipy-mathjax		doc/source/_static/scipy-mathjax
	ln -sf ../../submodules/scipy/_lib/boost				scipy/_lib/boost
	ln -sf ../../submodules/scipy/_lib/highs				scipy/_lib/highs
	ln -sf ../../submodules/scipy/_lib/unuran				scipy/_lib/unuran
	ln -sf ../../../../submodules/scipy/sparse/linalg/_propack/PROPACK	scipy/sparse/linalg/_propack/PROPACK
	echo "Building scipy with SCIPY_USE_PYTHRAN=$(SCIPY_USE_PYTHRAN)"

execute_before_dh_installdocs-indep:
	(export MPLCONFIGDIR=. ; \
	PYLIBPATH=`pybuild --print build_dir -p$(PY3_DEFAULT) | awk '{print $$3}'`; \
	echo "building docs using PYLIBPATH=$$PYLIBPATH"; \
	PYTHONPATH=$$PYLIBPATH make -C doc html PYTHONPATH=$$PYLIBPATH PYVER=3) || /bin/true

override_dh_auto_test:
	echo "Build-time tests will be run after the installation dir is installed"

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
execute_after_dh_auto_install:
	set -e; \
	SKIP_TESTS=""; \
	list_initialised=0; \
	for t in $(SKIP_TEST_LIST); do \
	    if [ $${list_initialised} = 0 ]; then \
	        SKIP_TESTS=$$t; \
	        list_initialised=1; \
	    else \
	        SKIP_TESTS="$${SKIP_TESTS} or $$t"; \
	    fi; \
	done; \
	if [ "x$${SKIP_TESTS}" != "x" ]; then \
	    SKIP_TESTS="not ( $${SKIP_TESTS} )"; \
	fi; \
	echo "skipping tests: $${SKIP_TESTS}"; \
	for py in $(PY3VERS); do \
	  PYLIBPATH=`pybuild --print build_dir -p$$py | awk '{print $$3}'`; \
	  PYTHONPATH=$$PYLIBPATH python$$py \
	        $(CURDIR)/runtests.py -n -- -k "$${SKIP_TESTS}"; \
	done
endif

execute_after_dh_install:
	dh_numpy3


# meson build is still work-in-progress, installs residual cython (pyx) files
# See cython_special comments in scipy/special/meson.build
# and https://github.com/scipy/scipy/issues/14890
# In the meantime clean up python3.X (not python3) files left after dh_python3
execute_after_dh_python3:
	rm -rf debian/python3-scipy/usr/lib/python$(PY3_DEFAULT)
