#!/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

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# Ensure the build aborts when there are still references to undefined symbols
# currently fails at least in the im-multipress im module
#LDFLAGS += -Wl,-z,defs
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-O1

APIVER := 3
SONAME := 0

LIBDIR := usr/lib/$(DEB_HOST_MULTIARCH)

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

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

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

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

# package names
SHARED_PKG := libgtk-$(APIVER)-$(SONAME)
COMMON_PKG := libgtk-$(APIVER)-common
DEV_PKG := libgtk-$(APIVER)-dev
UDEB_PKG := $(SHARED_PKG)-udeb
DOC_PKG := libgtk-$(APIVER)-doc
BIN_PKG := libgtk-$(APIVER)-bin
EXAMPLES_PKG := gtk-$(APIVER)-examples

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

# Avoid test failures on buildd environments
export HOME=$(CURDIR)/debian/build
export XDG_RUNTIME_DIR=$(HOME)
# So that gsettings can find the (uninstalled) gtk schemas
export XDG_DATA_DIRS=/usr/share:$(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
unexport XDG_CACHE_HOME
unexport XDG_CONFIG_DIRS
unexport XDG_CONFIG_HOME
unexport XDG_DATA_HOME

# 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#@VERSION@#$(DEB_VERSION_UPSTREAM)#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#@EXAMPLES_PKG@#$(EXAMPLES_PKG)#g" \
		-e "s#@GNOME_TEAM@#$(UPLOADERS)#g" \
		-e "s#@GTK_BINARY_VERSION@#$(GTK_BINARY_VERSION)#g" \
		-e "s#@LIBDIR@#$(LIBDIR)#g" \
		-e 's#@CONFDIR@#$(CONFDIR)#g' \
		-e "s#@MODULES_BASE_PATH@#$(MODULES_BASE_PATH)#g" \
		-e "s#@NEWS@#$(NEWS)#g" \
		-e "s#@README@#$(README)#g" \
		$@.in > $@

configure_flags_common = \
	-Dx11_backend=true \
	$(NULL)

configure_flags_deb = \
	--default-library=shared \
	-Dbroadway_backend=true \
	-Dinstalled_tests=true \
	-Dintrospection=true \
	-Dprint_backends=cups,file \
	$(NULL)

ifeq (,$(filter stage1,$(DEB_BUILD_PROFILES)))
configure_flags_deb += -Dcolord=yes
else
configure_flags_deb += -Dcolord=no
endif

ifeq (linux,$(DEB_HOST_ARCH_OS))
configure_flags_deb += -Dwayland_backend=true
else
configure_flags_deb += -Dwayland_backend=false
endif

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

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

configure_flags_udeb = \
	-Dbroadway_backend=false \
	-Dcolord=no \
	-Dgtk_doc=false \
	-Dintrospection=false \
	-Dman=false \
	-Dprint_backends=file \
	-Dwayland_backend=false \
	$(NULL)

%:
	dh $@

fuzzy_reftests =

# https://gitlab.gnome.org/GNOME/gtk/-/issues/5545
fuzzy_reftests += label-background

# Debian-specific
ignore_reftests += \
	flipping-icons \
	$(NULL)

ifneq (,$(filter $(DEB_HOST_ARCH_CPU),i386))
ignore_reftests += linear-gradient
endif

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

xfail_reftests = \
	$(NULL)

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
	# The build system does not automatically update the GResource files
	# when resources change. Force an update in case we ship a patch which
	# modifies the theme CSS files.
	rm -f gtk/theme/Adwaita/gtk-contained.css
	rm -f gtk/theme/Adwaita/gtk-contained-dark.css
	rm -f gtk/theme/HighContrast/gtk-contained.css
	rm -f gtk/theme/HighContrast/gtk-contained-inverse.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; \
		debian/convert-emoji $$input $$output; \
	done

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

override_dh_auto_build:
	( cd gtk/theme/Adwaita; ./parse-sass.sh )
	( cd gtk/theme/HighContrast; ./parse-sass.sh )
	dh_auto_build --builddirectory=debian/build/deb
ifneq ($(filter %-udeb,$(built_binaries)),)
	dh_auto_build --builddirectory=debian/build/udeb
endif

override_dh_auto_test:
# The tests require librsvg2-common which is only available on architectures
# where rust has been ported to, disable the tests on the other ones
ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf i386 mips64el mipsel ppc64el s390x powerpc ppc64 riscv64 sparc64))
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# So that gsettings can find the (uninstalled) gtk schemas
	mkdir -p debian/build/glib-2.0/schemas/
	cp gtk/org.gtk.* debian/build/glib-2.0/schemas/
	glib-compile-schemas debian/build/glib-2.0/schemas/
	env \
		BUILDDIR=$(CURDIR)/debian/build/deb \
		FUZZY_REFTESTS="$(fuzzy_reftests)" \
		IGNORE_REFTESTS="$(ignore_reftests)" \
		LIBGL_ALWAYS_SOFTWARE=1 \
		XFAIL_REFTESTS="$(xfail_reftests)" \
	debian/run-tests.sh --timeout-multiplier 4
endif
endif

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_install:
	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 $(EXAMPLES_PKG),$(built_binaries)),)
	chmod --recursive --changes a+rX,u+w,og-w debian/$(EXAMPLES_PKG)/usr/libexec/installed-tests
endif

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

override_dh_installchangelogs:
	dh_installchangelogs -XChangeLog

override_dh_installdocs:
	dh_installdocs -p$(COMMON_PKG) README.md NEWS
	# We need to install into /u/s/d/l-doc, not -dev according to
	# debhelper 11's defaults, otherwise it would have file conflicts
	dh_installdocs -p$(DOC_PKG) --doc-main-package=$(DOC_PKG) -X.in
	dh_installdocs --remaining-packages -A

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

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