# Template file for 'libwebkit2gtk41'
pkgname=libwebkit2gtk41
version=2.50.4
revision=1
build_style=cmake
build_helper="gir"
configure_args="-DPORT=GTK
 -DCMAKE_LINKER=${XBPS_CROSS_TRIPLET}-gcc -DCMAKE_SKIP_RPATH=ON
 -DENABLE_JOURNALD_LOG=OFF -DUSE_WOFF2=ON
 -DENABLE_WAYLAND_TARGET=ON
 $(vopt_bool minibrowser ENABLE_MINIBROWSER)
 $(vopt_bool jit ENABLE_JIT)
 $(vopt_bool gir ENABLE_INTROSPECTION)
 $(vopt_bool gtk_doc ENABLE_DOCUMENTATION)
 $(vopt_bool x11 ENABLE_X11_TARGET)
 $(vopt_bool sampling_profiler ENABLE_SAMPLING_PROFILER)
 $(vopt_bool bubblewrap ENABLE_BUBBLEWRAP_SANDBOX)
 -DUSE_JPEGXL=OFF -DUSE_LIBBACKTRACE=OFF -DUSE_GTK4=OFF"
# Don't remove which from hostmakedepends
# Otherwise, they invoke /usr/bin/ccache /usr/lib/ccache/bin/$CC
hostmakedepends="perl python3 pkg-config gperf flex ruby gettext glib-devel
 geoclue2 libharfbuzz which libpsl unifdef wayland-devel libxml2-devel
 $(vopt_if gtk_doc 'gi-docgen')"
makedepends="at-spi2-core-devel libjpeg-turbo-devel libpng-devel
 harfbuzz-devel gst-plugins-base1-devel gst-plugins-bad1-devel sqlite-devel
 libsoup3-devel libxslt-devel gnutls-devel icu-devel enchant2-devel
 dbus-glib-devel libwebp-devel gtk+3-devel gtk4-devel libgudev-devel
 libsecret-devel ruby-devel geoclue2-devel libnotify-devel hyphen-devel
 woff2-devel freetype-devel libavif-devel libmanette-devel sysprof-devel
 flite-devel
 libgcrypt-devel libnuspell-devel libpsl-devel $(vopt_if x11 libXt-devel)
 MesaLib-devel libxkbcommon-devel wayland-devel wayland-protocols"
depends="gst-plugins-good1"
short_desc="GTK+3 port of the WebKit browser engine (soup3)"
maintainer="Orphaned <orphan@voidlinux.org>"
license="LGPL-2.1-or-later, BSD-2-Clause"
homepage="https://webkitgtk.org/"
distfiles="https://webkitgtk.org/releases/webkitgtk-${version}.tar.xz"
checksum=d3bfa473845acfab72635bada5e0d134fda6792c5b95c5c5cd141b46125bd8e4
make_check=no # TODO

build_options="gir x11 bubblewrap jit sampling_profiler minibrowser
 clang lto gtk_doc"
build_options_default="gir x11 bubblewrap minibrowser"

desc_option_bubblewrap="Enable bubblewrap sandbox"
desc_option_jit="JavaScript JIT (Only some architectures)"
desc_option_sampling_profiler="Sampling profiler support (JIT + glibc only)"
desc_option_minibrowser="Build the minibrowser"
desc_option_clang="Use Clang to build"
desc_option_lto="Enable ThinLTO (needs Clang)"

export CFLAGS="-DNDEBUG"
export CXXFLAGS="$CFLAGS"

# WebKitCCache.cmake set this variable
# include_file_mtime was not enabled to avoid a race,
# but this is run under our control
export CCACHE_SLOPPINESS=time_macros,include_file_mtime

if [ -z "$CROSS_BUILD" ]; then
	build_options_default+=" gtk_doc"
fi

# detection + runtime
if [ "$build_option_bubblewrap" ]; then
	hostmakedepends+=" bubblewrap xdg-dbus-proxy"
	makedepends+=" libseccomp-devel"
	depends+=" bubblewrap xdg-dbus-proxy"
	configure_args+=" -DBWRAP_EXECUTABLE=/usr/bin/bwrap
	 -DDBUS_PROXY_EXECUTABLE=/usr/bin/xdg-dbus-proxy"
fi

if [ "$build_option_lto" -a -z "$build_option_clang" ]; then
	broken="LTO needs Clang"
fi

if [ "$build_option_clang" ]; then
	hostmakedepends+=" clang lld"
	configure_args+=" -DUSE_LD_LLD=ON"
	nocross=yes
fi

if [ "$build_option_lto" ]; then
	configure_args+=" -DLTO_MODE=thin"
fi

if [ "$build_option_sampling_profiler" ]; then
	if [ $XBPS_TARGET_LIBC != glibc ]; then
		broken="sampling_profiler is only supported on glibc"
	elif [ -z "$build_option_jit" ]; then
		broken="sampling_profiler requires JIT"
	fi
fi

