#!/usr/bin/make -f

#export DH_VERBOSE=1

# export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE  )
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE )
DEB_BUILD_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS  )
ARCH                := $(shell dpkg-architecture -qDEB_BUILD_ARCH     )

Q_LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed
export Q_LDFLAGS

# of course, qmake wants to do it its own way
QMAKE_CXXFLAGS += $(CPPFLAGS) -Wall -Wshadow -Wextra -ggdb3
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  QMAKE_CXXFLAGS_RELEASE = -O0
else
  QMAKE_CXXFLAGS_RELEASE = -O2
endif

export QT_SELECT=qt5

%:
	dh $@

override_dh_auto_build:
	qmake -makefile -after \
		QMAKE_CXXFLAGS="$(QMAKE_CXXFLAGS)" \
		QMAKE_CXXFLAGS_RELEASE="$(QMAKE_CXXFLAGS_RELEASE)" \
		QMAKE_LFLAGS="$(Q_LDFLAGS)"
	# libpthread is only used indirectly; no need for linking against it
	sed -i -e 's/-lpthread//' Makefile

	$(MAKE)

override_dh_clean:
	test ! -f Makefile  ||  $(MAKE) distclean
	dh_clean
