#!/usr/bin/make -f

# Using dh (debhelper command sequencer) with compat=10
# Automagically supports:
# * parallel build
# * dpkg-buildflags

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

package = libhdf5
pkgversion ?= $(shell dpkg-parsechangelog | awk '/^Version:/ {print $$2}')
upstreamversion ?= $(shell echo $(pkgversion) | sed -r 's/^([0-9\.]*)([-~][^-~+]*)?(\+repack[0-9]*)?-[^-]*$$/\1\2/')
libversion ?= $(shell echo $(pkgversion) | sed -r 's/^([0-9\.]*)([-~][^-~+]*)?(\+repack[0-9]*)?-[^-]*$$/\1/')

# Pkg name extension to force  transition
EXT := -1

# BEGIN - Get sonames from config/lt_vers.am
include config/lt_vers.am
INTERFACES := @ _CXX _F _HL _HL_CXX _HL_F
istr = $(subst @,,$(interface))
$(foreach interface,$(INTERFACES),$(eval \
  SONAME$(istr) := $(shell if [ -n "$(LT$(istr)_VERS_INTERFACE)" ]; then \
                             echo $$(($(LT$(istr)_VERS_INTERFACE) - $(LT$(istr)_VERS_AGE))); \
                           fi)))
# Default to SONAME when empty
$(foreach interface,$(INTERFACES),$(eval \
  SONAME$(istr) := $(shell soname=$(SONAME$(istr)); echo $${soname:-$(SONAME)})))

# Targets used by helper script debian/make-version-scripts
SONAM%: interface = $(subst SONAME,,$@)
SONAM%:
	@echo $(SONAME$(istr))
EXT:
	@echo $(EXT)
# END - Get sonames from config/lt_vers.am
INTERFACE_TO_API = $(subst -f,-fortran,$(subst -cxx,-cpp,$(shell echo "$(1)" | tr '[:upper:]_' '[:lower:]-')))

serpack = $(package)-$(SONAME)$(EXT)
serpackdev = $(package)-dev
sercpppack = $(package)-cpp-$(SONAME_CXX)$(EXT)
openmpipack = $(package)-openmpi-$(SONAME)$(EXT)
mpichpack = $(package)-mpich-$(SONAME)$(EXT)

# Crossbuild and multiarch variables
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)

# Get default MPI implementation name
ifneq (,$(wildcard /usr/share/mpi-default-dev/debian_defaults))
include /usr/share/mpi-default-dev/debian_defaults 
endif
DEFAULT_MPI=$(ARCH_DEFAULT_MPI_IMPL)

# update-alternatives priorities
UA_PRIORITY_serial := 20
UA_PRIORITY_mpi := 35
ifeq (openmpi,$(DEFAULT_MPI))
UA_PRIORITY_openmpi := 30
UA_PRIORITY_mpich := 10
else
UA_PRIORITY_openmpi := 10
UA_PRIORITY_mpich := 30
endif

# Arches to build MPI flavors on
# Setting empty OMPIARCHS and MPICHARCHS environment variables disables
# the related mpi targets
#
OMPIARCHS?=any
MPICHARCHS?=any

# BEGIN - Construct the FLAVORS list for current arch
FLAVORS := serial

ARCHS_openmpi := $(OMPIARCHS)
ARCHS_mpich := $(MPICHARCHS)

define CHECK_FLAVOR
# To build for $arch there must be either "any" or "$arch" and no "!$arch"
$(eval flavor_$(1) = $(and $(or $(filter any,$(ARCHS_$(1))),$(filter $(DEB_HOST_ARCH),$(ARCHS_$(1)))),$(if $(filter !$(DEB_HOST_ARCH),$(ARCHS_$(1))),,$(1))))
FLAVORS += $(flavor_$(1))
# If not selected, exclude this arch's binary 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.
$(if $(flavor_$(1)),,export DH_OPTIONS += $(foreach pkg,$(PACKAGES_$(1)),-N$(pkg)))
endef
$(foreach mpi,openmpi mpich,$(eval $(call CHECK_FLAVOR,$(mpi))))
# END - Construct the FLAVORS list

# BEGIN - Configure flags and environment variables
ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
       USE_PROD = production
else
       USE_PROD = debug
endif

