# Note that this is still a dynamic library, see https://gitlab.freedesktop.org/glvnd/libglvnd/-/issues/212

_pkgname=libglvnd
pkgname=static-compat-$_pkgname
pkgver=1.0.0  # intentionally outdated for compatibility
pkgrel=3
pkgdesc="The GL Vendor-Neutral Dispatch library"
arch=('x86_64')
url="https://github.com/NVIDIA/libglvnd"
license=('custom:BSD-like')
depends=('static-compat-libxext' 'mesa' 'opengl-driver')
makedepends=('static-compat-libx11' 'static-compat-xorgproto' 'python' 'static-compat-configure' 'patchelf')
options=(staticlibs)
source=("$_pkgname-$pkgver.tar.gz::https://gitlab.freedesktop.org/glvnd/libglvnd/-/archive/v${pkgver}/libglvnd-v${pkgver}.tar.gz"
        egl-conditional-x11.patch
        LICENSE)
sha512sums=('f53a909a3ad240c2efbc7c38c3b7cf97bb9b95722a86ab841fcf5f1a83e8a99eb4bbc271f1f249867a74faf4f9d9f65aaffcecc5f5600346186c62f1eee43edd'
            '35d5e06184ddcda3dabb59830d38c8e6f9b5bd891bdca57ddb311595d2b4ec0c951d65f78ab05429f851615f4d669a9d139ed58916f114ee86d3b86a63859bb7'
            'bf0f4a7e04220a407400f89226ecc1f798cc43035f2538cc8860e5088e1f84140baf0d4b0b28f66e4b802d4d6925769a1297c24e1ba39c1c093902b2931781a5')

prepare() {
  patch -d $_pkgname-v$pkgver/include/EGL -p1 -i "$PWD/egl-conditional-x11.patch"
}

build() {
  source static-compat-environment
  export PATH=$PWD:$PATH
  export CFLAGS+=' -Wno-error=array-parameter'
  export LDFLAGS+=" $static_compat_prefix/lib/libX11.a $static_compat_prefix/lib/libXau.a $static_compat_prefix/lib/libxcb.a $static_compat_prefix/lib/libXau.a $static_compat_prefix/lib/libXdmcp.a -ldl"
  export LD=$GCC
  printf '#!/usr/bin/bash\nexec /usr/bin/pkg-config --static "$@"\n' > pkg-config
  cd $_pkgname-v$pkgver
  ./autogen.sh
  static-compat-configure
  make
}

package() {
  source static-compat-environment
  cd $_pkgname-v$pkgver
  mkdir -p "$pkgdir/$static_compat_prefix/include"
  DESTDIR="$pkgdir" make install

  # install missing headers manually
  cp -r --target-directory="$pkgdir/$static_compat_prefix/include" include/{EGL,GL,GLES,KHR}

  # make shared libs explicitly depend on "our" libGLdispatch.so.0 and libdl.so.2 as otherwise
  # apparently sometimes the version from /usr/lib is used which leads to linker errors:
  # g++ test.cpp -lGLX -lOpenGL
  # /usr/bin/ld: /usr/static-compat/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib/libGLX.so: undefined reference to `dlsym@GLIBC_2.2.5'
  # /usr/bin/ld: /usr/static-compat/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib/libGLX.so: undefined reference to `dlclose@GLIBC_2.2.5'
  # /usr/bin/ld: /usr/static-compat/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib/libGLX.so: undefined reference to `dlopen@GLIBC_2.2.5'
  # /usr/bin/ld: /usr/lib/libGLdispatch.so.0: undefined reference to `dlsym@GLIBC_2.34'
  # collect2: error: ld returned 1 exit status
  # note: This problem occurred when the normal libglvnd was updated to 1.4.0-2 for adding the debug option (1.4.0-1 still worked). Note
  #       that the update removed `libdl.so.2` as needed shared library from those libs. Maybe that changed the linker behavior.
  for lib in "$pkgdir/$static_compat_prefix"/lib/lib*.so; do
    patchelf \
      --add-needed "$static_compat_prefix"/lib/libdl.so.2 \
      --replace-needed libGLdispatch.so.0 "$static_compat_prefix"/lib/libGLdispatch.so.0 \
      "$lib"
  done

  cd ..
  install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}
