#!/usr/bin/make -f

DISABLE_UPDATE_UPLOADERS = 1
include /usr/share/gnome-pkg-tools/1/rules/uploaders.mk
include /usr/share/dpkg/default.mk
include /usr/share/dpkg/pkg-info.mk

dh_options =
built_binaries := $(shell dh_listpackages)
all_binaries := $(shell sed -ne 's/^Package: *//p' < debian/control)

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

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

export APIVER := 4
export SONAME := 1

export LIBDIR := usr/lib/$(DEB_HOST_MULTIARCH)

# earliest version that this release has backwards binary compatibility for
export GTK_BINARY_VERSION := 4.0.0

# Gtk binary version virtual Provide
export GTK_BINVER_DEP := gtk$(APIVER)-binver-$(GTK_BINARY_VERSION)

# relative base directory for configuration
export CONFDIR := etc/gtk-4.0

# relative base directory for all types of modules
export MODULES_BASE_PATH := $(LIBDIR)/gtk-4.0/$(GTK_BINARY_VERSION)

# package names
export SHARED_PKG := libgtk-$(APIVER)-$(SONAME)
export COMMON_PKG := libgtk-$(APIVER)-common
export DEV_PKG := libgtk-$(APIVER)-dev
export UDEB_PKG := $(SHARED_PKG)-udeb
export DOC_PKG := libgtk-$(APIVER)-doc
export BIN_PKG := libgtk-$(APIVER)-bin
export MEDIA_GSTREAMER_PKG := libgtk-$(APIVER)-media-gstreamer
export MEDIA_FFMPEG_PKG := libgtk-$(APIVER)-media-ffmpeg
export EXAMPLES_PKG := gtk-$(APIVER)-examples
export TESTS_PKG := gtk-$(APIVER)-tests

# files larger than 4k in size will be compressed by dh_compress
# so append a .gz suffix to avoid dangling symlinks
export README_MD_MAYBE_GZ := README.md$(shell find -maxdepth 1 -size +4k -name README.md -exec echo ".gz" \;)

# macro computing the list of 'debian/<pkg>.*" files which have a
# corresponding ".in" file; pass the list of packages in $(1)
dh_subst_files = $(patsubst %.in,%,$(wildcard $(addprefix debian/, $(addsuffix *.in, $(1)))))

debian/%: debian/%.in
	dh_testdir
	sed \
		-e "s#@SONAME@#$(SONAME)#g" \
		-e "s#@APIVER@#$(APIVER)#g" \
		-e "s#@GTK_BINVER_DEP@#$(GTK_BINVER_DEP)#g" \
		-e "s#@SHARED_PKG@#$(SHARED_PKG)#g" \
		-e "s#@COMMON_PKG@#$(COMMON_PKG)#g" \
		-e "s#@DEV_PKG@#$(DEV_PKG)#g" \
		-e "s#@UDEB_PKG@#$(UDEB_PKG)#g" \
		-e "s#@DOC_PKG@#$(DOC_PKG)#g" \
		-e "s#@BIN_PKG@#$(BIN_PKG)#g" \
		-e "s#@MEDIA_GSTREAMER_PKG@#$(MEDIA_GSTREAMER_PKG)#g" \
		-e "s#@MEDIA_FFMPEG_PKG@#$(MEDIA_FFMPEG_PKG)#g" \
		-e "s#@EXAMPLES_PKG@#$(EXAMPLES_PKG)#g" \
		-e "s#@TESTS_PKG@#$(TESTS_PKG)#g" \
		-e "s#@GNOME_TEAM@#$(UPLOADERS)#g" \
		-e "s#@LIBDIR@#$(LIBDIR)#g" \
		-e "s#@MODULES_BASE_PATH@#$(MODULES_BASE_PATH)#g" \
		-e "s#@DEB_VERSION_UPSTREAM@#$(DEB_VERSION_UPSTREAM)#g" \
		$@.in > $@

configure_flags_deb = \
	-Dauto_features=enabled \
	-Dbroadway-backend=true \
	-Dx11-backend=true \
	${NULL}

ifeq (,$(filter stage1,$(DEB_BUILD_PROFILES)))
configure_flags_deb += -Dcolord=enabled
else
configure_flags_deb += -Dcolord=disabled
endif

ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
configure_flags_deb += -Dman-pages=true
else
configure_flags_deb += -Dman-pages=false
endif

