#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk

# soup2 build -> 4.0 API packages, soup3 build -> 4.1 API packages
ENABLE_SOUP2=YES
ENABLE_SOUP3=NO

# We can also disable the soup2/soup3 builds by removing the build deps
ifeq ($(shell pkg-config --exists libsoup-2.4 || echo no),no)
	ENABLE_SOUP2=NO
endif
ifeq ($(shell pkg-config --exists libsoup-3.0 || echo no),no)
	ENABLE_SOUP3=NO
endif

EXTRA_DH_ARGUMENTS =
EXTRA_CMAKE_ARGUMENTS = -DPORT=GTK \
	   -DCMAKE_INSTALL_LIBEXECDIR=lib/$(DEB_HOST_MULTIARCH) \
	   -DCMAKE_C_FLAGS_RELEASE="" \
	   -DCMAKE_C_FLAGS_DEBUG="" \
	   -DCMAKE_CXX_FLAGS_RELEASE="" \
	   -DCMAKE_CXX_FLAGS_DEBUG="" \
	   -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
	   -DENABLE_GAMEPAD=OFF \
	   -DUSE_LCMS=OFF \
	   -DENABLE_MINIBROWSER=ON
EXTRA_BUILD_ARGUMENTS =
DH_GENCONTROL_ARGS =

# Sacrifice speed in order to make it more likely resource limits
# won't be hit.
ifeq ($(DEB_BUILD_ARCH_BITS),32)
	EXTRA_CMAKE_ARGUMENTS += -DFORCE_32BIT=ON
	LDFLAGS += -Wl,--no-keep-memory
endif

# The debug packages produced by webkit are huge and cause problems in
# most buildds. Use -g1 in all architectures to make them smaller.
CFLAGS := $(CFLAGS:-g=-g1)

# Use the CLoop Javascript interpreter and disable the JIT. This is
# slow but it is the most compatible solution for old (non-SSE2) CPUs.
ifneq (,$(filter $(DEB_HOST_ARCH),i386))
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_JIT=OFF -DENABLE_C_LOOP=ON
endif

# Systemd/elogind and libmanette are Linux-only
ifneq ($(DEB_HOST_ARCH_OS),linux)
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_JOURNALD_LOG=OFF
endif

# See https://bugs.webkit.org/show_bug.cgi?id=197192
ifneq (,$(filter $(DEB_HOST_ARCH),arm64))
	EXTRA_CMAKE_ARGUMENTS += -DWTF_CPU_ARM64_CORTEXA53=OFF
endif

# https://bugs.webkit.org/show_bug.cgi?id=188862
ifneq (,$(filter $(DEB_HOST_ARCH),armel armhf))
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_JIT=OFF
endif

# Disable Gold where it causes build problems, see #949618
ifneq (,$(filter $(DEB_HOST_ARCH),powerpc))
	EXTRA_CMAKE_ARGUMENTS += -DUSE_LD_GOLD=OFF
endif

# Lower memory requirements on mips/mipsel with only 2 GB address space
ifneq (,$(filter $(DEB_HOST_ARCH),mips mipsel))
	CFLAGS := $(CFLAGS:-g1=-g0)
	CFLAGS := $(CFLAGS:-O2=-Os)
	CPPFLAGS += --param ggc-min-expand=10
endif

# See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93876
# and: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93877
ifneq (,$(filter $(DEB_HOST_ARCH),sh3 sh4))
	CFLAGS := $(CFLAGS:-O2=-O1)
endif

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	EXTRA_CMAKE_ARGUMENTS += -DUSE_SYSTEM_MALLOC=ON
	CPPFLAGS += -DRELEASE_WITHOUT_OPTIMIZATIONS
endif

ifeq (,$(filter %-doc,$(shell dh_listpackages)))
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_GTKDOC=OFF
else ifneq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_GTKDOC=OFF
else
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_GTKDOC=ON
endif

ifneq (,$(filter debug,$(DEB_BUILD_OPTIONS)))
	EXTRA_CMAKE_ARGUMENTS += -DCMAKE_BUILD_TYPE=Debug
