Files
bdi_podman_serverconf/containers/llamacpp/build-mesa.sh
T
2026-07-15 18:46:44 +02:00

217 lines
8.6 KiB
Bash

#!/usr/bin/env bash
###############################################################################
# build-mesa.sh
#
# Builda Mesa dal repository ufficiale upstream (gitlab.freedesktop.org),
# che su main contiene gia' il fix subgroup per Freedreno (commit
# 185c89084ab7 "freedreno/a6xx: Expose subgroup ops", non ancora presente in
# alcuna release stabile 26.1.x). Produce pacchetti .deb pronti da
# installare, salvati in ./deb/
#
# Nessun fork necessario: il fix e' gia' su main upstream.
#
# Uso:
# ./build-mesa.sh # clona main upstream in /tmp e builda
# ./build-mesa.sh /path/gia/clonato # usa un checkout gia' esistente
# ./build-mesa.sh <url-alternativo> # clona un repo/fork diverso, se serve
###############################################################################
set -euo pipefail
echo "=== Installazione dipendenze di build (primo step) ==="
sudo apt-get update
sudo apt-get build-dep -y mesa || {
echo "build-dep su pacchetto 'mesa' fallito o non disponibile, installo un set minimo esplicito."
sudo apt-get install -y \
git meson ninja-build pkg-config pkgconf python3 python3-mako python3-pip \
python3-ply python3-pycparser python3-yaml python3-setuptools \
libdrm-dev libx11-dev libx11-xcb-dev libxext-dev libxfixes-dev libxcb1-dev \
libxcb-glx0-dev libxxf86vm-dev libxrandr-dev libwayland-dev \
libwayland-egl-backend-dev libxcb-randr0-dev libxcb-shm0-dev \
libxcb-dri2-0-dev libxcb-dri3-dev libxcb-present-dev \
libxcb-sync-dev libxcb-xfixes0-dev libxshmfence-dev \
x11proto-dev linux-libc-dev libsensors-dev \
libva-dev libvdpau-dev libvulkan-dev libglvnd-core-dev \
wayland-protocols llvm-dev llvm-19-dev clang \
libclang-19-dev libclang-cpp19-dev libclc-19-dev libclc-19 \
spirv-tools glslang-tools llvm-spirv-19 libllvmspirvlib-19-dev \
flatbuffers-compiler libflatbuffers-dev libxtensor-dev \
byacc bison flex zlib1g-dev libelf-dev libzstd-dev libexpat1-dev \
rustc cargo rustfmt bindgen cbindgen \
librust-paste-dev librust-syn-dev \
valgrind dpkg-dev debhelper devscripts
}
echo
UPSTREAM_URL="https://gitlab.freedesktop.org/mesa/mesa.git"
ARG="${1:-}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEB_OUT_DIR="${SCRIPT_DIR}/deb"
# Determina se l'argomento e' un path esistente o un URL alternativo da clonare
if [ -n "${ARG}" ] && [ -d "${ARG}/.git" ]; then
MESA_SRC="${ARG}"
echo "=== Uso checkout gia' esistente: ${MESA_SRC} ==="
elif [ -n "${ARG}" ]; then
# argomento passato ma non e' una dir .git valida: trattalo come URL alternativo
CLONE_URL="${ARG}"
else
CLONE_URL="${UPSTREAM_URL}"
fi
if [ -z "${MESA_SRC:-}" ]; then
CLONE_DIR="/tmp/mesa-src"
if [ -d "${CLONE_DIR}/.git" ]; then
EXISTING_URL="$(git -C "${CLONE_DIR}" remote get-url origin 2>/dev/null || echo "")"
if [ "${EXISTING_URL}" = "${CLONE_URL}" ]; then
echo "=== Checkout gia' presente in ${CLONE_DIR}, aggiorno allineandolo a origin/main ==="
git -C "${CLONE_DIR}" fetch origin main
git -C "${CLONE_DIR}" checkout -B main origin/main
git -C "${CLONE_DIR}" reset --hard origin/main
else
echo "=== ${CLONE_DIR} esiste ma punta a un remote diverso, rifaccio il clone ==="
rm -rf "${CLONE_DIR}"
git clone --filter=blob:none --single-branch --branch main "${CLONE_URL}" "${CLONE_DIR}"
fi
else
echo "=== Clono ${CLONE_URL} in ${CLONE_DIR} (partial clone, filter=blob:none) ==="
git clone --filter=blob:none --single-branch --branch main "${CLONE_URL}" "${CLONE_DIR}"
fi
MESA_SRC="${CLONE_DIR}"
fi
BUILD_DIR="${MESA_SRC}/build-deb"
LOG_FILE="/tmp/build-mesa-$(date +%Y%m%d-%H%M%S).log"
echo "=== build-mesa.sh ==="
echo "Sorgente Mesa: ${MESA_SRC}"
echo "Output .deb: ${DEB_OUT_DIR}"
echo "Build dir: ${BUILD_DIR}"
echo "Log: ${LOG_FILE}"
echo
echo "=== Verifica commit fix subgroup presente ==="
mkdir -p "${DEB_OUT_DIR}"
cd "${MESA_SRC}"
SUBGROUP_COMMIT="185c89084ab7a84722b75af6a6026b56945ed44e"
if git merge-base --is-ancestor "${SUBGROUP_COMMIT}" HEAD 2>/dev/null; then
echo "OK: commit fix subgroup trovato nella history."
else
echo "ATTENZIONE: non trovo il commit atteso nella history di questo checkout." >&2
echo "Verifica di essere sul branch/commit giusto prima di continuare." >&2
read -p "Continuare comunque? [y/N] " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] || exit 1
fi
echo
echo "=== Build Mesa (meson/ninja) ==="
rm -rf "${BUILD_DIR}"
meson setup "${BUILD_DIR}" "${MESA_SRC}" \
-Dprefix=/usr \
-Dsysconfdir=/etc \
-Dgallium-drivers=freedreno \
-Dvulkan-drivers=freedreno \
-Dgallium-rusticl=true \
-Dgallium-rusticl-enable-drivers=freedreno \
-Dbuildtype=release \
2>&1 | tee -a "${LOG_FILE}"
ninja -C "${BUILD_DIR}" 2>&1 | tee -a "${LOG_FILE}"
echo
echo "=== Packaging .deb ==="
# Se il repo ha tooling Debian (cartella debian/), usalo.
if [ -d "${MESA_SRC}/debian" ]; then
echo "Trovata cartella debian/, uso dpkg-buildpackage."
cd "${MESA_SRC}"
dpkg-buildpackage -us -uc -b 2>&1 | tee -a "${LOG_FILE}"
# dpkg-buildpackage mette i risultati nella dir padre di MESA_SRC
find "$(dirname "${MESA_SRC}")" -maxdepth 1 -name "*.deb" -newer "${LOG_FILE}" \
-exec cp -v {} "${DEB_OUT_DIR}/" \;
else
echo "Nessuna cartella debian/ trovata: creo un pacchetto .deb minimale"
echo "con i soli file installati dalla build (DESTDIR + fpm/dpkg-deb)."
DESTDIR="${BUILD_DIR}/install-root"
rm -rf "${DESTDIR}"
DESTDIR="${DESTDIR}" ninja -C "${BUILD_DIR}" install 2>&1 | tee -a "${LOG_FILE}"
PKG_ROOT="${BUILD_DIR}/deb-pkg"
PKG_NAME="mesa-freedreno-subgroups"
PKG_VERSION="26.2.0-devel+subgroups1"
PKG_ARCH="arm64"
PKG_DIR="${PKG_ROOT}/${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}"
rm -rf "${PKG_ROOT}"
mkdir -p "${PKG_DIR}/DEBIAN"
cp -a "${DESTDIR}/usr" "${PKG_DIR}/"
[ -d "${DESTDIR}/etc" ] && cp -a "${DESTDIR}/etc" "${PKG_DIR}/"
cat > "${PKG_DIR}/DEBIAN/control" << EOF
Package: ${PKG_NAME}
Version: ${PKG_VERSION}
Section: libs
Priority: optional
Architecture: ${PKG_ARCH}
Maintainer: local-build
Description: Mesa build from local fork with Freedreno subgroup support
Build locale da fork Mesa (commit 185c89084ab7 "freedreno/a6xx: Expose
subgroup ops", non ancora presente in alcuna release stabile 26.1.x,
disponibile solo su trunk verso 26.2).
.
Include anche il driver Vulkan Turnip per Freedreno (-Dvulkan-drivers=
freedreno), buildato dallo stesso checkout main upstream cosi' da restare
alla stessa versione/patchset del resto dello stack (GL/EGL/GBM/rusticl).
.
Sostituisce i file installati da mesa-libgallium / mesa-opencl-icd /
mesa-vulkan-drivers di Debian con la build patchata. Da usare solo per test.
EOF
cat > "${PKG_DIR}/DEBIAN/README-patch.md" << 'EOF'
# Nota sulla patch inclusa
Commit: 185c89084ab7 "freedreno/a6xx: Expose subgroup ops"
Autore: upstream Mesa (author date 2026-02-11, committer/merge date 2026-05-18)
Motivo assenza in Debian: il branch stabile 26.1 e' stato diramato da main il
2026-04-14 (tag mesa-26.1.0 il 2026-05-06), quindi questo fix e' arrivato su
main DOPO la diramazione. Essendo una feature (non un bugfix), non verra'
backportato al branch 26.1.x per policy standard di Mesa. Il fix esiste solo
su trunk, verso la futura 26.2.
Verificato (vedi indagine precedente):
- Nessuna dipendenza da ioctl/kernel driver msm: i campi
shader_subgroup_supported_features / max_subgroups / subgroup_sizes sono
popolati da tabelle statiche compilate (freedreno_devices.py).
- Nessuna condizione che esclude a690 (FD690 e' CHIP.A6XX).
- Nessun commit successivo modifica subgroup/ballot/shuffle in freedreno tra
questo fix e HEAD: e' completo e autosufficiente, non un WIP parziale.
Questo pacchetto installa i file prodotti da una build meson/ninja pulita
di questo checkout, con gallium-drivers=freedreno, vulkan-drivers=freedreno
(Turnip) e rusticl abilitato per freedreno.
Nota: il pacchetto include il driver ICD Vulkan (libvulkan_freedreno.so +
freedreno_icd.json) ma NON il Vulkan loader (libvulkan.so.1), che resta
il pacchetto standard Debian "libvulkan1" installato separatamente nel
Containerfile.
EOF
dpkg-deb --build --root-owner-group "${PKG_DIR}" "${DEB_OUT_DIR}/${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}.deb"
fi
echo
echo "=== Contenuto pacchetto/i .deb prodotti ==="
for deb in "${DEB_OUT_DIR}"/*.deb; do
[ -e "${deb}" ] || continue
echo "--- ${deb} ---"
dpkg -c "${deb}"
echo
done
echo "=== Fatto ==="
echo "Pacchetti .deb disponibili in: ${DEB_OUT_DIR}"
echo "Log completo: ${LOG_FILE}"