#!/usr/bin/make -f

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 \
	-D CMAKE_BUILD_TYPE=RelWithDebInfo \
	-D GGML_BACKEND_DIR=/usr/lib/${DEB_HOST_MULTIARCH}/ggml/backends0 \
	-D GGML_BACKEND_DL=ON \
	-D GGML_BUILD_EXAMPLES=OFF \
	-D GGML_BUILD_NUMBER=$(GGML_BUILD_NUMBER) \
	-D GGML_BUILD_TESTS=ON \
	-D GGML_CCACHE=OFF \
	-D GGML_INSTALL_TESTS=ON

#
# See debian/README.Source
#

# Build BLAS and Vulkan everywhere
CMAKE_OPTS += -DGGML_BLAS=ON \
              -DGGML_VULKAN=ON

## CPU ##
# Build all CPU variants where supported
ifneq ($(filter $(DEB_HOST_ARCH),amd64 arm64 ppc64el),)
CMAKE_OPTS += -DGGML_CPU_ALL_VARIANTS=ON
# Disable all features where all CPU variants are not supported
else ifeq ($(DEB_HOST_ARCH),riscv64)
CMAKE_OPTS += -DGGML_RVV=OFF \
	      -DGGML_RV_ZICBOP=OFF \
	      -DGGML_RV_ZFH=OFF
else ifeq ($(DEB_HOST_ARCH),loong64)
CMAKE_OPTS += -DGGML_LASX=OFF
else ifeq ($(DEB_HOST_ARCH),s390x)
CMAKE_OPTS += -DGGML_VXE=OFF \
              -DGGML_NNPA=OFF
endif ## CPU ##

## HIP ##
ifneq ($(filter $(DEB_HOST_ARCH),amd64 arm64),)
ifeq ($(filter pkg.ggml.nohip,$(DEB_BUILD_PROFILES)),)
ifneq ($(filter pkg.ggml.minhip,$(DEB_BUILD_PROFILES)),)
export ROCM_TARGET_ARCH_FIXED=gfx1030
endif

BACKEND_HIP_ACTIVATED = 1
CMAKE_OPTS += -DGGML_HIP=ON -DCMAKE_HIP_ARCHITECTURES="$(shell rocm-target-arch --sep ';')"
endif
endif ## HIP ##

%:
	dh $@ --buildsystem=cmake

execute_after_dh_clean:
	rm -f shlibs.local

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_OPTS)

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

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