#!/usr/bin/make -f

# debian/rules for libhdf5,
# (c) 2001 Brian Russo, under GPL.
# (c) 2003 Josselin Mouette
# (c) 2007-2009 Francesco P. Lovergine.
# (c) 2010-2013 Sylvestre Ledru
# (c) 2014 Gilles Filippini

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

package = libhdf5
pkgversion ?= $(shell dpkg-parsechangelog | awk '/^Version:/ {print $$2}')
version ?= $(shell echo $(pkgversion) | sed 's/\([0-9\.]\)-[^-]*$$/\1/')
soname =  7
major_version = $(shell echo $(version) | cut -d. -f1,2)
serpack = $(package)-$(soname)
sercpppack = $(package)-cpp-$(soname)
openmpipack = $(package)-openmpi-$(soname)
mpichpack = $(package)-mpich-$(soname)
virtpack = $(package)-$(major_version)

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
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)

# To be used if binary packages were buildable on any arch
#ARCH_FLAG=-a
#ARCHS=any
ARCH_FLAG=-s
ARCHS=alpha arm64 armel armhf amd64 i386 ia64 powerpc powerpcspe ppc64 sparc kfreebsd-i386 kfreebsd-amd64 hurd-i386

ifeq (,$(filter $(DEB_HOST_ARCH),$(ARCHS)))
build_openmpi = no
else
build_openmpi = yes
endif

ifeq ($(build_openmpi),yes)
configure_stamp_openmpi = configure-stamp-openmpi
build_stamp_openmpi = build-stamp-openmpi
install_openmpi = install-openmpi
else
configure_stamp_openmpi =
build_stamp_openmpi =
install_openmpi =
endif

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
       USE_PROD = yes
else
       USE_PROD = no
endif

### let's keep it commented out in case a compiler error happens again
# ifeq (hppa-linux,$(DEB_BUILD_GNU_TYPE))
#   DEBCC=gcc-3.2
#   DEBCXX=g++-3.2
# else
#    DEBCC=gcc
#    DEBCXX=g++
#    DEBFORTRAN=gfortran
# With gcc 4.3+ force optimization off, else some tests fail...
#    DEBOPT=-O0
# endif
###

# export DEB_BUILD_OPTIONS="parallel=2"
#
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif	

CONFIGURE_FLAGS = --prefix=/usr --host=$(DEB_HOST_GNU_TYPE) \
		  --build=$(DEB_BUILD_GNU_TYPE) \
		  --mandir=\$${prefix}/share/man \
		  --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		  --with-pthread --enable-linux-lfs --enable-unsupported \
		  --enable-shared --enable-production=$(USE_PROD) \
		  --disable-sharedlib-rpath --with-zlib --with-default-api-version=v18
SERIAL_ONLY_FLAGS = --enable-fortran --enable-threadsafe --enable-cxx

configure: configure-stamp-debian configure-stamp \
	   $(configure_stamp_openmpi) configure-stamp-mpich

configure-stamp-debian: debian/control.in
	# This loop generates helper files from debian/*.*.in templates
	cd debian && for file in *.*.in; do \
	  for flavor in serial openmpi mpich; do \
	    helper=`basename $$file .in | sed 's/soname/$(soname)/g'` ; \
	    flavorstr="" ; \
	    ifmpi="#"; \
	    if [ "$$flavor" != "serial" ]; then \
	      if echo "$$helper" | grep -Eq '\-cpp|\.shlibs'; then \
	        continue ; \
	      fi ; \
	      flavorstr="-$$flavor" ; \
	      ifmpi="" ; \
	    fi ; \
	    helper=`echo $$helper | sed 's/-flavor/'$$flavorstr'/'` ; \
	    sed -e 's/@FLAVOR@/'$$flavor'/g' \
                -e 's/@IFMPI@/'$$ifmpi'/g' \
                -e 's/@MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' \
                -e 's/@SONAME@/$(soname)/g' \
	      $$file >$$helper ; \
	    done ; \
	  done
	#
	mkdir -p m4
	dh_autoreconf
	touch $@

configure-stamp: configure-stamp-debian
	dh_testdir
	-mkdir debian/build
