#!/usr/bin/make -f

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

CLANG_SKIP=i386 alpha hppa ia64 m68k sh4 x32
# i386 is due to https://github.com/simd-everywhere/simde/issues/683
# ppc64 is due to https://github.com/simd-everywhere/simde/issues/185

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

# https://github.com/simd-everywhere/simde/issues/982
ifneq (,$(filter $(DEB_BUILD_ARCH),armel))
  export DEB_LDFLAGS_MAINT_APPEND=-latomic
  export DEB_CFLAGS_MAINT_APPEND=-O0
  export DEB_CPPFLAGS_MAINT_APPEND=-O0
endif
ifneq (,$(filter $(DEB_BUILD_ARCH),armhf))
  export DEB_CFLAGS_MAINT_APPEND=-O0
  export DEB_CPPFLAGS_MAINT_APPEND=-O0
endif

# on arm64, -O2 (dpkg-buildflag's default) causes test/arm/neon/st4.cpp to not compile
#  in a reasonable time
ifneq (,$(filter $(DEB_BUILD_ARCH),arm64))
  export DEB_CFLAGS_MAINT_APPEND=-O0
  export DEB_CPPFLAGS_MAINT_APPEND=-O0
endif

#  https://github.com/simd-everywhere/simde/issues/981
# -O2 (dpkg-buildflag's default) triggers gcc-12 regressions
ifneq (,$(filter $(DEB_BUILD_ARCH),i386))
  export DEB_CFLAGS_MAINT_APPEND=-O0
  export DEB_CPPFLAGS_MAINT_APPEND=-O0
endif

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

override_dh_auto_configure:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
# Skip testing on clang for now owing to bug report #1002223
#   ifeq (,$(filter $(DEB_HOST_ARCH),$(CLANG_SKIP)))
# 	CC=clang CXX=clang++ dh_auto_configure --builddirectory=clang_test
#   endif

# https://github.com/simd-everywhere/simde/issues/867
ifneq (,$(filter $(DEB_BUILD_ARCH),i386))
  sed -i "/'dbsad'/d" meson.build
  rm test/x86/avx512/dbsad.c
endif

# According to https://wiki.debian.org/ArchitectureSpecificsMemo#Architecture_baselines
# It is odd that gcc doesn't include this automatically, but clang does
ifneq (,$(filter $(DEB_BUILD_ARCH),ppc64))
  export DEB_CFLAGS_MAINT_APPEND+=-maltivec
  export DEB_CXXFLAGS_MAINT_APPEND+=-maltivec
endif
	CC=gcc CXX=g++ dh_auto_configure --builddirectory=gcc_test
endif

override_dh_auto_build:
	echo skipping dh_auto_build in favor of dh_auto_test

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test --builddirectory=gcc_test -- ${shell meson test -C gcc_test  --list | grep -v emul}
# Skip testing on clang for now owing to bug report #1002223
#  ifeq (,$(filter $(DEB_HOST_ARCH),$(CLANG_SKIP)))
#	dh_auto_test --builddirectory=clang_test
#  endif
endif

override_dh_auto_clean:
	dh_auto_clean --builddirectory=gcc_test
ifeq (,$(filter $(DEB_HOST_ARCH),$(CLANG_SKIP)))
	dh_auto_clean --builddirectory=clang_test
endif

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