#!/usr/bin/make -f

include /usr/share/dpkg/default.mk
include debian/opts.mk

export DPKG_GENSYMBOLS_CHECK_LEVEL = 4

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@

# Enable verbose debugging output from the testsuite
export MIR_SERVER_LOGGING = on
override_dh_auto_test-arch:
ifeq ($(DEB_HOST_ARCH_ENDIAN),little)
	dh_auto_build -- GTEST_OUTPUT=xml:./ ARGS="-V" ptest
else
	echo "Testsuite disabled on $(DEB_HOST_ARCH) due to lack of big-endian support."
endif

override_dh_auto_test-indep:
	echo "Testsuite not run during arch:all builds."

COMMON_CONFIGURE_OPTIONS = \
  -DCMAKE_INSTALL_LIBEXECDIR="lib/$(DEB_HOST_MULTIARCH)/mir" \
  -DMIR_BUILD_INTERPROCESS_TESTS=OFF \
  -DMIR_ENABLE_RUST=OFF \
  -DMIR_ENABLE_TESTS=$(if $(filter mirtest-dev,$(shell dh_listpackages)),ON,OFF) \
  -DMIR_FATAL_COMPILE_WARNINGS=OFF

# Disable tests on Launchpad riscv64 (canonical/mir#3443, canonical/mir#3470)
ifeq ($(USER) $(DEB_HOST_ARCH),buildd riscv64)
	COMMON_CONFIGURE_OPTIONS += -DMIR_RUN_MIRAL_TESTS=OFF
	COMMON_CONFIGURE_OPTIONS += -DMIR_RUN_WINDOW_MANAGEMENT_TESTS=OFF
	COMMON_CONFIGURE_OPTIONS += -DMIR_RUN_WLCS_TESTS=OFF
endif

# Disable pre-compiled headers on GCC>=12
ifneq ($(shell gcc --version | grep '1[234].[[:digit:]]\+.[[:digit:]]\+$$'),)
	COMMON_CONFIGURE_OPTIONS += -DMIR_USE_PRECOMPILED_HEADERS=OFF
endif

$(info COMMON_CONFIGURE_OPTIONS: ${COMMON_CONFIGURE_OPTIONS})
$(info DEB_BUILD_MAINT_OPTIONS: ${DEB_BUILD_MAINT_OPTIONS})

ifneq ($(filter amd64 i386,$(DEB_HOST_ARCH)),)
       AVAILABLE_PLATFORMS=atomic-kms\;gbm-kms\;x11\;wayland\;eglstream-kms
else
       AVAILABLE_PLATFORMS=atomic-kms\;gbm-kms\;x11\;wayland
endif


override_dh_auto_configure:
ifneq ($(filter armhf,$(DEB_HOST_ARCH)),)
	dh_auto_configure -- \
	  $(COMMON_CONFIGURE_OPTIONS) \
	  -DMIR_RUN_ACCEPTANCE_TESTS=OFF \
	  -DMIR_RUN_INTEGRATION_TESTS=OFF \
	  -DMIR_PLATFORM=$(AVAILABLE_PLATFORMS) \
	  $(OVERRIDE_CONFIGURE_OPTIONS)
else
ifneq ($(filter arm64,$(DEB_HOST_ARCH)),)
	dh_auto_configure -- \
	  $(COMMON_CONFIGURE_OPTIONS) \
	  -DMIR_RUN_ACCEPTANCE_TESTS=OFF \
	  -DMIR_PLATFORM=$(AVAILABLE_PLATFORMS) \
	  $(OVERRIDE_CONFIGURE_OPTIONS)
else
	dh_auto_configure -- \
	  $(COMMON_CONFIGURE_OPTIONS) \
	  -DMIR_PLATFORM=$(AVAILABLE_PLATFORMS) \
	  $(OVERRIDE_CONFIGURE_OPTIONS)
endif
endif
	# Run cmake again to pick up wlcs tests, because reasons?
	cmake

# Reconstruct the MirAL version
MIRAL_VERSION_MAJOR = $(shell perl -n -e '/^set\(MIRAL_VERSION_MAJOR ([0-9]*)\)$$/ && print $$1' src/CMakeLists.txt)
MIRAL_VERSION_MINOR = $(shell perl -n -e '/^set\(MIRAL_VERSION_MINOR ([0-9]*)\)$$/ && print $$1' src/CMakeLists.txt)
MIRAL_VERSION_PATCH = $(shell perl -n -e '/^set\(MIRAL_VERSION_PATCH ([0-9]*)\)$$/ && print $$1' src/CMakeLists.txt)
MIRAL_RELEASE=$(MIRAL_VERSION_MAJOR).$(MIRAL_VERSION_MINOR).$(MIRAL_VERSION_PATCH)
MIRAL_VERSION=$(MIRAL_RELEASE).$(DEB_VERSION)

# TODO: we'll use a symbol file once mir is abi stable
override_dh_makeshlibs:
	dh_makeshlibs -Nlibmiral7 -V -Nmir-test-tools
	dh_makeshlibs -plibmiral7 -V -- -v$(MIRAL_RELEASE)

# Don't try to strip libraries used as test data
override_dh_strip:
	dh_strip -Xmir-test-data

# Don't try to find dependencies of libraries used as test data
override_dh_shlibdeps:
	dh_shlibdeps -Xmir-test-data

override_dh_gencontrol:
	dh_gencontrol -Nlibmiral7 -Nlibmiral-dev
	dh_gencontrol -plibmiral7 -plibmiral-dev -- -v$(MIRAL_VERSION) -V'libmiral7:Version=$(MIRAL_VERSION)'

execute_after_dh_auto_clean:
	# build cruft not cleaned up by CMake
	rm -f include/miral/miral/version.h include/server/mir/version.h

get-orig-source:
	uscan --noconf --force-download --rename --download-current-version --destdir=..
