#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# misc configs
export OPENBLASDIR=/usr
export CUDADIR=/usr
export DESTDIR=$(pwd)/debian/tmp
export prefix=/usr

# detect ccache
ifneq (,$(shell which ccache))
export NVCC=ccache nvcc
export CC=ccache cuda-gcc
export CXX=ccache cuda-g++
else
export NVCC=nvcc
export CC=cuda-gcc
export CXX=cuda-g++
endif

%:
	dh $@

make.inc:
	cp -v make.inc-examples/make.inc.openblas make.inc
	sed -i -e 's#^\(CC\)\s*=\(.*\)#\1 = $(CC)#' \
		-e 's#^\(CXX\)\s*=\(.*\)#\1 = $(CXX)#' \
		-e 's#^\(NVCC\)\s*=\(.*\)#\1 = $(NVCC)#' \
		-e 's#^\(CFLAGS\)\s*=\(.*\)#\1 = \2 $(CPPFLAGS) $(CFLAGS)#' \
		-e 's#^\(FFLAGS\)\s*=\(.*\)#\1 = \2 $(FFLAGS)#' \
		-e 's#^\(LDLAGS\)\s*=\(.*\)#\1 = \2 $(LDLAGS)#' \
		-e 's#-lopenblas#-lblas -llapack#' \
		-e 's@^#\(OPENBLASDIR\)\s*?=.*@\1 ?= /usr@' \
		-e 's@^#\(CUDADIR\)\s*?=.*@\1 ?= /usr@' \
		make.inc

override_dh_auto_configure: make.inc
	dh_auto_configure

override_dh_auto_build-arch:
	dh_auto_build

override_dh_auto_build-indep:
	$(MAKE) generate
	cd docs; doxygen Doxyfile
#	find docs -type f -name '*.html' -exec sed -i -e \
#		's@http://cdn.mathjax.org/mathjax/latest/MathJax.js@file:///usr/share/javascript/mathjax/MathJax.js@g' \
#		'{}' \;

override_dh_auto_install-arch:
	dh_auto_install
	# upstream install target does not install the test binaries
	for I in $$(find testing -type f -executable -name 'testing_*'); do \
		install -Dm0755 $$I debian/libmagma-test/usr/lib/libmagma-test/$$I; done

override_dh_auto_clean: make.inc
	dh_auto_clean
	-$(RM) make.inc
