#! /usr/bin/make -f

export DH_VERBOSE=1

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Drop flags on old gfortran < 10
GF_FLAGS:= $(if $(filter $(shell readlink /usr/bin/gfortran), gfortran-9),, -fallow-invalid-boz -fallow-argument-mismatch)

export MPICHLIB_CPPFLAGS=$(shell dpkg-buildflags --get CPPFLAGS)
export MPICHLIB_CFLAGS=$(shell dpkg-buildflags --get CFLAGS)
export MPICHLIB_CXXFLAGS=$(shell dpkg-buildflags --get CXXFLAGS)
export MPICHLIB_FFLAGS=$(shell dpkg-buildflags --get FFLAGS) $(GF_FLAGS)
export MPICHLIB_FCFLAGS=$(shell dpkg-buildflags --get FFLAGS) -cpp $(GF_FLAGS)
export FFLAGS=$(shell dpkg-buildflags --get FFLAGS |  sed -e 's/-g //') $(GF_FLAGS)
export F77=f77
export TZ=UTC+0

FC:=$(shell basename $(shell readlink /etc/alternatives/f95))
export FC


# Keep old library naming scheme
export MPILIBNAME=mpich

NO_ROMIO_ARCH:= hurd-i386
NO_VERBS_ARCH:= hurd-i386 kfreebsd-amd64 kfreebsd-i386 s390x
NO_SLURM_ARCH:= hurd-i386 kfreebsd-amd64 kfreebsd-i386 
NO_FABRIC_ARCH:= hurd-i386 kfreebsd-amd64 kfreebsd-i386 s390x powerpc sh4 x32
BUILTIN_ATOMICS_ARCH:= armhf riscv64 hppa
NO_REAL128_ARCH:= armel armhf mipsel hppa m68k sh5
# Pick one as default
NO_CH4_ARCH:= armel armhf i386 mipsel  hppa m68k sh4 powerpc x32 mips64el s390x
UCX_ARCH:= amd64  ppc64el arm64 

ifneq (,$(findstring  " $(DEB_HOST_ARCH)",$(NO_VERBS_ARCH)))
	VERBS := --with-ibverbs
endif
ifneq (,$(findstring $(DEB_HOST_ARCH),$(BUILTIN_ATOMICS_ARCH)))
	DISABLE_BUILTIN_ATOMICS:= --with-atomic-primitives=no
endif
ifneq (,$(findstring $(DEB_HOST_ARCH),$(NO_FABRIC_ARCH)))
	FABRIC := --with-libfabric=/usr
endif
ifneq (,$(findstring  $(DEB_HOST_ARCH),$(NO_REAL128_ARCH)))
        MPICHLIB_FCFLAGS:=$(MPICHLIB_FCFLAGS) -DNO_REAL128
endif
ifneq (,$(findstring $(DEB_HOST_ARCH),$(NO_SLURM_ARCH)))
	SLURM := --with-slurm=/usr 
endif

DEVICE:= --with-device=ch3
UCX:=
PMIX:=
ifeq (,$(findstring  $(DEB_HOST_ARCH),$(NO_CH4_ARCH)))
        DEVICE:= --with-device=ch4:ofi 
	# PMIX:=  --with-pmix=/usr/lib/$(DEB_HOST_MULTIARCH)/pmix2
endif
#ifneq (,$(findstring  $(DEB_HOST_ARCH),$(UCX_ARCH)))
#        DEVICE:= --with-device=ch4:ucx
#	UCX:= --with-ucx=/usr
#	PMIX:=  --with-pmix=/usr/lib/$(DEB_HOST_MULTIARCH)/pmix2
#endif

