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

EAPI=8

# Skia is pinned to the commit Ladybird's vcpkg overlay builds, so the
# build is reproducible against Ladybird's expectations.
SKIA_COMMIT="e7c90ecca9444fe09598f1630ab7cee2c0ee027a"
VULKAN_HEADERS_PV="1.4.363.0"
VMA_PV="3.5.0"

inherit toolchain-funcs

DESCRIPTION="Graphics engine for Chrome, Firefox, Ladybird, Android, Flutter"
HOMEPAGE="
	https://skia.org
	https://github.com/google/skia
"
SRC_URI="
	https://github.com/google/skia/archive/${SKIA_COMMIT}.tar.gz -> ${P}.tar.gz
	https://github.com/KhronosGroup/Vulkan-Headers/archive/refs/tags/vulkan-sdk-${VULKAN_HEADERS_PV}.tar.gz -> vulkan-headers-${VULKAN_HEADERS_PV}.tar.gz
	https://raw.githubusercontent.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/v${VMA_PV}/include/vk_mem_alloc.h -> vk_mem_alloc.h
"
S="${WORKDIR}/skia-${SKIA_COMMIT}"

LICENSE="BSD"
# Vendored Vulkan-Headers (Apache-2.0 WITH LLVM-exceptions) and
# VulkanMemoryAllocator (MIT).
LICENSE+=" Apache-2.0-with-LLVM-exceptions MIT"
SLOT="0"
KEYWORDS="~amd64"
IUSE="+vulkan"

RDEPEND="
	dev-libs/expat
	dev-libs/icu
	sys-libs/zlib
	media-libs/fontconfig
	media-libs/freetype
	media-libs/harfbuzz
	vulkan? (
		dev-util/vulkan-headers
		media-libs/vulkan-loader
	)
"
DEPEND="${RDEPEND}"
BDEPEND="
	dev-build/gn
	dev-lang/python
	dev-build/ninja
"

RESTRICT="test"

PATCHES=(
	# From the vcpkg skia port, applied there for all builds.
	"${FILESDIR}"/001-bentleyottmann-build.patch
	"${FILESDIR}"/002-graphite.patch
	"${FILESDIR}"/003-vulkan-headers.patch
	"${FILESDIR}"/004-pdfsubsetfont-uwp.patch
	"${FILESDIR}"/005-skparagraph-dllexport.patch
	"${FILESDIR}"/006-use-pkgconfig-to-find-gl.patch
	"${FILESDIR}"/007-dont-use-response-file.patch
	"${FILESDIR}"/008-fix-bsd.patch
	"${FILESDIR}"/009-allow-disabling-lib-dl.patch
	"${FILESDIR}"/010-always-build-pathops.patch
	"${FILESDIR}"/011-skstrendswith-dllexport.patch
	"${FILESDIR}"/012-dawn.patch
	"${FILESDIR}"/013-skcms-dllexport.patch
	"${FILESDIR}"/014-fix-direct3d.patch
)

src_prepare() {
	default

	# Use the system Vulkan headers instead of the vendored copy (see
	# 003-vulkan-headers.patch), and drop the GPU backends we do not build.
	rm -rf third_party/dawn || die

	mkdir -p third_party/externals || die
	tar xaf "${DISTDIR}/vulkan-headers-${VULKAN_HEADERS_PV}.tar.gz" \
		-C third_party/externals || die
	mv "third_party/externals/Vulkan-Headers-vulkan-sdk-${VULKAN_HEADERS_PV}" \
		third_party/externals/vulkan-headers || die

	# The Vulkan memory allocator is a single header.
	mkdir -p "${WORKDIR}/vma/include" || die
	cp "${DISTDIR}/vk_mem_alloc.h" "${WORKDIR}/vma/include/" || die
}

