#!/usr/bin/make -f
export CC=clang-14
export CXX=clang++-14
export DEB_BUILD_MAINT_OPTIONS = hardening=-all
export VERBOSE=1
#export AMD_LOG_LEVEL=4

export HIP_CLANG_PATH=/usr/bin
export DEVICE_LIB_PATH=/usr/lib/${DEB_HOST_MULTIARCH}/amdgcn/bitcode
export ROCM_PATH=/usr
export HIP_CLANG_HCC_COMPAT_MODE=1
export HIPCC_COMPILE_FLAGS_APPEND='--hip-version=5.0.0'
export HIPCC_LINK_FLAGS_APPEND='--hip-version=5.0.0'

CMAKE_FLAGS = \
	-DCMAKE_CXX_COMPILER=/usr/bin/hipcc \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DHIP_DEVICE_LIB_PATH=/usr/lib/${DEB_HOST_MULTIARCH}/amdgcn/bitcode \
	-DCMAKE_BUILD_TYPE=Release \
	-DENABLE_INLINE_ASM=1

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

%:
	dh $@ -Scmake

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_FLAGS)

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e \
	; if [ -r /dev/kfd ] \
	; then dh_auto_test --no-parallel -- \
	; 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/rocrand/lib/librocrand.so.1.1
	patchelf --remove-rpath ./debian/tmp/usr/hiprand/lib/libhiprand.so.1.1

override_dh_dwz:
	@echo "W: debug symbols unavailable for the moment[1]."
	@echo "W: [1]: https://github.com/ROCm-Developer-Tools/hipamd/issues/17"
