#! /usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.
# Handmodified by P. Frauenfelder for debhelper support, 5 Sept 1998

topdir=$(shell pwd)

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

# Read the MPI implementation provided by mpi-default-dev.
include /usr/share/mpi-default-dev/debian_defaults
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# This list of archs is maintained separately from that of the mpi-defaults
# package. If there is a mismatch between the two, the package will FTBFS. This
# is on purpose, to avoid silent breakage. See #740620 for more details.
OPENMPI_ARCHS=alpha amd64 arm64 armel armhf hppa hurd-i386 i386 kfreebsd-amd64 kfreebsd-i386 mips mips64el mipsel powerpc powerpcspe ppc64 ppc64el s390x sparc64 x32
MPICH_ARCHS=m68k sh4

# This list is to avoid the problem caused by Caught signal ‘Terminated’:
# terminating immediately in some build machines.

NO_TESTING = mips64el mipsel powerpc ppc64 sparc64 i386 alpha sh4
NO_TESTING_ARCH = $(shell dpkg-architecture -q DEB_BUILD_ARCH)
$(info NO_TESTING_ARCH is $(NO_TESTING_ARCH))

build-arch: build

build-indep: build

build: build-$(ARCH_DEFAULT_MPI_IMPL)

build-openmpi: build-stamp-openmpi check-stamp-openmpi

build-lam: build-stamp-lam check-stamp-lam

build-mpich: build-stamp-mpich check-stamp-mpich

build-mpich2: build-stamp-mpich2 check-stamp-mpich2

build-stamp-openmpi:
	dh_testdir
	[ -d TESTING/EXE ] || mkdir TESTING/EXE
# build the static libraries
	BASEDIR=$(topdir) MPI=openmpi make mpi
# the testing binaries
	cd TESTING && BASEDIR=$(topdir) \
	  BTLIBS='$$(BLACSFINIT) $$(BLACSLIB) $$(BLACSFINIT) $$(MPILIB)' \
	  BUILD=static MPI=openmpi make
# next is a clean
	BASEDIR=$(topdir) make cleanall
	cd TESTING && BASEDIR=$(topdir) make clean
