Skip to content

Instantly share code, notes, and snippets.

@shmerl
Last active October 18, 2026 00:13
Show Gist options
  • Select an option

  • Save shmerl/f4e5f76871239158cf083e37c5da56f4 to your computer and use it in GitHub Desktop.

Select an option

Save shmerl/f4e5f76871239158cf083e37c5da56f4 to your computer and use it in GitHub Desktop.
For building Mesa on Debian
#!/bin/bash
# Notes:
#
# 1. Works for tags and specific hash commits too (override mesa_branch variable with needed value).
#
# 2. By default builds for /opt/mesa-<branch> and places the result in ${HOME}/mnt/vmshare/mesa-<branch>
# You can override the build deployment location by setting dest_dir. For example this should put it right away
# in /opt/mesa-<branch>
#
# dest_dir=/ mesa_debian_build.sh
#
# 3. You can override the Mesa repo and resulting Mesa directory name. For instance, to build Mesa with ray tracing fixes
# (that development repo is picked just as an example):
#
# mesa_repo='https://gitlab.freedesktop.org/bnieuwenhuizen/mesa.git' mesa_branch='stable-rt-handles' mesa_dir='/opt/mesa-rt' mesa_debian_build.sh
#
# 4. Make sure deb-src section is configured for your Debian distro in your apt sources. That's needed to install build development packages.
#
# 5. If you aren't using Debian testing, set your debian_release to what you need (like "unstable").
#
# 6. To build with local libdrm, set local_libdrm=true. See libdrm_debian_build.sh for making a local build for it
debian_release=${debian_release:-"testing"} # Debian release (archive/suite), i.e. "testing", "unstable" etc.
mesa_branch=${mesa_branch:-"main"}
mesa_repo=${mesa_repo:-"https://gitlab.freedesktop.org/mesa/mesa.git"}
base_dir=${base_dir:-"${HOME}/build/mesa"}
src_dir=${src_dir:-"${base_dir}/source"}
build_dir=${build_dir:-"${base_dir}/build"}
patches_dir=${patches_dir:-"${base_dir}/patches"}
arch_dir="x86_64"
libdrm_local_pkg_dir=${libdrm_local_pkg_dir:-"${HOME}/build/libdrm/local/opt/mesa-main/${arch_dir}/pkgconfig"}
cpuarch=${cpuarch:-"znver4"} # Zen 4 by default. Change to your arch or "native" accordingly.
build_debug=${build_debug:-false}
verbose=${verbose:-false}
update_sources=${update_sources:-true}
reset_sources=${reset_sources:-true}
apply_patches=${apply_patches:-true}
local_libdrm=${local_libdrm:-false}
if [[ "$mesa_branch" != "mesa"* ]]; then
mesa_dir=${mesa_dir:-"/opt/mesa-${mesa_branch}"}
else
mesa_dir=${mesa_dir:-"/opt/${mesa_branch}"}
fi
dest_dir=${dest_dir:-"${HOME}/mnt/vmshare/$(basename ${mesa_dir})"}
function assert() {
rc=$1
message="$2"
if ((rc != 0)); then
echo $message
exit $rc
fi
}
function ensure() {
local rc=1
while (($rc != 0)); do
$@
rc=$?
if (($rc != 0)); then
sleep 1
fi
done
}
function setup_sources() {
echo "=== Setting up sources ===="
mkdir -v -p "$base_dir"
mkdir -v -p "$dest_dir"
rm -rfv ${dest_dir}/${mesa_dir}/*
ensure sudo apt install build-essential git
# Potential clean up of previously created repo directory due to repo URL mismatch.
if [[ -e "$src_dir" ]]; then
cd "$src_dir"
local prev_mesa_repo=$(git config --get remote.origin.url)
if [[ "$prev_mesa_repo" != "$mesa_repo" ]]; then
cd $(dirname "$src_dir")
rm -rfv $(basename "$src_dir")
fi
fi
cd $(dirname "$src_dir")
if ! [[ -e "$src_dir" ]]; then
git clone "$mesa_repo" $(basename "$src_dir")
fi
cd "$src_dir"
# If updating, reset is enforced regardless of the $reset_sources, to avoid possible merging confusion.
if $reset_sources || $update_sources; then
git reset --hard HEAD
git clean -df
fi
if $update_sources; then
git checkout main
git pull --rebase --prune
fi
git checkout $mesa_branch
if (($? != 0)); then
echo "Invalid branch or tag ${mesa_branch}! Aborting"
exit 2
fi
if $apply_patches; then
mkdir -v -p "$patches_dir"
for patch in ${patches_dir}/*; do
patch -p 1 < ${patch}
assert $? "Failed to apply patch ${patch}"
done
fi
mkdir -v -p "$build_dir"
rm -rfv ${build_dir}/*
}
function setup_packages() {
echo "=== Setting up packages for the build ===="
ensure sudo apt --target-release $debian_release build-dep mesa
}
configure() {
echo "==== Configuring build ===="
cd "$build_dir"
local build_type='plain'
local build_flags=''
local ndebug=true
local strip_option='--strip'
if $build_debug; then
build_type='debug'
build_flags='-g -O0'
ndebug=false
strip_option=''
else
build_flags='-O2'
fi
if $local_libdrm; then
export PKG_CONFIG_PATH="${libdrm_local_pkg_dir}:$PKG_CONFIG_PATH"
fi
export CFLAGS="-march=${cpuarch} ${build_flags} -ffile-prefix-map=${HOME}/build=. -Werror=implicit-function-declaration -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -Wall"
export CPPFLAGS="-Wdate-time -D_FORTIFY_SOURCE=2"
export CXXFLAGS="-march=${cpuarch} ${build_flags} -ffile-prefix-map=${HOME}/build=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -Wall"
export LDFLAGS="-Wl,-z,relro"
LC_ALL=C.UTF-8 meson setup "$src_dir" \
--wrap-mode=nodownload \
--buildtype=$build_type \
$strip_option \
--prefix="$mesa_dir" \
--sysconfdir=/etc \
--localstatedir=/var \
--libdir="$arch_dir" \
-D python.bytecompile=-1 \
-D dri-drivers-path="${mesa_dir}/${arch_dir}" \
-D platforms="['x11','wayland']" \
-D vulkan-drivers="['amd']" \
-D vulkan-layers="['device-select','overlay','screenshot']" \
-D vulkan-manifest-per-architecture=true \
-D glvnd=enabled \
-D b_ndebug=$ndebug \
-D glx-direct=true \
-D gbm=enabled \
-D gallium-extra-hud=true \
-D lmsensors=enabled \
-D llvm=disabled \
-D gallium-va=enabled \
-D va-libs-path="$arch_dir" \
-D video-codecs=all \
-D gallium-drivers="['radeonsi','zink']" \
-D gles1=disabled \
-D gles2=enabled \
-D teflon=false
assert $? "Configure failed!"
}
function build() {
echo "==== Building... ===="
cd "$build_dir"
if $verbose; then
LC_ALL=C.UTF-8 ninja -v
else
LC_ALL=C.UTF-8 ninja
fi
assert $? "build failed!"
}
function publish() {
cd "$build_dir"
DESTDIR="$dest_dir" LC_ALL=C.UTF-8 ninja install
}
function clean() {
ensure sudo apt autoremove --purge
}
################################################
shopt -s nullglob
setup_sources
setup_packages
configure
build
publish
clean
@shmerl

shmerl commented Jun 2, 2020

Copy link
Copy Markdown
Author

Fixed the empty directory issue, please test.

@Unterstrichmoepunterstrich

Copy link
Copy Markdown

Fixed the empty directory issue, please test.

where is the fixed code? In another gist? I would like to test, if the problem is solved.
I'm now on Debian bullseye and I can compile the it.
Can you put this script under an official open source licence? I would like to contribute some improvements, for example when you don't use deb-src from bullyseye, yo get an endless loop.

@shmerl

shmerl commented Jun 12, 2020 •

Copy link
Copy Markdown
Author

Same gist.

Good point on the need to use deb-src, I have it. I'll add a comment about it to make it more obvious.

@Unterstrichmoepunterstrich

Copy link
Copy Markdown

great it's working

@shmerl

shmerl commented Oct 9, 2025

Copy link
Copy Markdown
Author

Removed obsolete 32-bit build path

@shmerl

shmerl commented Nov 12, 2025

Copy link
Copy Markdown
Author

Removed llvm usage, it's not needed for any AMD drivers anymore they can work with ACO.

@shmerl

shmerl commented Oct 18, 2026

Copy link
Copy Markdown
Author

Dropped anti-lag layer, it's now part of radv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment