#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/architecture.mk
include /usr/share/mpi-default-dev/debian_defaults

FAILED=debian/failed

ifeq ($(ARCH_DEFAULT_MPI_IMPL),openmpi)
OPENMPI=yes
else
OPENMPI=no
endif

OMPT_ARCHS = amd64 arm64 ppc64el riscv64 loong64

CONFIGURE_ENV=
CONFIGURE=-DCMAKE_INSTALL_INCLUDEDIR=/usr/include/$(DEB_HOST_MULTIARCH)

ifeq (,$(filter $(DEB_HOST_ARCH),amd64 arm64 mips64el ppc64el s390x alpha ia64 sparc64 kfreebsd-amd64 ppc64 riscv64))
CONFIGURE+=-DEZTRACE_ENABLE_PNETCDF=OFF
endif

ifeq ($(DEB_HOST_ARCH), riscv64)
CLANG=clang-19
else
CLANG=clang
endif

%:
	dh $@

override_dh_auto_configure:
	$(CONFIGURE_ENV) dh_auto_configure -Bbuild-mpich -- \
		-DEZTRACE_ENABLE_MPI=ON \
		-DEZTRACE_ENABLE_STARPU=ON \
		-DEZTRACE_ENABLE_OPENMP=ON \
		-DEZTRACE_ENABLE_POSIXIO=ON \
		-DEZTRACE_ENABLE_PTHREAD=ON \
		-DEZTRACE_ENABLE_MEMORY=ON \
		-DEZTRACE_ENABLE_OMPT=OFF \
		-DEZTRACE_ENABLE_PNETCDF=ON \
		-DEZTRACE_ENABLE_NETCDF=ON \
		-DEZTRACE_ENABLE_IOTRACER=OFF \
		$(CONFIGURE) \
		-DMPICC=/usr/bin/mpicc.mpich -DMPIF90=/usr/bin/mpif90.mpich -DMPI_MODULE_NAME=mpich
ifeq ($(OPENMPI),yes)
	$(CONFIGURE_ENV) dh_auto_configure -Bbuild-openmpi -- \
		-DEZTRACE_ENABLE_MPI=ON \
		-DEZTRACE_ENABLE_STARPU=OFF \
		-DEZTRACE_ENABLE_OPENMP=OFF \
		-DEZTRACE_ENABLE_POSIXIO=OFF \
		-DEZTRACE_ENABLE_PTHREAD=OFF \
		-DEZTRACE_ENABLE_MEMORY=OFF \
		-DEZTRACE_ENABLE_OMPT=OFF \
		-DEZTRACE_ENABLE_PNETCDF=OFF \
		-DEZTRACE_ENABLE_NETCDF=OFF \
		-DEZTRACE_ENABLE_IOTRACER=OFF \
		$(CONFIGURE) \
		-DMPICC=/usr/bin/mpicc.openmpi -DMPIF90=/usr/bin/mpif90.openmpi -DMPI_MODULE_NAME=openmpi
endif
ifneq ($(filter $(DEB_HOST_ARCH),$(OMPT_ARCHS)),)
	$(CONFIGURE_ENV) CC=$(CLANG) dh_auto_configure -Bbuild-ompt -- \
		-DEZTRACE_ENABLE_MPI=OFF \
		-DEZTRACE_ENABLE_STARPU=OFF \
		-DEZTRACE_ENABLE_OPENMP=OFF \
		-DEZTRACE_ENABLE_POSIXIO=OFF \
		-DEZTRACE_ENABLE_PTHREAD=OFF \
		-DEZTRACE_ENABLE_MEMORY=OFF \
		-DEZTRACE_ENABLE_OMPT=ON \
		-DEZTRACE_ENABLE_PNETCDF=OFF \
		-DEZTRACE_ENABLE_NETCDF=OFF \
		-DEZTRACE_ENABLE_IOTRACER=OFF \
		$(CONFIGURE)
endif

override_dh_auto_build:
	@echo 'blhc: ignore-line-regexp: \[\ *[0-9]+%\]\ Building\ C\ object\ (src|test)/.+\.c\.o'
	dh_auto_build -Bbuild-mpich
ifeq ($(OPENMPI),yes)
	dh_auto_build -Bbuild-openmpi
