#! /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_LDFLAGS_MAINT_STRIP = -Wl,-Bsymbolic-functions
export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
export DEB_BUILD_ARCH_OS ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)

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 ; \
	  sed "s/-lscalapack/-lscalapack-$$flavor/; s|/usr/lib|/usr/lib/$(DEB_HOST_MULTIARCH)|" build-$$flavor/scalapack.pc > build-$$flavor/scalapack-$$flavor.pc ; \
	done

override_dh_install-arch:
	dh_install
	dh_missing --list-missing
	# test executables contain RUNPATH. Remove it.
	chrpath -d debian/scalapack-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/scalapack/*-tests/x*
	chrpath -d debian/scalapack-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/scalapack/*-tests/PBLAS/*tst
	chrpath -d debian/scalapack-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/scalapack/*-tests/PBLAS/TIMING/*tim

# add --max-parallel=1 to force test in sequential mode
# MPICH testsuite disabled as some tests fail -- why???
override_dh_auto_test:
	for flavor in openmpi mpich; do \
	  echo "Running tests for libscalapack-$$flavor" ; \
	  echo '************************************************'; \
	  echo "****** TESTING $$flavor BUILD"; \
	  echo '************************************************'; \
	  ( MPIEXEC="mpiexec.$$flavor --allow-run-as-root --oversubscribe -np " dh_auto_test --builddirectory=build-$$flavor 2>&1 | tee build-$$flavor/dh_auto_test.log ) || /bin/true; \
	  echo '************************************************'; \
	  echo "****** TESTING OF $$flavor BUILD IS COMPLETE"; \
	  echo '************************************************'; \
	done
	# mpich has been found to Fail some tests but openmpi should not, so only check the openmpi test logs.
	# openmpi may Timeout on slower arches, so only register actual Fails (or SegFaults), assigning them return value 2
	# Test only on linux, since openmpi on other OS (kfreebsd, hurd) does not support fakeroot
	#
	# Note: openmpi tests routinely (but unreproducibly) fail on some buildds,
	# so don't let test failure halt the build on these arches.
	# Currently the bad-listed arches are: mips64el powerpc
	if [ "$$DEB_BUILD_ARCH_OS" = "linux" ] && $$( grep -qE "Failed|SegFault" build-openmpi/dh_auto_test.log ); then \
	  echo "OpenMPI tests failed:"; \
	  grep -E "\(Failed\)|\(SEGFAULT\)" build-openmpi/dh_auto_test.log; \
	  if ! ( [ "$$DEB_HOST_ARCH" = "mips64el" ] || [ "$$DEB_HOST_ARCH" = "powerpc" ] ); then \
	    return 2; \
	  fi \
	fi

