#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS   = hardening=+all
export DEB_CXXFLAGS_MAINT_APPEND = -lpthread
export DEB_LDFLAGS_MAINT_APPEND  = -Wl,--as-needed
#export DPKG_GENSYMBOLS_CHECK_LEVEL = 0
ifneq (,$(shell command -v nvcc))
# CUDA
export CC = cuda-gcc
export CXX = cuda-g++
else ifneq (,$(shell command -v hipcc))
# HIP
export CC = /usr/bin/clang-21
export CXX = /usr/bin/clang++-21
# clang-21 自动选择最大版本 GCC 工具链；当前 amd64 构建环境中 gcc-16
# 只有 libgcc/libstdc++6(运行时) 而无 libstdc++-16-dev，导致链接
# "cannot find -lstdc++" 且编译 HIP 时找不到 libstdc++ 头文件
# (cmath/cstdlib)。这里强制 clang 使用包含 libstdc++.so 与对应头文件的
# GCC 安装目录（动态探测，不硬编码版本号/架构），并将同一目录同时传给
# C/CXX 与 HIP 编译器（HIP 语言需经 CMAKE_HIP_FLAGS 单独传参，
# 详见下方 override_dh_auto_configure 的 ROCm 分支）。
GCC_INSTALL_DIR := $(shell ls -d /usr/lib/gcc/$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)/*/libstdc++.so 2>/dev/null | xargs -n1 dirname | sort -V | tail -n1)
export DEB_CFLAGS_MAINT_APPEND := $(DEB_CFLAGS_MAINT_APPEND) --gcc-install-dir=$(GCC_INSTALL_DIR)
export DEB_CXXFLAGS_MAINT_APPEND := $(DEB_CXXFLAGS_MAINT_APPEND) --gcc-install-dir=$(GCC_INSTALL_DIR)
endif

%:
	dh $@ -Scmake+ninja

override_dh_auto_configure:
	mkdir third-party
	cd third-party; ln -s /usr/src/googletest googletest
ifneq (,$(shell command -v nvcc))
	# CUDA
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DUSE_REDIS=ON \
		-DUSE_IBVERBS=ON \
		-DUSE_MPI=ON \
		-DUSE_LIBUV=ON \
		-DUSE_CUDA=ON \
		-DGLOO_USE_CUDA_TOOLKIT=ON \
		-DUSE_NCCL=ON \
		-DUSE_ROCM=OFF \
		-DUSE_RCCL=OFF \
		-DBUILD_TEST=OFF \
		-DBUILD_BENCHMARK=OFF \
		-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
		-DBUILD_SHARED_LIBS=ON
else ifneq (,$(shell command -v hipcc))
	# ROCm
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DUSE_REDIS=ON \
		-DUSE_IBVERBS=ON \
		-DUSE_MPI=ON \
		-DUSE_LIBUV=ON \
		-DUSE_CUDA=OFF \
		-DGLOO_USE_CUDA_TOOLKIT=OFF \
		-DUSE_NCCL=OFF \
		-DUSE_ROCM=ON \
		-DUSE_RCCL=OFF \
		-DBUILD_TEST=OFF \
		-DBUILD_BENCHMARK=OFF \
		-DBUILD_SHARED_LIBS=ON \
		-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
		-DCMAKE_HIP_FLAGS="--gcc-install-dir=$(GCC_INSTALL_DIR)" \
		-DCMAKE_HIP_ARCHITECTURES="$(shell rocm-target-arch --sep ';')" \
		-DROCM_HOME=/usr
else
	# CPU
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DUSE_REDIS=ON \
		-DUSE_IBVERBS=ON \
		-DUSE_MPI=ON \
		-DUSE_LIBUV=ON \
		-DUSE_CUDA=OFF \
		-DGLOO_USE_CUDA_TOOLKIT=OFF \
		-DUSE_NCCL=OFF \
		-DUSE_ROCM=OFF \
		-DUSE_RCCL=OFF \
		-DBUILD_TEST=ON \
		-DBUILD_BENCHMARK=OFF \
		-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
		-DBUILD_SHARED_LIBS=ON
endif

# there is no 'test' target generated, do not use dh_auto_test
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
#	the test is flaky -- do not run it for now
#	-find obj-* -type f -name 'gloo_test' -executable -exec sh -c '{}' +
endif

override_dh_auto_clean:
	-$(RM) -rf third-party
	dh_auto_clean

ifneq (,$(shell command -v hipcc))
override_dh_dwz:
	# Fails with: Unknown debugging section .debug_addr
	dh_dwz --exclude=libgloo.so. --exclude=libgloo_hip.so.
endif
