#!/usr/bin/make -f

binaries := $(shell dh_listpackages)

export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow
export LC_ALL=C.UTF-8

# Ensure the build aborts when there are still references to undefined symbols
DEB_LDFLAGS_MAINT_APPEND += -Wl,-z,defs

# NB: do NOT use -Wl,--as-needed to build glib; for instance the link to
# pthread is carefully crafted to allow dlopen()ing pthread-using libs; see
# http://mid.gmane.org/1257999019.21780.15.camel@marzipan
DEB_LDFLAGS_MAINT_APPEND += -Wl,--no-as-needed

# Make the linker work a bit harder so dynamic loading can be done faster
DEB_LDFLAGS_MAINT_APPEND += -Wl,-O1

include /usr/share/dpkg/default.mk

# We open-code the Meson clean, configure, build, test, install steps
# because the debhelper in SteamRT 1 'scout' is too old to have built-in
# knowledge of Meson. Ideally we'd use --buildsystem=none, but scout is
# too old for that too.
%:
	dh $@ --with gnome,python3 --buildsystem=makefile

override_dh_autoreconf:
	@:

ifneq ($(filter hppa,$(DEB_HOST_ARCH_CPU)),)
stack_grows_up = true
else
stack_grows_up = false
endif

ifneq ($(filter kfreebsd,$(DEB_HOST_ARCH_OS)),)
have_non_stub_xattr = false
else
have_non_stub_xattr = true
endif

override_dh_auto_configure:
	mkdir -p debian/build/deb
	cd debian/build/deb && if ! meson setup ../../.. \
		--buildtype=plain \
		--localstatedir=/var \
		--prefix=/usr \
		--sysconfdir=/etc \
		--wrap-mode=nodownload \
		$(DEB_CONFIGURE_EXTRA_FLAGS) \
		$(DEB_CONFIGURE_FLAGS_deb) \
	; then \
		tail -v -n+0 meson-logs/meson-log.txt; \
		exit 1; \
	fi

override_dh_clean:
	rm -rf $(CURDIR)/debian/build \
	       $(CURDIR)/debian/install \
	       $(CURDIR)/debian/tmp-xdg-runtime-dir
	dh_clean -Xorg.gtk.test.gschema.xml.orig -Xorg.gtk.test.gschema.override.orig


override_dh_makeshlibs:
	dh_makeshlibs -plibglib2.0-tests -n
	dh_makeshlibs --remaining-packages -V -- -c4

# (Ubuntu-specific)
# Don't strip translations from the installed tests; makes them environment dependent.
override_dh_translations:
	dh_translations -Xinstalled-tests

# Do not clutter $HOME with ~/.dbus-keyrings and avoid failure on the buildds
# where creating /home/buildd/.dbus-keyrings fails
export HOME=$(CURDIR)/debian/build

# Put these back to their defaults if we are not running with a clean
# environment, so that they are based on the temporary $HOME above.
# Otherwise, some tests would try to write to the user's real XDG_DATA_HOME,
# causing unwanted side-effects or FTBFS depending whether the user's
# real HOME is available in the build chroot (if used)
unexport XDG_CACHE_HOME
unexport XDG_CONFIG_DIRS
unexport XDG_CONFIG_HOME
unexport XDG_DATA_HOME
unexport XDG_DATA_DIRS

# Make sure that everything that uses D-Bus is creating its own temporary
# session rather than polluting the developer's (or failing, on buildds)
export DBUS_SESSION_BUS_ADDRESS=this-should-not-be-used-and-will-fail:

# 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 arm hppa mips% sparc%,$(DEB_HOST_ARCH_CPU)),)
$(info Slow architecture detected, increasing test timeout)
test_timeout_multiplier = 5
endif

ifneq ($(filter m68k riscv64 sh4,$(DEB_HOST_ARCH_CPU)),)
$(info Architecture with qemu buildds detected, increasing test timeout a lot)
test_timeout_multiplier = 20
endif

