#!/usr/bin/make -f

# export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-z,noexecstack

# disable hardening-stackclash on armhf
# On armhf, valgrind test dies with a SIGSEGV when
# built with either -fstack-clash-protection or -fstack-check.
# And also on ppc64el.
ifeq ($(DEB_TARGET_ARCH), armhf)
export DEB_BUILD_MAINT_OPTIONS = hardening=hardening=+all,-stackclash
endif
ifeq ($(DEB_TARGET_ARCH), ppc64el)
export DEB_BUILD_MAINT_OPTIONS = hardening=hardening=+all,-stackclash
endif

# crossbuild
ifneq  ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
include /usr/share/dpkg/buildtools.mk
endif

%:
	dh $@ --with python3

# The debian/compiler directory contains gcc and clang wrappers.
# It adds Debian specifific options distributed in
# $CC, $CFLAGS, $CPPFLAGS and $LDFLAGS
export PATH := $(shell pwd)/debian/compiler:$(PATH)

# Detects the correct ABINAME from the DEB_BUILD_ARCH
# instead of run-time detection. Fixes FTBR.
ABINAME=$(DEB_BUILD_ARCH)
ifeq ($(ABINAME), x32)
ABINAME=amd64
endif

COMMAND_VALGRIND = $(shell command -v valgrind)

ifeq ($(filter /usr/bin/valgrind,$(COMMAND_VALGRIND)),)
DISABLE_VALGRIND += --no-valgrind
endif

override_dh_auto_configure:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	head -v $(shell find . -name mceliece.1)
	(! command -v pandoc) || autogen/man
	head -v $(shell find . -name mceliece.1)
endif
	./configure --host=$(ABINAME) --prefix=/usr $(DISABLE_VALGRIND)

override_dh_auto_clean:
	rm -rf ./build

# disable auto installation
override_dh_auto_install:
