#!/usr/bin/make -f

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

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 (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifeq ($(DEB_HOST_ARCH_ENDIAN),little)
        # Code to run the package test suite.
	GTEST_OUTPUT=xml:./ dh_auto_build -- ARGS="-V" ptest
else
	echo "Testsuite disabled on $(DEB_HOST_ARCH) due to lack of big-endian support."
endif
endif

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

ifeq ($(filter noopt,$(DEB_BUILD_OPTIONS)),noopt)
	COMMON_CONFIGURE_OPTIONS += -DMIR_LINK_TIME_OPTIMIZATION=OFF
else
	COMMON_CONFIGURE_OPTIONS += -DMIR_LINK_TIME_OPTIMIZATION=ON
endif

# Disable LTO on s390x, due to failing to build
ifeq ($(DEB_HOST_ARCH),s390x)
	COMMON_CONFIGURE_OPTIONS += -DMIR_LINK_TIME_OPTIMIZATION=OFF
endif

# Disable Mir EGL outside of Ubuntu 16.04 and 17.10
ifneq ($(filter-out xenial artful,$(DEB_DISTRIBUTION)),)
	COMMON_CONFIGURE_OPTIONS += -DMIR_EGL_SUPPORTED=OFF
endif

$(info COMMON_CONFIGURE_OPTIONS: ${COMMON_CONFIGURE_OPTIONS})

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=mesa-kms\;mesa-x11 \
	  $(OVERRIDE_CONFIGURE_OPTIONS)
else
ifneq ($(filter amd64 i386,$(DEB_HOST_ARCH)),)
	dh_auto_configure -- \
	  $(COMMON_CONFIGURE_OPTIONS) \
	  -DMIR_PLATFORM=mesa-kms\;mesa-x11 \
	  $(OVERRIDE_CONFIGURE_OPTIONS)
else
	dh_auto_configure -- \
	  $(COMMON_CONFIGURE_OPTIONS) \
	  -DMIR_PLATFORM=mesa-kms\;mesa-x11 \
	  $(OVERRIDE_CONFIGURE_OPTIONS)
endif
endif

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

# 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 -Nlibmiral3 -V -Nmir-test-tools
	# libmiral3 *has* a symbols file, so we don't need to -V it!
	dh_makeshlibs -plibmiral3 -- -v$(MIRAL_RELEASE)

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 -pmir-utils usr/bin/mirvanity

ifeq ($(filter xenial yakkety zesty,$(DEB_DISTRIBUTION)),)
	dh_install
else
	# xenial, yakkety & zesty predate dh_missing
	rm debian/tmp/usr/bin/mirvanity
	dh_install --fail-missing
endif

# 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 -Nlibmiral3 -Nlibmiral-dev
	dh_gencontrol -plibmiral3 -plibmiral-dev -- -v$(MIRAL_VERSION) -V'libmiral3:Version=$(MIRAL_VERSION)'
