# Copyright 2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

# Tag 2026.48.0 points at this commit; the GitHub tag tarball unpacks to
# ladybird-2026.48.0. The full commit is recorded in REVISION at build time
# for build stamps and the metainfo version.
LADYBIRD_COMMIT="87556a80543995e031dc402c2e4e554d5570d8a5"
# LLVM libc math headers are header-only and version-coupled to this commit of
# llvm-project main (matches Ladybird's vcpkg overlay-ports/llvm-libc pin).
# A headers-only tarball of this commit is hosted on the overlay's GitHub
# release to avoid shipping the ~150MB full llvm-project source.
LLVM_LIBC_PV="e54169423e7ee5d8b41c2f1b6f7a59000c901ee0"
WUFFS_PV="0.3.5"

RUST_MIN_VER="1.98.0"

inherit cmake cargo desktop xdg-utils


DESCRIPTION="Truly independent web browser"
HOMEPAGE="
	https://ladybird.org
	https://github.com/LadybirdBrowser/ladybird
"
# Extra distfiles:
#   - llvm-libc: header-only LLVM libc math headers used by AK and the Rust
#     crates (headers-only tarball of the pinned llvm-project commit)
#   - wuffs: image decoder headers (GIF, etc.)
#   - hsts-preload.json: Chromium's HSTS preload list, baked in at build
#     time
#   - ffmpeg-*.tar.xz: release headers for runtime hardware video decoding
#     against system FFmpeg builds of other major versions
# The Rust crates are vendored into a single tarball (cargo_home/gentoo layout,
# matching the cargo eclass vendor dir) hosted on the overlay's GitHub release,
# instead of listing all ~423 crates individually in SRC_URI.
SRC_URI="
	https://github.com/LadybirdBrowser/ladybird/archive/refs/tags/${PV}.tar.gz -> ${P}.tar.gz
	https://github.com/bennypowers/gentoo-overlay/releases/download/${PN}/${P}-crates.tar.xz
	https://github.com/bennypowers/gentoo-overlay/releases/download/ladybird-llvm-libc-${LLVM_LIBC_PV:0:7}/llvm-libc-${LLVM_LIBC_PV}-headers.tar.xz
	https://github.com/google/wuffs/archive/refs/tags/v${WUFFS_PV}.tar.gz -> wuffs-${WUFFS_PV}.tar.gz
	https://raw.githubusercontent.com/chromium/chromium/main/net/http/transport_security_state_static.json -> hsts-preload.json
	https://ffmpeg.org/releases/ffmpeg-6.1.6.tar.xz
	https://ffmpeg.org/releases/ffmpeg-7.1.5.tar.xz
	https://ffmpeg.org/releases/ffmpeg-8.1.3.tar.xz
	https://ffmpeg.org/releases/ffmpeg-9.0.2.tar.xz
"
S="${WORKDIR}/${P}"

LICENSE="BSD-2"
# Licenses of the vendored LLVM libc headers, WUFFS, and Rust crates. The
# crate set is dual/triple-licensed in places (e.g. "Unlicense OR MIT",
# "MIT OR Apache-2.0 OR LGPL-2.1-or-later"), so all available options are
# listed; adblock is single-licensed MPL-2.0.
# The FFmpeg header tarballs are dual-licensed LGPL-2.1+/GPL-2.0 (see
# media-video/ffmpeg); LGPL-2.1+ also covers r-efi's optional clause.
LICENSE+=" Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD ISC MIT MPL-2.0 Unlicense Unicode-3.0 ZLIB"
LICENSE+=" || ( GPL-2+ LGPL-2.1+ )"
SLOT="0"
KEYWORDS="~amd64"

PATCHES=(
	"${FILESDIR}/ladybird-2026.48.0-icu-version.patch"
	"${FILESDIR}/ladybird-2026.48.0-mimalloc-v3.patch"
	"${FILESDIR}/ladybird-2026.48.0-cmake-no-third-party-warnings.patch"
)

