#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export NINJA_PATH=/usr/bin/ninja
export NINJAFLAGS=-v
include /usr/share/dpkg/default.mk

DEB_HOST_ARCH_BITS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_BITS)

# TODO: properly integrate with the other debug setting
ifeq (32,$(DEB_HOST_ARCH_BITS))
       export DEB_CFLAGS_MAINT_APPEND = -g1
       export DEB_CXXFLAGS_MAINT_APPEND = -g1
endif

export CFLAGS := $(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)
export QT_SELECT := qt5

PARALLEL_OPTION = $(filter parallel=%,$(DEB_BUILD_OPTIONS))
ifneq (,$(PARALLEL_OPTION))
	NUMJOBS = $(patsubst parallel=%,%,$(PARALLEL_OPTION))
	export NINJAJOBS = -j$(NUMJOBS)
endif

ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64))
	export NINJAJOBS = -j2
endif
endif

gstab_architectures :=
fulldebug_architectures :=

small_architectures := armhf mipsel
no_gold_architectures := arm64 armhf mipsel

config_args = -proprietary-codecs \
              -feature-webengine-system-libvpx \
              -feature-webengine-system-openjpeg2 \
              -webengine-jumbo-build 0 \
              -system-opus \
              -system-webp \
              -system-webengine-icu \
              -webengine-kerberos

touch_files = src/3rdparty/chromium/third_party/devtools-frontend/src/front_end/third_party/lighthouse/lighthouse-dt-bundle.js \
	      src/3rdparty/chromium/third_party/devtools-frontend/src/front_end/third_party/lighthouse/report-assets/report-generator.js \
	      src/3rdparty/chromium/third_party/devtools-frontend/src/front_end/diff/diff_match_patch.js \
	      src/3rdparty/chromium/third_party/devtools-frontend/src/front_end/third_party/acorn/package/dist/acorn.js \
	      src/3rdparty/chromium/third_party/web-animations-js/sources/web-animations-next-lite.min.js

%:
	dh $@ --with pkgkde_symbolshelper

override_dh_auto_clean:
	dh_auto_clean
	rm -f .qmake.cache
	rm -f config.tests/.qmake.cache
	rm -f qtwebengine-config.h

	rm -rf $(CURDIR)/test_root
	rm -rf $(CURDIR)/docs
	rm -rf $(CURDIR)/.local
	rm -f $(CURDIR)/src/3rdparty/chromium/third_party/jstemplate/jstemplate_compiled.js
	rm -f $(CURDIR)/examples/webenginewidgets/contentmanipulation/jquery*.js

	for fname in $(touch_files); do \
		rm -f $(CURDIR)/$${fname}; \
	done

override_dh_auto_configure:
	# Run qmake once to create .qmake.conf and be sure to append the following values.
	qmake QT_BUILD_PARTS+=tests QMAKE_EXTRA_ARGS+="$(config_args)" QMAKE_PYTHON3=python3

# Enable gstabs debugging symbols only on gstab_architectures.
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), $(gstab_architectures)))
	echo "QMAKE_CXXFLAGS -= -g" >> .qmake.conf
	echo "QMAKE_CXXFLAGS += -gstabs" >> .qmake.conf
# Enable normal debugging symbols only on fulldebug_architectures.
else ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), $(fulldebug_architectures)))
	echo "QMAKE_CXXFLAGS_DEBUG += -fdebug-types-section" >> .qmake.conf
	echo "QMAKE_CXXFLAGS += -fdebug-types-section" >> .qmake.conf
	echo "QMAKE_CXXFLAGS_RELEASE_WITH_DEBUGINFO += -fdebug-types-section" >> .qmake.conf
	echo "QMAKE_CXXFLAGS_RELEASE += -fdebug-types-section" >> .qmake.conf
	echo "QMAKE_LFLAGS += -fdebug-types-section" >> .qmake.conf
else
# Disable debugging symbols in all the other archs.
	echo "QMAKE_CFLAGS -= -g" >> .qmake.conf
	echo "QMAKE_CFLAGS -= -gstabs" >> .qmake.conf
	echo "QMAKE_CXXFLAGS -= -g" >> .qmake.conf
	echo "QMAKE_CXXFLAGS -= -gstabs" >> .qmake.conf
	echo "QT_CONFIG -= force_debug_info" >> .qmake.conf
	echo "QT_CONFIG -= separate_debug_info" >> .qmake.conf
endif

ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), $(small_architectures)))
	echo "QMAKE_CXXFLAGS += --param ggc-min-expand=20" >> .qmake.conf
	echo "QMAKE_CFLAGS += --param ggc-min-expand=20" >> .qmake.conf
	echo "QMAKE_LFLAGS -= Wl,--gc-sections" >> .qmake.conf
endif

ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), $(no_gold_architectures)))
	# Do not use ld.gold
	echo "QMAKE_LFLAGS -= -fuse-ld=gold" >> .qmake.conf
	echo "QMAKE_LFLAGS += -fuse-ld=bfd" >> .qmake.conf
endif

	# Make ld not cache the symbol tables of input files in memory to avoid memory exhaustion during the linking phase.
	echo "QMAKE_LFLAGS += -Wl,--no-keep-memory" >> .qmake.conf

	# Missing hardening flag detected by blhc
	echo "QMAKE_LFLAGS += -Wl,-z,now" >> .qmake.conf

ifeq ($(DEB_HOST_ARCH),i386)
	# Make the following linker warning on i386 not fatal:
	# /usr/bin/ld: .../src/core/release/obj/third_party/ffmpeg/ffmpeg_nasm/dct32.o: warning: relocation in read-only section `.text'
	# /usr/bin/ld: warning: creating DT_TEXTREL in a shared object
	# Upstream does the same for lld linker, see third_party/ffmpeg/BUILD.gn.
	echo "QMAKE_LFLAGS += -Wl,-z,notext" >> .qmake.conf
endif

	# Run qmake again now with the proper values.
	qmake QT_BUILD_PARTS+=tests QMAKE_EXTRA_ARGS+="$(config_args)" QMAKE_PYTHON3=python3

	# Create js files, that are needed for building step for arch and indep builds
	cd $(CURDIR)/src/3rdparty/chromium/third_party/jstemplate/; \
		cat util.js jsevalcontext.js jstemplate.js exports.js > jstemplate_compiled.js

	>examples/webenginewidgets/contentmanipulation/jquery.js cat debian/missing-sources/jquery-*.js
	yui-compressor --type js "examples/webenginewidgets/contentmanipulation/jquery.js" -o "examples/webenginewidgets/contentmanipulation/jquery.min.js"

	set -ex; for fname in $(touch_files); do \
		mkdir -p $(CURDIR)/$$(dirname $${fname}); \
		touch $(CURDIR)/$${fname}; \
	done

	/usr/lib/qt5/bin/syncqt.pl Source -version ${DEB_VERSION_UPSTREAM:+dfsg=}

override_dh_auto_build-arch:
	dh_auto_build -- -Onone

#we also need the resources.pak files, so we need the normal build.
override_dh_auto_build-indep: override_dh_auto_build-arch
	dh_auto_build -- docs

override_dh_auto_install-arch:
	dh_auto_install

	# Remove rpath from the offending binaries
	chrpath -d $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/libexec/QtWebEngineProcess

	# Fix wrong path in pkgconfig files
	find $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig -type f -name '*.pc' \
	-exec sed -i -e 's/$(DEB_HOST_MULTIARCH)\/$(DEB_HOST_MULTIARCH)/$(DEB_HOST_MULTIARCH)/g' {} \;

	# Remove libtool-like files
	rm -fv debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.la

	# Do not require matching versions of Qt dependencies
	set -eu && \
	VERSION=${DEB_VERSION_UPSTREAM:+dfsg=} && \
	QT_VERSION=$$(qmake -query QT_VERSION) && \
	sed -i "s/$$VERSION /$$QT_VERSION /" debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/*/*Config.cmake

override_dh_auto_install-indep: override_dh_auto_install-arch
	dh_auto_build -- INSTALL_ROOT=$(CURDIR)/debian/tmp install_docs

override_dh_link:
	dh_link --package=qtwebengine5-examples \
		usr/share/javascript/jquery/jquery.min.js usr/lib/$(DEB_HOST_MULTIARCH)/qt5/examples/webenginewidgets/contentmanipulation/jquery.min.js \
		usr/share/javascript/marked/marked.min.js usr/lib/$(DEB_HOST_MULTIARCH)/qt5/examples/webenginewidgets/markdowneditor/resources/marked.js
	dh_link --package=qtwebengine5-dev-tools \
		usr/lib/qt5/bin/qwebengine_convert_dict usr/lib/$(DEB_HOST_MULTIARCH)/qt5/bin/qwebengine_convert_dict

	dh_link --remaining-packages

override_dh_auto_test-arch:
	$(MAKE) install -Csrc/core INSTALL_ROOT=$(CURDIR)/test_root
	-QTWEBENGINEPROCESS_PATH=$(CURDIR)/libexec/QtWebEngineProcess xvfb-run -a \
		-s "-screen 0 1024x768x24 +extension RANDR +extension RENDER +extension GLX" \
		dh_auto_test --no-parallel -- -k \
		QML2_IMPORT_PATH=$(CURDIR)/test_root/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/qml \
		LD_LIBRARY_PATH=$(CURDIR)/lib

override_dh_auto_test-indep:
	# Do not attempt to run anything to make build-indep work

export define GET_FILES_EXCLUDED
from debian import copyright
with open('$(CURDIR)/debian/copyright') as copyright_file:
    c = copyright.Copyright(copyright_file)
for pattern in c.header.files_excluded:
    print(pattern)
endef

get-orig-source:
	@set -eux && \
	TEMPDIR=$$(mktemp -d) && \
	VERSION=${DEB_VERSION_UPSTREAM:+dfsg=} && \
	SUBMODULE_COMMIT=e48df7803c7c98b0b2471c94057d32e44a301ad5 && \
	echo "$${GET_FILES_EXCLUDED}" >$${TEMPDIR}/get_files_excluded.py && \
	cd $${TEMPDIR} && \
	wget https://github.com/qt/qtwebengine/archive/v$${VERSION}-lts.tar.gz && \
	tar xzf v$${VERSION}-lts.tar.gz && \
	cd qtwebengine-$${VERSION}-lts/src/3rdparty && \
	wget https://github.com/qt/qtwebengine-chromium/archive/$${SUBMODULE_COMMIT}.tar.gz && \
	tar xz --strip-components=1 -f $${SUBMODULE_COMMIT}.tar.gz && \
	rm $${SUBMODULE_COMMIT}.tar.gz && \
	cd $${TEMPDIR}/qtwebengine-$${VERSION}-lts && \
	python3 $${TEMPDIR}/get_files_excluded.py | while read pattern; do rm -rf $$pattern; done && \
	cd src/3rdparty/chromium/third_party/devtools-frontend/src/node_modules && \
	ls -A | grep -Ev '^(@types|rollup|typescript)$$' | xargs rm -rf && \
	cd $${TEMPDIR} && \
	tar cJ --owner=root --group=root --sort=name -f $(CURDIR)/../qtwebengine-opensource-src_$${VERSION}+dfsg.orig.tar.xz qtwebengine-$${VERSION}-lts && \
	rm -rf $${TEMPDIR}