# Template file for 'qt5-webengine'
pkgname=qt5-webengine
version=5.15.16
revision=2
_version="${version}-lts"
_chromium_commit=207c2ac45ca3386d153770c6b0d2ea2ec21ca880
archs="x86_64* i686* armv[67]* ppc64* aarch64*"
create_wrksrc=yes
build_style=qmake
configure_args="--
 -webengine-icu -system-ffmpeg -webengine-opus -webengine-webp
 -webengine-pepper-plugins -webengine-printing-and-pdf -webengine-proprietary-codecs
 -webengine-pulseaudio -webengine-spellchecker -webengine-webrtc -webengine-geolocation
 -webengine-kerberos -no-webengine-embedded-build $(vopt_if sndio '' '-no')-webengine-sndio
 $(vopt_if pipewire '' '-no')-webengine-webrtc-pipewire"
# Rely on auto detection (fails if forced for cross builds) -webengine-alsa
hostmakedepends="qt5-qmake gperf ninja qt5-host-tools flex pkg-config nodejs
 which perl python3.11 protobuf nss-devel libevent-devel zlib-devel icu-devel
 libjpeg-turbo-devel libpng-devel libwebp-devel freetype-devel
 harfbuzz-devel"
makedepends="qt5-webchannel-devel qt5-location-devel qt5-tools-devel qt5-devel
 qt5-declarative-devel libevent-devel snappy-devel icu-devel ffmpeg6-devel
 libwebp-devel opus-devel cups-devel nss-devel minizip-devel libxslt-devel
 libvpx-devel libXtst-devel libXcursor-devel libXcomposite-devel
 jsoncpp-devel harfbuzz-devel lcms2-devel protobuf-devel pulseaudio-devel
 libXrandr-devel MesaLib-devel mit-krb5-devel alsa-lib-devel libxkbcommon-devel
 libxkbfile-devel libXdamage-devel
 $(vopt_if sndio sndio-devel) $(vopt_if pipewire pipewire-devel)"
short_desc="Cross-platform application and UI framework (QT5) - WebEngine component"
maintainer="John <me@johnnynator.dev>"
license="GPL-3.0-or-later, LGPL-3.0-or-later"
homepage="https://qt.io/"
distfiles="https://github.com/qt/qtwebengine/archive/v${_version}.tar.gz
 https://github.com/qt/qtwebengine-chromium/archive/${_chromium_commit}.tar.gz"
checksum="d1c0f2993476a9f7b7c09f0fb774338c1a5ddc4d602440a6443cc8b9f337e13a
 725f5b35f5361ce05f225e703ae02df808a29da408af16e599b8039f4faf794f"

no_generic_pkgconfig_link=yes
build_options="sndio pipewire"
build_options_default="pipewire"
desc_option_pipewire="Enable support for screen sharing for WebRTC via PipeWire"

if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	makedepends+=" musl-legacy-compat"
fi

if [ "$XBPS_LIBC" = "musl" ]; then
	hostmakedepends+=" musl-legacy-compat"
fi

if [ "$XBPS_WORDSIZE" -eq 32 ]; then
	nodebug=yes  # prevent OOM
fi

if [ ! "$XBPS_WORDSIZE" = "$XBPS_TARGET_WORDSIZE" ]; then
	broken="webengine can be built only if word size matches"
fi

if [ "$XBPS_TARGET_ENDIAN" != "le" ]; then
	broken="webengine only supports little endian"
fi

if [ "$XBPS_TARGET_NO_ATOMIC8" ]; then
	LDFLAGS+=" -latomic"
fi

case "$XBPS_TARGET_MACHINE" in
	ppc64*-musl) makedepends+=" libucontext-devel" ;;
esac

_bootstrap_gn() {
	local opts
	# Bootstrap gn (generate ninja)
	echo "Bootstrapping 'gn'"
	cd ${wrksrc}/src/3rdparty/gn
	CFLAGS="$CFLAGS_host" CXXFLAGS="$CXXFLAGS_host" LDFLAGS="$LDFLAGS_host" \
	PKGCONFIG=/usr/bin/pkgconfig PKG_CONFIG_PATH="/usr/lib/pkgconfig:/usr/share/pkgconfig" \
		python3.11 build/gen.py --no-last-commit-position --out-path \
			${wrksrc}/src/3rdparty/gn/out/Release --cc "${CC_host:-$CC}" \
			--cxx "${CXX_host:-$CXX}" --ld "${CXX_host:-$CXX}" --ar "${AR_host:-$AR}" $opts
	ninja -C out/Release gn
	cd ${wrksrc}
}