# configure serial version
	cd debian/build && CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" FFLAGS="$(FFLAGS)" CC=/usr/bin/cc CXX=/usr/bin/c++ FC=gfortran \
		../../configure $(CONFIGURE_FLAGS) $(SERIAL_ONLY_FLAGS) || { cat config.log; exit 1; }
	touch $@

configure-stamp-openmpi: configure-stamp-debian
	dh_testdir
	-mkdir debian/build-openmpi
# configure version with openmpi
	cd debian/build-openmpi && CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" FFLAGS="$(FFLAGS)" \
		CC=mpicc.openmpi FC=mpif90.openmpi F9X=mpif90.openmpi RUNPARALLEL=/usr/bin/mpirun.openmpi \
		OMPI_MCA_disable_memory_allocator=1 ../../configure $(CONFIGURE_FLAGS) --enable-fortran \
		--enable-parallel=yes || { cat config.log; exit 1; }
	touch $@

configure-stamp-mpich: configure-stamp-debian
	dh_testdir
	-mkdir debian/build-mpich
# configure version with mpich
	cd debian/build-mpich && CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" FFLAGS="$(FFLAGS)" \
		CC=mpicc.mpich \
		CXX=mpicxx.mpich FC=mpif90.mpich F9X=mpif90.mpich RUNPARALLEL=/usr/bin/mpirun.mpich \
		../../configure $(CONFIGURE_FLAGS) --enable-fortran \
		--enable-parallel=yes || { cat config.log; exit 1; }
	touch $@

prep: 
	dh_prep


build: build-arch build-indep
build-arch: build-stamp $(build_stamp_openmpi) build-stamp-mpich
build-indep:

build-stamp: configure-stamp
	dh_testdir
	$(MAKE) $(MAKEFLAGS) -C debian/build/
	touch $@

build-stamp-openmpi: configure-stamp-openmpi 
	dh_testdir
	$(MAKE)  $(MAKEFLAGS) -C debian/build-openmpi/ OMPI_MCA_disable_memory_allocator=1
	touch $@

build-stamp-mpich: configure-stamp-mpich 
	dh_testdir
	$(MAKE)  $(MAKEFLAGS) -C debian/build-mpich/
	touch $@

#
# Be sure to not have a previous version installed before running
# the test suite
#
test: build-stamp
	$(MAKE) -C debian/build/ test

clean: 
	test -f debian/control.in
	-rm -rf debian/build
	-rm -rf debian/build-openmpi
	-rm -rf debian/build-mpich
	grep -v ^\% debian/control.in | sed -e "s/@SONAME@/$(soname)/g" -e "s/@ARCHS@/$(ARCHS)/" -e "s/@MAJOR_VERSION@/$(major_version)/" > debian/control
	dh_autoreconf_clean
	rm -rf m4
	-cd debian && rm -f `ls libhdf5-*.install libhdf5-*.shlibs libhdf5-*.doc | grep -v 'libhdf5-doc'`
	dh_clean configure-stamp* build-stamp* install-stamp*

install: build prep install-serial $(install_openmpi) install-mpich install-doc

install-serial: build-stamp
	dh_testdir
	dh_testroot
	-mkdir debian/build/tmpinst
	$(MAKE) -C debian/build/ install prefix=$(CURDIR)/debian/build/tmpinst/usr
	chrpath -d $(CURDIR)/debian/build/tmpinst/usr/lib/*/libhdf5*so*
	dh_install -p$(serpack) -p$(package)-dev -phdf5-helpers -phdf5-tools -p$(sercpppack) \
		--sourcedir=debian/build/tmpinst
	find debian/$(serpack)/usr/lib -name '*cpp*' -delete
	install -m644 -D debian/hdf5.pc debian/$(package)-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/hdf5.pc

install-openmpi: build-stamp-openmpi
	dh_testdir
	dh_testroot
	-mkdir debian/build-openmpi/tmpinst
	$(MAKE) -C debian/build-openmpi/ install prefix=$(CURDIR)/debian/build-openmpi/tmpinst/usr
	chrpath -d $(CURDIR)/debian/build-openmpi/tmpinst/usr/lib/*/libhdf5*so*
	dh_install -p$(openmpipack) -p$(package)-openmpi-dev \
		--sourcedir=debian/build-openmpi/tmpinst
	install -m644 -D debian/hdf5-openmpi.pc debian/$(package)-openmpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/hdf5.pc

