#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

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

# Following upstream method to compute the API version
MUTTER_MINOR_VERSION = $(shell echo $(DEB_VERSION_UPSTREAM) | cut -d. -f2)
MUTTER_API_VERSION = $(shell echo $$(( ($(MUTTER_MINOR_VERSION) - 23)/2 )) )
MUTTER_SONAME = 0

export MUTTER_API_VERSION
export MUTTER_SONAME

%:
	dh $@

ifeq ($(DEB_HOST_ARCH_OS),linux)
CONFFLAGS = \
	-Degl_device=true \
	-Dremote_desktop=true \
	-Dwayland_eglstream=true
else
CONFFLAGS += \
	-Dudev=false \
	-Dlibwacom=false \
	-Dwayland=false \
	-Dcore_tests=false \
	-Dnative_backend=false \
	-Dremote_desktop=false
endif

ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH),armel armhf))
CONFFLAGS += \
	-Ddefault_driver=gles2
else
CONFFLAGS += \
	-Ddefault_driver=gl
endif

override_dh_auto_configure:
	dh_auto_configure -- \
		-Dinstalled_tests=false \
		-Dprofiler=false \
		$(CONFFLAGS)

# Use meson test directly as it allows us to use the timeout multiplier
BUILDDIR=$(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)
TEST_COMMAND_BASE=env \
	GSETTINGS_SCHEMA_DIR=$(BUILDDIR)/data \
	dbus-run-session -- xvfb-run -s '+iglx -noreset' -a \
	dh_auto_test -- -C $(BUILDDIR) --no-rebuild --verbose --timeout-multiplier 6 \
	           --no-stdsplit --print-errorlogs
TEST_COMMAND=$(TEST_COMMAND_BASE) --no-suite flaky
TEST_COMMAND_FLAKY=$(TEST_COMMAND_BASE) --suite flaky

# Ignore test failures on hurd and kfreebsd. Although the test results
# indicate there may be a serious issue, the port maintainers may
# prefer we do the build anyway. I plan to file a bug for this later.
# Don't run the tests on mips, riscv64 and other ports since they either
# time out or fail too much, blocking migration to testing or ports
override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	glib-compile-schemas $(BUILDDIR)/data
ifneq (,$(filter hurd-i386 kfreebsd-i386 kfreebsd-amd64,$(DEB_HOST_ARCH)))
	-$(TEST_COMMAND)
	-$(TEST_COMMAND_FLAKY)
else ifeq (,$(filter mips mips64el mipsel riscv64 alpha hppa powerpc sparc64 x32,$(DEB_HOST_ARCH)))
	$(TEST_COMMAND)
	-$(TEST_COMMAND_FLAKY)
endif
endif

override_dh_girepository:
	dh_girepository /usr/lib/$(DEB_HOST_MULTIARCH)/mutter-$(MUTTER_API_VERSION)

override_dh_makeshlibs:
	dh_makeshlibs -V

override_dh_shlibdeps:
	dh_shlibdeps -Llibmutter-$(MUTTER_API_VERSION)-$(MUTTER_SONAME) \
		-l/usr/lib/$(DEB_HOST_MULTIARCH)/mutter-$(MUTTER_API_VERSION)
