#!/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

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_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)

# ctest runs checktest.sh to check output values, same as make checkpar
# but on some architectures they vary a little from the reference values, see Bug#126042
# so ctest fails.  Use the simpler make check on these arches.
ARCH_NO_CHECKTEST_LIST = arm64 i386 loong64 riscv64 s390x powerpc ppc64 sparc64

ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(ARCH_NO_CHECKTEST_LIST)$(space)))
  RUN_CHECKTEST=no
endif

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 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 $$BITS" ; \
	    cd $(CURDIR)/$(BUILDDIR_BASE)$${LIBNAME_SUFFIX}; \
	    if [ "x$(RUN_CHECKTEST)" = "xno" ]; then \
	      LD_LIBRARY_PATH=$(CURDIR)/$(BUILDDIR_BASE)$${LIBNAME_SUFFIX}/lib:$(LD_LIBRARY_PATH) make check; \
	    else \
	      ctest --output-on-failure $(TEST_ARGS_SKIP); \
	    fi; \
	done; fi
