#!/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}')
upstreamversionfull ?= $(shell echo $(pkgversion) | sed -r 's/^([0-9\.]*)([-~][^-~+]*)?(\+repack[0-9]*)?-[^-]*$$/\1\2/')
upstreamversion ?= $(shell echo $(pkgversion) | sed -r 's/^([0-9\.]*)([-~][^-~+]*)?(\+repack[0-9]*)?-[^-]*$$/\1/')
libversion ?= $(shell echo $(upstreamversion) | sed -r 's/^([0-9]+\.[0-9]+\.[0-9])\.([0-9]+)$$/\1-\2/')

# 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))
# 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)
serpackdev = $(package)-dev
sercpppack = $(package)-cpp-$(SONAME_CXX)
openmpipack = $(package)-openmpi-$(SONAME)
mpichpack = $(package)-mpich-$(SONAME)

# 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
ALL_FLAVORS = serial openmpi mpich
FLAVORS := serial

# OpenMPI not available on 32bit archs anymore
ifneq (${DEB_HOST_ARCH_BITS},32)
ARCHS_openmpi := $(OMPIARCHS)
else
ARCHS_openmpi :=
endif
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))
endef
$(foreach mpi,openmpi mpich,$(eval $(call CHECK_FLAVOR,$(mpi))))
# 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.
export DH_OPTIONS += $(foreach flavor,$(filter-out $(FLAVORS),$(ALL_FLAVORS)),$(foreach pkg,$(PACKAGES_$(flavor)),-N$(pkg)))
# END - Construct the FLAVORS list

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

CMAKE_FLAGS =     -DHDF5_INSTALL_LIB_DIR=lib/$(DEB_HOST_MULTIARCH) \
                  -DDEFAULT_API_VERSION=v114 \
                  -DCMAKE_BUILD_TYPE=$(USE_PROD) \
                  -DHDF5_ENABLE_OPTIMIZATION:BOOL=ON \
                  -DBUILD_SHARED_LIBS:BOOL=ON \
                  -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF \
                  -DHDF5_ENABLE_SZIP_SUPPORT:BOOL=ON \
                  -DHDF5_ENABLE_Z_LIB_SUPPORT:BOOL=ON \
                  -DHDF5_BUILD_FORTRAN:BOOL=ON \
                  -DHDF5_BUILD_HL_LIB:BOOL=ON \
                  -DHDF5_BUILD_TOOLS:BOOL=ON \
                  -DHDF5_ENABLE_ROS3_VFD:BOOL=ON \

FLAVOR_FLAGS =	  -DHDF5_BUILD_FLAVOR=$(1) \
                  -DHDF5_INSTALL_INCLUDE_DIR=include/hdf5/$(1) \
                  -DH5_DEFAULT_PLUGINDIR=lib/$(DEB_HOST_MULTIARCH)/hdf5/$(1)/plugins \

# -DHDF5_ENABLE_THREADSAFE=ON -DHDF5_BUILD_CPP_LIB=ON are not officially supported then -DALLOW_UNSUPPORTED=ON
SERIAL_FLAGS =    $(call FLAVOR_FLAGS,serial) \
                  -DALLOW_UNSUPPORTED:BOOL=ON \
                  -DHDF5_ENABLE_THREADSAFE:BOOL=ON \
                  -DHDF5_BUILD_CPP_LIB:BOOL=ON \

# No java >= 1.7 on hppa and hurd
ifeq (,$(filter $(DEB_HOST_ARCH),hppa hurd-amd64 hurd-i386)$(filter nojava,$(DEB_BUILD_PROFILES)))
# Target Java 1.8 bytecode
SERIAL_FLAGS +=   -DHDF5_BUILD_JAVA:BOOL=ON \
                  -DHDF5_INSTALL_JAR_DIR=lib \
                  -DJAVA_AWT_INCLUDE_PATH="NotNeeded" \
                  -DCMAKE_JAVA_COMPILE_FLAGS="-source;1.8;-target;1.8"
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) \
                  -DALLOW_UNSUPPORTED:BOOL=ON \
                  -DHDF5_BUILD_CPP_LIB:BOOL=ON \
                  -DHDF5_ENABLE_PARALLEL:BOOL=ON
MPICH_FLAGS =     $(call FLAVOR_FLAGS,mpich) \
                  -DALLOW_UNSUPPORTED:BOOL=ON \
                  -DHDF5_BUILD_CPP_LIB:BOOL=ON \
                  -DHDF5_ENABLE_PARALLEL:BOOL=ON

SERIAL_LDFLAGS := -Wl,--version-script,$(CURDIR)/debian/map_serial.ver
OPENMPI_LDFLAGS := -Wl,--version-script,$(CURDIR)/debian/map_mpi.ver
MPICH_LDFLAGS := -Wl,--version-script,$(CURDIR)/debian/map_mpi.ver
MPI_ENV = 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_CPPFLAGS := -I/usr/lib/jvm/default-java/include/bsd
endif

# END - Configure flags and environment variables

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) $(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) $(package)-mpich-hl-cpp-$(SONAME_HL_CXX) $(package)-mpich-dev

export DEB_CFLAGS_MAINT_STRIP = -Werror=format-security -Werror=implicit-function-declaration
export DEB_CXXFLAGS_MAINT_STRIP = -Werror=format-security

# 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_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)",$$0); \
      gsub("soname","$(SONAME)",$$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

# Informative target
info: VARIABLES := package pkgversion upstreamversionfull 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))%: 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))))

# 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/%: 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/@UA_PRIORITY_MPI@/$(UA_PRIORITY_mpi)/g" \
	    -e "s/@VERSION@/$(upstreamversion)/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
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)%: 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' \
	  $$< >$$@
endef
$(foreach prefix,hdf5 libhdf5,$(foreach flavor,$(FLAVORS),$(eval $(call helper_flavor_rule,$(flavor),$(flavorpkg),$(prefix)))))

# End of helper files generation snippets


%:
	dh $@ --buildsystem cmake $(DH_HELPERS)

override_dh_auto_clean: debian/control
	rm -f $(DEBIAN_OUT)
	rm -fr debian/build-*

override_dh_clean:
	dh_clean -Xh5ex_g_iterate.orig

override_dh_autoreconf:;



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) info
	CFLAGS="$$CFLAGS $($(FLAVOR)_CPPFLAGS)" \
	LDFLAGS="$$LDFLAGS $($(FLAVOR)_LDFLAGS)" \
	$($(FLAVOR)_ENV) dh_auto_configure --builddirectory=$(builddir) -- $(CMAKE_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_$(flavor).settings $(builddir)/src/H5build_settings.c

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_%:
	dh_auto_install --builddirectory=$(builddir) --destdir=$(destdir)
	chrpath -d $(destdir)/usr/lib/*/libhdf5*so*
	debian/flavor-layout $(flavor)

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)
	install -D $(serial_destdir)/usr/lib/$(DEB_HOST_MULTIARCH)/libhdf5_java.so \
		$(serial_destdir)/usr/lib/$(DEB_HOST_MULTIARCH)/jni/libhdf5_java.so
	rm $(serial_destdir)/usr/lib/$(DEB_HOST_MULTIARCH)/libhdf5_java.so*
	chrpath -d $(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)

override_dh_install-arch: $(foreach flavor,$(FLAVORS),dh_install_$(flavor))
	rm debian/libhdf5*-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libhdf5_tools*
	# 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$(upstreamversion)
	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)

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

dh_install_java: build_serial
	ln -s jarhdf5-$(libversion).jar debian/build-serial/java/src/hdf/hdf5lib/jarhdf5.jar

override_dh_auto_build-indep: 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)
