#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-O1 -Wl,--as-needed

include /usr/share/dpkg/default.mk

ifeq ($(DEB_HOST_ARCH_OS), hurd)
	EXTRA_CONFIGURE_OPTIONS=-Dlibv4l=disabled -Dlibv4l2=disabled
else
	EXTRA_CONFIGURE_OPTIONS=
endif

# Upstream test timeouts assume an otherwise unloaded system, but that
# isn't necessarily the case for a porterbox or multiple parallel builds
test_timeout_multiplier = 3

ifneq ($(filter aarch64 arm% hppa mips% sparc%,$(DEB_HOST_ARCH_CPU)),)
# These architectures are typically slower, particularly for the sort of
# floating-point processing we're doing here
test_timeout_multiplier = 10
endif

ifneq ($(filter m68k riscv64 sh4,$(DEB_HOST_ARCH_CPU)),)
# These architectures are known to have qemu buildds, which are really slow
test_timeout_multiplier = 20
endif

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
		-Dauto_features=enabled \
		-Ddocs=true \
		-Djasper=disabled \
		-Dlua=disabled \
		-Dmrg=disabled \
		-Dpygobject=disabled \
		-Dsdl=disabled \
		$(EXTRA_CONFIGURE_OPTIONS)

# grey2.json appears to only be needed for distcheck according to git log
override_dh_missing:
	dh_missing --fail-missing -Xgrey2.json

override_dh_makeshlibs:
	dh_makeshlibs -X/usr/lib/$(DEB_HOST_MULTIARCH)/gegl-0.4/ -- -c4

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	if env \
		LC_ALL=C.UTF-8 \
		MESON_TESTTHREADS=1 \
	meson test \
		-C obj-$(DEB_HOST_GNU_TYPE) \
		--no-rebuild \
		--num-processes 1 \
		--timeout-multiplier $(test_timeout_multiplier) \
		--verbose \
	; then \
		:; \
	else \
		tail -v -n +0 obj-$(DEB_HOST_GNU_TYPE)/meson-logs/testlog.txt; \
		exit 1; \
	fi
endif