override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# Remove LD_PRELOAD so we don't run with fakeroot, which makes dbus-related tests fail
	if ! env -u LD_PRELOAD \
		DEB_BUILD_TIME_TESTS=1 \
		MESON_TESTTHREADS=1 \
	meson test -C debian/build/deb \
		--no-rebuild \
		--num-processes 1 \
		--timeout-multiplier $(test_timeout_multiplier) \
	; then \
		tail -v -n+0 debian/build/deb/meson-logs/testlog.txt; \
		exit 1; \
	fi
endif

# Skip build-time tests if all we are building is documentation; running
# them once per architecture is plenty
override_dh_auto_test-indep:
	@:

# The tests assume this directory exists and is writable
export XDG_RUNTIME_DIR=$(CURDIR)/debian/tmp-xdg-runtime-dir
# Let's get failing tests' stdout and stderr so we have some information when
# a build fails
export VERBOSE=1

ifeq ($(DEB_HOST_ARCH_OS),linux)
is_linux := true
else
is_linux := false
endif

ifneq ($(filter hurd kfreebsd,$(DEB_HOST_ARCH_OS)),)
use_fam := true
else
use_fam := false
endif

# configure flags
DEB_CONFIGURE_EXTRA_FLAGS := \
	--libdir=lib/$(DEB_HOST_MULTIARCH) \
	--libexecdir=lib/glib2.0 \
	-Dinternal_pcre=true \
	-Dselinux=false \
	$(NULL)

DEB_CONFIGURE_FLAGS_deb := \
	--default-library both \
	-Dfam=$(use_fam) \
	-Dinstalled_tests=true \
	-Dlibmount=false \
	-Dman=true \
	-Dsystemtap=true \
	-Dxattr=$(have_non_stub_xattr) \
	$(NULL)

DEB_CONFIGURE_FLAGS_deb += -Dgtk_doc=false

override_dh_auto_build:
	mkdir -p debian/tmp-xdg-runtime-dir
	ninja -v -C debian/build/deb

override_dh_auto_install:
	set -e; for script in postinst postrm triggers; do \
		sed -e"s/#MULTIARCH#/$(DEB_HOST_MULTIARCH)/g" \
		    -e"s/#ARCH#/$(DEB_HOST_ARCH)/g" \
		    debian/libglib2.0-0.$$script.in \
		    > debian/libglib2.0-0.$$script ; \
	done
	mkdir -p debian/install/deb
	DESTDIR=$(CURDIR)/debian/install/deb ninja -C debian/build/deb install

override_dh_python3:
	# dh_python3 can't process both a private directory and /usr/bin
	# in the same invocation
	dh_python3 -plibglib2.0-dev-bin /usr/share/glib-2.0/codegen
	dh_python3 -plibglib2.0-dev-bin

override_dh_install:
	# Unwanted bits from the .deb build
	rm -fr debian/install/deb/usr/share/glib-2.0/codegen/__pycache__
	# https://gitlab.gnome.org/GNOME/glib/issues/1539
	chmod -v 0644 debian/install/deb/usr/lib/glib2.0/installed-tests/glib/desktop-files/usr/applications/org.gnome.clocks.desktop
	chmod -v 0644 debian/install/deb/usr/lib/glib2.0/installed-tests/glib/echo-script.bat
	chmod -v 0644 debian/install/deb/usr/share/bash-completion/completions/gio
	chmod -v 0644 debian/install/deb/usr/share/glib-2.0/codegen/codegen_main.py
	# Re-created by .links to point into /lib
	rm -f debian/install/deb/usr/lib/*/libglib-2.0.so
	# Put the gdb script in .../gdb/auto-load/lib, as we install libglib*.so in there
	mkdir -p debian/install/deb/usr/share/gdb/auto-load/lib/${DEB_HOST_MULTIARCH}/
	mv debian/install/deb/usr/share/gdb/auto-load/usr/lib/${DEB_HOST_MULTIARCH}/libglib*so*py \
		debian/install/deb/usr/share/gdb/auto-load/lib/${DEB_HOST_MULTIARCH}/
	dh_install --remaining-packages --sourcedir=debian/install/deb

override_dh_missing:
	dh_missing --sourcedir=debian/install/deb --fail-missing
