# Build recipe for mesa.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017-2021 Matias Fonzo <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=mesa
version=21.2.5
release=3

# Define a category for the output of the package name
pkgcategory=xorg_mesa

tarname=${program}-${version}.tar.xz

# Remote source(s)
fetch=https://archive.mesa3d.org/$tarname

description="
Mesa is an OpenGL compatible 3-D graphics library.

The Mesa project began as an open-source implementation of the OpenGL
specification - a system for rendering interactive 3D graphics.
"

homepage=https://www.mesa3d.org/
license=MIT

# Source documentation
docs="README* REVIEWERS VERSION"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # To increment compatibility with Musl (Thanks to "Alpine Linux")
    patch -p1 < "${worktree}/patches/mesa/musl-fix-includes.patch"
    patch -p1 < "${worktree}/patches/mesa/add-use-elf-tls.patch"

    # To provide a few xdemos (Thanks to BLFS!)
    patch -p1 < "${worktree}/patches/mesa/mesa-21.2.4-add_xdemos-1.patch"

    MESA_GIT_SHA1_OVERRIDE='53b2b224dc2de982c37915a0ad218e33365ff75e'
    export MESA_GIT_SHA1_OVERRIDE
    python3 bin/git_sha1_gen.py --output include/git_sha1.h

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    case $arch in
    amd64 | x32 | i?86 )
        _extra_options="-Dglx-read-only-text=true"
        ;;
    esac

    rm -rf BUILD
    mkdir BUILD
    cd BUILD

    CPPFLAGS="$QICPPFLAGS -D_XOPEN_SOURCE=700" \
    CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" LDFLAGS="$QILDFLAGS" \
    meson setup $configure_args \
     --libdir /usr/lib${libSuffix} \
     --buildtype=release \
     --strip \
     -Dplatforms=x11 \
     -Ddri-drivers="i915,i965,r100,r200,nouveau" \
     -Dgallium-drivers="nouveau,r300,r600,svga,radeonsi,swrast,virgl,iris,crocus" \
     -Dglx=dri \
     -Dosmesa=true \
     -Dllvm=enabled \
     -Dshared-llvm=enabled \
     -Dgallium-xvmc=enabled \
     -Dgallium-va=enabled \
     -Dgallium-xa=disabled \
     -Dgallium-vdpau=enabled \
     -Dgles1=enabled \
     -Dgles2=enabled \
     -Dopengl=true \
     -Dlmsensors=enabled \
     -Dgallium-nine=true \
     -Dvalgrind=disabled \
     $_extra_options \
     ..

    ninja -j${jobs}
    DESTDIR="$destdir" ninja -j${jobs} install

    unset -v MESA_GIT_SHA1_OVERRIDE _extra_options

    cd ../

    # Handle .new files via graft(1), if any
    if test -d "${destdir}/etc"
    then
        touch "${destdir}/etc/.graft-config"
    fi
    touch "${destdir}/usr/share/drirc.d/.graft-config"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