# Ladybird hard-requires ANGLE (EGL + OpenGL ES) on Linux. media-libs/angle
# provides the real ANGLE (libEGL_angle / libGLESv2_angle); it dlopen's the
# native GL at runtime, so media-libs/mesa is also required for the hardware
# path. src_prepare() also ships an angle.pc shim as a fallback for the
# pkg-config code path (see below).
RDEPEND="
	dev-cpp/cpptrace
	dev-cpp/fast_float
	dev-cpp/simdutf
	dev-db/sqlite
	dev-libs/icu
	dev-libs/libedit
	dev-libs/mimalloc
	dev-libs/openssl
	dev-libs/simdjson
	dev-libs/vma
	dev-libs/zmij
	dev-qt/qtbase:6[dbus,gui,icu,network,opengl,vulkan,wayland,widgets,X]
	dev-qt/qtpositioning:6
	dev-util/vulkan-headers
	media-libs/fontconfig
	media-libs/harfbuzz
	media-libs/libavif[dav1d]
	media-libs/libjpeg-turbo
	media-libs/libpng[apng]
	media-libs/angle
	media-libs/libsdl3
	media-libs/libwebp
	media-libs/mesa[opengl]
	media-libs/skia[vulkan]
	media-libs/vulkan-loader
	media-libs/woff2
	media-video/ffmpeg[dav1d]
	net-libs/libpsl
	net-misc/curl[brotli,http2,http3,openssl,websockets,zstd]
	sys-libs/zlib
"
# RUST_MIN_VER (above) makes the rust eclass add the toolchain to BDEPEND
# (>=1.98.0, matching Ladybird's rust-toolchain.toml pin, which src_prepare
# neutralises to use the system toolchain).
BDEPEND="
	dev-lang/python
	dev-vcs/git
	virtual/pkgconfig
	|| ( llvm-core/lld sys-devel/mold )
"

RESTRICT="test"

src_unpack() {
	cargo_gen_config
	unpack ${P}.tar.gz
	# The vendored crate tarball holds the cargo_home/gentoo layout, so it
	# unpacks straight into the cargo eclass vendor dir (${ECARGO_VENDOR}).
	tar xJf "${DISTDIR}/${P}-crates.tar.xz" -C "${WORKDIR}" || die
}

src_prepare() {
	cmake_src_prepare

	# The build invokes cargo for each Rust crate; use the system toolchain
	# instead of the rustup-pinned one, but keep the file because ninja rules
	# depend on it.
	echo "# system toolchain" > rust-toolchain.toml || die

	# Rust 1.100 renamed Atomic::fetch_update() to try_update() and deprecated
	# the old name. Ladybird builds its workspace crates with -Dwarnings, so
	# the deprecation is a hard error. The rename is mechanical (same
	# signature), so rewrite every call site rather than patch each file.
	find . -name '*.rs' -print0 | xargs -0 sed -i 's/\.fetch_update(/.try_update(/g' || die

	# Source tarballs have no git history; record the commit for build stamps
	# and the metainfo version.
	echo "${LADYBIRD_COMMIT}" > REVISION || die

	# LLVM libc math headers, expected at <prefix>/llvm-libc/shared/math.h.
	# The tarball holds the header tree under llvm-libc/ (mirroring Ladybird's
	# vcpkg overlay-ports/llvm-libc layout). Extract into llvm-libc-include/ so
	# that find_path( ... NAMES llvm-libc/shared/math.h ) resolves it via
	# CMAKE_INCLUDE_PATH.
	mkdir -p "${WORKDIR}/llvm-libc-include" || die
	tar xJf "${DISTDIR}/llvm-libc-${LLVM_LIBC_PV}-headers.tar.xz" \
		-C "${WORKDIR}/llvm-libc-include" || die
	[[ -f ${WORKDIR}/llvm-libc-include/llvm-libc/shared/math.h ]] ||
		die "llvm-libc headers missing"

	# WUFFS is a single-file C amalgamation; only wuffs-v0.3.c is needed, at
	# <prefix>/wuffs/wuffs-v0.3.c (LibGfx does #include <wuffs/wuffs-v0.3.c>).
	tar xaf "${DISTDIR}/wuffs-${WUFFS_PV}.tar.gz" \
		"wuffs-${WUFFS_PV}/release/c/wuffs-v0.3.c" || die
	mkdir -p "${WORKDIR}/include/wuffs" || die
	mv "wuffs-${WUFFS_PV}/release/c/wuffs-v0.3.c" \
		"${WORKDIR}/include/wuffs/" || die
	rm -rf "wuffs-${WUFFS_PV}" || die

	# CMake looks the FFmpeg libraries up under the -ladybird suffix used by
	# the upstream vcpkg build; redirect it to the system FFmpeg.
	local comp
	mkdir -p "${WORKDIR}/pc-shim"
	for comp in avcodec avutil swresample; do
		cat > "${WORKDIR}/pc-shim/lib${comp}-ladybird.pc" <<-EOF || die
prefix=/usr
libdir=/usr/$(get_libdir)
includedir=/usr/include

Name: lib${comp}-ladybird
Description: Shim redirecting to the system lib${comp}
Version: $(pkg-config --modversion lib${comp})
Requires: lib${comp}
EOF
	done

	# Ladybird resolves ANGLE via find_package(unofficial-angle CONFIG) when the
	# system ANGLE package (media-libs/angle) is present; that provides the CMake
	# config plus the renamed libs (libEGL_angle / libGLESv2_angle) and the
	# ANGLE-specific headers. As a fallback for the pkg-config code path, provide
	# an `angle` module pointing at those same system libs.
	if ! pkg-config --exists angle; then
		cat > "${WORKDIR}/pc-shim/angle.pc" <<-EOF || die
prefix=/usr
libdir=/usr/$(get_libdir)
includedir=/usr/include

Name: angle
Description: ANGLE EGL + GLESv2 (redirect to the system media-libs/angle)
Version: 7258
Cflags:
Libs: -lEGL_angle -lGLESv2_angle
EOF
	fi

	# CMake config shim for the header-only VulkanMemoryAllocator.
	mkdir -p "${WORKDIR}/vma-cmake"
	cp "${FILESDIR}/VulkanMemoryAllocatorConfig.cmake" "${WORKDIR}/vma-cmake/" || die

	# Build-time downloads are disabled; stage the files CMake expects in
	# LADYBIRD_CACHE_DIR.
	mkdir -p "${BUILD_DIR}/caches/HSTSPreload" "${BUILD_DIR}/caches/FFmpegHeaders" || die
	cp "${DISTDIR}/hsts-preload.json" \
		"${BUILD_DIR}/caches/HSTSPreload/transport_security_state_static.json" || die
	cp "${DISTDIR}"/ffmpeg-6.1.6.tar.xz "${DISTDIR}"/ffmpeg-7.1.5.tar.xz \
		"${DISTDIR}"/ffmpeg-8.1.3.tar.xz "${DISTDIR}"/ffmpeg-9.0.2.tar.xz \
		"${BUILD_DIR}/caches/FFmpegHeaders/" || die
}

