#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

ifeq (riscv64, $(DEB_HOST_ARCH))
	# FIXME: qt6-webengine does not build on riscv64 with GCC as it
	# complains about a misused '%c' asm variable, so we chose to use
	# Clang as a workaround, however it does not support the
	# -ffat-lto-objects option as defined by optimize=+lto.
	#
	# Disable LTO for riscv64 as a secondary workaround.
	export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
else
	export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=+lto
endif

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

# Reduce memory consumption & parallelism on certain architectures
ifeq (armhf, $(DEB_HOST_ARCH))
	export LDFLAGS+=-Wl,--no-keep-memory
	export NINJAJOBS = -j2
endif

ifeq (i386, $(DEB_HOST_ARCH))
	export LDFLAGS+=-Wl,--no-keep-memory
	export NINJAJOBS = -j2
endif


%:
	dh $@ --with pkgkde_symbolshelper --buildsystem=cmake+ninja

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

ifeq (loong64, $(DEB_HOST_ARCH))
	TARGET_PATCH = patch_loong64
endif

ifeq (riscv64, $(DEB_HOST_ARCH))
	USE_CC = clang-17
	USE_CXX = clang++-17
else
	USE_CC = gcc
	USE_CXX = g++
endif

patch_loong64:
	patch -p1 < debian/patches/loong64.patch

override_dh_autoreconf: $(TARGET_PATCH)
	dh_autoreconf

override_dh_auto_clean:
	dh_auto_clean

	rm -f $(CURDIR)/src/3rdparty/chromium/third_party/jstemplate/jstemplate_compiled.js
	for fname in $(touch_files); do \
		rm -f $(CURDIR)/$${fname}; \
	done

# Turn on system FFmpeg libraries for 6.3
override_dh_auto_configure:
	# 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

	# jQuery
	cp /usr/share/javascript/jquery/jquery.min.js examples/webenginewidgets/contentmanipulation/
	cp /usr/share/javascript/jquery/jquery.min.js src/3rdparty/chromium/third_party/pycoverage/coverage/htmlfiles/
	cp /usr/share/javascript/jquery-tablesorter/jquery.tablesorter.min.js src/3rdparty/chromium/third_party/pycoverage/coverage/htmlfiles/

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

	dh_auto_configure -- \
		--log-level=STATUS \
		-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH) \
		-DFEATURE_webengine_system_icu=ON \
		-DFEATURE_webengine_system_libevent=ON \
		-DFEATURE_webengine_system_libopenjpeg2=ON \
		-DFEATURE_webengine_system_libpng=ON \
		-DFEATURE_webengine_system_libtiff=ON \
		-DFEATURE_qtpdf_build=ON \
		-DFEATURE_qtpdf_widgets_build=ON \
		-DFEATURE_qtpdf_quick_build=ON \
		-DFEATURE_webengine_proprietary_codecs=ON \
		-DFEATURE_webengine_kerberos=ON \
		-DCMAKE_C_COMPILER=/usr/bin/$(USE_CC) \
		-DCMAKE_CXX_COMPILER=/usr/bin/$(USE_CXX)

override_dh_auto_build:
	dh_auto_build -- WebEngineCore_sync_all_public_headers
	dh_auto_build
	dh_auto_build -- docs

execute_after_dh_auto_install:
	# Reproducible builds: remove build paths from .prl files
	sed -i -e '/^QMAKE_PRL_BUILD_DIR/d' debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libQt6*.prl
	# Install documentation.
	DESTDIR=$(CURDIR)/debian/tmp dh_auto_build -- install_docs
	# Remove build path from index files.
	find $(CURDIR)/debian/tmp/usr/share/qt6/doc -type f -name *.index -exec \
               sed -i 's@$(CURDIR)/@@g' {} \;

override_dh_dwz:
