#!/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 -> 1.0 API packages, soup3 build -> 1.1 API packages
USE_SOUP_VERSION=2

# Set to YES to use the docs from the tarball instead of generating them.
# Use only if gi-docgen is not available.
USE_PREBUILT_DOCS=NO

EXTRA_DH_ARGUMENTS =
EXTRA_CMAKE_ARGUMENTS = -DPORT=WPE \
	   -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_WPE_QT_API=OFF \
	   -DENABLE_MINIBROWSER=ON
DH_GENCONTROL_ARGS =

ifeq ($(USE_SOUP_VERSION),2)
	EXTRA_CMAKE_ARGUMENTS += -DUSE_SOUP2=ON
	WK_API_VERSION = 1.0
	LIBWPEWEBKIT_PKG = libwpewebkit-1.0-3
	SOUPDEV_PKG = libsoup2.4-dev
	USE_PREBUILT_DOCS=YES
else
	EXTRA_CMAKE_ARGUMENTS += -DUSE_SOUP2=OFF
	WK_API_VERSION = 1.1
	LIBWPEWEBKIT_PKG = libwpewebkit-1.1-0
	SOUPDEV_PKG = libsoup-3.0-dev
endif

# gcc 10 segfaults when building webkit in amd64 (#1008098) so use clang
# However in i386, mipsel and arm64 clang is the buggy one (#1010329, #1016811)
ifneq (,$(filter $(DEB_HOST_ARCH),amd64))
	EXTRA_CMAKE_ARGUMENTS += \
	   -DCMAKE_C_COMPILER=clang \
	   -DCMAKE_CXX_COMPILER=clang++
endif

# 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

# 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

# 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 architectures with only 2 GB address space
ifneq (,$(filter $(DEB_HOST_ARCH),mips mipsel sh4))
	CFLAGS := $(CFLAGS:-g1=-g0)
	CFLAGS := $(CFLAGS:-O2=-Os)
	CPPFLAGS += --param ggc-min-expand=10
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_UNIFIED_BUILDS=OFF
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_DOCUMENTATION=OFF -DENABLE_INTROSPECTION=OFF
	USE_PREBUILT_DOCS=NO
else ifneq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_DOCUMENTATION=OFF -DENABLE_INTROSPECTION=OFF
	USE_PREBUILT_DOCS=NO
else ifeq ($(USE_PREBUILT_DOCS),YES)
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_DOCUMENTATION=OFF -DENABLE_INTROSPECTION=OFF
else
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_DOCUMENTATION=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

# Disable the bubblewrap sandbox if libseccomp-dev is not available
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"
else
	EXTRA_CMAKE_ARGUMENTS += -DENABLE_BUBBLEWRAP_SANDBOX=OFF
endif

# Lower max parallel jobs on some of Ubuntu's builders
ifeq ($(shell dpkg-vendor --is Ubuntu && echo yes),yes)
ifneq (,$(filter $(DEB_HOST_ARCH),arm64))
	EXTRA_DH_ARGUMENTS += --max-parallel=2
endif
endif

CXXFLAGS=$(CFLAGS)

# 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 = \
	libwpewebkit-1.0-3.docs \
	libwpewebkit-1.0-3.install \
	libwpewebkit-1.0-3.lintian-overrides \
	libwpewebkit-1.0-3.symbols \
	libwpewebkit-1.0-dev.install

# This converts debhelper file names and contents to the soup3 versions
WEBKIT_DH_RENAME = sed -e 's/1\.0-3/1.1-0/g' \
		       -e 's/1\.0\.so\.3/1.1.so.0/g' \
		       -e 's/1\.0/1.1/g'

debian/control: debian/control.in debian/rules
	echo '# This file is autogenerated. DO NOT EDIT' > debian/control
	cat debian/control.in | \
		sed -e 's/@SOUPDEV_PKG@/$(SOUPDEV_PKG)/g' \
		    -e 's/@WK_API_VERSION@/$(WK_API_VERSION)/g' \
		    -e 's/@LIBWPEWEBKIT_PKG@/$(LIBWPEWEBKIT_PKG)/g' \
		    -e 's/@SOUP_VERSION@/$(USE_SOUP_VERSION)/g' \
		>> debian/control
ifeq ($(USE_PREBUILT_DOCS),YES)
	sed -i -e 's/^\(Build-Depends-Indep:\)/# \1/' debian/control
endif

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

override_dh_gencontrol:
	dh_gencontrol -- $(DH_GENCONTROL_ARGS)

override_dh_auto_configure:
	if [ "$(USE_SOUP_VERSION)" = "3" ]; 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
	dh_auto_configure -- $(EXTRA_CMAKE_ARGUMENTS)

override_dh_auto_clean: debian/control
	dh_auto_clean
	find Source Tools -type f -name "*.pyc" -exec rm {} \;
	find Source Tools -depth -type d -name __pycache__ -exec rmdir {} \;

# If USE_PREBUILT_DOCS=YES then install them. Otherwise create dummy
# doc directories in case the "nodoc" build option is set.
override_dh_install:
ifeq ($(USE_PREBUILT_DOCS),YES)
	mkdir -p debian/tmp/usr/share/gtk-doc/html
	cp -r Documentation/wpe-webkit-1.1 debian/tmp/usr/share/gtk-doc/html/
	cp -r Documentation/wpe-web-extension-1.1 debian/tmp/usr/share/gtk-doc/html/
	cp -r Documentation/wpe-javascriptcore-1.1 debian/tmp/usr/share/gtk-doc/html/
else
	mkdir -p debian/tmp/usr/share/gtk-doc/html/wpe-webkit-1.1
	touch debian/tmp/usr/share/gtk-doc/html/wpe-webkit-1.1/index.html
	mkdir -p debian/tmp/usr/share/gtk-doc/html/wpe-web-extension-1.1
	touch debian/tmp/usr/share/gtk-doc/html/wpe-web-extension-1.1/index.html
	mkdir -p debian/tmp/usr/share/gtk-doc/html/wpe-javascriptcore-1.1
	touch debian/tmp/usr/share/gtk-doc/html/wpe-javascriptcore-1.1/index.html
endif
	dh_install
	jdupes -rl debian/libwpewebkit-1.0-doc/usr

override_dh_shlibdeps:
	dh_shlibdeps -XMiniBrowser

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:
