#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=mesa
PKGVERSION=18.3.5
PKGREVISION=1
PKGLICENSE="CUSTOM"
PKGSUMMARY="opensource OpenGL implementation"
PKGDOWNLOAD="https://mesa.freedesktop.org/archive/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="dev/meson:build"
PKGOPTIONS="DRI DRM EGL GALLIUM GLVND LLVM PLATFORMS SELINUX VULKAN WAYLAND X"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${DRI:=i965 r100 r200 nouveau}                        # driver list or "no"
: ${DRM:=yes}
: ${EGL:=yes}
: ${GALLIUM:=i915 radeonsi r300 r600 virgl swrast}      # driver list or "no"
: ${GLVND:=no}
: ${LLVM:=yes}
: ${SELINUX:=no}
: ${VULKAN:=intel amd}                                  # driver list or "no"
: ${WAYLAND:=yes}
: ${X:=yes}

# Convert user options separated by spaces to comma.
with_platforms=""
with_dri_drivers=""
with_gallium_drivers=""
with_vulkan_drivers=""

if [ "$DRM" = "yes" ]; then
	PKGDEPENDS="graphics/libdrm $PKGDEPENDS"
	with_platforms="drm"
fi

if [ "$DRI" != "no" ]; then
	with_dri_drivers=$(echo $DRI | sed -e "s/ /,/g")
fi

if [ "$EGL" = "yes" ]; then
	with_egl="-D egl=true"
else
	with_egl="-D egl=false"
fi

if [ "$GALLIUM" != "no" ]; then
	with_gallium_drivers=$(echo $GALLIUM | sed -e "s/ /,/g")
fi

if [ "$GLVND" = "yes" ]; then
	with_glvnd="-D glvnd=true"
else
	with_glvnd="-D glvnd=false"
fi

if [ "$LLVM" = "yes" ]; then
	PKGDEPENDS="dev/llvm $PKGDEPENDS"
	with_llvm="-D llvm=true"
else
	with_llvm="-D llvm=false"
fi

if [ "$VULKAN" != "no" ]; then
	with_vulkan_drivers=$(echo $VULKAN | sed -e "s/ /,/g")
fi

if [ "$WAYLAND" = "yes" ]; then
	PKGDEPENDS="graphics/wayland graphics/wayland-protocols $PKGDEPENDS"
	with_platforms="wayland,$with_platforms"
fi

if [ "$X" = "yes" ]; then
	PKGDEPENDS="x11/libx11
	            x11/libxdamage
	            x11/libxext
	            x11/libxrandr
	            x11/libxshmfence
	            x11/libxxf86vm
	            $PKGDEPENDS"
	with_platforms="x11,$with_platforms"
	with_dri="-D dri3=true"
	with_glx="-D glx=dri"
else
	with_dri="-D dri3=false"
	with_glx="-D glx=disabled"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvf $PKGNAME-$PKGVERSION.tar.xz
	cd $PKGNAME-$PKGVERSION

	patch -p0 < ../disable-tls.patch
	# https://git.alpinelinux.org/aports/plain/main/mesa/musl-fix-includes.patch
	patch -p1 < ../musl.patch
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	meson . build \
		--prefix / \
		--buildtype release \
		--default-library shared
		-D libdir=lib \
		-D bpm=true \
		-D platforms="$with_platforms" \
		-D dri-drivers="$with_dri_drivers" \
		-D gallium-drivers="$with_gallium_drivers" \
		-D vulkan-drivers="$with_vulkan_drivers" \
		$with_dri \
		$with_egl \
		$with_glvnd \
		$with_glx \
		$with_llvm
	ninja -C build
	DESTDIR=$DESTDIR ninja -C build install
	sed -i -e "s|prefix=/|prefix=|" $DESTDIR/lib/pkgconfig/*.pc

	cd ..
	rm -rf $PKGNAME-$PKGVERSION
