#!/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\.]\)\(+docs[0-9]*\)\?-[^-]*$$/\1/')

# Get sonames from config/lt_vers.am
include config/lt_vers.am
INTERFACES := @ _CXX _F _HL _HL_CXX _F _TOOLS
istr = $(subst @,,$(interface))
$(foreach interface,$(INTERFACES),$(eval \
  SONAME$(istr) := $(shell echo $$(($(LT$(istr)_VERS_INTERFACE) - $(LT$(istr)_VERS_AGE))))))

# Informative target
sonames:
	@$(foreach interface,$(INTERFACES),echo "SONAME$(istr) = $(SONAME$(istr))" &&) true

# Targets used by helper script make-version-scripts
SONAM%: interface = $(subst SONAME,,$@)
SONAM%:
	@echo $(SONAME$(istr))

major_version = $(shell echo $(version) | cut -d. -f1,2)
serpack = $(package)-$(SONAME)
sercpppack = $(package)-cpp-$(SONAME_CXX)
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)

# Arches to build MPI flavors on
# Setting empty OMPIARCHS and MPICHARCHS environment variables disables
# the related mpi targets
#
ARCH_FLAG=-a
# openmpi broken on archs hppa m68k sh4 (2016-02-24)
OMPIARCHS?=any !hppa !m68k !sh4
MPICHARCHS?=any

# To build for $arch there must be either "any" or "$arch" and no "!$arch"
ifneq (,$(and $(if $(filter !$(DEB_HOST_ARCH),$(OMPIARCHS)),,yes),$(or $(filter any,$(OMPIARCHS)),$(filter $(DEB_HOST_ARCH),$(OMPIARCHS)))))
build_openmpi = yes
else
build_openmpi = no
# Exclude binary openmpi packages from the build. See debhelper manpage.
# This is in case an arch is disabled with "!$arch". These strings can't
# appear in debian/control, but the build have to be disabled anyway for
# the related archs.
export DH_OPTIONS += -N$(openmpipack) -N$(openmpipack)-dbg -Nlibhdf5-openmpi-dev
endif

ifneq (,$(and $(if $(filter !$(DEB_HOST_ARCH),$(MPICHARCHS)),,yes),$(or $(filter any,$(MPICHARCHS)),$(filter $(DEB_HOST_ARCH),$(MPICHARCHS)))))
build_mpich = yes
else
build_mpich = no
# See above comment for build_openmpi
export DH_OPTIONS += -N$(mpichpack) -N$(mpichpack)-dbg -Nlibhdf5-mpich-dev
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 ($(build_mpich),yes)
configure_stamp_mpich = configure-stamp-mpich
build_stamp_mpich = build-stamp-mpich
install_mpich = install-mpich
else
configure_stamp_mpich =
build_stamp_mpich =
install_mpich =
endif

# Don't build doc on archs where php5-cli is not available:
NODOCARCHS ?= !m68k !sh4
ifeq (,$(filter !$(DEB_HOST_ARCH),$(NODOCARCHS)))
build_stamp_doc = build-stamp-doc
install_doc = install-doc
else
build_stamp_doc =
install_doc =
export DH_OPTIONS += -Nlibhdf5-doc
endif

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
       USE_PROD = yes
else
       USE_PROD = no
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 \
		  --with-pthread --enable-linux-lfs --enable-unsupported \
		  --enable-shared --enable-production=$(USE_PROD) \
		  --disable-sharedlib-rpath --with-zlib --with-default-api-version=v18 \
		  --with-szlib \
		  --enable-fortran --enable-fortran2003
SERIAL_FLAGS = --enable-threadsafe --enable-cxx --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) --includedir=\$${prefix}/include/hdf5/serial --with-flavor=serial
OPENMPI_FLAGS = --enable-parallel=yes --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) --includedir=\$${prefix}/include/hdf5/openmpi --with-flavor=openmpi
MPICH_FLAGS = --enable-parallel=yes --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) --includedir=\$${prefix}/include/hdf5/mpich --with-flavor=mpich

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_cxx/$(SONAME_CXX)/g;s/soname/$(SONAME)/g'` ; \
	    flavorpkgstr="" ; \
	    flavorlibstr="" ; \
	    ifmpi="#"; \
	    ifserial=""; \
	    if [ "$$flavor" != "serial" ]; then \
	      if echo "$$helper" | grep -Eq '\-cpp'; then \
	        continue ; \
	      fi ; \
	      flavorpkgstr="-$$flavor" ; \
	      ifmpi="" ; \
	      ifserial="#"; \
	    fi ; \
	    flavorlibstr="_$$flavor" ; \
	    helper=`echo $$helper | sed 's/-flavor/'$$flavorpkgstr'/'` ; \
	    sed -e 's/@FLAVORLIB@/'$$flavorlibstr'/g' \
		-e 's/@FLAVORPKG@/'$$flavorpkgstr'/g' \
		-e 's/@FLAVOR@/'$$flavor'/g' \
                -e 's/@IFMPI@/'$$ifmpi'/g' \
                -e 's/@IFSERIAL@/'$$ifserial'/g' \
                -e 's/@MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' \
                -e 's/@SONAME_CXX@/$(SONAME_CXX)/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 \
		H5_LDFLAGS="-Wl,--version-script,\$$(top_srcdir)/debian/map_serial.ver" \
		../../configure $(CONFIGURE_FLAGS) $(SERIAL_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)" \
		H5_LDFLAGS="-Wl,--version-script,\$$(top_srcdir)/debian/map_mpi.ver" \
		CC=mpicc.openmpi FC=mpif90.openmpi F9X=mpif90.openmpi RUNPARALLEL=/usr/bin/mpirun.openmpi \
		OMPI_MCA_disable_memory_allocator=1 ../../configure $(CONFIGURE_FLAGS) $(OPENMPI_FLAGS) \
		|| { cat config.log; exit 1; }
	ln debian/man/h5pcc.1 debian/man/h5pcc.openmpi.1
	ln debian/man/h5pfc.1 debian/man/h5pfc.openmpi.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)" \
		H5_LDFLAGS="-Wl,--version-script,\$$(top_srcdir)/debian/map_mpi.ver" \
		CC=mpicc.mpich \
		CXX=mpicxx.mpich FC=mpif90.mpich F9X=mpif90.mpich RUNPARALLEL=/usr/bin/mpirun.mpich \
		../../configure $(CONFIGURE_FLAGS) $(MPICH_FLAGS) \
		|| { cat config.log; exit 1; }
	ln debian/man/h5pcc.1 debian/man/h5pcc.mpich.1
	ln debian/man/h5pfc.1 debian/man/h5pfc.mpich.1
	touch $@

