#!/usr/bin/make -f

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_ARCH_ENDIAN ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_ENDIAN)
EXACT_PACKAGE_VERSION = $(shell dpkg-parsechangelog | grep Version | cut -d' ' -f 2)

export DPKG_GENSYMBOLS_CHECK_LEVEL = 4

%:
	dh $@ --parallel --fail-missing --with python3

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

COMMON_CONFIGURE_OPTIONS = \
  -DCMAKE_INSTALL_LIBEXECDIR="lib/$(DEB_HOST_MULTIARCH)/mir"

override_dh_auto_configure:
ifeq ($(DEB_HOST_ARCH),armhf)
	dh_auto_configure -- \
	  $(COMMON_CONFIGURE_OPTIONS) \
	  -DMIR_RUN_ACCEPTANCE_TESTS=OFF \
	  -DMIR_RUN_INTEGRATION_TESTS=OFF \
	  -DMIR_PLATFORM=android\;mesa-kms\;mesa-x11
else
ifneq ($(filter amd64 i386,$(DEB_HOST_ARCH)),)
	dh_auto_configure -- \
	  $(COMMON_CONFIGURE_OPTIONS) \
	  -DMIR_PLATFORM=mesa-kms\;mesa-x11\;android
else
	dh_auto_configure -- \
	  $(COMMON_CONFIGURE_OPTIONS) \
	  -DMIR_PLATFORM=mesa-kms\;mesa-x11
endif
endif

# Only build the docs when we need them
override_dh_auto_build-indep:
	dh_auto_build -- doc

# TODO: we'll use a symbol file once mir is abi stable
override_dh_makeshlibs:
	dh_makeshlibs -V

override_dh_install:
# Nothing outside Mir should link to libmirprotobuf directly.
# Delete the symlink so that --fail-missing doesn't think we've missed it
# accidentally.
	-rm debian/tmp/usr/lib/*/libmirprotobuf.so
	dh_install -p libmirplatform-dev -Xmesa --fail-missing
	dh_install --remaining-packages --fail-missing