src_configure() {
	local mygnargs=(
		# target_os / target_cpu are auto-detected by gn; not settable via args
		is_component_build=true
		is_official_build=true
		is_debug=false
		cc="$(tc-getCC)"
		cxx="$(tc-getCXX)"
		skia_enable_android_utils=false
		skia_enable_gpu_debug_layers=false
		skia_enable_graphite=false
		skia_enable_pdf=false
		skia_enable_spirv_validation=false
		skia_enable_tools=false
		skia_use_dng_sdk=false
		skia_use_expat=true
		skia_use_fontconfig=true
		skia_use_freetype=true
		skia_use_gl=false
		skia_use_harfbuzz=true
		skia_use_icu=true
		skia_use_jpeg_gainmaps=false
		skia_use_libavif=false
		skia_use_libjpeg_turbo_decode=false
		skia_use_libjpeg_turbo_encode=false
		skia_use_libpng_decode=false
		skia_use_libpng_encode=false
		skia_use_libwebp_decode=false
		skia_use_libwebp_encode=false
		skia_use_lua=false
		skia_use_no_jpeg_encode=true
		skia_use_no_png_encode=true
		skia_use_no_webp_encode=true
		skia_use_system_zlib=true
		skia_use_wuffs=false
	)
	use vulkan && mygnargs+=(
		skia_use_vulkan=true
		skia_vulkan_memory_allocator_dir="${WORKDIR}/vma"
	)

	# In the gn arg parser only the literals true/false are safe unquoted;
	# any other bare value (gcc, g++, a path) is parsed as an identifier
	# reference. Quote the value (not the key) of every non-boolean arg.
	local arg key value args
	args=""
	for arg in "${mygnargs[@]}"; do
		key="${arg%%=*}"
		value="${arg#*=}"
		if [[ ${value} == true || ${value} == false ]]; then
			args+=" ${arg}"
		else
			args+=" ${key}=\"${value}\""
		fi
	done
	gn gen out --args="${args# }" || die "gn gen failed"
}

src_compile() {
	ninja -C out skia modules || die "ninja failed"
}

src_install() {
	local lib

	# Shared libraries: core library and the module libraries.
	for lib in skia skparagraph skshaper skunicode_core skunicode_icu bentleyottmann; do
		if [[ -f out/lib${lib}.so ]]; then
			dolib.so out/lib${lib}.so*
		fi
	done

	# Static module libraries, linked directly by consumers (see skia.pc).
	local skia_libs="-lskia"
	for lib in skparagraph skshaper skunicode_core skunicode_icu bentleyottmann; do
		[[ -f out/lib${lib}.so ]] && skia_libs+=" -l${lib}"
	done
	for lib in skcms pathkit pathops; do
		if [[ -f out/lib${lib}.a ]]; then
			dolib.a out/lib${lib}.a
			skia_libs+=" -l${lib}"
		fi
	done

	# Headers in the vcpkg layout: /usr/include/skia/{include,modules,src}
	# plus a legacy duplicate of include/ so that <core/SkCanvas.h> works
	# with -I/usr/include/skia.
	local hdr dest
	# dodir() takes paths relative to /, not absolute ED paths.
	dodir /usr/include/skia
	for hdr in $(find include modules src -type f \( -name '*.h' -o -name '*.inc' \)); do
		dest="usr/include/skia/${hdr}"
		dodir "/${dest%/*}"
		cp -p "${S}/${hdr}" "${ED}/${dest}" || die
	done
	for hdr in $(find include -type f \( -name '*.h' -o -name '*.inc' \)); do
		dest="usr/include/skia/${hdr#include/}"
		dodir "/${dest%/*}"
		cp -p "${S}/${hdr}" "${ED}/${dest}" || die
	done

	# pkg-config file; Ladybird requires Version >= 148 via skia.pc.
	einfo "Writing skia.pc"
	dodir /usr/$(get_libdir)/pkgconfig
	cat > "${ED}/usr/$(get_libdir)/pkgconfig/skia.pc" <<-EOF || die
prefix=/usr
libdir=/usr/$(get_libdir)
includedir=/usr/include/skia

Name: skia
Description: ${DESCRIPTION}
Version: ${PV}
Cflags: -I\${includedir}
Libs: -L\${libdir} ${skia_libs}
EOF

	einstalldocs
}