else
	EXTRA_CMAKE_ARGUMENTS += -DCMAKE_BUILD_TYPE=Release
	CPPFLAGS += -DNDEBUG -DG_DISABLE_CAST_CHECKS
endif

# Don't recommend xdg portals on Ubuntu i386 where it doesn't exist
ifeq ($(shell dpkg-vendor --is Ubuntu && echo yes) $(DEB_HOST_ARCH), yes i386)
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_BUBBLEWRAP_SANDBOX=ON
	DH_GENCONTROL_ARGS += -Vbwrap:Depends="bubblewrap (>= 0.3.1), \
					xdg-dbus-proxy"
# Disable the bubblewrap sandbox if libseccomp-dev is not available
else ifeq ($(shell pkg-config --exists libseccomp && echo yes),yes)
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_BUBBLEWRAP_SANDBOX=ON
	DH_GENCONTROL_ARGS += -Vbwrap:Depends="bubblewrap (>= 0.3.1), \
					xdg-dbus-proxy"
	DH_GENCONTROL_ARGS += -Vbwrap:Suggests="xdg-desktop-portal-gtk"
else
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_BUBBLEWRAP_SANDBOX=OFF
endif

# Disable WPE on Ubuntu since the required backend is in universe
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
	EXTRA_CMAKE_ARGUMENTS += -DUSE_WPE_RENDERER=OFF
# Disable the WPE renderer if libwpebackend-fdo-1.0-dev is not available
else ifeq ($(shell pkg-config --exists wpebackend-fdo-1.0 && echo yes),yes)
	EXTRA_CMAKE_ARGUMENTS += -DUSE_WPE_RENDERER=ON
else
	EXTRA_CMAKE_ARGUMENTS += -DUSE_WPE_RENDERER=OFF
endif

# Lower max parallel jobs on Ubuntu's arm64 builder
ifeq ($(shell dpkg-vendor --is Ubuntu && echo yes) $(DEB_HOST_ARCH), yes arm64)
	EXTRA_BUILD_ARGUMENTS += --max-parallel=2
endif

# set the distributor agent for Ubuntu
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
	EXTRA_CMAKE_ARGUMENTS += -DUSER_AGENT_BRANDING="Ubuntu"
endif

# gstreamer1.0-libav and gstreamer1.0-plugins-bad are not in Ubuntu "main"
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
	DH_GENCONTROL_ARGS += -Vgst:Suggests="gstreamer1.0-libav, gstreamer1.0-plugins-bad"
else
	DH_GENCONTROL_ARGS += -Vgst:Recommends="gstreamer1.0-libav, gstreamer1.0-plugins-bad"
endif

CXXFLAGS=$(CFLAGS)

# Disable commands and binary packages of the builds that we don't want
ifneq ($(ENABLE_SOUP2),YES)
	SOUP2_BUILD = @:
	EXTRA_DH_ARGUMENTS += \
		-Ngir1.2-javascriptcoregtk-4.0 \
		-Ngir1.2-webkit2-4.0 \
		-Nlibjavascriptcoregtk-4.0-18 \
		-Nlibjavascriptcoregtk-4.0-bin \
		-Nlibjavascriptcoregtk-4.0-dev \
		-Nlibwebkit2gtk-4.0-37 \
		-Nlibwebkit2gtk-4.0-dev \
		-Nlibwebkit2gtk-4.0-doc \
		-Nwebkit2gtk-driver
endif
ifneq ($(ENABLE_SOUP3),YES)
	SOUP3_BUILD = @:
endif

