#!/usr/bin/make -f

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


include /usr/share/dpkg/architecture.mk

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

# int = default build (using int, giving 32-bit indexing)
# int64 = 64-bit indexing only (with standard integers)
# int64i = full 64-bit integers and indexing
BIT_BUILDS = int int64 int64i

BUILDDIR_BASE=obj-$(DEB_HOST_MULTIARCH)

# Add here any variable or target overrides you need.
# where sources are
DEB_SRCDIR = $(CURDIR)/src

# no check for this software
#DEB_MAKE_CHECK_TARGET :=

CMAKE_FLAGS_COMMON = -DBUILD_SHARED_LIBS=ON \
			-DUSE_ZLIB=ON \
			-DUSE_BZ2=ON \
			-DUSE_LZMA=ON \
			-DTHREADS=ON \
			-DMPI_THREAD_MULTIPLE=ON \
			-DSCOTCH_DETERMINISTIC=FIXED_SEED \
			-DLIBSCOTCHERR=scotcherr \
			-DLIBPTSCOTCHERR=ptscotcherr

SCOTCH_CONFIG_CFLAGS = -DSCOTCH_PTHREAD_NUMBER=2 \
			-DCOMMON_PTHREAD_FILE

# hurd does not have POSIX time and therefore needs sys/time.h to define struct timeval
# hurd does not support pthread_setaffinity_np (-DSCOTCH_PTHREAD_AFFINITY_LINUX)
ifeq ($(DEB_BUILD_ARCH_OS),hurd)
  SCOTCH_CONFIG_CFLAGS += -DHAVE_SYS_TIME_H=ON
endif

# By default, halt on test (check) failures. But,
# - hurd times out on buildd during build-time checks
# - m68k segfaults inside the qemu emulator used for builds
# - powerpcspe segfaults on test_scotch_graph_order in INTSIZE64
# so don't halt on test failures on these arches.
# In fact don't run tests at all on hurd.
ifeq ($(DEB_HOST_ARCH_OS),hurd)
  HALT_ON_TEST_ERROR = no
  SKIP_TESTS = yes
else ifeq ($(DEB_HOST_ARCH),m68k)
  HALT_ON_TEST_ERROR = no
else ifeq ($(DEB_HOST_ARCH),powerpcspe)
  HALT_ON_TEST_ERROR = no
else
  HALT_ON_TEST_ERROR = yes
endif
ifeq (nocheck,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
  SKIP_TESTS = yes
endif


# mord_ship001 fails with full 64-bit support (_64i) on some architectures
# skip the flakey test on these arches
64i_SKIP_FLAKEY_TESTS_ARCH_LIST = armel armhf ppc64el s390x alpha powerpc ppc64
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(64i_SKIP_FLAKEY_TESTS_ARCH_LIST)$(space)))
  CMAKE_EXTRA_FLAGS_64i = -DSKIP_FLAKEY_TESTS=ON
endif


DEB_STRIPPED_UPSTREAM_VERSION = $(shell echo $(DEB_UPSTREAM_VERSION) | sed -n -e 's/\.dfsg.*$$//p')
SCOTCH_VERSION=$(DEB_STRIPPED_UPSTREAM_VERSION)

export PRTE_MCA_plm_ssh_agent=/bin/false      # workaround to start MPI-applications in chroot
export OMPI_MCA_btl_tcp_if_include=lo         # avoid complaints about unreachable pairing between local and remote
export PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe  # oversubscribing allows s390x to use hardware threads

export DEB_CFLAGS_MAINT_APPEND = $(SCOTCH_CONFIG_CFLAGS)

%:
	dh $@ --buildsystem=cmake

override_dh_auto_configure:
	for BITS in $(BIT_BUILDS); do \
            if [ $$BITS = int64 ]; then \
                LIBNAME_SUFFIX=_64; \
                bit_flags="-DIDXSIZE=64 -DSCOTCH_SONAME_SUFFIX=$${LIBNAME_SUFFIX}"; \
            elif [ $$BITS = int64i ]; then \
                LIBNAME_SUFFIX=_64i; \
                bit_flags="-DINTSIZE=64 -DIDXSIZE=64 -DSCOTCH_SONAME_SUFFIX=$${LIBNAME_SUFFIX} $(CMAKE_EXTRA_FLAGS_64i)"; \
            else \
                LIBNAME_SUFFIX=; \
                bit_flags=; \
            fi; \
            echo "Configure for scotch $$BITS" ; \
	    dh_auto_configure --builddirectory=$(BUILDDIR_BASE)$${LIBNAME_SUFFIX} -- $(CMAKE_FLAGS_COMMON) $${bit_flags}; \
	done

override_dh_auto_build:
	set -e; \
	for BITS in $(BIT_BUILDS); do \
            if [ $$BITS = int64 ]; then \
                LIBNAME_SUFFIX=_64; \
            elif [ $$BITS = int64i ]; then \
                LIBNAME_SUFFIX=_64i; \
            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 = int64i ]; then \
                LIBNAME_SUFFIX=_64i; \
            else \
                LIBNAME_SUFFIX=; \
            fi; \
            echo "Testing for $$BITS" ; \
	    dh_auto_test -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 = int64i ]; then \
                LIBNAME_SUFFIX=_64i; \
            else \
                LIBNAME_SUFFIX=; \
            fi; \
            echo "Installing for $$BITS" ; \
	    dh_auto_install -O--parallel --builddirectory=$(BUILDDIR_BASE)$${LIBNAME_SUFFIX}; \
	done

override_dh_installman:
	dh_installman
	mv debian/scotch/usr/share/man/man1/gmap.1 debian/scotch/usr/share/man/man1/scotch_gmap.1
	mv debian/scotch/usr/share/man/man1/gbase.1 debian/scotch/usr/share/man/man1/scotch_gbase.1
	(cd debian/scotch/usr/share/man/man1 && ln -sf scotch_gmap.1.gz scotch_gpart.1.gz)

execute_after_dh_auto_clean:
	for BITS in $(BIT_BUILDS); do \
            if [ $$BITS = int64 ]; then \
                LIBNAME_SUFFIX=_64; \
            elif [ $$BITS = int64i ]; then \
                LIBNAME_SUFFIX=_64i; \
            else \
                LIBNAME_SUFFIX=; \
            fi; \
	   rm -rf $(BUILDDIR_BASE)$${LIBNAME_SUFFIX}; \
	done
