#!/usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.

include /usr/share/dpkg/default.mk
include /usr/share/dpkg/architecture.mk

BUILDDIR_BASE=obj-$(DEB_HOST_MULTIARCH)

DEB_VERSION := $(shell dpkg-parsechangelog -S Version)
DEB_NOEPOCH_VERSION := $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//')

HYPRE_UPSTREAM_VERSION=$(DEB_UPSTREAM_VERSION)
HYPRE_X_Y_VERSION=$(shell echo $(HYPRE_UPSTREAM_VERSION) | sed "s/^\(.*\)\.\([^.]*\)$$/\1/")
HYPRE_MAJOR_VERSION=$(shell echo $(HYPRE_UPSTREAM_VERSION) | cut -d '.' -f 1)
HYPRE_MINOR_VERSION=$(shell echo $(HYPRE_UPSTREAM_VERSION) | cut -d '.' -f 2)

export HYPRE_SOVERSION=$(shell echo $$(( $(HYPRE_MAJOR_VERSION)*100 + $(HYPRE_MINOR_VERSION) )) )

export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic -O3
export DEB_CXXFLAGS_MAINT_APPEND  = -Wall -pedantic -O3
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--no-as-needed
export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# int = default build (using int, giving 32-bit indexing)
BIT_BUILDS = int

%:
	dh $@ --buildsystem=cmake --sourcedirectory=src

# to avoid lintian warnings
CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

# 64-bit build is not supported on all arches
ifeq (64,$(DEB_HOST_ARCH_BITS))
# int64 = full 64-bit integers and indexing
# int64m = mixed support (64-bit indexing only with standard integers)
BIT_BUILDS += int64 int64m
else
export DH_OPTIONS += \
	-Nlibhypre64-dev -Nlibhypre64-${HYPRE_SOVERSION} \
	-Nlibhypre64m-dev -Nlibhypre64m-${HYPRE_SOVERSION}
endif

CMAKE_FLAGS_COMMON += -DBUILD_SHARED_LIBS=On \
			-DHYPRE_ENABLE_FMANGLE=ONE_UNDERSCORE \
			-DHYPRE_ENABLE_DSUPERLU=ON \
			-DHYPRE_BUILD_TESTS=On \
			-DCMAKE_SKIP_INSTALL_RPATH=True

export PRTE_MCA_plm_ssh_agent=/bin/false
export PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe
export OMPI_MCA_btl_tcp_if_include=lo

override_dh_auto_clean:
	for BITS in $(BIT_BUILDS); do \
	    if [ $$BITS = int64 ]; then \
	        LIBNAME_SUFFIX=64; \
	    elif [ $$BITS = int64m ]; then \
	        LIBNAME_SUFFIX=64m; \
	    else \
	        LIBNAME_SUFFIX=; \
	    fi; \
	   dh_auto_clean --builddirectory=$(BUILDDIR_BASE)$${LIBNAME_SUFFIX}; \
	done
	rm -f src/seq_mv/csr_spgemm_device_numer?*.c src/seq_mv/csr_spgemm_device_symbl?*.c

override_dh_auto_configure:
	for BITS in $(BIT_BUILDS); do \
	    if [ $$BITS = int64 ]; then \
	        LIBNAME_SUFFIX=64; \
	        bit_flags="-DHYPRE_ENABLE_BIGINT=On"; \
	    elif [ $$BITS = int64m ]; then \
	        LIBNAME_SUFFIX=64m; \
	        bit_flags="-DHYPRE_ENABLE_MIXEDINT=On"; \
	    else \
	        LIBNAME_SUFFIX=; \
	        bit_flags=; \
	    fi; \
	    echo "Configure for scotch $$BITS" ; \
	    dh_auto_configure --builddirectory=$(BUILDDIR_BASE)$${LIBNAME_SUFFIX} -- $(CMAKE_FLAGS_COMMON) $${bit_flags} -DHYPRE_LIBNAME_EXT=$${LIBNAME_SUFFIX}; \
	done


old_override_dh_auto_configure:
	dh_auto_configure -- --enable-shared $(extra_flags_common) \
	  --with-blas=yes --with-blas-lib="$(shell pkg-config --libs blas)" \
	  --with-lapack=yes \
	    --with-lapack-lib="$(shell pkg-config --libs lapack)" \
	    --with-fmangle-lapack="one-underscore"   \
	  --prefix=$(CURDIR)/debian/tmp/usr
	dh_auto_configure -Dsrc-static -- $(extra_flags_common) \
	  --with-blas=yes --with-blas-lib="$(shell pkg-config --libs blas)" \
	  --with-lapack=yes \
	    --with-lapack-lib="$(shell pkg-config --libs lapack)" \
	    --with-fmangle-lapack="one-underscore"   \
	  --prefix=$(CURDIR)/debian/tmp-static/usr