# The debhelper files for the soup2 and soup3 builds are identical
# (apart from the API version) so we generate the latter from the former.
WEBKIT_DH_FILES = \
	gir1.2-javascriptcoregtk-4.0.install \
	gir1.2-javascriptcoregtk-4.0.lintian-overrides \
	gir1.2-webkit2-4.0.install \
	gir1.2-webkit2-4.0.lintian-overrides \
	libjavascriptcoregtk-4.0-18.install \
	libjavascriptcoregtk-4.0-dev.install \
	libjavascriptcoregtk-4.0-dev.lintian-overrides \
	libwebkit2gtk-4.0-37.docs \
	libwebkit2gtk-4.0-37.install \
	libwebkit2gtk-4.0-37.lintian-overrides \
	libwebkit2gtk-4.0-37.symbols \
	libwebkit2gtk-4.0-dev.install \
	libwebkit2gtk-4.0-dev.lintian-overrides

# This converts debhelper file names and contents to the soup3 versions
WEBKIT_DH_RENAME = sed -e 's/4\.0-18/4.1-0/g' \
		       -e 's/4\.0-37/4.1-0/g' \
		       -e 's/4\.0\.so\.37/4.1.so.0/g' \
		       -e 's/4\.0/4.1/g'

%:
	dh $@ --with gir --buildsystem=cmake+ninja $(EXTRA_DH_ARGUMENTS)

override_dh_gencontrol:
	dh_gencontrol -- $(DH_GENCONTROL_ARGS)

override_dh_auto_configure:
	if [ "$(ENABLE_SOUP3)" = "YES" ]; then \
		echo debian/clean > debian/clean ; \
		for src in $(WEBKIT_DH_FILES); do \
			dst=`echo $$src | $(WEBKIT_DH_RENAME)` ; \
			$(WEBKIT_DH_RENAME) debian/$$src > debian/$$dst ; \
			echo debian/$$dst >> debian/clean ; \
		done ; \
	fi
	$(SOUP2_BUILD) dh_auto_configure -Bbuild-soup2 -- \
		$(EXTRA_CMAKE_ARGUMENTS) -DUSE_SOUP2=ON
	$(SOUP3_BUILD) dh_auto_configure -Bbuild-soup3 -- \
		$(EXTRA_CMAKE_ARGUMENTS) -DUSE_SOUP2=OFF \
		-DENABLE_WEBDRIVER=OFF -DENABLE_GTKDOC=OFF

override_dh_auto_build:
	$(SOUP2_BUILD) dh_auto_build -Bbuild-soup2 $(EXTRA_BUILD_ARGUMENTS)
	$(SOUP3_BUILD) dh_auto_build -Bbuild-soup3 $(EXTRA_BUILD_ARGUMENTS)

override_dh_auto_install:
	$(SOUP2_BUILD) dh_auto_install -Bbuild-soup2
	$(SOUP3_BUILD) dh_auto_install -Bbuild-soup3

override_dh_auto_clean:
	$(SOUP2_BUILD) dh_auto_clean -Bbuild-soup2
	$(SOUP3_BUILD) dh_auto_clean -Bbuild-soup3
	find Source Tools -type f -name "*.pyc" -exec rm {} \;
	find Source Tools -depth -type d -name __pycache__ -exec rmdir {} \;

# Create a dummy doc directory in case the "nodoc" build option is set
override_dh_install:
	mkdir -p debian/tmp/usr/share/gtk-doc/html/webkit2gtk-4.0
	touch debian/tmp/usr/share/gtk-doc/html/webkit2gtk-4.0/index.html
	dh_install
	jdupes -rl debian/libwebkit2gtk-4.0-doc/usr

override_dh_makeshlibs:
	$(SOUP2_BUILD) dh_makeshlibs -plibwebkit2gtk-4.0-37 -Xinjected-bundle
	$(SOUP2_BUILD) dh_makeshlibs -plibjavascriptcoregtk-4.0-18
	$(SOUP3_BUILD) dh_makeshlibs -plibwebkit2gtk-4.1-0 -Xinjected-bundle
	$(SOUP3_BUILD) dh_makeshlibs -plibjavascriptcoregtk-4.1-0

override_dh_missing:
	dh_missing --fail-missing

override_dh_builddeb:
	DEB_BUILD_OPTIONS="$(filter-out parallel=%,$(DEB_BUILD_OPTIONS))" \
		dh_builddeb

override_dh_auto_test:
