#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow

include /usr/share/dpkg/architecture.mk

extra_flags += \
	-DCMAKE_Fortran_COMPILER=gfortran \
	-DBUILD_SHARED_LIBS:BOOL=ON \
	-DBUILD_STATIC_LIBS:BOOL=ON \
	-DFCMIX_ENABLE:BOOL=ON \
	-DMPI_ENABLE:BOOL=ON \
	\
	-DSUNDIALS_INDEX_TYPE=int32_t \
	-DLAPACK_ENABLE:BOOL=ON \
	\
	-DEXAMPLES_ENABLE_C:BOOL=ON \
	-DEXAMPLES_ENABLE_CXX:BOOL=ON \
	-DEXAMPLES_ENABLE_F77:BOOL=ON \
	-DEXAMPLES_INSTALL:BOOL=ON \
	\
	-DOPENMP_ENABLE:BOOL=ON \
	-DPTHREAD_ENABLE:BOOL=ON \
	\
	-DKLU_ENABLE:BOOL=ON \
	-DKLU_INCLUDE_DIR=/usr/include/suitesparse \
	-DKLU_LIBRARY_DIR=/usr/lib/$(DEB_HOST_MULTIARCH) \
	\
	-DPETSC_ENABLE:BOOL=ON \
	\
	-DHYPRE_ENABLE:BOOL=ON \
	-DHYPRE_INCLUDE_DIR=/usr/include/hypre \
	-DHYPRE_LIBRARY=-lHYPRE_core



# Debian doesn't ship superlu-mt currently, so I turn off this support
extra_flags += -DSUPERLUMT_ENABLE:BOOL=OFF


BUILDDIR = $(CURDIR)/debian/build


%:
	dh $@ --buildsystem=cmake  --builddirectory=$(BUILDDIR)

override_dh_auto_configure:
	dh_auto_configure -- $(extra_flags)

# Hack from https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=839387
# The OpenMPI variable prevents FTBFS when there are not enough CPUs (see #910352 and #849974)
override_dh_auto_test:
	$(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),true,(cd debian/build; OMPI_MCA_rmaps_base_oversubscribe=1 ../nofakeroot ctest))

override_dh_auto_install:
	dh_auto_install
# I strip out all the RPATH/RUNPATH tags from my shared objects. These all point
# to libraries that actually live in standard locations, so the tags are not
# needed
	find debian/tmp/ -name '*.so' -o -name '*.so.*' -exec chrpath -d '{}' \;

# Strip extra license file
	rm debian/tmp/usr/include/sundials/LICENSE

# We copy READMEs by hand because they need to be renamed
override_dh_installdocs:
	dh_installdocs
	mkdir -p debian/libsundials-dev/usr/share/doc/libsundials-dev
	for module in arkode cvode cvodes ida idas kinsol; do \
		cp src/$${module}/README debian/libsundials-dev/usr/share/doc/libsundials-dev/README.$${module}; \
	done

override_dh_compress:
	dh_compress -Xexamples/

override_dh_auto_clean:
	dh_auto_clean
	rm -rf $(BUILDDIR)