#!/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))
export CC = cuda-gcc
export CXX = cuda-g++
endif

%:
	dh $@ -Scmake

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=OFF \
		-DUSE_CUDA=ON \
        -DGLOO_USE_CUDA_TOOLKIT=ON \
		-DUSE_NCCL=ON \
		-DUSE_ROCM=OFF \
		-DUSE_RCCL=OFF \
		-DBUILD_TEST=OFF \
		-DBUILD_BENCHMARK=OFF \
		-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=OFF \
		-DUSE_CUDA=OFF \
        -DGLOO_USE_CUDA_TOOLKIT=OFF \
		-DUSE_NCCL=OFF \
		-DUSE_ROCM=ON \
		-DUSE_RCCL=ON \
		-DBUILD_TEST=OFF \
		-DBUILD_BENCHMARK=OFF \
		-DBUILD_SHARED_LIBS=ON \
		-DROCM_HOME=/usr
else
	# CPU
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DUSE_REDIS=ON \
		-DUSE_IBVERBS=ON \
		-DUSE_MPI=ON \
		-DUSE_LIBUV=OFF \
		-DUSE_CUDA=OFF \
        -DGLOO_USE_CUDA_TOOLKIT=OFF \
		-DUSE_NCCL=OFF \
		-DUSE_ROCM=OFF \
		-DUSE_RCCL=OFF \
		-DBUILD_TEST=OFF \
		-DBUILD_BENCHMARK=OFF \
		-DBUILD_SHARED_LIBS=ON
endif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test
	-find obj-* -type f -name 'gloo_test' -executable -exec sh -c '{}' +
endif

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