CONFIGURE_FLAGS = --prefix=/usr --host=$(DEB_HOST_GNU_TYPE) \
		  --build=$(DEB_BUILD_GNU_TYPE) \
		  --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		  --mandir=\$${prefix}/share/man \
		  --with-pthread --enable-linux-lfs \
		  --enable-shared --enable-build-mode=$(USE_PROD) \
		  --disable-sharedlib-rpath --with-zlib --with-default-api-version=v18 \
		  --with-szlib \
		  --enable-fortran --enable-fortran2003 \
		  --enable-hltools \
		  --enable-ros3-vfd

FLAVOR_FLAGS =	  --includedir=\$${prefix}/include/hdf5/$(1) \
		  --with-flavor=$(1) \
		  --with-default-plugindir=/usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/$(1)/plugins

# --enable-threadsafe --enable-cxx are not officially supported then --enable-unsupported
SERIAL_FLAGS = $(call FLAVOR_FLAGS,serial) --enable-unsupported --enable-threadsafe --enable-cxx

# No java >= 1.7 on hppa and hurd-i386
ifeq (,$(filter $(DEB_HOST_ARCH),hppa hurd-i386))
SERIAL_FLAGS += --enable-java
DH_HELPERS = --with javahelper
install_jni := install_jni
dh_install_java := dh_install_java
PACKAGES_java := libhdf5-java libhdf5-jni
else
export DH_OPTIONS += -Nlibhdf5-java -Nlibhdf5-jni
endif

# --enable-parallel --enable-cxx are not officially supported then --enable-unsupported
OPENMPI_FLAGS = $(call FLAVOR_FLAGS,openmpi) --enable-unsupported --enable-cxx --enable-parallel=yes
MPICH_FLAGS = $(call FLAVOR_FLAGS,mpich) --enable-unsupported --enable-cxx --enable-parallel=yes

SERIAL_ENV := H5_LDFLAGS="-Wl,--version-script,\$$(top_srcdir)/debian/map_serial.ver"
MPI_ENV = H5_LDFLAGS="-Wl,--version-script,\$$(top_srcdir)/debian/map_mpi.ver" \
	CC=mpicc.$(1) CXX=mpicxx.$(1) FC=mpif90.$(1) F9X=mpif90.$(1) RUNPARALLEL=/usr/bin/mpirun.$(1)
OPENMPI_ENV := $(call MPI_ENV,openmpi)
MPICH_ENV := $(call MPI_ENV,mpich)

# Fix FTBFS on kfreebsd-*
ifneq (,$(filter $(DEB_HOST_ARCH),kfreebsd-i386 kfreebsd-amd64))
SERIAL_ENV += H5_CPPFLAGS="-I/usr/lib/jvm/default-java/include/bsd"
endif

# Target Java 1.8 bytecode
SERIAL_ENV += JAVACFLAGS="-source 1.8 -target 1.8"

# END - Configure flags and environment variables

# pkg-config 'Requires' flag
PKGCONFIG_REQ_serial =
PKGCONFIG_REQ_openmpi = ompi
PKGCONFIG_REQ_mpich = mpich

PACKAGES_serial := $(serpack) $(package)-fortran-$(SONAME_F) $(package)-hl-$(SONAME_HL) $(package)-hl-fortran-$(SONAME_HL_F) $(package)-dev hdf5-helpers hdf5-tools $(sercpppack) $(package)-hl-cpp-$(SONAME_HL_CXX) $(PACKAGES_java)
PACKAGES_openmpi := $(openmpipack) $(package)-openmpi-fortran-$(SONAME_F) $(package)-openmpi-hl-$(SONAME_HL) $(package)-openmpi-hl-fortran-$(SONAME_HL_F) $(package)-openmpi-cpp-$(SONAME_CXX)$(EXT) $(package)-openmpi-hl-cpp-$(SONAME_HL_CXX) $(package)-openmpi-dev
PACKAGES_mpich := $(mpichpack) $(package)-mpich-fortran-$(SONAME_F) $(package)-mpich-hl-$(SONAME_HL) $(package)-mpich-hl-fortran-$(SONAME_HL_F) $(package)-mpich-cpp-$(SONAME_CXX)$(EXT) $(package)-mpich-hl-cpp-$(SONAME_HL_CXX) $(package)-mpich-dev

# Compose the packages' name flavor part from $(flavor)
flavorpkg = $(subst -serial,,-$(flavor))

