#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export CFLAGS=$(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export FFLAGS=$(shell dpkg-buildflags --get FFLAGS)

# [[ common flags ]]
# TODO: openmp or pthreads, or both?
# https://github.com/flame/libflame/issues/28#issuecomment-561791815
CONFFLAGS = \
	--prefix=/usr \
	--libdir=/usr/lib/$(DEB_HOST_MULTIARCH)/ \
	--enable-max-arg-list-hack \
	--enable-dynamic-build \
	--enable-lapack2flame \
	--enable-verbose-make-output \
	--enable-multithreading=openmp \
	--enable-supermatrix \
	--enable-memory-alignment=16 \
	--enable-ldim-alignment

# [[ arch-specific flags ]]
ifeq (amd64,$(DEB_HOST_ARCH))
CONFFLAGS+= --enable-vector-intrinsics=sse
endif

%:
	dh $@ -Smakefile

override_dh_clean:
	dh_clean -X.orig -X.bak
	-$(RM) libflame1.install libflame1.links
	-$(RM) libflame1.postinst libflame1.prerm

override_dh_autoreconf:

override_dh_auto_configure:
	./configure $(CONFFLAGS)
	sed -e "s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g" \
		< debian/libflame1.install.in \
		> debian/libflame1.install
	#sed -e "s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g" \
	#	< debian/libflame1.links.in \
	#	> debian/libflame1.links
	#sed -e "s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g" \
	#	< debian/libflame1.postinst.in \
	#	> debian/libflame1.postinst
	#sed -e "s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g" \
	#	< debian/libflame1.prerm.in \
	#	> debian/libflame1.prerm

override_dh_auto_build:
	dh_auto_build --parallel -- V=1

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p test/obj
	dh_auto_build --parallel -- -C test
	cd test; ./test_libflame.x
endif
