# 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=2
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"
        LICENSE)
sha512sums=('c6040b0a508a6b74d7113f8e375fcaf93ec6f2875504953f30e2882e58e22f954bd4027a7d9ab8ac41abef266039e5834fdaf4ce2a69d5d98acf3bbdd54f04b6'
            'bf0f4a7e04220a407400f89226ecc1f798cc43035f2538cc8860e5088e1f84140baf0d4b0b28f66e4b802d4d6925769a1297c24e1ba39c1c093902b2931781a5')

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
}
