#!/usr/bin/make -f

# export DH_VERBOSE=1


include /usr/share/dpkg/architecture.mk

PKGDIR = debian/libgasnet-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
TOPDIR=$(shell pwd)


%:
	dh $@ 

NO_PSHM_ARCH:=loong64
PSHM:=$(if $(filter $(DEB_TARGET_ARCH), $(NO_PSHM_ARCH)),--disable-pshm, --enable-pshm)
UCX_ARCH:= $(shell sed -n -r '/^Build/,/^$$/s/.*libucx-dev *\[([^]]*)\].*/\1/p' debian/control)
UCX:= $(if $(filter $(DEB_TARGET_ARCH), $(UCX_ARCH)),--enable-ucx,)
PMIX_ARCH:= $(shell sed -n -r '/^Build/,/^$$/s/.*libpmix-dev *\[([^]]*)\].*/\1/p' debian/control)
PMIX:= $(if $(filter $(DEB_TARGET_ARCH), $(UCX_ARCH)),--with-pmi-version=x,)
CLANG_ARCH:= $(shell sed -n -r '/^Build/,/^$$/s/.*clang-21 *\[([^]]*)\].*/\1/p' debian/control)

PMI_HOME=/usr/lib/$(DEB_HOST_MULTIARCH)/pmix2
export PMIX

ifneq (,$(filter $(DEB_TARGET_ARCH), $(CLANG_ARCH)))
CC=clang-21
CXX=clang++-21
else
CC=gcc
CXX=g++
endif

export CC CXX

override_dh_auto_configure:
	dh_auto_configure -- $(UCX) $(PSHM) $(PMIX) \
		--disable-mpi-compat \
                --enable-seq --disable-par --disable-parsync \
      		--disable-segment-everything

execute_after_dh_install: strip_bad_directives 
	# Move the perlstart scripts
	mkdir -p debian/gasnet/usr/share/gasnet
	mv debian/gasnet/usr/bin/*.pl debian/gasnet/usr/share/gasnet
	# Name collision with rcs
	mv debian/gasnet/usr/bin/ident debian/gasnet/usr/bin/gasnet-ident
	chmod +x debian/gasnet/usr/share/gasnet/*.pl

# Strip bad directives from  pkgconf files
strip_bad_directives: $(PKGDIR)/*
	sed -i -e 's%-D_FORTIFY_SOURCE=2%%' -e 's%-O2%%' \
		-e 's%-O3%%' -e 's%-DNDEBUG%%' -e 's%-Wdate-time%%' -e 's%-Wno-dangling-pointer%%' \
		-e 's%-Wno-unused-parameter%%' -e 's%-Wno-unused%%' -e 's%-Wunused-result%%' \
		-e 's%-Wl,-z,relro%%' -e 's%-Wno-address%%' -e 's%-Wno-use-after-free%%' \
		-e 's%-Wno-stringop-overflow%%' -e 's%-Wno-array-bounds%%' $^
	sed -i -e 's%$(TOPDIR)%/buildroot%' $^
	sed -i -e 's%/usr/include%/usr/include/${DEB_HOST_MULTIARCH}/%g' $^

override_dh_dwz:
	@echo "Not running dwz on clang libs"