if [ "$build_option_jit" ]; then
	configure_args+=" -DENABLE_C_LOOP=OFF"
else
	# ENABLE_WEBASSEMBLY conflicts with ENABLE_C_LOOP
	configure_args+=" -DENABLE_WEBASSEMBLY=OFF -DENABLE_C_LOOP=ON"
fi

# only a few platform support JIT
case "$XBPS_TARGET_MACHINE" in
	aarch64|x86_64|riscv64)
		build_options_default+=" jit sampling_profiler"
		;;
	aarch64*|x86_64*|riscv64*)
		build_options_default+=" jit"
		;;
	i686*)
		# JIT requires SSE2 so disabled by default
		# it's also currently broken
		if [ "$build_option_jit" ]; then
			broken="OffsetExtractor: No magic values found."
		fi
		CXXFLAGS+=" --param ggc-min-expand=10"
		;;
	*)
		if [ "$build_option_jit" ]; then
			broken="JIT is not supported on ${XBPS_TARGET_MACHINE}"
		fi
		;;
esac

case "$XBPS_TARGET_MACHINE" in
	armv6*)
		# Disable ANGLE on armv6 for now.
		# Requires support for lock-free atomics (armv6k+).
		configure_args+=" -DENABLE_WEBGL=OFF"
		;;
esac

_depends="${depends}"

pre_configure() {
	if [ "$build_option_clang" ]; then
		export CC=clang
		export CXX=clang++
	fi

	# the debug builds are huge and cause problems when debugging
	export CFLAGS="${CFLAGS/-g/-g1}"
	export CXXFLAGS="${CXXFLAGS/-g/-g1}"

	if [ "$XBPS_WORDSIZE" = "32" ]; then
		export LDFLAGS+=" -Wl,--no-keep-memory"
	fi

	# the above is not enough for 32-bit ppc and debug
	case "$XBPS_TARGET_MACHINE" in
		ppc64*) ;;
		ppc*)
			export CFLAGS="${CFLAGS/-O2/-Os}"
			export CXXFLAGS="${CXXFLAGS/-O2/-Os}"
			;;
	esac
}

post_configure() {
	mkdir -p build-gtk4
	configure_args="${configure_args} -DUSE_GTK4=ON -DENABLE_WEBDRIVER=OFF" \
	cmake_builddir=build-gtk4 do_configure
}

do_build() {
	export NINJA_STATUS="[1/2][%f/%t] "
	ninja -C build ${makejobs}
	export NINJA_STATUS="[2/2][%f/%t] "
	ninja -C build-gtk4 ${makejobs}
}

post_install() {
	DESTDIR=${DESTDIR} cmake -P build-gtk4/cmake_install.cmake

	vlicense Source/WebCore/LICENSE-APPLE
	vlicense Source/WebCore/LICENSE-LGPL-2.1
	vlicense Source/WebCore/LICENSE-LGPL-2
}

libwebkit2gtk41-devel_package() {
	depends="gtk+3-devel libsoup3-devel libwebkit2gtk41>=${version}_${revision}"
	short_desc="GTK+3 port of the WebKit browser engine (soup3) - development files"
	pkg_install() {
		vmove usr/include/webkitgtk-4.1
		vmove "usr/lib/pkgconfig/*-4.1.pc"
		if [ "$build_option_gir" ]; then
			vmove "usr/share/gir-1.0/*-4.1.gir"
		fi
		if [ "$build_option_gtk_doc" ]; then
			vmove "usr/share/doc/*-4.1"
		fi
		vmove "usr/lib/*-4.1.so"
	}
}

libwebkitgtk60_package() {
	depends="${_depends}"
	short_desc="GTK4 port of the WebKit browser engine"
	pkg_install() {
		vmove usr/libexec/webkitgtk-6.0
		vmove usr/lib/webkitgtk-6.0
		if [ "$build_option_gir" ]; then
			vmove "usr/lib/girepository-1.0/*-6.0.typelib"
		fi
		vmove "usr/lib/*-6.0.so.*"
		for file in $(find ${DESTDIR}/usr/share/locale -name "*-6.0.mo"); do
			vmove ${file/$DESTDIR/}
		done
		vlicense Source/WebCore/LICENSE-APPLE
		vlicense Source/WebCore/LICENSE-LGPL-2.1
		vlicense Source/WebCore/LICENSE-LGPL-2
	}
}

libwebkitgtk60-devel_package() {
	depends="gtk4-devel libsoup3-devel libwebkitgtk60>=${version}_${revision}"
	short_desc="GTK4 port of the WebKit browser engine - development files"
	pkg_install() {
		vmove usr/include/webkitgtk-6.0
		vmove "usr/lib/pkgconfig/*-6.0.pc"
		if [ "$build_option_gir" ]; then
			vmove "usr/share/gir-1.0/*-6.0.gir"
		fi
		if [ "$build_option_gtk_doc" ]; then
			vmove "usr/share/doc/*-6.0"
		fi
		vmove "usr/lib/*-6.0.so"
	}
}