install-mpich: build-stamp-mpich
	dh_testdir
	dh_testroot
	-mkdir debian/build-mpich/tmpinst
	$(MAKE) -C debian/build-mpich/ install prefix=$(CURDIR)/debian/build-mpich/tmpinst/usr
	chrpath -d $(CURDIR)/debian/build-mpich/tmpinst/usr/lib/*/libhdf5*so*
	dh_install -p$(mpichpack) -p$(package)-mpich-dev \
		--sourcedir=debian/build-mpich/tmpinst
	install -m644 -D debian/hdf5-mpich.pc debian/$(package)-mpich-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/hdf5.pc

install-doc: build-indep
	dh_testdir
	dh_testroot
	dh_install -p$(package)-doc -X Dependencies -X Makefile.in -X CppUserNotes.doc

binary-indep: install-doc
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i -k release_docs/RELEASE.txt
	dh_compress -i -X.pdf
	dh_fixperms -i
	dh_installdeb -i
	dh_lintian -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: prep install-serial $(install_openmpi) install-mpich
	dh_testdir
	dh_testroot
	echo "hdf5-mpi-dev=libhdf5-$(DEFAULT_MPI)-dev" > debian/libhdf5-mpi-dev.substvars
	dh_installdocs $(ARCH_FLAG)
	dh_installman $(ARCH_FLAG)
	dh_lintian $(ARCH_FLAG)
	dh_installchangelogs -a -k release_docs/RELEASE.txt
	dh_link $(ARCH_FLAG)
	dh_strip -phdf5-tools
	dh_strip -phdf5-helpers
	dh_strip -p$(serpack) --dbg-package=$(serpack)-dbg
	dh_strip -p$(sercpppack) --dbg-package=$(sercpppack)-dbg
	dh_strip -p$(mpichpack) --dbg-package=$(mpichpack)-dbg
ifeq ($(build_openmpi),yes)
	dh_strip -p$(openmpipack) --dbg-package=$(openmpipack)-dbg
endif
	dh_compress $(ARCH_FLAG)
	dh_fixperms $(ARCH_FLAG)
ifeq ($(build_openmpi),yes)
	dh_makeshlibs -p$(openmpipack) -V $(openmpipack) -- -v$(version)
endif
	dh_makeshlibs -p$(mpichpack) -V $(mpichpack) -- -v$(version)
	dh_makeshlibs -p$(serpack) -V $(serpack) -- -v$(version)
	dh_makeshlibs -p$(sercpppack) -V $(sercpppack) -- -v$(version)
	dh_installdeb $(ARCH_FLAG)
	dh_shlibdeps -p$(serpack) -L$(serpack) -lusr/lib/$(DEB_HOST_MULTIARCH)
	dh_shlibdeps -p$(sercpppack) -L$(serpack) -lusr/lib/$(DEB_HOST_MULTIARCH)
	dh_shlibdeps -phdf5-tools -L$(serpack) -lusr/lib/$(DEB_HOST_MULTIARCH)
	dh_shlibdeps -phdf5-helpers -L$(serpack) -lusr/lib/$(DEB_HOST_MULTIARCH)
ifeq ($(build_openmpi),yes)
	dh_shlibdeps -p$(openmpipack) -L$(openmpipack) -lusr/lib/$(DEB_HOST_MULTIARCH)
endif
	dh_shlibdeps -p$(mpichpack) -L$(mpichpack) -lusr/lib/$(DEB_HOST_MULTIARCH)
	dh_gencontrol $(ARCH_FLAG)
	dh_md5sums $(ARCH_FLAG)
	dh_builddeb $(ARCH_FLAG)

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install configure \
		install-doc install-serial install-openmpi install-mpich prep

.NOTPARALLEL: configure-stamp-debian configure-stamp \
	   $(configure_stamp_openmpi) configure-stamp-mpich