_cleanup_wrksrc_leak() {
	if [ -d "${PKGDESTDIR}/usr/lib/cmake" ]; then
		# Replace references to ${wrksrc} in cmake files
		vsed -i ${PKGDESTDIR}/usr/lib/cmake/*/*.cmake \
			-e "s;${wrksrc}/host;/usr/lib/qt5;g" \
			-e "s;devices/void-${XBPS_CROSS_TRIPLET}-g++;linux-g++;g"
	fi
	if [ -d "${PKGDESTDIR}/usr/lib/pkgconfig" ]; then
		# Replace references to ${wrksrc} in pkgconfig files
		vsed -i ${PKGDESTDIR}/usr/lib/pkgconfig/*.pc \
			-e "s;${wrksrc}/host;/usr/lib/qt5;g" \
			-e "s;devices/void-${XBPS_CROSS_TRIPLET}-g++;linux-g++;g"
	fi
	# Remove QMAKE_PRL_BUILD_DIR from hint files for static libraries
	# and replace references to ${wrksrc}
	find ${PKGDESTDIR} -iname "*.prl" -exec sed -i "{}" \
		-e "/^QMAKE_PRL_BUILD_DIR/d" \
		-e "s;-L${wrksrc}/qtbase/lib;-L/usr/lib;g" \;
	# Replace ${wrksrc} in project include files
	find ${PKGDESTDIR} -iname "*.pri" -exec sed -i "{}" \
		-e "s;${wrksrc}/qtbase;/usr/lib/qt5;g" \;
}

post_extract() {
	mv qtwebengine-${_version}/*  qtwebengine-${_version}/.??* .
	rm -r ${wrksrc}/src/3rdparty
	mv -v ${wrksrc}/qtwebengine-chromium-${_chromium_commit} ${wrksrc}/src/3rdparty
	# ¯\_(ツ)_/¯
	mkdir -p ${wrksrc}/.git ${wrksrc}/src/3rdparty/chromium/.git
}

post_patch() {
	mkdir -p ${wrksrc}/src/3rdparty/chromium/media/audio/{sndio,openbsd}
	cp ${FILESDIR}/sndio-files/sndio_*put.* \
		${wrksrc}/src/3rdparty/chromium/media/audio/sndio
	cp ${FILESDIR}/sndio-files/audio_manager_openbsd.* \
		${wrksrc}/src/3rdparty/chromium/media/audio/openbsd
	# Eternal 5.15.2, this can't ever be increased since other components are 5.15.2
	sed -e "/^MODULE_VERSION/s/5.*/5.15.2/" -i .qmake.conf
	# enable libucontext on ppc with musl for breakpad
	case "$XBPS_TARGET_MACHINE" in
		ppc64*-musl)
			vsed -i 's,#libs +=,libs +=,' \
				${wrksrc}/src/3rdparty/chromium/third_party/breakpad/BUILD.gn
			;;
	esac
}

pre_configure() {
	cp ${FILESDIR}/resolv_compat.h ${wrksrc}/src/3rdparty/chromium/net/dns
	case "$XBPS_TARGET_MACHINE" in
		armv5*|armv6*)  # Disable "yield" assembler instruction
			vsed -i src/3rdparty/chromium/base/allocator/partition_allocator/spin_lock.cc \
				src/3rdparty/chromium/third_party/pdfium/third_party/base/allocator/partition_allocator/spin_lock.cc \
				-e 's;"yield";"nop";'
			;;
	esac
	case "$XBPS_TARGET_MACHINE" in
		*-musl) export CXXFLAGS+=" -D_POSIX_THREAD_ATTR_STACKSIZE=2097152";;
	esac
	case "$XBPS_TARGET_MACHINE" in
		mips*)  # sgidefs.h is in /usr/include/asm
			for f in \
				qtwebengine/src/3rdparty/chromium/third_party/lss/linux_syscall_support.h \
				qtwebengine/src/3rdparty/chromium/third_party/breakpad/breakpad/src/common/android/include/asm-mips/fpregdef.h \
				qtwebengine/src/3rdparty/chromium/third_party/breakpad/breakpad/src/common/android/include/asm-mips/regdef.h \
				qtwebengine/src/3rdparty/chromium/third_party/breakpad/breakpad/src/common/android/include/asm-mips/asm.h \
				qtwebengine/src/3rdparty/chromium/third_party/tcmalloc/vendor/src/base/linux_syscall_support.h \
				qtwebengine/src/3rdparty/chromium/third_party/tcmalloc/chromium/src/base/linux_syscall_support.h \
				qtscript/src/3rdparty/javascriptcore/JavaScriptCore/wtf/Platform.h; do
					vsed -i $f -e "s;<sgidefs.h>;<asm/sgidefs.h>;"
			done
			# Assume that RGBA order is correct for big endian CPUs
			vsed -i qtwebengine/src/3rdparty/chromium/skia/config/SkUserConfig.h \
				-e '/#error Read the comment at this location/d'
			;;
	esac

	_bootstrap_gn
}

pre_build() {
	export NINJAJOBS=${makejobs}
	# Qt used to preped ulimit in front of $$QMAKE_LINK
	# but they do not do this anymore if ccache is in use
	ulimit -n 4096
}

qt5-webengine-devel_package() {
	depends="qt5-devel>=5.15.2_1 ${sourcepkg}>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/cmake
		vmove usr/lib/pkgconfig
		vmove usr/lib/qt5/mkspecs
		#TODO: FIX!!!
		if [ -z "$CROSS_BUILD" ]; then
			vmove usr/lib/qt5/plugins/designer
		fi
		vmove "usr/lib/*.so"
		vmove "usr/lib/*.prl"
		vmove "usr/lib/*.la"
		_cleanup_wrksrc_leak
	}
}