ifeq (linux,$(DEB_HOST_ARCH_OS))
configure_flags_deb += -Dsysprof=enabled
configure_flags_deb += -Dwayland-backend=true
else
configure_flags_deb += -Dsysprof=disabled
configure_flags_deb += -Dvulkan=disabled
configure_flags_deb += -Dwayland-backend=false
endif

ifeq ($(filter %-doc,$(built_binaries)),)
configure_flags_deb += -Ddocumentation=false
else
configure_flags_deb += -Ddocumentation=true
endif

ifeq ($(filter %-examples,$(built_binaries)),)
configure_flags_deb += -Dbuild-examples=false
configure_flags_deb += -Ddemos=false
else
configure_flags_deb += -Dbuild-examples=true
configure_flags_deb += -Ddemos=true
endif

ifeq ($(filter %-tests,$(built_binaries)),)
configure_flags_deb += -Dinstall-tests=false
else
configure_flags_deb += -Dinstall-tests=true
endif

# Upstream consider the ffmpeg backend to be experimental, let's not
# build that by default.
ifeq ($(filter $(MEDIA_FFMPEG_PKG),$(built_binaries)),)
configure_flags_deb += -Dmedia-ffmpeg=disabled
else
configure_flags_deb += -Dmedia-ffmpeg=enabled
endif

ifeq ($(shell dpkg-vendor --is Ubuntu && echo yes), yes)
# Not in main repository
configure_flags_deb += -Dcloudproviders=disabled
else
configure_flags_deb += -Dcloudproviders=enabled
endif

configure_flags_udeb = \
	-Dauto_features=disabled \
	-Dbroadway-backend=false \
	-Dbuild-examples=false \
	-Ddemos=false \
	-Dbuild-tests=false \
	-Dinstall-tests=false \
	-Dwayland-backend=false \
	-Dx11-backend=true \
	$(NULL)

%:
	dh $@ --with gir --buildsystem=meson $(dh_options)

override_dh_clean: debian/control
	# gross kludge to force control generation with the %.in target
	touch debian/control.in
	rm -f $(call dh_subst_files,$(all_binaries))
	rm -rf debian/build debian/install
	# Rebuild the CSS from source. Note that gtk.css is hand-written,
	# not generated, so don't delete that one.
	rm -f gtk/theme/Default/Default-*.css
	dh_clean

# Add dependencies to generate files from the debian/*.in ones
override_dh_auto_build: $(call dh_subst_files,$(all_binaries))

override_dh_auto_build: rebuild-emoji
rebuild-emoji:
	gcc -o debian/convert-emoji gtk/emoji/convert-emoji.c `pkg-config --cflags --libs json-glib-1.0`
	set -eu; for input in debian/missing-sources/emojibase/packages/data/??/data.raw.json; do \
		lang="$$(echo "$$input" | sed -ne 's,^.*/\([a-z][a-z]\)/data.raw.json$$,\1,p')"; \
		output="gtk/emoji/$$lang.data"; \
		test -f "$$output" || continue; \
		debian/convert-emoji "$$input" "$$output"; \
	done

override_dh_auto_configure: debian/control
	dh_auto_configure \
		--builddirectory=debian/build/deb \
		-- \
		$(configure_flags_deb) \
		$(NULL)
ifneq ($(filter %-udeb,$(built_binaries)),)
	dh_auto_configure \
		--builddirectory=debian/build/udeb \
		-- \
		$(configure_flags_udeb) \
		$(NULL)
endif

override_dh_auto_build:
	dh_auto_build --builddirectory=debian/build/deb
ifneq ($(filter %-udeb,$(built_binaries)),)
	dh_auto_build --builddirectory=debian/build/udeb
endif

test_backends = x11
test_timeout_multiplier = 1

ifeq (linux,$(DEB_HOST_ARCH_OS))
	test_backends += wayland
endif

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

