#!/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

VERSION_CLEAN_UPSTREAM = $(call dpkg_late_eval,VERSION_CLEAN_UPSTREAM,echo '$(DEB_VERSION_UPSTREAM)' | sed -e 's/\(~\|+\).*//')

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
ifeq ($(DEB_HOST_ARCH),arm64)
	export NINJAFLAGS += -j2
endif
endif

gstab_architectures :=
fulldebug_architectures :=
disabled_jit_architectures := armel mips mipsel
disabled_pch_architectures := amd64 powerpc s390x

small_architectures := armhf mipsel
no_gold_architectures := arm64 armhf mipsel

config_args = -proprietary-codecs \
              -feature-webengine-system-libvpx \
              -system-ffmpeg \
              -system-opus \
              -system-webp \
              -system-webengine-icu

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/formatter_worker/acorn/acorn.js \
	      src/3rdparty/chromium/third_party/polymer/v3_0/components-chromium/polymer/polymer_bundled.min.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)/src/3rdparty/chromium/third_party/mocha/mocha.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_PYTHON2=python2

# 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

# Disable JIT on selected architectures
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), $(disabled_jit_architectures)))
	echo "QMAKE_CXXFLAGS += -DENABLE_JIT=0" >> .qmake.conf
endif

# Disable header precompliation as it creates invalid includes on certain
# architectures causing build failure. LP: 1395661
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), $(disabled_pch_architectures)))
	echo "CONFIG -= precompile_header" >> .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

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

	# Create js files, that are needed for building step for arch and indep builds
	cd $(CURDIR)/src/3rdparty/chromium/third_party/jstemplate/; \
		closure-compiler --compilation_level ADVANCED_OPTIMIZATIONS \
		--js util.js \
		--js jsevalcontext.js \
		--js jstemplate.js \
		--js exports.js \
		--js_output_file 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"
	>src/3rdparty/chromium/third_party/mocha/mocha.js tar xaf debian/missing-sources/mocha*.tar.gz --wildcards --to-stdout '*/mocha.js'


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

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

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