src_configure() {
	cargo_gen_config
	export CARGO_HOME

	export PKG_CONFIG_PATH="${WORKDIR}/pc-shim:$(pkg-config --variable pc_path pkg-config)"

	# Ladybird compiles with -Werror. The system OpenSSL 4.x deprecates several
	# X509/ASN1 APIs it still calls (they remain functional), which -Werror would
	# turn into hard errors. -Wdeprecated-declarations is not part of -Wall, so
	# disabling it via CXXFLAGS (which precedes Ladybird's own -Wall/-Werror)
	# sticks. The WUFFS/suggest-override and calloc cases are handled by a CMake
	# patch instead, because -Wall re-enables those groups after CXXFLAGS.
	append-flags -Wno-deprecated-declarations

	local mycmakeargs=(
		-DCMAKE_BUILD_TYPE=$(usex debug Debug Release)
		-DENABLE_NETWORK_DOWNLOADS=OFF
		-DLADYBIRD_CACHE_DIR="${BUILD_DIR}/caches"
		-DLADYBIRD_ENABLE_UPDATE_CHECK=OFF
		-DLADYBIRD_BUILD_QT=OFF
		-DENABLE_CLANG_PLUGINS=OFF
		-DCMAKE_INCLUDE_PATH="${WORKDIR}/include;${WORKDIR}/llvm-libc-include"
		-DVulkanMemoryAllocator_DIR="${WORKDIR}/vma-cmake"
	)
	cmake_src_configure
}

src_compile() {
	# CMake invokes cargo per-crate during the build; each phase is a fresh
	# shell, so re-establish the offline vendored-registry environment here.
	cargo_gen_config
	export CARGO_HOME
	cmake_src_compile
}

src_install() {
	cmake_src_install
}

pkg_postinst() {
	xdg_desktop_database_update
	xdg_icon_cache_update
	xdg_mimeinfo_database_update

	ewarn "Ladybird is in alpha; expect rough edges and breakage."
}

pkg_postrm() {
	xdg_desktop_database_update
	xdg_icon_cache_update
	xdg_mimeinfo_database_update
}