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

EAPI=8

inherit cmake

# ANGLE is a Chromium/GN project with no native CMake build. This ebuild drives
# the same WebKit GN->CMake conversion tooling the vcpkg port uses to build the
# Linux OpenGL backend plus EGL and GLESv2. The libraries are installed renamed
# (libEGL_angle / libGLESv2_angle) so they do not clash with mesa, and a
# `unofficial-angle` CMake package config is installed for consumers (e.g.
# www-client/ladybird) via find_package(unofficial-angle CONFIG).

MY_P="angle-${PV}"
DESCRIPTION="OpenGL ES 2.0/3.0 implementation that translates calls to native APIs"
HOMEPAGE="
	https://github.com/google/angle
	https://www.angleproject.org/
"

# ANGLE has no traditional release; it syncs with Chromium. Pin the exact commit
# (Chromium branch 7258) that www-client/ladybird's vcpkg baseline resolves to.
ANGLE_COMMIT="d9fc4a372074b1079c193c422fc4a180e79b6636"
WK_COMMIT="0742522b24152262b04913242cb0b3c48de92ba0"
VCPKG_COMMIT="8f363bdc11359ede4bc51e5eb32a2617538049c8"
CHROMIUM_ZLIB_COMMIT="4028ebf8710ee39d2286cb0f847f9b95c59f84d8"

# Distfile layout (see comments inline):
#   - ANGLE source at the pinned commit
#   - WebKit gni-to-cmake.py + include/CMakeLists.txt (GN->CMake tooling)
#   - vcpkg cmake-buildsystem tree + version-header templates + fix patch +
#     unofficial-angle consumer config
#   - chromium zlib (only the google/ compression wrapper is compiled in;
#     the real zlib comes from the system via find_package(ZLIB))
SRC_URI="
	https://github.com/google/angle/archive/${ANGLE_COMMIT}.tar.gz
		-> ${MY_P}-${ANGLE_COMMIT:0:12}.tar.gz
	https://github.com/WebKit/WebKit/raw/${WK_COMMIT}/Source/ThirdParty/ANGLE/gni-to-cmake.py
		-> angle-gni-to-cmake.py
	https://github.com/WebKit/WebKit/raw/${WK_COMMIT}/Source/ThirdParty/ANGLE/include/CMakeLists.txt
		-> angle-webkit-include-CMakeLists.txt
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/cmake-buildsystem/CMakeLists.txt
		-> angle-cmake-buildsystem-CMakeLists.txt
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/cmake-buildsystem/PlatformLinux.cmake
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/cmake-buildsystem/PlatformMac.cmake
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/cmake-buildsystem/PlatformWin.cmake
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/cmake-buildsystem/linux.cmake
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/cmake-buildsystem/cmake/DetectSSE2.cmake
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/cmake-buildsystem/cmake/WebKitCommon-minimal.cmake
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/cmake-buildsystem/cmake/WebKitCompilerFlags-minimal.cmake
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/cmake-buildsystem/cmake/WebKitMacros-minimal.cmake
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/angle_commit.h.in
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/ANGLEShaderProgramVersion.h.in
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/001-fix-builder-error.patch
	https://raw.githubusercontent.com/microsoft/vcpkg/${VCPKG_COMMIT}/ports/angle/unofficial-angle-config.cmake
	https://chromium.googlesource.com/chromium/src/third_party/zlib/+archive/${CHROMIUM_ZLIB_COMMIT}.tar.gz
		-> chromium-zlib-${CHROMIUM_ZLIB_COMMIT:0:12}.tar.gz
"
S="${WORKDIR}/angle-${ANGLE_COMMIT}"

LICENSE="BSD"
SLOT="0"
KEYWORDS="~amd64"
IUSE=""

RDEPEND="
	sys-libs/zlib
"
DEPEND="${RDEPEND}"
BDEPEND="
	dev-lang/python
	dev-python/ply
"

src_unpack() {
	# Only unpack the ANGLE source. The chromium zlib tarball has no top-level
	# directory, so auto-unpacking it would scatter its files across ${WORKDIR};
	# it is extracted manually in src_prepare instead.
	unpack "${MY_P}-${ANGLE_COMMIT:0:12}.tar.gz"
}

