#! /usr/bin/make -f

export DH_VERBOSE=1

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

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)
export MPICHLIB_FCFLAGS=$(shell dpkg-buildflags --get FFLAGS)
export FC=f95
export F77=f77
export TZ=UTC+0

AUTOGENERATED:= libmpich-dev.prerm libmpich-dev.postinst libmpich-dev.links \
	mpich.preinst libmpich-dev.preinst

# Keep old library naming scheme
export MPILIBNAME=mpich

NO_ROMIO_ARCH:= hurd-i386
NO_VERBS_ARCH:= hurd-i386 kfreebsd-amd64 kfreebsd-i386 s390x
FABRIC_ARCH:= amd64 i386 
BUILTIN_ATOMICS_ARCH:= armhf riscv64 hppa

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),$(FABRIC_ARCH)))
	FABRIC := --with-libfabric
endif

extra_flags += \
	$(VERBS) \
	$(FABRIC) \
	$(DISABLE_BUILTIN_ATOMICS) \
	--enable-shared \
	--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

%:
	dh $@  --with=fortran_mod

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_clean:
	dh_clean
	rm -f $(patsubst %, debian/%, ${AUTOGENERATED})

override_dh_auto_configure:
	for f in ${AUTOGENERATED} ; do \
                sed -e 's%TRIPLET%${DEB_HOST_MULTIARCH}%g' < debian/$$f.in  > debian/$$f ; \
                done
	dh_auto_configure -- $(extra_flags) CPPFLAGS="" CFLAGS="" CXXFLAGS="" FFLAGS="" FCFLAGS=""

override_dh_auto_build-indep:
	dh_auto_build -i
	# Disable rebuilding pdfs for the moment due to pdflatex problem 
	# cd $(CURDIR)/doc/installguide; rm *.pdf; $(MAKE) install.pdf
	# cd $(CURDIR)/doc/logging; rm *.pdf; $(MAKE) logging.pdf
	#cd $(CURDIR)/doc/smpd; rm *.pdf; $(MAKE) smpd_pmi.pdf
	#cd $(CURDIR)/doc/userguide; rm *.pdf; $(MAKE) user.pdf

override_dh_auto_install-arch:
	dh_auto_install -a

	# Rename executables (ignore upstream symlinks)
	for i in mpicc mpicxx mpifort; do \
	  (cd debian/tmp/usr/bin/ && mv $$i $$i.mpich); \
	done

    # Rename manpages
	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


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:

endif

