#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2015-2016, 2026 InterGenJLU
#
# mesa 25.3.5 — OpenGL, Vulkan, and OpenCL implementation
# BLFS 13.0

configure() {
    set -e
    # Pre-place Rust crate tarballs for offline build.
    # NVK (Nouveau Vulkan) and other Rust-based components require 27 crates
    # from crates.io. Since the chroot has no internet, we pre-download them
    # on the host and archive them as mesa-25.3.5-rust-crates.tar.gz.
    # Meson checks subprojects/packagecache/ before attempting downloads.
    if [ -f "${IGOS_SOURCES}/mesa-25.3.5-rust-crates.tar.gz" ]; then
        mkdir -p subprojects/packagecache
        tar -xf "${IGOS_SOURCES}/mesa-25.3.5-rust-crates.tar.gz" \
            -C subprojects/packagecache
    fi

    # Post-patch fix for the 2020 BLFS xdemos patch (mesa-add_xdemos-4.patch,
    # diffed against mesa-20.2.1): it links glxgears/glxinfo via
    # `link_with: [libgl, glx_gallium_link]` — Mesa's INTERNAL gl target, which
    # under GLVND (enabled here; libglvnd is in-tree) is the vendor lib
    # (libGLX_mesa) and does NOT export the public gl*/glX* entry points, so the
    # demos fail to link with undefined references (glTranslated, glXSwapBuffers,
    # glMatrixMode, ...). Those symbols live in the GLVND public libGL. Re-link
    # the demos against it (gl.pc -> -lGL) instead of Mesa's internal target.
    # 2026-06-02. (Durable alternative, if this patch keeps drifting per Mesa
    # bump: move the demos to a standalone mesa-demos package, their upstream home.)
    local xdemos=src/glx/xdemos/meson.build
    if [ -f "$xdemos" ]; then
        sed -i \
            -e "/link_with : \[libgl, glx_gallium_link\],/d" \
            -e "s/dependencies : \[x11_dep\]/dependencies : [x11_dep, dependency('gl')]/" \
            "$xdemos"
    fi

    mkdir -p build
    cd    build

    # Build flags rationale:
    #   -D platforms=x11,wayland     X11 (XWayland) + native Wayland WSI
    #   -D gallium-drivers=auto      enables radeonsi (AMD GCN1+) +
    #                                r600/r300 (pre-GCN AMD) + nouveau
    #                                + iris (Intel) + zink + others on x86_64
    #   -D vulkan-drivers=auto       enables radv (AMD Vulkan) + anv
    #                                (Intel Vulkan) + lavapipe (software)
    #   -D valgrind=disabled         no Valgrind suppressions; runtime cost
    #   -D video-codecs=all          enable every VAAPI codec the radeonsi
    #                                back-end supports (H264/H265/AV1/etc)
    #   -D libunwind=disabled        no libunwind dep; uses glibc backtrace
    #   -D gallium-rusticl=true      enable Rusticl, Mesa's open-source
    #                                OpenCL implementation. Targets radeonsi
    #                                (AMD) and iris (Intel) gallium drivers.
    #                                Cross-distro alignment: Arch enables;
    #                                Fedora enables; Debian enables. Builds
    #                                /usr/lib/libRusticlOpenCL.so +
    #                                /etc/OpenCL/vendors/rusticl.icd manifest.
    #                                Activated at runtime by setting
    #                                RUSTICL_ENABLE=radeonsi (or iris). Zero
    #                                cost when not activated.
    #   (SPIR-V kernel ingestion for rusticl is now IMPLICIT — the separate
    #    `opencl-spirv` meson option was REMOVED upstream in Mesa 25.x because
    #    SPIR-V is mandatory for rusticl, not optional. `gallium-rusticl=true`
    #    alone enables it; the libclc + spirv-tools + spirv-llvm-translator deps
    #    already in tree provide the libraries. 25.3.5 configure errors on the
    #    old option as "Unknown option". 2026-06-02.)
    meson setup ..                  \
          --prefix=/usr             \
          --libdir=/usr/lib         \
          --buildtype=release       \
          --wrap-mode=nodownload    \
          -D platforms=x11,wayland  \
          -D gallium-drivers=auto   \
          -D vulkan-drivers=auto    \
          -D valgrind=disabled      \
          -D video-codecs=all       \
          -D libunwind=disabled     \
          -D gallium-rusticl=true
}

build() {
    set -e
    cd build
    ninja
}

do_install() {
    set -e
    cd build
    DESTDIR="$DESTDIR" ninja install
}