endif
ifneq ($(filter $(DEB_HOST_ARCH),$(OMPT_ARCHS)),)
	dh_auto_build -Bbuild-ompt
endif

override_dh_auto_clean:
	dh_auto_clean -Bbuild-mpich
ifeq ($(OPENMPI),yes)
	dh_auto_clean -Bbuild-openmpi
endif
ifneq ($(filter $(DEB_HOST_ARCH),$(OMPT_ARCHS)),)
	dh_auto_clean -Bbuild-ompt
endif

override_dh_auto_install:
	dh_auto_install -Bbuild-mpich
ifeq ($(OPENMPI),yes)
	dh_auto_install -Bbuild-openmpi -- -C src/modules/mpi
endif
ifneq ($(filter $(DEB_HOST_ARCH),$(OMPT_ARCHS)),)
	dh_auto_install -Bbuild-ompt -- -C src/modules/ompt
endif

# Some MPI drivers assume we won't fork, but we will, so tell them to be safe.
export RDMAV_FORK_SAFE=1

override_dh_auto_test:
	@echo 'blhc: ignore-line-regexp: ([0-9]*:\ )?.*src/modules/omp/bin/eztrace_cc\ .*'
	@echo 'blhc: ignore-line-regexp: ([0-9]*:\ )?\[eztrace_cc\].*'
	@echo 'blhc: ignore-line-regexp: ([0-9]*:\ )?cc\ -o\ test_task.*'
	@echo 'blhc: ignore-line-regexp: ([0-9]*:\ )?cc\ -o\ starpu_.*'
	@echo 'blhc: ignore-line-regexp: ([0-9]*:\ )?cc\ -o\ .*\ -pthread'
	@echo 'blhc: ignore-line-regexp: ([0-9]*:\ )?cc.*\ memory.c.*\ -o\ memory'
	@echo 'blhc: ignore-line-regexp: ([0-9]*:\ )?cc\ -o\ foo\ foo.c.*'
	@echo 'blhc: ignore-line-regexp: ([0-9]*:\ )?g\+\+\ -o\ vector\ vector.cxx'
	@echo 'blhc: ignore-line-regexp: ([0-9]*:\ )?.*/cc\ .*\ -c\ .*/test/.*.c'
	# This is an mpich smoke test. If this fails, please report to mpich, not eztrace
	-mpirun.mpich -np 2 --bind-to none ./build-mpich/test/mpi/mpi_ping | grep "10000	16" || touch $(FAILED)
ifeq ($(OPENMPI),yes)
	# This is an openmpi smoke test. If this fails, please report to openmpi, not eztrace
	-mpirun.openmpi -np 2 --map-by :OVERSUBSCRIBE --bind-to none ./build-openmpi/test/mpi/mpi_ping | grep "10000	16" || touch $(FAILED)
endif
	make -C test/mpi clean
	-verbose=1 STARPU_HOME=$(CURDIR) MPICC_PATH=mpicc.mpich MPIRUN_PATH=mpirun.mpich MPI_MODULE_NAME=mpich dh_auto_test --no-parallel -Bbuild-mpich -- -k ARGS\+=--extra-verbose || touch $(FAILED)
	-rm -fr .starpu
ifeq ($(OPENMPI),yes)
	make -C test/mpi clean
ifneq (,$(filter $(DEB_HOST_ARCH),amd64 arm64 ppc64el))
	-verbose=1 MPIRUN_CLI_OPTION=--oversubscribe MPICC_PATH=mpicc.openmpi MPIRUN_PATH=mpirun.openmpi MPI_MODULE_NAME=openmpi dh_auto_test --no-parallel -Bbuild-openmpi -- -k ARGS\+=--extra-verbose || touch $(FAILED)
else
	verbose=1 MPIRUN_CLI_OPTION=--oversubscribe MPICC_PATH=mpicc.openmpi MPIRUN_PATH=mpirun.openmpi MPI_MODULE_NAME=openmpi dh_auto_test --no-parallel -Bbuild-openmpi -- -k ARGS\+=--extra-verbose || touch $(FAILED)
endif
endif
ifneq ($(filter $(DEB_HOST_ARCH),$(OMPT_ARCHS)),)
	-verbose=1 dh_auto_test --no-parallel -Bbuild-ompt -- -k ARGS\+=--extra-verbose || touch $(FAILED)
endif

override_dh_dwz:
	:
