#!/usr/bin/make -f

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

DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow
DEB_CFLAGS_MAINT_PREPEND = -Wall

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

# The following architectures still fail the test suite:
#   m68k
#     (emulated, no proper test results)
#   sh4
#     (emulated, no proper test results)
#   x32
#     (20: io_setup failed)
#   riscv64
#     (5: 512 instead of EFAULT on write)
#   sparc64
#     (5: 512 instead of EFAULT on write)
#     (19: mremap EINVAL)
#   hppa
#     (5: 512 instead of EFAULT on write)
#     (19: mremap EINVAL)
#     (17: looping, timeout?)
ifneq (,$(filter $(DEB_HOST_ARCH),m68k sh4 x32 riscv64 sparc64 hppa))
  TESTS_FLAKY = yes
endif

export CC

build-indep:

build-arch:
	dh_testdir
	
	$(MAKE) CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"

build: build-indep build-arch

clean:
	dh_testdir
	dh_testroot
	
	$(MAKE) clean
	
	dh_clean

check-arch: build-arch
	dh_testdir
	
ifeq (testsuitedisabled,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifeq ($(TESTS_FLAKY),yes)
	@echo "Notice: Running tests in flaky mode, errors will be ignored."
endif
	$(if $(TESTS_FLAKY),-)$(MAKE) partcheck \
	  CPPFLAGS="$(CPPFLAGS)" \
	  CFLAGS="$(CFLAGS)" \
	  LDFLAGS="$(LDFLAGS)"
endif

install-arch: check-arch
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs
	
	$(MAKE) install \
	  DESTDIR=$(CURDIR)/debian/tmp \
	  libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

binary: binary-indep binary-arch

binary-indep:
	# Nothing to do.

binary-arch: install-arch
	dh_testdir
	dh_testroot
	dh_install -a
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	dh_installexamples -a
	dh_installman -a
	dh_lintian -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

.PHONY: clean build-indep build-arch build
.PHONY: install-arch binary-indep binary-arch binary