extra_flags += \
	$(VERBS) \
	$(FABRIC) \
	$(SLURM) \
	$(PMIX) \
	$(UCX) \
	$(DEVICE) \
	$(DISABLE_BUILTIN_ATOMICS) \
	--with-pm=hydra \
	--with-hwloc-prefix=/usr \
	--with-wrapper-dl-type=none \
	--enable-shared \
	--without-yaksa \
	--prefix=/usr \
	--enable-fortran=all \
	--disable-rpath \
	--disable-wrapper-rpath \
	--sysconfdir=/etc/mpich \
	--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	--includedir=/usr/include/$(DEB_HOST_MULTIARCH)/mpich \
	--docdir=/usr/share/doc/mpich 


# On sparc and sh4, OPA isn't available, so we need to fallback to
# emulation mode. There's a performance penalty, unfortunately.
ifneq (,$(findstring $(DEB_HOST_GNU_CPU),sparc sh4))
	extra_flags += --with-atomic-primitives=no
endif

AUTOGENERATED	:= $(patsubst %.in,%,$(wildcard debian/*.in))

%: %.in
	sed 's%@DEB_HOST_MULTIARCH@%$(DEB_HOST_MULTIARCH)%g' < $< > $@

%:
	dh $@ 

override_dh_autoreconf:
	./autogen.sh
	#autoreconf -f -i src/mpl src/openpa src/mpi/romio src/pm/hydra \
	#	src/mpid/ch4/netmod/ucx/ucx src/mpid/ch4/netmod/ofi/libfabric

override_dh_auto_configure: $(AUTOGENERATED)
	dh_auto_configure -- $(extra_flags) CPPFLAGS="" CFLAGS="" CXXFLAGS="" FFLAGS="$(FFLAGS)" FCFLAGS="$(FFLAGS)" BASH_SHELL=/bin/bash
	( cd src/pm/hydra && ./configure --with-hwloc-prefix=/usr $(DEVICE) FFLAGS="$(FFLAGS)"  --prefix=/usr )

override_dh_auto_build-indep:
	dh_auto_build -i
	for d in installguide userguide design ; do \
		( cd $(CURDIR)/doc/$$d ; rm -f *.pdf ; $(MAKE) ) ; done

override_dh_auto_install-arch:
	dh_auto_install -a
	$(MAKE) -C src/pm/hydra install DESTDIR=$(CURDIR)/debian/tmp

	# Rename executables (ignore upstream symlinks)
	for i in mpicc mpicxx mpifort; do \
	  (cd debian/tmp/usr/bin/ && mv $$i $$i.mpich); \
	done
    # Rename manpages
    # TODO: manpages not shipped in beta ?
	#for i in mpicc mpicxx mpiexec mpif77 mpifort; do \
	#  (cd debian/tmp/usr/share/man/man1/ && mv $$i.1 $$i.mpich.1);\
	#done

	# Fix incorrect paths in /usr/bin/mpi{cc,cxx,fort}.mpich
	for fn in debian/tmp/usr/bin/mpicc.mpich debian/tmp/usr/bin/mpicxx.mpich \
		debian/tmp/usr/bin/mpifort.mpich; do \
		sed -i 's:$(CURDIR)/debian/tmp/::g' $$fn ;\
	done

execute_after_dh_auto_clean:
	$(RM) $(AUTOGENERATED)

override_dh_fixperms-arch:
	# Hack. placed here to be run after dh_auto_install _after_ override
	chrpath -d debian/*/usr/bin/mpivars debian/*/usr/bin/mpichversion
	chrpath -d debian/*/usr/lib/*/lib*.so.*
	-rm debian/*/usr/share/doc/mpich/examples/cpi.o
	dh_fixperms -a

override_dh_installchangelogs:
	dh_installchangelogs README

# Some tests are failing on hurd-i386.
# Disable them, to be investiated later
# disable_test_archs = hurd-i386
disable_test_archs = 
# ifneq (,$(filter $(DEB_HOST_ARCH),$(disable_test_archs)))
override_dh_auto_test:
	@echo "Don't fail on tests in experimental"
	- dh_auto_test || true
# endif

