#!/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 )

# stats/tests/test_continuous_basic.py::test_cont_basic[500-200-ncf-arg74] test fails with IntegrationWarning
# https://github.com/scipy/scipy/issues/14519
SKIP_TEST_LIST += "test_cont_basic[500-200-ncf-arg74]"

# Bug#1017862 TestMLS fails due to broken pythran support on 32 bit arches
ifneq (,$(findstring $(DEB_HOST_ARCH_BITS), 32))
    SKIP_TEST_LIST += test_mls_inputs test_mls_output
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
ifneq (,$(findstring $(DEB_HOST_ARCH), mips64el))
    SKIP_TEST_LIST += test_first_step test_classes
endif


%:
	dh $@ --with python3,sphinxdoc --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_auto_configure:
	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)

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
