#!/usr/bin/make -f
export CXX=hipcc
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
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 \
	-DROCBLAS_TENSILE_LIBRARY_DIR=/usr/lib/$(DEB_HOST_MULTIARCH)/rocblas/2.45.0 \
	-DTensile_TEST_LOCAL_PATH="../tensile" \
	-DTensile_LOGIC=asm_full \
	-DTensile_CODE_OBJECT_VERSION=V3 \
	-DTensile_LIBRARY_FORMAT=msgpack \
	-DRUN_HEADER_TESTING=OFF

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
CMAKE_FLAGS += -DBUILD_CLIENTS_TESTS=ON
endif

%:
	dh $@ -Scmake

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_FLAGS)

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

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

override_dh_auto_test:
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:
	# 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
