#!/usr/bin/make -f
export CXX=hipcc
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
export DEB_CXXFLAGS_MAINT_PREPEND = -gdwarf-4
export VERBOSE=1
export TENSILE_ROCM_ASSEMBLER_PATH=/usr/bin/clang++-15
export TENSILE_ROCM_OFFLOAD_BUNDLER_PATH=/usr/bin/clang-offload-bundler-15
#export AMD_LOG_LEVEL=4

CXXFLAGS := $(subst -fstack-protector-strong,-Xarch_host -fstack-protector-strong,$(CXXFLAGS))

CMAKE_FLAGS = \
	-DCMAKE_BUILD_TYPE=Release \
	-DROCM_SYMLINK_LIBS=OFF \
	-DBUILD_FILE_REORG_BACKWARD_COMPATIBILITY=OFF \
	-DAMDGPU_TARGETS="gfx803;gfx900;gfx906:xnack-;gfx908:xnack-;gfx90a:xnack+;gfx90a:xnack-;gfx1010;gfx1030" \
	-DROCBLAS_TENSILE_LIBRARY_DIR=/usr/lib/$(DEB_HOST_MULTIARCH)/rocblas/2.47.0 \
	-DTensile_TEST_LOCAL_PATH="../tensile" \
	-DTensile_LOGIC=asm_full \
	-DTensile_LIBRARY_FORMAT=msgpack \
	-DTensile_LAZY_LIBRARY_LOADING=OFF \
	-DTensile_SEPARATE_ARCHITECTURES=OFF \
	-DRUN_HEADER_TESTING=OFF \
	-DBUILD_CLIENTS_TESTS=ON

%:
	dh $@ -Scmake

override_dh_auto_configure-arch:
	dh_auto_configure -- $(CMAKE_FLAGS)

# strip cannot handle code object files
override_dh_strip-arch:
	dh_strip --exclude=.hsaco --exclude=.co

# dwz cannot handle code object files
override_dh_dwz-arch:
	dh_dwz --exclude=.hsaco --exclude=.co

# note: openblas crashes in dot for complex values with M=128, N=800000, incx=-3, incy=-3
override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e \
	; if [ -r /dev/kfd ] \
	; then obj-$(DEB_HOST_MULTIARCH)/clients/staging/rocblas-test --gtest_filter="-*known_bug*" \
	; else echo "W: /dev/kfd unreadable: no available AMD GPU." \
	;      echo "W: tests skipped." \
	; fi
endif

execute_before_dh_install-arch:
	# Note the rpath field setting may cause reproducible build issues.
	# This should be removed earlier in the toolchain if possible.
	patchelf --remove-rpath ./debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/librocblas.so.0.1
	patchelf --remove-rpath ./debian/tmp/usr/bin/rocblas-test
