SUMMARY="Multi-platform GL implementation"
DESCRIPTION="Mesa is an open-source implementation of the OpenGL \
specification. The OpenGL specification documents a system for rendering \
interactive 3D graphics. Mesa fills the role of the Haiku OpenGL kit \
providing 3D rendering to Haiku applications."
HOMEPAGE="https://www.mesa3d.org/"
COPYRIGHT="1999-2022 Brian Paul"
LICENSE="MIT"
REVISION="1"
SOURCE_URI="https://mesa.freedesktop.org/archive/mesa-${portVersion}.tar.xz"
CHECKSUM_SHA256="96bb761fd546e9aa41d025fcc025225c5668443839dae21e3731959beb096736"
SOURCE_DIR="mesa-${portVersion}"
PATCHES="mesa-$portVersion.patchset"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

libVersion=1.0.0
libOSMesaVersion=8.0.0
libVersionCompat="$libVersion compat >= ${libVersion%%.*}"
libOSMesaVersionCompat="$libOSMesaVersion compat >= ${libOSMesaVersion%%.*}"

PROVIDES="
	mesa$secondaryArchSuffix = $portVersion
	lib:libEGL$secondaryArchSuffix = $libVersionCompat
	lib:libGL$secondaryArchSuffix = $libVersionCompat
	lib:libOSMesa$secondaryArchSuffix = $libOSMesaVersionCompat
	lib:libglesv1_cm$secondaryArchSuffix = 1.1.0
	lib:libglesv2$secondaryArchSuffix = 2.0.0
	lib:libglapi$secondaryArchSuffix = 0.0.0
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	lib:libLLVM_9$secondaryArchSuffix
	lib:libexpat$secondaryArchSuffix
	"

PROVIDES_devel="
	mesa${secondaryArchSuffix}_devel = $portVersion
	devel:libEGL$secondaryArchSuffix = $libVersionCompat
	devel:libGL$secondaryArchSuffix = $libVersionCompat
	devel:libOSMesa$secondaryArchSuffix = $libOSMesaVersionCompat
	devel:libglesv1_cm$secondaryArchSuffix = 1.1.0
	devel:libglesv2$secondaryArchSuffix = 2.0.0
	devel:libglapi$secondaryArchSuffix = 0.0.0
	"
REQUIRES_devel="
	mesa$secondaryArchSuffix == $portVersion base
	"

SUMMARY_swpipe="The Mesa LLVM enhanced Gallium software pipe renderer"
DESCRIPTION_swpipe="This 3D BGLRenderer add-on provides Gallium LLVM \
enhanced software rendering. Software pipe rendering performs all \
3D rendering on the systems CPU and doesn't require any specialized \
hardware. The usage of LLVM over traditional rasterization gives this \
renderer a boost in performance.

Gallium software pipe rendering is in an extremely early state as of \
this version of Mesa, and may not function as expected."

PROVIDES_swpipe="
	mesa${secondaryArchSuffix}_swpipe = $portVersion
	"
REQUIRES_swpipe="
	mesa$secondaryArchSuffix == $portVersion base
	lib:libLLVM_9$secondaryArchSuffix
	"

SUMMARY_lavapipe="Vulkan software rasteriser driver"
DESCRIPTION_lavapipe="Lavapipe is Mesa's software-based Vulkan 1.2 \
implementation akin to LLVMpipe for OpenGL."

PROVIDES_lavapipe="
	mesa${secondaryArchSuffix}_lavapipe = $portVersion
	"
REQUIRES_lavapipe="
	lib:libLLVM_9$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libz$secondaryArchSuffix
	devel:libLLVM_9$secondaryArchSuffix
	devel:libexpat$secondaryArchSuffix
	devel:libvulkan$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	python3$secondaryArchSuffix
	mako_python3
	cmd:ninja
	cmd:bison
	cmd:find
	cmd:flex
	cmd:meson
	cmd:git
	cmd:llvm_config >= 9
	cmd:egrep
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:pkg_config$secondaryArchSuffix
	cmd:which
	"

defineDebugInfoPackage mesa$secondaryArchSuffix \
	"${addOnsDir/mesa$secondaryArchSuffix/mesa${secondaryArchSuffix}_swpipe}/opengl/Software Pipe" \
	"${addOnsDir/mesa$secondaryArchSuffix/mesa${secondaryArchSuffix}_lavapipe}/vulkan/icd.d/libvulkan_lvp.so" \
	"$libDir"/libEGL.so.$libVersion \
	"$libDir"/libGL.so.$libVersion \
	"$libDir"/libOSMesa.so.$libOSMesaVersion \

BUILD()
{
	if [ -n "$secondaryArchSuffix" ]; then
		export HAIKU_SECONDARY_ARCH="$effectiveTargetArchitecture"
	fi

	mkdir -p build
	meson build \
		-Dosmesa=true \
		-Degl=true \
		-Dgallium-drivers=swrast \
		-Dvulkan-drivers=swrast \
		-Dvulkan-icd-dir=$addOnsDir/vulkan/icd.d \
		--buildtype=debugoptimized \
		--prefix=$prefix \
		--libdir=$libDir \
		--includedir=$includeDir
	ninja -C build $jobArgs
}

INSTALL()
{
	ninja -C build install

	# Do a little cleanup
	rm -rf $prefix/share

	# Our rendering add-ons
	mkdir -p $addOnsDir/opengl
	mv $(find $libDir -name 'libswpipe.so') \
		"$addOnsDir/opengl/Software Pipe"
	mv $libDir/libvulkan_lvp.so \
		"$addOnsDir/vulkan/icd.d"
	rm -rf $libDir/haiku

	# Set some nice version info
	appVersion=${portVersion//\./ }
	setversion "$libDir/libGL.so" -app $appVersion -long "Haiku OpenGL kit"
	setversion "$libDir/libEGL.so.1" -app $appVersion -long "Mesa EGL"
	setversion "$addOnsDir/opengl/Software Pipe" -app $appVersion -long \
		"Gallium LLVM software pipe renderer"
	setversion "$addOnsDir/vulkan/icd.d/libvulkan_lvp.so" -app $appVersion -long \
		"Vulkan software rasteriser driver"

	prepareInstalledDevelLibs \
		libGL libEGL libOSMesa
	fixPkgconfig

	# for compatibility
	symlinkRelative -s $libDir/libGL.so.1 $libDir/libGL.so

	# OpenGL Kit
	mkdir -p $includeDir/os/opengl
	cp ./include/HaikuGL/OpenGLKit.h $includeDir/os/
	cp ./include/HaikuGL/GLView.h $includeDir/os/opengl/
	cp ./include/HaikuGL/GLRenderer.h $includeDir/os/opengl/

	# Create GL symlink in opengl kit
	symlinkRelative -s $includeDir/GL $includeDir/os/opengl/GL
	# Create EGL symlink in opengl kit
	symlinkRelative -s $includeDir/EGL $includeDir/os/opengl/EGL
	# Create KHR symlink in opengl kit
	symlinkRelative -s $includeDir/KHR $includeDir/os/opengl/KHR

	# devel package
	packageEntries devel \
		$developDir

	# swpipe renderer package
	packageEntries swpipe \
		"$addOnsDir/opengl/Software Pipe"

	# lavapipe renderer package
	packageEntries lavapipe \
		"$addOnsDir/vulkan/icd.d"

	rmdir "$addOnsDir"/opengl
}

TEST()
{
	make check
}
