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

%:
	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; \
	for py in $(PY3VERS); do \
	  PYLIBPATH=`pybuild --print build_dir -p$$py | awk '{print $$3}'`; \
	  cd /tmp; \
	  PYTHONPATH=$$PYLIBPATH python$$py -c "import scipy; exit( not scipy.test() )"; \
	  cd $(CURDIR); \
	done
endif

execute_after_dh_install:
	dh_numpy3
