#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.
#export DH_VERBOSE = 1


# See FEATURE AREAS in dpkg-buildflags(1).
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Temporarily disable LTO https://gitlab.gnome.org/GNOME/gimp/-/issues/9633
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto

# See ENVIRONMENT in dpkg-buildflags(1).
# Package maintainers to append CFLAGS.
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# Package maintainers to append LDFLAGS.
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

export DEB_LDFLAGS_MAINT_APPEND = -Wl,-O1
export DPKG_GENSYMBOLS_CHECK_LEVEL = 4

ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
	BUGTRACKER = https://bugs.launchpad.net/ubuntu/+source/gimp/+filebug?no-redirect
else
	BUGTRACKER = https://www.debian.org/Bugs/Reporting
endif

built_binaries := $(shell dh_listpackages)

ifneq ($(filter %-doc,$(built_binaries)),)
	BUILD_DOCS := -Dgi-docgen=enabled
else
	BUILD_DOCS := -Dgi-docgen=disabled
endif

# Check Filters > Development > Plug-in Examples
# If this is enabled, a JavaScript version is installed but there is no menu button for it
# Therefore, we disable it
ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armhf i386 mips64el ppc64el riscv64 s390x loong64 ppc64 sparc64))
	JS = disabled
else
	JS = disabled
endif

# Upstream has marked the Lua plugins as experimental and disabled by default
# One of the plugins is an example like JavaScript but there is no menu button for it either
ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf i386 mips64el riscv64 s390x loong64 powerpc))
	LUA = false
else
	LUA = false
endif

%:
	dh $@


# dh_make generated override targets.
# This is an example for Cmake (see <https://bugs.debian.org/641051>).
#override_dh_auto_configure:
#	dh_auto_configure -- \
#	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

override_dh_auto_configure:
	dh_auto_configure -- \
		-Dlibexecdir=/usr/lib/$(DEB_HOST_MULTIARCH)/gimp \
		-Dauto_features=enabled \
		$(BUILD_DOCS) \
		-Dcheck-update=no \
		-Dappdata-test=disabled \
		-Dheadless-tests=disabled \
		-Djavascript=$(JS) \
		-Dlua=$(LUA) \
		-Dmng=disabled \
		-Dilbm=disabled \
		-Dbug-report-url=$(BUGTRACKER)

override_dh_makeshlibs:
	dh_makeshlibs -X/usr/lib/$(DEB_HOST_MULTIARCH)/gimp/3.0/modules

# Give tests more time on slow architectures like riscv64
override_dh_auto_test:
	NO_AT_BRIDGE=1 xvfb-run -s -noreset -a dh_auto_test --  --verbose --timeout-multiplier 5
