#!/usr/bin/make -f

ifneq (,$(shell command -v nvcc))
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
else ifneq (,$(shell command -v hipcc))
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
export DEB_CXXFLAGS_MAINT_PREPEND = -gz -Wno-pass-failed

# filter incompatible options from affecting device code
CXXFLAGS := $(subst -fstack-protector-strong,-Xarch_host -fstack-protector-strong,$(CXXFLAGS))
CXXFLAGS := $(subst -fcf-protection,-Xarch_host -fcf-protection,$(CXXFLAGS))
endif

# misc configs
ifneq (,$(shell command -v nvcc))
export OPENBLASDIR=/usr
export DESTDIR=$(pwd)/debian/tmp
export prefix=/usr
export CUDADIR=/usr
else ifneq (,$(shell command -v hipcc))
endif

ifneq (,$(shell command -v nvcc))
# CUDA
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
else ifneq (,$(shell command -v hipcc))
# HIP
export CXX=hipcc
ifneq (,$(shell which ccache))
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
endif

CMAKE_FLAGS = \
	-DAMDGPU_TARGETS="gfx803;gfx900;gfx906;gfx908;gfx90a;gfx1010;gfx1030;gfx1100;gfx1101;gfx1102" \
	-DBLA_VENDOR=OpenBLAS \
	-DCMAKE_SKIP_RPATH=ON \
	-DMAGMA_ENABLE_HIP=ON
endif

ifneq (,$(shell command -v nvcc))
# CUDA
%:
	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#^\(CXXFLAGS\)\s*=\(.*\)#\1 = \2 $(CPPFLAGS) $(CXXFLAGS)#' \
		-e 's#^\(FFLAGS\)\s*=\(.*\)#\1 = \2 $(FFLAGS)#' \
		-e 's#^\(LDLAGS\)\s*=\(.*\)#\1 = \2 $(LDLAGS)#' \
		-e 's#^\(DEVCCFLAGS\)\s*=\(.*\)#\1 = \2 -g -G#' \
		-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
	cp -v debian/missing-source/*.py tools/
	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_clean: make.inc
	dh_auto_clean
	-$(RM) make.inc
else ifneq (,$(shell command -v hipcc))
# HIP
%:
	dh $@ -Scmake

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_FLAGS)

override_dh_auto_build-arch:
	dh_auto_build

override_dh_auto_build-indep:
	cd docs; doxygen Doxyfile

# dwz doesn't fully support DWARF-5 yet, see #1016936
override_dh_dwz:
	:
endif

override_dh_auto_install-arch:
	dh_auto_install
	# upstream install target does not install the test binaries
ifneq (,$(shell command -v nvcc))
	for I in $$(find testing -type f -executable -name 'testing_*'); do \
		install -Dm0755 $$I debian/libmagma-test/usr/lib/libmagma-test/$$I; done
else ifneq (,$(shell command -v hipcc))
	for I in $$(find obj-$(DEB_HOST_MULTIARCH)/testing -type f -executable -name 'testing_*' -printf '%P\n'); do \
		install -Dm0755 obj-$(DEB_HOST_MULTIARCH)/testing/$$I debian/libmagma-rocm-test/usr/lib/$(DEB_HOST_MULTIARCH)/libmagma-rocm-test/testing/$$I; done
endif
