# Maintainer: Laurent Carlier <lordheavym@gmail.com>
# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Maintainer: Jan de Groot <jgc@archlinux.org>
# Contributor: Andreas Radke <andyrtr@archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - removed intel DRI drivers
#  - enable lto for aarch64

highmem=1

pkgname=mesa-amber
pkgver=21.3.9
pkgrel=6
pkgdesc="classic OpenGL (non-Gallium3D) drivers"
url="https://www.mesa3d.org/"
arch=('x86_64')
license=('MIT AND BSD-3-Clause AND SGI-B-2.0')
makedepends=(
  'cmake'
  'elfutils'
  'libdrm'
  'libglvnd'
  'libunwind'
  'libx11'
  'libxdamage'
  'libxml2'
  'libxrandr'
  'libxshmfence'
  'libxxf86vm'
  'meson'
  'python-mako'
  'valgrind'
  'wayland'
  'wayland-protocols'
  'xorgproto'
  'zstd'
)
source=(
  https://mesa.freedesktop.org/archive/mesa-${pkgver}.tar.xz{,.sig}
  LICENSE
)
sha256sums=('91fe6f20339884e37a7c47bfba42fb1cd397512c9ed5ad423de369f047cd8b5c'
            'SKIP'
            '7052ba73bb07ea78873a2431ee4e828f4e72bda7d176d07f770fa48373dec537')
b2sums=('79669f9492c505b1729c526adf5a8261737b6088bdf01db654a0dca8ba22600ec985b574edfeee3a28c76e6e0f815782362973876e1d75b58f4417f45df70add'
        'SKIP'
        '1ecf007b82260710a7bf5048f47dd5d600c168824c02c595af654632326536a6527fbe0738670ee7b921dd85a70425108e0f471ba85a8e1ca47d294ad74b4adb')
validpgpkeys=('71C4B75620BC75708B4BDB254C95FAAB3EB073EC') # Dylan Baker <dylan@pnwbakers.com>

prepare() {
  cd mesa-$pkgver

  # Include package release in version string so Chromium invalidates
  # its GPU cache; otherwise it can cause pages to render incorrectly.
  # https://bugs.launchpad.net/ubuntu/+source/chromium-browser/+bug/2020604
  echo "$pkgver-arch$epoch.$pkgrel" >VERSION
}

build() {
  local meson_options=(
    -D b_lto=$([[ $CARCH == aarch64 ]] && echo true || echo false) \
    -D amber=true
    -D b_ndebug=true
    -D dri-drivers=r100,r200,nouveau \
    -D dri3=enabled
    -D egl=enabled
    -D gallium-drivers=swrast
    -D gbm=enabled
    -D gles1=disabled
    -D gles2=enabled
    -D glvnd=true
    -D glx=dri
    -D libunwind=enabled
    -D llvm=disabled
    -D lmsensors=disabled
    -D microsoft-clc=disabled
    -D osmesa=true
    -D platforms=x11,wayland
    -D shared-glapi=enabled
    -D valgrind=enabled
    -D vulkan-drivers=auto
  )

  # Build only minimal debug info to reduce size
  CFLAGS+=' -g1'
  CXXFLAGS+=' -g1'

  arch-meson mesa-$pkgver build "${meson_options[@]}"
  meson configure build # Print config
  meson compile -C build
}

package() {
  depends=(
    'libdrm'
    'libglvnd'
    'libxfixes'
    'libxshmfence'
    'libxxf86vm'
    'wayland'
    'zstd'
  )
  conflicts=('mesa')
  provides=(
    "mesa=${pkgver}"
    "opengl-driver"
  )

  meson install -C build --destdir "${pkgdir}"

  # indirect rendering
  ln -s /usr/lib/libGLX_amber.so.0 "${pkgdir}/usr/lib/libGLX_indirect.so.0"

  install -m644 -Dt "${pkgdir}/usr/share/licenses/${pkgname}" LICENSE
}