src_prepare() {
	# Lay the WebKit/vcpkg CMake buildsystem over the GN source tree FIRST, so
	# the cmake eclass finds a CMakeLists.txt in S during its own prepare.
	local f
	cp "${DISTDIR}/angle-cmake-buildsystem-CMakeLists.txt" "${S}/CMakeLists.txt" || die
	for f in PlatformLinux.cmake PlatformMac.cmake PlatformWin.cmake linux.cmake; do
		cp "${DISTDIR}/${f}" "${S}/${f}" || die
	done
	mkdir -p "${S}/cmake" || die
	for f in DetectSSE2.cmake WebKitCommon-minimal.cmake \
		WebKitCompilerFlags-minimal.cmake WebKitMacros-minimal.cmake; do
		cp "${DISTDIR}/${f}" "${S}/cmake/${f}" || die
	done
	cp "${DISTDIR}/angle-webkit-include-CMakeLists.txt" "${S}/include/CMakeLists.txt" || die
	cp "${DISTDIR}/unofficial-angle-config.cmake" "${S}/unofficial-angle-config.cmake" || die

	# The vcpkg buildsystem hardcodes the lib install dir and keeps the shared
	# "lib" prefix, which on amd64 puts the libs in /usr/lib and doubles the
	# prefix (OUTPUT_NAME libGLESv2_angle -> liblibGLESv2_angle.so). Install to
	# the correct multilib libdir and drop the shared prefix so the files are
	# libEGL_angle.so / libGLESv2_angle.so.
	# Also skip EGL/eglext_angle.h: mesa (libglvnd) already ships that identical
	# ANGLE-extension header, so installing it here would collide.
	sed -i \
		-e '/^project(angle CXX C)/a include(GNUInstallDirs)\nset(CMAKE_SHARED_LIBRARY_PREFIX "")' \
		-e 's|LIBRARY DESTINATION lib$|LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}|' \
		-e 's|ARCHIVE DESTINATION lib$|ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}|' \
		-e 's|PATTERN "EGL/eglext.h" EXCLUDE|PATTERN "EGL/eglext.h" EXCLUDE\n    PATTERN "EGL/eglext_angle.h" EXCLUDE|' \
		"${S}/CMakeLists.txt" || die

	cmake_src_prepare

	eapply "${DISTDIR}/001-fix-builder-error.patch"

	# Generate the version headers the build expects (normally produced by the
	# port from the pinned commit hash).
	local commit_hash=${ANGLE_COMMIT:0:12}
	local commit_hdr=angle_commit.h
	local program_hdr=ANGLEShaderProgramVersion.h
	for dest in "${S}" "${S}/src/common"; do
		cat > "${dest}/${commit_hdr}" <<-EOF || die
		#define ANGLE_COMMIT_HASH "${commit_hash}"
		#define ANGLE_COMMIT_HASH_SIZE ${#commit_hash}
		#define ANGLE_COMMIT_DATE "invalid-date"
		#define ANGLE_REVISION ${PV}
		EOF
		cat > "${dest}/${program_hdr}" <<-EOF || die
		#define ANGLE_PROGRAM_VERSION "${commit_hash}"
		#define ANGLE_PROGRAM_VERSION_HASH_SIZE ${#commit_hash}
		EOF
	done

	# Place chromium zlib's google/ compression wrapper where the build expects it.
	local czlib="${WORKDIR}/czlib"
	rm -rf "${czlib}" || die
	mkdir -p "${czlib}" || die
	tar xzf "${DISTDIR}/chromium-zlib-${CHROMIUM_ZLIB_COMMIT:0:12}.tar.gz" -C "${czlib}" || die
	mkdir -p "${S}/third_party/zlib" || die
	cp -r "${czlib}/google" "${S}/third_party/zlib/google" || die

	# Convert the GN/GNI build files to CMake (WebKit tooling), mirroring vcpkg.
	pushd "${S}" >/dev/null || die
	local gnicmake="${DISTDIR}/angle-gni-to-cmake.py"
	einfo "Generating Compiler.cmake"
	python3 "${gnicmake}" src/compiler.gni Compiler.cmake || die
	einfo "Generating GLESv2.cmake"
	python3 "${gnicmake}" src/libGLESv2.gni GLESv2.cmake || die
	einfo "Generating D3D.cmake"
	python3 "${gnicmake}" src/libANGLE/renderer/d3d/BUILD.gn D3D.cmake \
		--prepend src/libANGLE/renderer/d3d/ || die
	einfo "Generating GL.cmake"
	python3 "${gnicmake}" src/libANGLE/renderer/gl/BUILD.gn GL.cmake \
		--prepend src/libANGLE/renderer/gl/ || die
	einfo "Generating Metal.cmake"
	python3 "${gnicmake}" src/libANGLE/renderer/metal/BUILD.gn Metal.cmake \
		--prepend src/libANGLE/renderer/metal/ || die
	popd >/dev/null || die
}

src_configure() {
	local mycmakeargs=(
		-DBUILD_SHARED_LIBS=ON
		$(usex amd64 "-DANGLE_IS_64_BIT_CPU=1" "-DANGLE_IS_32_BIT_CPU=1")
		-DPORT=Linux
		-DANGLE_USE_D3D11_COMPOSITOR_NATIVE_WINDOW=OFF
		-DVCPKG_TARGET_IS_WINDOWS=OFF
		-DUSE_METAL=OFF
	)
	cmake_src_configure
}