# Generate helper files list from debian/*.in
# debian/libhdf5-flavor-cpp-soname_cxx.install.in will produce:
# - debian/libhdf5-flavor-cpp-$(SONAME_CXX).install
# - debian/libhdf5-serial-cpp-$(SONAME_CXX).install
# - debian/libhdf5-openmpi-cpp-$(SONAME_CXX).install
# - debian/libhdf5-mpich-cpp-$(SONAME_CXX).install
# debian/hdf5-flavor.pc.in will produce:
# - debian/hdf5-flavor.pc
# - debian/hdf5-serial.pc
# - debian/hdf5-openmpi.pc
# - debian/hdf5-mpich.pc
DEBIAN_OUT := $(shell \
  ls -1 debian/*.in | \
  awk '\
    BEGIN {$(foreach flavor,$(FLAVORS),flavor["$(flavor)"]="$(flavorpkg)";)} \
    { \
      gsub(".in$$","",$$0); \
      gsub("soname_f","$(SONAME_F)",$$0); \
      gsub("soname_hl_f","$(SONAME_HL_F)",$$0); \
      gsub("soname_hl_cxx","$(SONAME_HL_CXX)",$$0); \
      gsub("soname_hl","$(SONAME_HL)",$$0); \
      gsub("soname_cxx","$(SONAME_CXX)$(EXT)",$$0); \
      gsub("soname","$(SONAME)$(EXT)",$$0); \
    } \
    /-flavor/{helpers=$$0; for (f in flavor) {helper=$$0; gsub("-flavor",flavor[f],helper); helpers=helpers" "helper}; $$0=helpers} \
    !/control/ {print $$0} \
  ' \
)
debian_out: $(DEBIAN_OUT);
# Requested to prevent deletion of $(DEBIAN_OUT) files as intermediate files
.SECONDARY: $(DEBIAN_OUT)

# Force debian/control update
.PHONY: debian/control.in

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

# Informative target
info: VARIABLES := package pkgversion upstreamversion libversion $(foreach interface,$(INTERFACES),SONAME$(istr))
info: VARIABLES += FLAVORS DH_OPTIONS DEBIAN_OUT
info: VARIABLES += $(foreach flavor,serial openmpi mpich,PACKAGES_$(flavor))
info: VARIABLES += UA_PRIORITY_serial UA_PRIORITY_mpi UA_PRIORITY_openmpi UA_PRIORITY_mpich
info:
	@$(foreach var,$(VARIABLES), echo "$(shell printf "%30s" "$(var)") = $($(var))" &&) true


# Link soname helper files to their respective templates
# debian/libhdf5-flavor-cpp-$(SONAME_CXX).install.in -> debian/libhdf5-flavor-cpp-soname_cxx.install.in
# debian/libhdf5-flavor-hl-fortran-$(SONAME_HL_F).install.in -> debian/libhdf5-flavor-hl-fortran-soname_hl_f.install.in
define helper_soname_rule
debian/libhdf5-flavor$(1)-$($(2))$(3)%: debian/libhdf5-flavor$(1)-$(shell echo $(2) | tr '[:upper:]' '[:lower:]')%
	ln -s $$$$(basename $$<) $$@
endef
$(foreach interface,$(INTERFACES),$(eval $(call helper_soname_rule,$(call INTERFACE_TO_API,$(istr)),SONAME$(istr),$(if $(filter $(interface),@ _CXX),$(EXT),))))

# These snippets generate every needed helper file from their respective
# template
# debian/control: debian/control.in
# debian/libhdf5-jni.install: debian/libhdf5-jni.install.in
# debian/libhdf5-flavor-cpp-$(SONAME_CXX).install: debian libhdf5-flavor-cpp-$(SONAME_CXX).install.in
# debian/hdf5-flavor.pc: debian/hdf5-flavor.pc.in
debian/%: debian/%.in
	sed -e 's/@MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' \
	    -e 's/@SONAME_CXX@/$(SONAME_CXX)/g' \
	    -e 's/@SONAME_HL_CXX@/$(SONAME_HL_CXX)/g' \
	    -e 's/@SONAME_F@/$(SONAME_F)/g' \
	    -e 's/@SONAME_HL@/$(SONAME_HL)/g' \
	    -e 's/@SONAME_HL_F@/$(SONAME_HL_F)/g' \
	    -e 's/@SONAME@/$(SONAME)/g' \
	    -e "s/@OMPIARCHS@/$(filter-out !%,$(OMPIARCHS))/" \
	    -e "s/@MPICHARCHS@/$(filter-out !%,$(MPICHARCHS))/" \
	    -e "s/@NODOCARCHS@/$(NODOCARCHS)/g" \
	    -e "s/@DEFAULT_MPI@/$(DEFAULT_MPI)/" \
	    -e "s/@EXT@/$(EXT)/g" \
	    -e "s/@UA_PRIORITY_MPI@/$(UA_PRIORITY_mpi)/g" \
	    -e "s/@VERSION@/$(libversion)/g" \
	  $< >$@

# debian/libhdf5-cpp-$(SONAME_CXX).install: debian/libhdf5-flavor-cpp-$(SONAME_CXX).install
# debian/libhdf5-openmpi-cpp-$(SONAME_CXX).install: debian/libhdf5-flavor-cpp-$(SONAME_CXX).install
# debian/libhdf5-serial-cpp-$(SONAME_CXX).install: debian/libhdf5-flavor-cpp-$(SONAME_CXX).install
# debian/hdf5.pc: debian/hdf5-flavor.pc
# debian/hdf5-openmpi.pc: debian/hdf5-flavor.pc
# debian/hdf5-mpich.pc: debian/hdf5-flavor.pc
define helper_flavor_rule
debian/$(3)$(2)%: flavor = $(1)
debian/$(3)$(2)%: ifmpi = $(if $(findstring mpi,$(1)),,\#)
debian/$(3)$(2)%: ifserial = $(if $(findstring mpi,$(1)),\#,)
debian/$(3)$(2)%: ua_priority_flavor = $(UA_PRIORITY_$(1))
debian/$(3)$(2)%: pkgconfig_req_flavor = $(PKGCONFIG_REQ_$(1))
debian/$(3)$(2)%: debian/$(3)-flavor%
	sed -e 's/@FLAVORLIB@/'_$$(flavor)'/g' \
	    -e 's/@FLAVORPKG@/'$$(flavorpkg)'/g' \
	    -e 's/@FLAVOR@/'$$(flavor)'/g' \
	    -e 's/@IFMPI@/'$$(ifmpi)'/g' \
	    -e 's/@IFSERIAL@/'$$(ifserial)'/g' \
	    -e 's/@UA_PRIORITY_FLAVOR@/$$(ua_priority_flavor)/g' \
	    -e 's/@PKGCONFIG_REQ_FLAVOR@/$$(pkgconfig_req_flavor)/g' \
	  $$< >$$@
endef
$(foreach prefix,hdf5 libhdf5,$(foreach flavor,$(FLAVORS),$(eval $(call helper_flavor_rule,$(flavor),$(flavorpkg),$(prefix)))))

# End of helper files generation snippets


%:
	dh $@ $(DH_HELPERS)

override_dh_auto_clean: debian/control
	rm -f $(DEBIAN_OUT)
	rm -fr debian/build-*
	rm -f c++/src/cpp_doc_config_u
	rm -fr html

override_dh_clean:
	dh_clean -Xh5ex_g_iterate.orig

configure_%: flavor = $(patsubst configure_%,%,$@)
configure_%: FLAVOR = $(shell echo $(flavor) | tr '[:lower:]' '[:upper:]')
configure_%: flavor_map = $(or $(findstring mpi,$(flavor)),serial)
configure_%: builddir = debian/build-$(flavor)
configure_%: $(DEBIAN_OUT)
	$($(FLAVOR)_ENV) dh_auto_configure --builddirectory=$(builddir) -- $(CONFIGURE_FLAGS) $($(FLAVOR)_FLAGS)
	# Remove build path from libhdf5-$(flavor).settings (Reproducible Builds)
	# This have to be done before the build because this file is included
	# into the library
	sed -i 's:$(CURDIR):$$(top_srcdir):g' $(builddir)/src/libhdf5.settings

override_dh_auto_configure: $(foreach flavor,$(FLAVORS),configure_$(flavor))

build_%: flavor = $(patsubst build_%,%,$@)
build_%: builddir = debian/build-$(flavor)
build_%:
	dh_auto_build --builddirectory=$(builddir)

override_dh_auto_build-arch: $(foreach flavor,$(FLAVORS),build_$(flavor))

install_%: flavor = $(patsubst install_%,%,$@)
install_%: builddir = $(CURDIR)/debian/build-$(flavor)
install_%: destdir = $(builddir)/tmpinst
install_%: destdir_jni = $(builddir)/tmpinst-jni
install_%:
	dh_auto_install --builddirectory=$(builddir) --destdir=$(destdir)
	chrpath -d $(destdir)/usr/lib/*/libhdf5*so*

install_jni: serial_builddir = $(CURDIR)/debian/build-serial
install_jni: serial_destdir = $(serial_builddir)/tmpinst
install_jni: install_serial
	dh_auto_install --builddirectory=$(serial_builddir)/java/src/jni --destdir=$(serial_destdir)
	jnilib=$(serial_destdir)/usr/lib/$(DEB_HOST_MULTIARCH)/libhdf5_java.so && \
	install -D $(serial_destdir)/usr/lib/$(DEB_HOST_MULTIARCH)/libhdf5_java.so \
		$(serial_destdir)/usr/lib/$(DEB_HOST_MULTIARCH)/jni/libhdf5_java.so

override_dh_auto_install-arch: $(foreach flavor,$(FLAVORS),install_$(flavor)) $(install_jni)

dh_install_%: flavor = $(patsubst dh_install_%,%,$@)
dh_install_%: builddir = debian/build-$(flavor)
dh_install_%: destdir = $(builddir)/tmpinst
dh_install_%: devpkg = $(package)$(flavorpkg)-dev
dh_install_%:
	dh_install $(foreach pkg,$(PACKAGES_$(flavor)),-p$(pkg)) --sourcedir=$(destdir)
	install -d debian/$(devpkg)/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	install -m644 debian/hdf5$(flavorpkg).pc debian/$(devpkg)/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/hdf5-$(flavor).pc

override_dh_install-arch: $(foreach flavor,$(FLAVORS),dh_install_$(flavor))
	rm -f debian/$(serpackdev)/usr/lib/$(DEB_HOST_MULTIARCH)/libhdf5_java*
	# Drop dpkg-buildflags from helpers to improve reproducibility
	sed -i $(foreach flags,CFLAGS CXXFLAGS CPPFLAGS FCFLAGS LDFLAGS,-e '/_$(flags)=/s/$(shell dpkg-buildflags --get $(flags) | sed 's:/:\\/:g')//') \
	  $(wildcard debian/hdf5-helpers/usr/bin/h5* debian/libhdf5-*mpi*-dev/usr/bin/h5*)

override_dh_makeshlibs:
	dh_makeshlibs -- -v$(libversion)
	echo "hdf5-mpi-dev=libhdf5-$(DEFAULT_MPI)-dev" >> debian/libhdf5-mpi-dev.substvars

override_dh_shlibdeps:
	# Workaround for #842881; this forces /usr/lib/$(DEB_HOST_MULTIARCH)
	# before /usr/lib in the search path, so that the right
	# libmpich.so.12 is matched first
	dh_shlibdeps -l/usr/lib/$(DEB_HOST_MULTIARCH)

build_doc:
	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 -fr html/cpplus_RM
	mkdir -p html
	mv c++/src/cpplus_RM html/cpplus_RM

dh_install_doc: HTML_DIR := debian/$(package)-doc/usr/share/doc/$(package)-doc/html
dh_install_doc:
	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" "{}" \;

dh_install_java: build_serial
	mv debian/build-serial/java/src/jarhdf5-$(upstreamversion).jar debian/build-serial/java/src/jarhdf5.jar

override_dh_auto_build-indep: build_doc build_serial
override_dh_auto_install-indep: $(dh_install_java)
override_dh_install-indep: dh_install_serial dh_install_doc

override_dh_installchangelogs:
	dh_installchangelogs -k release_docs/RELEASE.txt

override_dh_compress:
	dh_compress -Xpdf

auto_test_%: flavor = $(patsubst auto_test_%,%,$@)
auto_test_%: builddir = debian/build-$(flavor)
auto_test_%:
	dh_auto_test -B$(builddir)

# HDFFV-10309 - long double tests fail on ppc64el
#    elmt 107:
#       src =  00 3f ff ff ff ff ff ff                                     18014398509481983
#       dst =  00 00 00 00 00 00 00 00 43 50 00 00 00 00 00 00      18014398509481984.000000
#       ans =  bf f0 00 00 00 00 00 00 43 50 00 00 00 00 00 00      18014398509481983.000000
ifneq (ppc64el,$(DEB_HOST_ARCH))
auto_test_serial := auto_test_serial
endif
override_dh_auto_test: $(auto_test_serial)
