#! /usr/bin/make -f


# eventually add -O3 -march=native -mtune=native
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_CXXFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
export DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

ifneq (,$(wildcard /usr/share/mpi-default-dev/debian_defaults))
include /usr/share/mpi-default-dev/debian_defaults 
endif
DEFAULT_MPI=$(ARCH_DEFAULT_MPI_IMPL)

%:
	dh $@ --buildsystem=cmake  --parallel

#export OMPI_MCA_plm_rsh_agent=/bin/false                #workaround to start MPI-applications in chroot
export OMPI_MCA_orte_rsh_agent=/bin/false                #workaround to start MPI-applications in chroot

disable_mpi_archs = armel armhf kfreebsd-amd64 kfreebsd-i386 mips mipsel
export LD_LIBRARY_PATH :=  $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH):$(LD_LIBRARY_PATH); 

export NUMJOBS=1
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
export NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

extra_flags += \
 -DBUILD_SHARED_LIBS=ON

override_dh_auto_configure:
	for flavor in openmpi mpich; do \
          echo "Configure for libscalapack-$$flavor" ; \
          dh_auto_configure --builddirectory=build-$$flavor -- $(extra_flags) -DMPI_C_COMPILER=mpicc.$$flavor -DMPI_Fortran_COMPILER=mpif90.$$flavor -DMPI_FLAVOR=$$flavor ; \
	done

override_dh_auto_build:
	for flavor in openmpi mpich; do \
	  echo "Building libscalapack-$$flavor" ; \
	  dh_auto_build -O--parallel --builddirectory=build-$$flavor ; \
	done

override_dh_auto_install:
	echo "scalapack-mpi-dev=libscalapack-$(DEFAULT_MPI)-dev" > debian/libscalapack-mpi-dev.substvars
	for flavor in openmpi mpich; do \
	  echo "Installing scalapack-$$flavor" ; \
	  dh_auto_install -O--parallel --builddirectory=build-$$flavor ; \
	done
	dh_install --list-missing

# add --max-parallel=1 to force test in sequential mode
# MPICH testsuite disabled as some tests fail -- why???
override_dh_auto_test:
	MPIEXEC="mpiexec --allow-run-as-root --oversubscribe -np " dh_auto_test --builddirectory=build-openmpi
	# for flavor in openmpi mpich; do \
	#   echo "Running tests for libscalapack-$$flavor" ; \
	#   MPIEXEC="mpiexec.$$flavor --allow-run-as-root --oversubscribe -np " dh_auto_test --builddirectory=build-$$flavor ; \
	# done