ifeq (64,$(DEB_HOST_ARCH_BITS))
	dh_auto_configure -Dsrc64 -- --enable-bigint --with-libname-ext=64 \
	  --enable-shared $(extra_flags_common) \
	  --with-blas=yes --with-blas-lib="$(shell pkg-config --libs blas64)" \
	  --with-lapack=yes \
	    --with-lapack-lib="$(shell pkg-config --libs lapack64)" \
	    --with-fmangle-lapack="one-underscore"   \
	  --prefix=$(CURDIR)/debian/tmp64/usr
	dh_auto_configure -Dsrc64m -- --enable-mixedint --with-libname-ext=64m \
	  --enable-shared $(extra_flags_common) \
	  --with-blas=yes --with-blas-lib="$(shell pkg-config --libs blas64)" \
	  --with-lapack=yes \
	    --with-lapack-lib="$(shell pkg-config --libs lapack64)" \
	    --with-fmangle-lapack="one-underscore"   \
	  --prefix=$(CURDIR)/debian/tmp64m/usr
	dh_auto_configure -Dsrc64-static -- --enable-bigint --with-libname-ext=64 \
	  $(extra_flags_common) \
	  --with-blas=yes --with-blas-lib="$(shell pkg-config --libs blas64)" \
	  --with-lapack=yes \
	    --with-lapack-lib="$(shell pkg-config --libs lapack64)" \
	    --with-fmangle-lapack="one-underscore"   \
	  --prefix=$(CURDIR)/debian/tmp64-static/usr
	dh_auto_configure -Dsrc64m-static -- --enable-mixedint --with-libname-ext=64m \
	  $(extra_flags_common) \
	  --with-blas=yes --with-blas-lib="$(shell pkg-config --libs blas64)" \
	  --with-lapack=yes \
	    --with-lapack-lib="$(shell pkg-config --libs lapack64)" \
	    --with-fmangle-lapack="one-underscore"   \
	  --prefix=$(CURDIR)/debian/tmp64m-static/usr
	#dh_auto_configure -p libhypre-complex -- $(extra_flags) --enable-shared --enable-complex
endif

override_dh_auto_build:
	set -e; \
	for BITS in $(BIT_BUILDS); do \
	    if [ $$BITS = int64 ]; then \
	        LIBNAME_SUFFIX=64; \
	    elif [ $$BITS = int64m ]; then \
	        LIBNAME_SUFFIX=64m; \
	    else \
	        LIBNAME_SUFFIX=; \
	    fi; \
	   echo "Compiling for $$BITS"; \
	   dh_auto_build -O--parallel --builddirectory=$(BUILDDIR_BASE)$${LIBNAME_SUFFIX}; \
	done

override_dh_auto_test:
	set -e; \
	for BITS in $(BIT_BUILDS); do \
	    if [ $$BITS = int64 ]; then \
	        LIBNAME_SUFFIX=64; \
	    elif [ $$BITS = int64m ]; then \
	        LIBNAME_SUFFIX=64m; \
	    else \
	        LIBNAME_SUFFIX=; \
	    fi; \
	    echo "Testing for $$BITS" ; \
	    cd $(CURDIR)/$(BUILDDIR_BASE)$${LIBNAME_SUFFIX}; ctest --output-on-failure $(TEST_ARGS_SKIP); \
	done

override_dh_auto_install:
	set -e; \
	for BITS in $(BIT_BUILDS); do \
	    if [ $$BITS = int64 ]; then \
	        LIBNAME_SUFFIX=64; \
	    elif [ $$BITS = int64m ]; then \
	        LIBNAME_SUFFIX=64m; \
	    else \
	        LIBNAME_SUFFIX=; \
	    fi; \
	    echo "Installing for $$BITS" ; \
	    dh_auto_install -O--parallel --builddirectory=$(BUILDDIR_BASE)$${LIBNAME_SUFFIX}; \
	done

# don't run tests if the nocheck build option has been specified
RUNTEST=yes
ifeq (nocheck,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
  RUNTEST=no
endif

empty :=
space := $(empty)$(empty)

# ia64 does not fail to run tests, it fails to build them
ARCH_NO_TEST_LIST = ia64

ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(ARCH_NO_TEST_LIST)$(space)))
  RUNTEST=no
endif

# some arches consistently hang in checkpar tests
# possibly due to pmix, see hypre upstream issue #955
ARCH_NO_CHECKPAR_LIST = ppc64el hppa hurd-amd64 hurd-i386

ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(ARCH_NO_CHECKPAR_LIST)$(space)))
  RUN_CHECKPAR=no
endif

# Standard tolerance 1e-6 is too tight on many arches
# so relax them to 1e-3
ARCH_RELAX_TOL_LIST = arm64 i386 riscv64 s390x loong64 powerpc ppc64

ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(ARCH_RELAX_TOL_LIST)$(space)))
  RELAX_TOL = HYPRE_CHECKPAR_TOL=0.05
  RUN_CHECKPAR=no
endif

ifeq (64,$(DEB_HOST_ARCH_BITS))
TEST_SRC_DIRS = src src-static src64 src64-static
else
TEST_SRC_DIRS = src src-static
endif

old_override_dh_auto_test:
	set -e; \
	  echo "max available processors nproc=`nproc`"; \
	if [ "$(RUNTEST)" = "no" ]; then \
	  echo Tests have been disabled on $(DEB_HOST_ARCH); \
	else echo "=== Building tests ==="; \
	  for build in $(TEST_SRC_DIRS); do \
	    echo "=== Running tests from $$build ==="; \
	    cd $$build; \
	    TEST_LD_LIBRARY_PATH=$(CURDIR)/$$build/lib:$(LD_LIBRARY_PATH); \
	    echo "== running make check (single) =="; \
	    LD_LIBRARY_PATH=$${TEST_LD_LIBRARY_PATH} make check; \
	    echo "== running make check (MPI -P 3 1 1) =="; \
	    LD_LIBRARY_PATH=$${TEST_LD_LIBRARY_PATH} CHECKRUN="mpirun -n 3" PARMS='-P 3 1 1' make check; \
	    if [ "x$(RUN_CHECKPAR)" = "xno" ]; then \
	      echo "== skipping make checkpar on $(DEB_HOST_ARCH) =="; \
	    else \
	      echo "== running make checkpar =="; \
	      LD_LIBRARY_PATH=$${TEST_LD_LIBRARY_PATH} make checkpar $(RELAX_TOL); \
	    fi; \
	    cd ..; \
	  done; \
	fi
