#!/usr/bin/make -f
# -*- makefile -*-

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# define ARCH_DEFAULT_MPI_IMPL
include /usr/share/mpi-default-dev/debian_defaults

PY3DEF = $(shell py3versions -vd)
PY3VERS = $(shell py3versions -vs)

LAST_CHANGE = $(shell dpkg-parsechangelog -S Date)
BUILD_DATE  = $(shell LC_ALL=C date -u "+%B %d, %Y" -d "$(LAST_CHANGE)")

export PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe

# avoid network-related (OPAL/libucs) errors, see Bug#1100120
export OMPI_MCA_btl_tcp_if_include=lo

# Specify a specific version of MPI to use in the build, otherwise
# just use the default.
# MPI=.openmpi

export  http_proxy=http://127.0.0.1:9/
export  https_proxy=http://127.0.0.1:9/


# disable build tests for these arches:
# x32 consistently times out on tests
NO_TEST_ARCH_LIST = x32

empty :=
space := $(empty)$(empty)

# run tests, or not
RUNTEST=yes
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(NO_TEST_ARCH_LIST)$(space)))
  RUNTEST=no
endif
ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
  RUNTEST=no
endif

ifeq ($(DEB_HOST_ARCH),mips64el)
# mips64el times out. Run tests on default python only.
    PY3VERS_TESTS = $(PY3DEF)
else
    PY3VERS_TESTS = $(PY3VERS)
endif

# hurd fails a couple of tests
# but hell, hurd needs all the help it can get, so let it pass
ARCH_IGNORE_FAILED_TESTS = hurd-i386

# normally, do not ignore test failures
IGNORE_FAILED_TESTS=/bin/false
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(ARCH_IGNORE_FAILED_TESTS)$(space)))
  IGNORE_FAILED_TESTS=/bin/true
endif

# OpenMPI is triggering both PMIX and spawn errors
CHECK_OPENMPI = $(MPI) $(ARCH_DEFAULT_MPI_IMPL)
ifneq (,$(findstring $(space)openmpi$(space), $(space)$(CHECK_OPENMPI)$(space)))
  # OpenMPI is triggering spawn errors
  # see Bug#1100120
  SKIP_TEST_LIST += test_spawn
endif

# failing RMA tests from test_rma_nb
SKIP_RMA_ACCUMULATE_ARCH_LIST = s390x ppc64 sparc64
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(SKIP_RMA_ACCUMULATE_ARCH_LIST)$(space)))
    SKIP_TEST_LIST += testAccumulate testGetAccumulate
endif

ifeq ($(DEB_HOST_ARCH),mips64el)
    SKIP_TEST_LIST += TestRMA
endif

ifeq ($(DEB_HOST_ARCH),sparc64)
    # OpenMPI broke memory alignment on sparc64, see https://github.com/mpi4py/mpi4py/issues/147
    SKIP_TEST_LIST += testPackUnpackExternal
endif


%:
	dh $@ --with sphinxdoc --buildsystem pybuild

cythonize:
	@echo "D: removing previously generated by Cython sources"
	find -iname *.c | xargs grep -l 'Generated by Cython' | xargs -r rm
	python3 setup.py build_src

override_dh_autoreconf:
	dh_autoreconf debian/rules -- cythonize

override_dh_auto_build-arch:
	dh_auto_build $@ -- \
		--build-args "--mpicc=/usr/bin/mpicc$(MPI) --mpicxx=/usr/bin/mpicxx$(MPI)"

override_dh_auto_build-indep: override_dh_auto_build-arch
	PYTHONPATH=`pybuild -p $(PY3DEF) --print "{build_dir}"` \
	  make -C docs/source/ html man info latexpdf SPHINXOPTS="-D today=\"$(BUILD_DATE)\""

override_dh_auto_install:
	dh_auto_install

	: # Remove python-mpi binaries
	find $(CURDIR)/debian/tmp -name python-mpi -delete
	find $(CURDIR)/debian/tmp -type d -empty -delete

