#!/usr/bin/make -f
#export DH_VERBOSE = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Generate pseudo-build-number using the date of the upstream version
include /usr/share/dpkg/pkg-info.mk
GGML_BUILD_NUMBER = $(basename $(subst 0.0~git,,$(DEB_VERSION_UPSTREAM)))


# GGML_NATIVE:ON corresponds to -march=native
CMAKE_OPTS  = -DGGML_NATIVE=OFF \
              -DCMAKE_BUILD_TYPE=RelWithDebInfo \
              -DCMAKE_INSTALL_RPATH=/usr/lib/${DEB_HOST_MULTIARCH}/ggml \
              -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=OFF \
              -DGGML_BUILD_NUMBER=$(GGML_BUILD_NUMBER) \
              -DGGML_BUILD_TESTS=ON \
              -DGGML_BUILD_EXAMPLES=OFF \
              -DGGML_INSTALL_TESTS=ON \
              -DGGML_BACKEND_DL=ON \
              -DGGML_BACKEND_DIR=/usr/lib/${DEB_HOST_MULTIARCH}/ggml/backends0

#
# See debian/README.Source
#
ifneq ($(filter pkg.ggml.cuda,$(DEB_BUILD_PROFILES)),)
CMAKE_OPTS += -DGGML_CPU=OFF \
	      -DGGML_CUDA=ON
else # not pkg.ggml.cuda
CMAKE_OPTS += -DGGML_CPU_ALL_VARIANTS=ON \
              -DGGML_BLAS=ON \
              -DGGML_VULKAN=ON

ifeq ($(filter pkg.ggml.nohip,$(DEB_BUILD_PROFILES)),)
ifneq ($(filter $(DEB_HOST_ARCH),amd64 arm64 ppc64el),)
export HIPCXX=clang-17
CMAKE_OPTS += -DGGML_HIP=ON -DCMAKE_HIP_ARCHITECTURES="$(shell rocm-target-arch --sep ';')"
endif
endif # pkg.ggml.nohip
endif # pkg.ggml.cuda


%:
	dh $@ --buildsystem=cmake

execute_after_dh_clean:
	rm -f shlibs.local

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_OPTS)

# No tests for now, as many need some kind of model we don't have
override_dh_auto_test:
	:

# dwz doesn't fully support DWARF-5 yet, see #1016936
# The HIP build produces DWARF-5 stuff
override_dh_dwz:
	:

override_dh_makeshlibs:
ifneq ($(filter pkg.ggml.cuda,$(DEB_BUILD_PROFILES)),)
	# When building only CUDA, we don't have bin:libggml0
	echo 'libggml-base 0 libggml0 (>= $(DEB_VERSION_UPSTREAM))' > debian/shlibs.local
endif
	# Libraries are private
	dh_makeshlibs --no-scripts

override_dh_shlibdeps:
ifneq ($(filter pkg.ggml.cuda,$(DEB_BUILD_PROFILES)),)
	# When building only CUDA, we don't have bin:libggml0
	dh_shlibdeps -l$(CURDIR)/debian/tmp/usr/lib/x86_64-linux-gnu/ggml
else
	dh_shlibdeps
endif

override_dh_gencontrol:
	dh_gencontrol -- -Vrocm:GPU-Architecture="$(shell rocm-target-arch)"