prep: 
	dh_prep


build: build-arch build-indep
build-arch: build-stamp $(build_stamp_openmpi) $(build_stamp_mpich)
build-indep: $(build_stamp_doc)

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 $@

build-stamp-doc: configure-stamp
	echo "builddeps:Built-Using=doxygen (= $$(dpkg-query -W -f='$${source:Version}' doxygen))" >>debian/libhdf5-doc.substvars
	# Upgrade the doxygen configuration file
	cp c++/src/cpp_doc_config c++/src/cpp_doc_config_u
	cd c++/src && doxygen -s -u cpp_doc_config_u
	# Generate doc
	cd c++/src && doxygen cpp_doc_config_u >/dev/null
	rm -rf html/cpplus_RM
	mv c++/src/cpplus_RM html/cpplus_RM
	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/@SONAME_CXX@/$(SONAME_CXX)/g" \
	  -e "s/@OMPIARCHS@/$(filter-out !%,$(OMPIARCHS))/" \
	  -e "s/@MPICHARCHS@/$(filter-out !%,$(MPICHARCHS))/" \
	  -e "s/@MAJOR_VERSION@/$(major_version)/" \
	  -e "s/@NODOCARCHS@/$(NODOCARCHS)/g" \
	  > debian/control
	dh_autoreconf_clean
	rm -f m4/l*
	-cd debian && rm -f `ls libhdf5-*.install libhdf5-*.shlibs libhdf5-*.doc | grep -v 'libhdf5-doc'`
	-rm -rf c++/src/HTML
	-rm -rf html/cpplus_RM
	-rm -f c++/src/cpp_doc_config_u*
	-rm -f debian/man/h5pcc.*.1
	-rm -f debian/man/h5pfc.*.1
	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 -d debian/$(package)-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	sed 's/@MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' debian/hdf5-serial.pc >debian/$(package)-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/hdf5-serial.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 -d debian/$(package)-openmpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	sed 's/@MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' debian/hdf5-openmpi.pc >debian/$(package)-openmpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/hdf5-openmpi.pc
	cd debian/$(package)-openmpi-dev/usr/bin && \
		mv h5pcc h5pcc.openmpi && \
		mv h5pfc h5pfc.openmpi

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 -d debian/$(package)-mpich-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	sed 's/@MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' debian/hdf5-mpich.pc >debian/$(package)-mpich-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/hdf5-mpich.pc
	cd debian/$(package)-mpich-dev/usr/bin && \
		mv h5pcc h5pcc.mpich && \
		mv h5pfc h5pfc.mpich

install-doc: HTML_DIR := debian/$(package)-doc/usr/share/doc/$(package)-doc/html
install-doc: build-indep
	dh_testdir
	dh_testroot
	dh_install -p$(package)-doc -X Dependencies -X Makefile.in -X CppUserNotes.doc
	# process php in html files
	find "$(HTML_DIR)"/ -type f -iname '*.html' \
	  -execdir /bin/sh -c "/usr/bin/php -f '{}' > '{}.new'" \; \
	  -exec mv "{}.new" "{}" \;

binary-indep: $(install_doc)
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i -k release_docs/RELEASE.txt
	dh_link
	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
ifeq ($(build_mpich),yes)
	dh_strip -p$(mpichpack) --dbg-package=$(mpichpack)-dbg
endif
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
ifeq ($(build_mpich),yes)
	dh_makeshlibs -p$(mpichpack) -V $(mpichpack) -- -v$(version)
endif
	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
ifeq ($(build_mpich),yes)
	dh_shlibdeps -p$(mpichpack) -L$(mpichpack) -lusr/lib/$(DEB_HOST_MULTIARCH)
endif
	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)
