#!/usr/bin/make -f
# -*- makefile -*-

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

# compile with c++11 threads only architectures with 64bit __atomic_exchange,
# which seems to be unsupported on powerpc, powerpcspe, m68k, mips, mipsel.
# see buildd log
# https://buildd.debian.org/status/fetch.php?pkg=clasp&arch=mips&ver=3.2.1-1&stamp=1480664797
# and
# https://gcc.gnu.org/wiki/Atomic
# https://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary
arch = $(shell dpkg-architecture -qDEB_BUILD_ARCH | egrep -v -x "(powerpc|powerpcspe|m68k|mips|mipsel)")

# use custom hardening config flags for compiling clasp
CONFIG=hardening
CONFIGPARMS = --prefix=/usr
TOPBUILDDIR = build/$(CONFIG)

# use c++11 threads, threading implies _mt builddir
ifeq ($(if $(arch),good), good)
	CONFIGPARMS += --with-mt=std
	TOPBUILDDIR = build/$(CONFIG)_mt
endif

#
# export CXXFLAGS and LDFLAGS for configure.sh
#
# add -O3 and NDEBUG and enable all hardening flags
#
DEB_CXXFLAGS_MAINT_APPEND = -O3 -DNDEBUG
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# inject CPPFLAGS to CXXFLAGS
CXXFLAGS += $(CPPFLAGS)

%:
	dh $@

# call ./configure.sh $(CONFIGPARMS)
override_dh_auto_configure:
	./configure.sh --config=$(CONFIG) $(CONFIGPARMS)

# build in $(TOPBUILDDIR)
override_dh_auto_build:
	dh_auto_build --builddirectory=$(TOPBUILDDIR)

# install clasp binary built in $(TOPBUILDDIR)
override_dh_auto_install:
	dh_auto_install --builddirectory=$(TOPBUILDDIR) -- INSTALL_DIR=$(CURDIR)/debian/clasp/usr/bin

# remove the whole build dir
override_dh_auto_clean:
	rm -rf ./build

# install CHANGES as changelog
override_dh_installchangelogs:
	dh_installchangelogs CHANGES
