#! /usr/bin/make -f
export CC=clang
export CXX=clang++
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
export DEB_CXXFLAGS_MAINT_PREPEND = -gz
export VERBOSE=1
export PATH:=/usr/lib/llvm-21/bin:$(PATH)
#export AMD_LOG_LEVEL=4
##DEBUG_FLAGS=-DCMAKE_FIND_DEBUG_MODE=ON
#DEBUG_FLAGS=

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

ifdef CCACHE_DIR
CMAKE_CACHE_FLAGS = \
        -D CMAKE_C_COMPILER_LAUNCHER=ccache -D CMAKE_CXX_COMPILER_LAUNCHER=ccache
CCACHE=ccache
endif

CMAKE_FLAGS = \
	-DCMAKE_BUILD_TYPE=Release \
	$(CMAKE_CACHE_FLAGS)

# From TheRock build/logs/hipBLASLt_configure.log
CMAKE_FLAGS += \
	-DHIP_PLATFORM=$(shell hipconfig --platform)

CMAKE_FLAGS += \
	-DUSE_SYSTEM_NANOBIND=ON \
	-DDEBIAN_MUT=ON

ifndef ROCM_TARGET_ARCH_FIXED
# Working reduced set 
#export ROCM_TARGET_ARCH_FIXED:="gfx90a:xnack-"
#export ROCM_TARGET_ARCH_FIXED:="gfx1102"
#export ROCM_TARGET_ARCH_FIXED:="gfx90a gfx1102"
# Non working reduced set
#export ROCM_TARGET_ARCH_FIXED:="gfx11-generic"
#export ROCM_TARGET_ARCH_FIXED:="gfx942"
#export ROCM_TARGET_ARCH_FIXED:="gfx950"
# TheRock Reduced Set
#export ROCM_TARGET_ARCH_FIXED:="gfx1100 gfx1101"
# Default, when you let cmake choose is broken
#export ROCM_TARGET_ARCH_FIXED:="gfx908 gfx90a gfx942 gfx950 gfx1100 gfx1101 gfx1103 gfx1150 gfx1151 gfx1200 gfx1201"
# Removing broken gfx942 gfx950, adding gfx1102
# Full working Set
export ROCM_TARGET_ARCH_FIXED:="gfx908 gfx90a gfx1100 gfx1101 gfx1102 gfx1103 gfx1150 gfx1151 gfx1200 gfx1201"
endif

CMAKE_FLAGS += \
	-DGPU_TARGETS=$(shell rocm-target-arch --sep ';')
	

# rocRoller not packaged yet
CMAKE_FLAGS += \
	-DHIPBLASLT_ENABLE_ROCROLLER=OFF \
	-DHIPBLASLT_USE_ROCROLLER=OFF

# rocTracer not packaged yet
CMAKE_FLAGS += \
	-DHIPBLASLT_ENABLE_MARKER=OFF

# blis/tests FLAGS
CMAKE_FLAGS += \
	-DHIPBLASLT_ENABLE_BLIS=ON \
	-DTENSILELITE_BUILD_TESTING=ON

# blas/lapack FLAGS (cf install.sh)
CMAKE_FLAGS += \
	-DBLAS_LIBRARIES=/usr/lib/$(DEB_HOST_MULTIARCH)/blas/libblas.a \
	-DLAPACK_LIBRARIES='/usr/lib/$(DEB_HOST_MULTIARCH)/lapack/liblapack.a;/usr/lib/$(DEB_HOST_MULTIARCH)/blas/libblas.a' \
	-DBLA_STATIC=ON

ifeq (,$(filter nocheck,$(DEB_BUILD_PROFILES)))
CMAKE_FLAGS += \
	-DHIPBLASLT_BUILD_TESTING=ON \
	-DCMAKE_CXX_FLAGS="-I/usr/src/googletest/googlemock/include" \
	-DCMAKE_C_FLAGS="-I/usr/src/googletest/googlemock/include"
endif

%:
	dh $@ -Scmake

override_dh_auto_configure-arch:
	dh_auto_configure -- $(CMAKE_FLAGS)

override_dh_strip-arch:
	dh_strip --exclude=.hsaco --exclude=.co --no-automatic-dbgsym

override_dh_dwz-arch:

override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e \
	; if [ -r /dev/kfd ] \
	; then obj-$(DEB_HOST_MULTIARCH)/clients/hipblaslt-test \
	    --gtest_filter="*quick*" \
	; else echo "W: /dev/kfd unreadable: no available AMD GPU." \
	;      echo "W: tests skipped." \
	; fi
endif

execute_before_dh_install-arch:

override_dh_auto_configure-indep:

execute_after_dh_auto_build-indep: export http_proxy=127.0.0.1:9
execute_after_dh_auto_build-indep: export https_proxy=127.0.0.1:9
execute_after_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	rocm-docs-build
	# Fix privacy
	find build/html -name data-type-support.html \
		-exec perl -ni -e 'print unless /cdn\.jsdelivr\.net/' {} \;
	find build/html -name what-is-hipBLASLt.html \
		-exec perl -ni -e 'print unless /cdn\.jsdelivr\.net/' {} \;
	# Remove build path in some html files
	find build/html -type f -name '*.html' -exec sed -i 's|$(CURDIR)/||g' {} +
endif

override_dh_auto_test-indep:

override_dh_auto_install-indep:

LIB=libhipblaslt1
LIBV=$(shell head -1  debian/changelog | sed 's/.*(\(.*\)-.*).*$$/\1/')
REV=$(shell head -1  debian/changelog | sed 's/.*(.*-\(.*\)).*$$/\1/')
gensymbols:
	rm -rf /tmp/libhipblaslt1
	dpkg-deb -x ../$(LIB)_$(LIBV)-$(REV)_amd64.deb /tmp/$(LIB)
	dpkg-gensymbols -v$(LIBV) -p$(LIB) -P/tmp/$(LIB) -Odebian/$(LIB).symbols
	#Add "* Build-Depends-Package: libhipblaslt-dev" on line 2
	#sed -i '2i * Build-Depends-Package: libhipblaslt-dev' debian/$(LIB).symbols
