#!/usr/bin/make -f
export CXX=clang++
export CC=clang
#export HIPCXX=clang++
export PATH:=/usr/lib/llvm-21/bin:$(PATH)

export PYBUILD_NAME=ck4inductor
# You must remove unused comment lines for the released package.
#export DH_VERBOSE = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-O1
# Compilation uses a lot of memory 2Gb/proc was not enough
#CPUS := $(shell nproc)
#PARALLEL := $(shell expr $(CPUS) / 16 \| 1)
#export DEB_BUILD_OPTIONS = parallel=$(PARALLEL)
#export DEB_BUILD_OPTIONS = parallel=2

# 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))

#export CCACHE_DIR=/var/tmp/bayle/ccache
#export DEB_BUILD_OPTIONS=parallel=16
ifdef CCACHE_DIR
CMAKE_CACHE_FLAGS = \
	-D CMAKE_C_COMPILER_LAUNCHER=ccache -D CMAKE_CXX_COMPILER_LAUNCHER=ccache
endif

# Looks like default _FORTIFY_SOURCE=2 stucks build in an infinity loop
# https://clang.debian.net/status.php?version=13.0.0&key=BUILD_TIMEOUT
# Indeed disabling _FORTIFY_SOURCE allow a reasonnable amount of memory to build
FORTIFLAG0=-DCMAKE_C_FLAGS="-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0" \
	-DCMAKE_CXX_FLAGS="-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0"
FORTIFLAG1=-DCMAKE_C_FLAGS="-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1" \
	-DCMAKE_CXX_FLAGS="-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1"
FORTIFLAG=-DCMAKE_C_FLAGS="-U_FORTIFY_SOURCE" \
	-DCMAKE_CXX_FLAGS="-U_FORTIFY_SOURCE"
#GPUFLAG=-DGPU_ARCHS="gfx908;gfx90a;gfx942;gfx1030;gfx1100;gfx1101;gfx1102;gfx1200;gfx1201"

CMAKE_FLAGS = \
	-DCMAKE_BUILD_TYPE=Release \
	-DHIP_PLATFORM=$(shell hipconfig --platform) \
	-DBUILD_TESTING=ON \
	$(CMAKE_CACHE_FLAGS) \
	$(FORTIFLAG) \
	$(GPUFLAG)

#(CDNA|CDNA2)	-D GPU_ARCHS="gfx908;gfx90a"
#(CDNA3)	-D GPU_ARCHS="gfx942"
#(RDNA2)	-D GPU_ARCHS="gfx1030"
#(RDNA3)	-D GPU_ARCHS="gfx1100;gfx1101;gfx1102"
#(RDNA4)	-D GPU_ARCHS="gfx1200;gfx1201"

# The next make not building tests
#	-DMIOPEN_REQ_LIBS_ONLY=ON
	
# From CMakeLists.txt
# In order to build just the CK library (without tests and examples) for all supported GPU targets
#	-D GPU_ARCHS="gfx908;gfx90a;gfx942;gfx1030;gfx1100;gfx1101;gfx1102;gfx1200;gfx1201"

# In order to build CK along with all tests and examples it should be OK 
# to set GPU_TARGETS to just 1 or 2 similar architectures.
#
# From TheRock builds
#	-DGPU_TARGETS="gfx1100;gfx1101;gfx1102"

# When using GPU_TARGETS the build will only work if the architectures are similar
# 	-DGPU_TARGETS="gfx908;gfx90a"
#	-DGPU_TARGETS="gfx1100;gfx1101;gfx1102"

# None of the following worked (more target than managed for GPU_TARGETS
# GPU_ARCHS disable tests
#	-DGPU_TARGETS="$(shell rocm-target-arch --sep ';')"
#	-DGPU_ARCHS="$(shell rocm-target-arch --sep ';')"

# From The Rock
# -GNinja
# -DCMAKE_BUILD_TYPE=Release
# -DHIP_PLATFORM=amd
# -DBUILD_TESTING=ON
# -DMIOPEN_REQ_LIBS_ONLY=ON
# -DGPU_TARGETS="gfx1100;gfx1101;gfx1102"

%:
	dh $@

override_dh_auto_configure-arch:
	dh_auto_configure -O--buildsystem=cmake -- $(CMAKE_FLAGS)

override_dh_auto_configure-indep:
	dh_auto_configure -O--buildsystem=pybuild

override_dh_auto_build:
	dh_auto_build -O--buildsystem=pybuild
	dh_auto_build -O--buildsystem=cmake

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)))
	perl -pi -e 's/^FULL_PATH_NAMES.*/FULL_PATH_NAMES = NO/' docs/doxygen/Doxyfile
	perl -pi -e 's|^STRIP_FROM_PATH.*|STRIP_FROM_PATH = $(CURDIR)/|' docs/doxygen/Doxyfile
	perl -pi -e 's/^TIMESTAMP.*/TIMESTAMP = NO/' docs/doxygen/Doxyfile
	export DOXYGEN_STRIP_FROM_PATH=$(CURDIR)/; \
	rocm-docs-build
	rm -rf build/html/_static/fonts
	rm -rf build/html/_static/vendor
	rm -f build/html/doxygen/html/jquery.js
	find build/html -name Composable-Kernel-math.html \
		-exec perl -ni -e 'print unless /cdn\.jsdelivr\.net/' {} \;
	find build/html -type f -name '*.html' -exec sed -i 's|$(CURDIR)/||g' {} +
endif

override_dh_auto_install:
	dh_auto_install -O--buildsystem=cmake -O--builddirectory=$(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)
	dh_auto_install --destdir=debian/python3-ck4inductor/ -O--buildsystem=pybuild
	find debian -type f -name LICENSE -exec rm -f {} \;

override_dh_clean:
	rm -rf example/ck_tile/01_fmha/codegen/__pycache__/ \
		example/ck_tile/01_fmha/codegen/ops/__pycache__/ \
		tile_engine/ops/gemm/__pycache__/ \
		build/  \
		docs/_doxygen/ \
		docs/doxygen/html/ \
		docs/doxygen/xml/ \
		docs/sphinx/_toc.yml
	dh_clean

override_dh_strip:
	dh_strip --no-automatic-dbgsym

#LIB=composable-kernel
#LIBV=7.0.1
#REV=1~exp1
#gensymbols: 
#	dpkg-deb -x ../$(LIB)_$(LIBV)-$(REV)_amd64.deb /tmp/$(LIB)
#	dpkg-gensymbols -v$(LIBV) -p$(LIB) -P/tmp/$(LIB) -Odebian/$(LIB).symbols
