#!/usr/bin/make -f

# DH_VERBOSE := 1
export LC_ALL=C.UTF-8

CLANG_SKIP=alpha hppa ia64 m68k sh4 x32
# ppc64 is due to https://github.com/nemequ/simde/issues/185

include /usr/share/dpkg/default.mk
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

ifneq (,$(filter $(DEB_BUILD_ARCH),armel))
  export DEB_LDFLAGS_MAINT_APPEND=-latomic
endif

GCC_VERS = 8 9 10
CLANG_VERS = 9 10 11

%:
	dh $@ --buildsystem=meson+ninja

override_dh_auto_configure:
#ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
  ifeq (,$(filter $(DEB_HOST_ARCH),$(CLANG_SKIP)))
	$(foreach VER,$(CLANG_VERS),CC=clang-$(VER) CXX=clang++-$(VER) dh_auto_configure --builddirectory=clang$(VER)_test --buildsystem=meson+ninja && ) true ;
  endif
ifneq (,$(filter $(DEB_BUILD_ARCH),ppc64))
	export DEB_CFLAGS_MAINT_APPEND += -maltivec
	export DEB_CXXFLAGS_MAINT_APPEND += -maltivec
# According to https://wiki.debian.org/ArchitectureSpecificsMemo#Architecture_baselines
# It is odd that gcc doesn't include this automatically, but clang does
endif
	$(foreach VER,$(GCC_VERS),CC=gcc-$(VER) CXX=g++-$(VER) dh_auto_configure --builddirectory=gcc$(VER)_test --buildsystem=meson+ninja && ) true ;
#else
#	echo DEB_BUILD_OPTIONS contains "nocheck" so there is no need to configure the tests.
#endif

override_dh_auto_build:
#ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(foreach VER,$(GCC_VERS),dh_auto_build --builddirectory=gcc$(VER)_test --buildsystem=meson+ninja && ) true ;
  ifeq (,$(filter $(DEB_HOST_ARCH),$(CLANG_SKIP)))
	$(foreach VER,$(CLANG_VERS),dh_auto_build --builddirectory=clang$(VER)_test --buildsystem=meson+ninja && ) true ;
  endif
#endif

override_dh_auto_test:
#ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifeq (,$(filter $(DEB_HOST_ARCH),$(CLANG_SKIP)))
	RESULT=0 $(foreach VER,$(CLANG_VERS),dh_auto_test --builddirectory=clang$(VER)_test --buildsystem=meson+ninja || { true ; RESULT=1 ; } ; ) \
	  $(foreach VER,$(GCC_VERS),dh_auto_test --builddirectory=gcc$(VER)_test --buildsystem=meson+ninja || { true ; RESULT=1 ; } ; ) exit $${RESULT}
else
	RESULT=0 $(foreach VER,$(GCC_VERS),dh_auto_test --builddirectory=gcc$(VER)_test --buildsystem=meson+ninja || { true ; RESULT=1 ; } ; ) exit $${RESULT}
endif
#endif

override_dh_auto_clean:
	$(foreach VER,$(GCC_VERS),dh_auto_clean --builddirectory=gcc$(VER)_test --buildsystem=meson+ninja ; )
ifeq (,$(filter $(DEB_HOST_ARCH),$(CLANG_SKIP)))
	$(foreach VER,$(CLANG_VERS),dh_auto_clean --builddirectory=clang$(VER)_test --buildsystem=meson+ninja ; )
endif

override_dh_auto_install:
	echo This is a header-only library, no need to install any binaries