override_dh_install-arch:
	dh_install
	: # create symlinks for .h files
	: # Can have python$$v symlink pointing to python3.?m or python3.?mu
	: # see #700995 for more details.  So first look where it points to
	: # and use that directory
	set -e; for v in $(PY3VERS); do \
	ABITAG=`python$$v -c "import sys; print(sys.abiflags)"`; \
	[ -d $(CURDIR)/debian/python3-mpi4py/usr/include/python$$v$$ABITAG ] || \
		mkdir -p $(CURDIR)/debian/python3-mpi4py/usr/include/python$$v$$ABITAG; \
		dh_link -ppython3-mpi4py usr/lib/python3/dist-packages/mpi4py/include/mpi4py usr/include/python$$v$$ABITAG/mpi4py; \
	done

override_dh_auto_test:
	set -e; \
	if [ "$(RUNTEST)" = "no" ]; then \
	  echo Build tests have been disabled; \
	else \
	SKIP_TESTS=""; \
	list_initialised=0; \
	for t in $(SKIP_TEST_LIST); do \
	    if [ $${list_initialised} = 0 ]; then \
	        SKIP_TESTS="-x $$t"; \
	        list_initialised=1; \
	    else \
	        SKIP_TESTS="$${SKIP_TESTS} -x $$t"; \
	    fi; \
	done; \
	echo "skipping tests: $${SKIP_TESTS}"; \
	  for vv in $(PY3VERS_TESTS); do \
	  echo "I: testing using python$$vv"; \
	   echo "I: running tests with single process"; \
	   PYTHONPATH=`pybuild -p $$vv --print "{build_dir}"` \
	    GITHUB_ACTIONS=true MPI4PY_TEST_SPAWN=false \
	    /usr/bin/python$$vv $(CURDIR)/test/runtests.py -v $${SKIP_TESTS} || $(IGNORE_FAILED_TESTS); \
	   nproc=`nproc`; MIN_PROC=$$(( nproc > 2 ? nproc : 2 )); \
	   NUM_PROC=$$(( MIN_PROC > 5 ? 5 : MIN_PROC )); \
	   echo "I: running tests with MPI ($$NUM_PROC processes)"; \
	   PYTHONPATH=`pybuild -p $$vv --print "{build_dir}"` \
	    GITHUB_ACTIONS=true MPI4PY_TEST_SPAWN=false \
	    mpirun -v -n $$NUM_PROC /usr/bin/python$$vv $(CURDIR)/test/runtests.py -fv $${SKIP_TESTS} || $(IGNORE_FAILED_TESTS); \
	  done; \
	fi

override_dh_sphinxdoc:
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	dh_sphinxdoc -ppython-mpi4py-doc
endif

override_dh_installchangelogs:
	dh_installchangelogs CHANGES.rst

## Immediately useable documentation
override_dh_compress:
	dh_compress -X.py -X.html -X.css -X.jpg -X.txt -X.js -X.json -X.rtc -X.par -X.bin -Xobjects.inv -X.pdf

override_dh_dwz:
	echo "dh_dwz is currently deactivated since it generates an error: \".debug_line reference above end of section\""

override_dh_auto_clean:
	dh_auto_clean $@
	rm -rf `find -name build -type d`
	rm -rf `find -name _build -type d`
	rm -rf docs/source/reference

	: # Remove Cython generated files
	rm -f src/mpi4py.MPE.c src/mpi4py.MPI.c src/include/mpi4py/mpi4py.MPI_api.h

get-orig-dev-source:
	cd ../mpi4py-hg/; \
	uver=`sed -ne "/__version__/s,.*= *'\(.*\)' *$$,\1,gp" src/__init__.py`; \
	tarball=../tarballs/mpi4py_$$uver+hg`date +"20%y%m%d"`.orig.tar.gz; \
	echo "I: Generating $$tarball"; \
	hg archive --exclude 'misc' -t tgz $$tarball;