ifneq ($(filter mips%,$(DEB_HOST_ARCH_CPU)),)
$(info Disabling use of llvmpipe due to mips family CPU, see https://bugs.debian.org/993550)
export GALLIUM_DRIVER=softpipe
export LIBGL_ALWAYS_SOFTWARE=true
endif

fuzzy_gsk_compare = \
	$(NULL)

ignore_gsk_compare = \
	unaligned-offscreen \
	$(NULL)

xfail_gsk_compare = \
	$(NULL)

fuzzy_reftests = \
	border-image-url \
	label-attribute-preference \
	label-wrap-justify \
	pseudoclass-on-box \
	quit-mnemonic \
	$(NULL)

ignore_reftests = \
	$(NULL)

xfail_reftests = \
	label-sizing \
	sizegroups-evolution-identity-page \
	$(NULL)

skipped_suites = \
	failing \
	flaky \
	wayland_failing \
	wayland_gles_failing \
	$(NULL)

# https://bugs.debian.org/1050312
ifeq (i386,$(DEB_HOST_ARCH_CPU))
ignore_reftests += \
	hbox-with-ellipsizing-wrapping-label \
	window-border-width \
	$(NULL)
endif

# These succeed with llvmpipe, but fail with softpipe
# https://gitlab.gnome.org/GNOME/gtk/-/issues/6085
# https://bugs.debian.org/1050077
# https://bugs.debian.org/1051168
fuzzy_gsk_compare += opacity-overdraw
ignore_gsk_compare += border-one-rounded
fuzzy_reftests += opacity

# https://bugs.debian.org/1024391
ifeq (big,$(DEB_HOST_ARCH_ENDIAN))
	ignore_reftests += border-image-excess-size
endif

# TODO: Known failures: https://gitlab.gnome.org/GNOME/gtk/-/issues/4110
# s390x: many (endianness?)
ifeq (big,$(DEB_HOST_ARCH_ENDIAN))
	skipped_suites += \
		gsk-compare-broadway \
		gsk-compare-gl \
		$(NULL)
endif

override_dh_auto_test:
	env \
		BUILDDIR=debian/build/deb \
		FUZZY_GSK_COMPARE="$(fuzzy_gsk_compare)" \
		FUZZY_REFTESTS="$(fuzzy_reftests)" \
		IGNORE_GSK_COMPARE="$(ignore_gsk_compare)" \
		IGNORE_REFTESTS="$(ignore_reftests)" \
		XFAIL_GSK_COMPARE="$(xfail_gsk_compare)" \
		XFAIL_REFTESTS="$(xfail_reftests)" \
		BACKENDS="$(test_backends)" \
	debian/run-tests.sh -t $(test_timeout_multiplier) \
		$(addprefix --no-suite=,$(skipped_suites)) \
		$(NULL)

override_dh_auto_install:
	dh_auto_install --builddirectory=debian/build/deb --destdir=debian/install/deb
ifneq ($(filter %-udeb,$(built_binaries)),)
	dh_auto_install --builddirectory=debian/build/udeb --destdir=debian/install/udeb
endif

override_dh_installman:
	dh_installman --sourcedir=debian/install/deb

override_dh_install:
	rm -fv debian/install/udeb/usr/bin/gtk4-update-icon-cache
	rm -frv debian/install/udeb/usr/share/gtk-4.0/valgrind
	rm -frv debian/install/udeb/usr/share/metainfo
	:
	dh_install -p$(UDEB_PKG) --sourcedir=debian/install/udeb
	dh_install --remaining-packages --sourcedir=debian/install/deb

# debhelper >= 13.4 makes all of /usr/libexec executable, which is not
# quite right for installed-tests
override_dh_fixperms:
	dh_fixperms -Xusr/libexec/installed-tests
ifneq ($(filter %-tests,$(built_binaries)),)
	chmod --recursive --changes a+rX,u+w,og-w debian/*-tests/usr/libexec/installed-tests
endif

override_dh_missing:
ifneq ($(filter %-udeb,$(built_binaries)),)
	dh_missing --sourcedir=debian/install/udeb --list-missing -X.la
endif
	dh_missing --sourcedir=debian/install/deb --fail-missing -X.la

override_dh_installdocs:
	dh_installdocs -p$(COMMON_PKG) AUTHORS README.md NEWS
	dh_installdocs -p$(DOC_PKG) AUTHORS -X.in
	dh_installdocs --remaining-packages

override_dh_makeshlibs:
	dh_makeshlibs -p$(SHARED_PKG) -V --add-udeb=$(UDEB_PKG) -X$(MODULES_BASE_PATH) -- -c4
	dh_makeshlibs --remaining-packages -X$(MODULES_BASE_PATH)