# build the shared libraries
	BASEDIR=$(topdir) FPIC=-fPIC MPI=openmpi make mpi
	mkdir -p tmp
	set -e ;\
	for i in blacs blacsF77init blacsCinit ; do \
	  cd tmp ;\
	  ar x ../LIB/$${i}_MPI-LINUX-0.a ;\
	  mkdir -p tmp ;\
	  for j in $$(find -name "*.C") ;\
	    do mv $$j tmp/$$(echo $$j | sed 's,C$$,o,g') ;\
	  done;\
	  cd .. ;\
	  gcc -shared -Wl,-soname=lib$$i-openmpi.so.1 -o lib$$i-openmpi.so.1.1 \
	    $$(find tmp -name "*.o") $$(pkg-config mpi-fort --libs); \
	  ln -fs lib$$i-openmpi.so.1.1 lib$$i-openmpi.so.1 ;\
	  ln -fs lib$$i-openmpi.so.1 lib$$i-openmpi.so ;\
	  rm -f tmp/tmp/* ; rmdir tmp/tmp ; rm tmp/* ;\
	done
	rmdir tmp
# the testing binaries
	cd TESTING && BASEDIR=$(topdir) \
	  BTLIBS='-L.. -lblacsF77init-openmpi -lblacs-openmpi -lblacsF77init-openmpi $$(MPILIB)' \
	  BUILD=shared MPI=openmpi make

	touch build-stamp-openmpi

build-stamp-lam:
	dh_testdir
	[ -d TESTING/EXE ] || mkdir TESTING/EXE
# next is a clean
	BASEDIR=$(topdir) make cleanall
	cd TESTING && BASEDIR=$(topdir) make clean
# build the static libraries
	BASEDIR=$(topdir) MPI=lam make mpi
# the testing binaries
	cd TESTING && BASEDIR=$(topdir) \
	  BTLIBS='$$(BLACSFINIT) $$(BLACSLIB) $$(BLACSFINIT) $$(MPILIB)' \
	  BUILD=static MPI=lam make
# next is a clean
	BASEDIR=$(topdir) make cleanall
	cd TESTING && BASEDIR=$(topdir) make clean
# build the shared libraries
	BASEDIR=$(topdir) FPIC=-fPIC MPI=lam make mpi
	mkdir -p tmp
	set -e ;\
	for i in blacs blacsF77init blacsCinit ; do \
	  cd tmp ;\
	  ar x ../LIB/$${i}_MPI-LINUX-0.a ;\
	  mkdir -p tmp ;\
	  for j in $$(find -name "*.C") ;\
	    do mv $$j tmp/$$(echo $$j | sed 's,C$$,o,g') ;\
	  done;\
	  cd .. ;\
	  gcc -shared -Wl,-soname=lib$$i-lam.so.1 -o lib$$i-lam.so.1.1 \
	    $$(find tmp -name "*.o") -lmpi;\
	  ln -fs lib$$i-lam.so.1.1 lib$$i-lam.so.1 ;\
	  ln -fs lib$$i-lam.so.1 lib$$i-lam.so ;\
	  rm -f tmp/tmp/* ; rmdir tmp/tmp ; rm tmp/* ;\
	done
	rmdir tmp
# the testing binaries
	cd TESTING && BASEDIR=$(topdir) \
	  BTLIBS='-L.. -lblacsF77init-lam -lblacs-lam -lblacsF77init-lam $$(MPILIB)' \
	  BUILD=shared MPI=lam make

	touch build-stamp-lam

build-stamp-mpich:
	dh_testdir
	[ -d TESTING/EXE ] || mkdir TESTING/EXE
# next is a clean
	BASEDIR=$(topdir) make cleanall
	cd TESTING && BASEDIR=$(topdir) make clean
# build the static libraries
	BASEDIR=$(topdir) MPI=mpich make mpi
# the testing binaries
	cd TESTING && BASEDIR=$(topdir) \
	  BTLIBS='$$(BLACSFINIT) $$(BLACSLIB) $$(BLACSFINIT) $$(MPILIB)' \
	  BUILD=static MPI=mpich make
# next is a clean
	BASEDIR=$(topdir) make cleanall
	cd TESTING && BASEDIR=$(topdir) make clean
# build the shared libraries
	BASEDIR=$(topdir) FPIC=-fPIC MPI=mpich make mpi
	mkdir -p tmp
	set -e ;\
	for i in blacs blacsF77init blacsCinit ; do \
	  cd tmp ;\
	  ar x ../LIB/$${i}_MPI-LINUX-0.a ;\
	  mkdir -p tmp ;\
	  for j in $$(find -name "*.C") ;\
	    do mv $$j tmp/$$(echo $$j | sed 's,C$$,o,g') ;\
	  done;\
	  cd .. ;\
	  gcc -shared -Wl,-soname=lib$$i-mpich.so.1 -o lib$$i-mpich.so.1.1 \
	    $$(find tmp -name "*.o") -lmpich -lmpichfort;\
	  ln -fs lib$$i-mpich.so.1.1 lib$$i-mpich.so.1 ;\
	  ln -fs lib$$i-mpich.so.1 lib$$i-mpich.so ;\
	  rm -f tmp/tmp/* ; rmdir tmp/tmp ; rm tmp/* ;\
	done
	rmdir tmp
# the testing binaries
	cd TESTING && BASEDIR=$(topdir) \
	  BTLIBS='-L.. -lblacsF77init-mpich -lblacs-mpich -lblacsF77init-mpich $$(MPILIB)' \
	  BUILD=shared MPI=mpich make

	touch build-stamp-mpich

build-stamp-mpich2:
	dh_testdir
	[ -d TESTING/EXE ] || mkdir TESTING/EXE
# next is a clean
	BASEDIR=$(topdir) make cleanall
	cd TESTING && BASEDIR=$(topdir) make clean
# build the static libraries
	BASEDIR=$(topdir) MPI=mpich2 make mpi
# the testing binaries
	cd TESTING && BASEDIR=$(topdir) \
	  BTLIBS='$$(BLACSFINIT) $$(BLACSLIB) $$(BLACSFINIT) $$(MPILIB)' \
	  BUILD=static MPI=mpich2 make
# next is a clean
	BASEDIR=$(topdir) make cleanall
	cd TESTING && BASEDIR=$(topdir) make clean
# build the shared libraries
	BASEDIR=$(topdir) FPIC=-fPIC MPI=mpich2 make mpi
	mkdir -p tmp
	set -e ;\
	for i in blacs blacsF77init blacsCinit ; do \
	  cd tmp ;\
	  ar x ../LIB/$${i}_MPI-LINUX-0.a ;\
	  mkdir -p tmp ;\
	  for j in $$(find -name "*.C") ;\
	    do mv $$j tmp/$$(echo $$j | sed 's,C$$,o,g') ;\
	  done;\
	  cd .. ;\
	  gcc -shared -Wl,-soname=lib$$i-mpich2.so.1 -lmpich -o lib$$i-mpich2.so.1.1 \
	    $$(find tmp -name "*.o");\
	  ln -fs lib$$i-mpich2.so.1.1 lib$$i-mpich2.so.1 ;\
	  ln -fs lib$$i-mpich2.so.1 lib$$i-mpich2.so ;\
	  rm -f tmp/tmp/* ; rmdir tmp/tmp ; rm tmp/* ;\
	done
	rmdir tmp
# the testing binaries
	cd TESTING && BASEDIR=$(topdir) \
	  BTLIBS='-L.. -lblacsF77init-mpich2 -lblacs-mpich2 -lblacsF77init-mpich2 $$(MPILIB)' \
	  BUILD=shared MPI=mpich2 make

	touch build-stamp-mpich2

check-stamp-openmpi:

ifneq ($(filter $(NO_TESTING_ARCH),$(NO_TESTING)),)
	touch check-stamp-openmpi
else
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	 dh_testdir
	if [ -x /usr/bin/mpiexec.openmpi ]; then \
    	  (cd TESTING/EXE; for i in *-openmpi; do \
    	    echo "Running test $$i"; \
    	    LD_LIBRARY_PATH=../../ mpiexec.openmpi -np 4 ./$$i > $$i.log 2>&1 ; \
    	    grep FAILED $$i.log | sed s/DOUBLE// | \
    	    awk '{total += $$4; passed += $$6; skipped += $$8; failed += $$10} END \
    	      {print "total: " total " passed: " passed " skipped: " skipped " failed: " failed ; \
    	      if (failed > 0) {exit 1}}' ; \
    	  done) \
    	fi
    endif
	touch check-stamp-openmpi
endif

check-stamp-lam:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_testdir
	if [ -x /usr/bin/mpiexec.lam ]; then \
	  (cd TESTING/EXE; for i in *-lam; do \
	    echo "Running test $$i"; \
	    LD_LIBRARY_PATH=../../ mpiexec.lam -np 4 ./$$i > $$i.log 2>&1 ; \
	    grep FAILED $$i.log | sed s/DOUBLE// | \
	    awk '{total += $$4; passed += $$6; skipped += $$8; failed += $$10} END \
	      {print "total: " total " passed: " passed " skipped: " skipped " failed: " failed ; \
	      if (failed > 0) {exit 1}}' ; \
	  done) \
	fi
endif
	touch check-stamp-lam

check-stamp-mpich:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_testdir
	if [ -x /usr/bin/mpiexec.mpich ]; then \
	  (cd TESTING/EXE; for i in *-mpich; do \
	    echo "Running test $$i"; \
	    LD_LIBRARY_PATH=../../ mpiexec.mpich -np 4 ./$$i > $$i.log 2>&1 ; \
	    grep FAILED $$i.log | sed s/DOUBLE// | \
	    awk '{total += $$4; passed += $$6; skipped += $$8; failed += $$10} END \
	      {print "total: " total " passed: " passed " skipped: " skipped " failed: " failed ; \
	      if (failed > 0) {exit 1}}' ; \
	  done) \
	fi
endif
	touch check-stamp-mpich

check-stamp-mpich2:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_testdir
	if [ -x /usr/bin/mpiexec.mpich2 ]; then \
	  (cd TESTING/EXE; for i in *-mpich2; do \
	    echo "Running test $$i"; \
	    LD_LIBRARY_PATH=../../ mpiexec.mpich2 -np 4 ./$$i > $$i.log 2>&1 ; \
	    grep FAILED $$i.log | sed s/DOUBLE// | \
	    awk '{total += $$4; passed += $$6; skipped += $$8; failed += $$10} END \
	      {print "total: " total " passed: " passed " skipped: " skipped " failed: " failed ; \
	      if (failed > 0) {exit 1}}' ; \
	  done) \
	fi
endif
	touch check-stamp-mpich2

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp-* install-stamp-*
	BASEDIR=$(topdir) make cleanall
	cd TESTING && BASEDIR=$(topdir) make clean
	cd LIB && rm -f *.a
	rm -f *so*
	rm -rf TESTING/EXE
	dh_clean

	{ \
	  echo "# This file is autogenerated. DO NOT EDIT!" ; \
	  echo "#" ; \
	  echo "# Modifications should be made to debian/control.in instead." ; \
	  echo "# This file is regenerated automatically in the clean target." ; \
	  echo ; \
	  sed "s/@OPENMPI_ARCHS@/$(OPENMPI_ARCHS)/g;s/@MPICH_ARCHS@/$(MPICH_ARCHS)/g;" debian/control.in ; } \
	> debian/control

install: install-$(ARCH_DEFAULT_MPI_IMPL)

install-openmpi: install-stamp-openmpi

install-lam: install-stamp-lam

install-mpich: install-stamp-mpich

install-mpich2: install-stamp-mpich2

install-stamp-openmpi: build-stamp-openmpi
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -v -a

	# The shared library are installed via debian/*.install, to avoid
	# creating an empty package if default MPI implementation changes on
	# the arch
	dh_install

	set -e ;\
	for i in blacs blacsF77init blacsCinit ; do \
	  cp -a lib$$i-openmpi.so \
	    `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH) ;\
	done

	set -e ;\
	for i in shared static ; do \
	  install TESTING/EXE/xCbtest_MPI-LINUX-0-$$i-openmpi \
	  `pwd`/debian/blacs-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/blacs/cblacs_test_$$i-openmpi ;\
	  install TESTING/EXE/xFbtest_MPI-LINUX-0-$$i-openmpi \
	  `pwd`/debian/blacs-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/blacs/fblacs_test_$$i-openmpi ;\
	done

	set -e ;\
	install LIB/blacsCinit_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacsCinit-openmpi.a
	install LIB/blacsF77init_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacsF77init-openmpi.a
	install LIB/blacs_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacs-openmpi.a

	touch install-stamp-openmpi

install-stamp-lam: build-stamp-lam
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -v -a

	set -e ;\
	for i in blacs blacsF77init blacsCinit ; do \
	  cp -a lib$$i-lam.so.* \
	    `pwd`/debian/libblacs-mpi1/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	  cp -a lib$$i-lam.so \
	    `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	done

	set -e ;\
	for i in shared static ; do \
	  install TESTING/EXE/xCbtest_MPI-LINUX-0-$$i-lam \
	  `pwd`/debian/blacs-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/blacs/cblacs_test_$$i-lam ;\
	  install TESTING/EXE/xFbtest_MPI-LINUX-0-$$i-lam \
	  `pwd`/debian/blacs-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/blacs/fblacs_test_$$i-lam ;\
	done

	set -e ;\
	install LIB/blacsCinit_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacsCinit-lam.a
	install LIB/blacsF77init_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacsF77init-lam.a
	install LIB/blacs_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacs-lam.a

	touch install-stamp-lam

install-stamp-mpich: build-stamp-mpich
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -v -a

	# The shared library are installed via debian/*.install, to avoid
	# creating an empty package if default MPI implementation changes on
	# the arch
	dh_install

	set -e ;\
	for i in blacs blacsF77init blacsCinit ; do \
	  cp -a lib$$i-mpich.so \
	    `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	done

	set -e ;\
	for i in shared static ; do \
	  install TESTING/EXE/xCbtest_MPI-LINUX-0-$$i-mpich \
	  `pwd`/debian/blacs-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/blacs/cblacs_test_$$i-mpich ;\
	  install TESTING/EXE/xFbtest_MPI-LINUX-0-$$i-mpich \
	  `pwd`/debian/blacs-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/blacs/fblacs_test_$$i-mpich ;\
	done

	set -e ;\
	install LIB/blacsCinit_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacsCinit-mpich.a
	install LIB/blacsF77init_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacsF77init-mpich.a
	install LIB/blacs_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacs-mpich.a

	touch install-stamp-mpich

install-stamp-mpich2: build-stamp-mpich2
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -v -a

	set -e ;\
	for i in blacs blacsF77init blacsCinit ; do \
	  cp -a lib$$i-mpich2.so.* \
	    `pwd`/debian/libblacs-mpi1/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	  cp -a lib$$i-mpich2.so \
	    `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	done

	set -e ;\
	for i in shared static ; do \
	  install TESTING/EXE/xCbtest_MPI-LINUX-0-$$i-mpich2 \
	  `pwd`/debian/blacs-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/blacs/cblacs_test_$$i-mpich2 ;\
	  install TESTING/EXE/xFbtest_MPI-LINUX-0-$$i-mpich2 \
	  `pwd`/debian/blacs-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/blacs/fblacs_test_$$i-mpich2 ;\
	done

	set -e ;\
	install LIB/blacsCinit_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacsCinit-mpich2.a
	install LIB/blacsF77init_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacsF77init-mpich2.a
	install LIB/blacs_MPI-LINUX-0.a \
	  `pwd`/debian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libblacs-mpich2.a

	touch install-stamp-mpich2

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a README
	dh_installexamples -a
	dh_installman -a
	dh_installchangelogs -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_strip -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -Lblacs-mpi-test -ldebian/blacs-mpi-test/usr/lib/$(DEB_HOST_MULTIARCH)/blacs/ -Llibblacs-mpi-dev -ldebian/libblacs-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/ \
	LD_LIBRARY_PATH="/usr/lib/$(DEB_HOST_MULTIARCH)/openmpi/lib$${LD_LIBRARY_PATH:+:}$$LD_LIBRARY_PATH" -Llibblacs-mpi1 -l/usr/lib/$(DEB_HOST_MULTIARCH)/openmpi/lib/
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary-indep:
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i
	dh_installdocs -i README
	dh_installchangelogs -i

	cd TESTING && BASEDIR=$(topdir) make dat
	set -e ;\
	for i in $$(find TESTING/EXE -name "*.dat"); do \
	  install -m 644 $$i `pwd`/debian/blacs-test-common/usr/share/blacs ;\
	done

	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean build build-arch build-indep
