# Importada de Alpine aports por `hammer import-alpine` (Etapa G). PUNTO DE PARTIDA — pero
# YA trae los parches de musl de Alpine (lo que un import de nix pierde). Pendiente: el
# sha256 del tarball (el wrapper lo calcula), y adaptar build/install del shell de abuild.
name    = "mesa"
version = "26.1.1"

[source]
tarball = "https://mesa.freedesktop.org/archive/mesa-${pkgver/_/-}.tar.xz"
# FIXME sha256: el wrapper lo calcula (Alpine publica sha512). sha512 de Alpine:
# sha512 = "384a7db33d2f06e287337329f2f17503082ba6b1cd876a1884e940fdb6193ffae6d6b14344625256f3b3b39972ad697130a4d2ea25c40979f7aa76c2176f7988"
sha256  = "FIXME-sha256"
patches = ["23575.patch", "riscv64-tls.patch", "llvm22-armhf.patch", "armv7-exynos4.patch"]

[build]
compiler = "zig-cc"
target   = "x86_64-linux-musl"
link     = "static"
flags    = []

[build.phases]
# de build() de Alpine (traducido; el lab provee $CBUILD/$CHOST — Etapa G Fase 3; revisá --shared para estático):
compile = '''
_abuild_phase() {
# use -g1 to generate less debug info:
	# 485 MiB -> ~80 MiB
	export CFLAGS="$CFLAGS -O2 -g1"
	export CXXFLAGS="$CXXFLAGS -O2 -g1"
	export CPPFLAGS="$CPPFLAGS -O2 -g1"

	case "$CARCH" in
	armhf|armv7)
		# gnu2 tlsdesc is broken in binutils, see
		# https://gitlab.alpinelinux.org/alpine/aports/-/issues/14140
		# and
		# https://gitlab.freedesktop.org/mesa/mesa/-/issues/13236
		export CFLAGS="$CFLAGS -mtls-dialect=gnu"
		export CXXFLAGS="$CXXFLAGS -mtls-dialect=gnu"
		;;
	esac

	PATH="$PATH:/usr/lib/llvm$_llvmver/bin" \
	abuild-meson \
		-Db_ndebug=true \
		-Db_lto=$_lto \
		-Dallow-kcmp=enabled \
		-Dexpat=enabled \
		-Dintel-rt=$_intel_rt \
		-Dshader-cache=enabled \
		-Dxlib-lease=enabled \
		-Dxmlconfig=enabled \
		-Dzstd=enabled \
		-Dbackend_max_links=2 \
		-Dbuild-tests="$(want_check && echo true || echo false)" \
		-Ddri-drivers-path=$_dri_driverdir \
		-Dgallium-drivers=$_gallium_drivers \
		-Dvulkan-drivers=$_vulkan_drivers \
		-Dvulkan-layers=$_vulkan_layers \
		-Damdgpu-virtio=true \
		-Dfreedreno-kmds=msm,virtio \
		-Dplatforms=x11,wayland \
		-Dllvm=enabled \
		-Dshared-llvm=enabled \
		-Dgbm=enabled \
		-Dglx=dri \
		-Dopengl=true \
		-Dgles1=enabled \
		-Dgles2=enabled \
		-Degl=enabled \
		-Dgallium-extra-hud=true \
		-Dgallium-rusticl=$_rusticl \
		-Dgallium-rusticl-enable-drivers=auto,freedreno \
		-Dgallium-va=enabled \
		-Drust_std=2021 \
		-Dvideo-codecs=all \
		. output

	# Print config
	meson configure --no-pager output

	meson compile -C output
}
_abuild_phase
'''
# de package() de Alpine (traducido $pkgdir→/out):
install = '''
_abuild_phase() {
DESTDIR="/out" meson install --no-rebuild -C output
}
_abuild_phase
'''

# depends de runtime de Alpine (NO build-deps): python3

[deps]
build = ["clang$_llvmver", "libclc-dev~$_llvmver", "rust-bindgen", "spirv-llvm-translator", "clang22", "libclc-dev~22"]
