%bcond_without  mesa_glvnd_default

Name:           libglvnd
Version:        1.3.2
Release:        2%{?_dist_release}
Summary:        The GL Vendor-Neutral Dispatch library
Group:          system
Distribution:   Vine Linux
Vendor:         Project Vine

License:        MIT
URL:            https://github.com/NVIDIA/libglvnd
Source0:        %{url}/archive/v%{version}/%{name}-%{version}.tar.gz
Source1000:     ChangeLog.fedora
Patch0:         libglvnd-python3.patch
Patch1:         0001-glx-Add-another-fallback-library-name.patch

BuildRequires: make
BuildRequires:  libtool
BuildRequires:  gcc
BuildRequires:  python3-rpm-macros
BuildRequires:  python3-libxml2
BuildRequires:  pkgconfig(glproto)
BuildRequires:  pkgconfig(x11)
BuildRequires:  pkgconfig(xext)
# for tests
BuildRequires:  xorg-x11-server-Xvfb
BuildRequires:  xorg-x11-xauth
BuildRequires:  xorg-x11-xkb-utils
BuildRequires:  xkeyboard-config

%if %{without mesa_glvnd_default}
%global _without_mesa_glvnd_default 1
%endif

%{?_without_mesa_glvnd_default:
%global __provides_exclude_from %{_libdir}/%{name}
%global __requires_exclude_from %{_libdir}/%{name}
}

%description
libglvnd is an implementation of the vendor-neutral dispatch layer for
arbitrating OpenGL API calls between multiple vendors on a per-screen basis.


%package        devel
Summary:        Development files for %{name}
Group:          programming
Requires:       %{name}%{?_isa} = %{version}-%{release}
Requires:       %{name}-opengl%{?_isa} = %{version}-%{release}
Requires:       %{name}-gles%{?_isa} = %{version}-%{release}
Requires:       %{name}-glx%{?_isa} = %{version}-%{release}
Requires:       %{name}-egl%{?_isa} = %{version}-%{release}
Requires:       %{name}-core-devel%{?_isa} = %{version}-%{release}
# Required by any glx.h users.
Requires:       libX11-devel%{?_isa}
# We might split into more sub-packages
Obsoletes:      mesa-libGLES-devel < 21.0.0
Provides:       mesa-libGLES-devel = %{version}-%{release}
Provides:       mesa-libGLES-devel%{?_isa} = %{version}-%{release}
Provides:       mesa-khr-devel = %{version}-%{release}
Provides:       mesa-khr-devel%{?_isa} = %{version}-%{release}
Provides:       libGLES-devel = %{version}-%{release}
Provides:       libGLES-devel%{?_isa} = %{version}-%{release}

%description    devel
The %{name}-devel package contains libraries and header files for
developing applications that use %{name}.


%package        core-devel
Summary:        Core development files for %{name}
Group:          programming

%description core-devel
The %{name}-core-devel package is a bootstrap trick for Mesa, which wants
to build against the %{name} headers but does not link against any of
its libraries (and, initially, has file conflicts with them). If you are
not Mesa you almost certainly want %{name}-devel instead.


%package        opengl
Summary:        OpenGL support for libglvnd
Group:          system
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description    opengl
libOpenGL is the common dispatch interface for the workstation OpenGL API.


%package        gles
Summary:        GLES support for libglvnd
Group:          system
Requires:       %{name}%{?_isa} = %{version}-%{release}
%{!?_without_mesa_glvnd_default:
# mesa is the default EGL implementation provider
Requires:       mesa-libEGL%{?_isa} > 21.0.0
Obsoletes:      mesa-libGLES < 21.0.0
Provides:       mesa-libGLES = 21.0.0
Provides:       mesa-libGLES%{?_isa} = 21.0.0
Provides:       libGLES
Provides:       libGLES%{?_isa}
# workaround for "apt-get dist-upgrade"
Requires:       mesa-libglapi%{?_isa} > 21.0.0
}

%description    gles
libGLESv[12] are the common dispatch interface for the GLES API.


%package        egl
Summary:        EGL support for libglvnd
Group:          system
Requires:       %{name}%{?_isa} = %{version}-%{release}
%{!?_without_mesa_glvnd_default:
# mesa is the default EGL implementation provider
Requires:       mesa-libEGL%{?_isa} > 21.0.0
Provides:       libEGL
Provides:       libEGL%{?_isa}
}

%description    egl
libEGL are the common dispatch interface for the EGL API.


%package        glx
Summary:        GLX support for libglvnd
Group:          system
Requires:       %{name}%{?_isa} = %{version}-%{release}
%{!?_without_mesa_glvnd_default:
# mesa is the default GL implementation provider
Requires:       mesa-libGL%{?_isa} > 21.0.0
Provides:       libGL
Provides:       libGL%{?_isa}
}

%description    glx
libGL and libGLX are the common dispatch interface for the GLX API.


%debug_package


%prep
%autosetup -p1 -n %{name}-%{version}
autoreconf -vif


%build
export PYTHON=%{__python3}
#Prefer asm and tls for x86* and ppc64*
#armhfp and aarch64 fallback to asm and tsd
#Others arches fallback to pure-c and tls.
%configure \
  --disable-static \
  --enable-asm \
  --enable-tls

%make_build V=1


%install
%make_install INSTALL="install -p"
find %{buildroot} -name '*.la' -delete

%{?_without_mesa_glvnd_default:
# Avoid conflict with mesa-libGL
mkdir -p %{buildroot}%{_libdir}/%{name}/
for l in libEGL libGL libGLESv1_CM libGLESv2 libGLX; do
  mv %{buildroot}%{_libdir}/${l}.so* \
    %{buildroot}%{_libdir}/%{name}/
done
}

# Create directory layout
mkdir -p %{buildroot}%{_sysconfdir}/glvnd/egl_vendor.d/
mkdir -p %{buildroot}%{_datadir}/glvnd/egl_vendor.d/
mkdir -p %{buildroot}%{_sysconfdir}/egl/egl_external_platform.d/
mkdir -p %{buildroot}%{_datadir}/egl/egl_external_platform.d/

cp -pf %{SOURCE1000} ./


%check
export DO_X11_TESTS=1
xvfb-run -e xvfb-error.log -s '-screen 0 640x480x24' -d make check V=1 || \
%ifarch s390x ppc64
    :
%else
    if [ `find . -name test-suite.log | wc --lines` -gt 0 ]; then
        (cat `find . -name test-suite.log` ; exit 1)
    else
        if [ -f xvfb-error.log ]; then
            cat xvfb-error.log
        else
            echo "unknown error occured."
        fi
        exit 1
    fi
%endif


%files
%doc README.md ChangeLog.fedora
%dir %{_sysconfdir}/glvnd/
%dir %{_datadir}/glvnd/
%{_libdir}/libGLdispatch.so.0*

%ldconfig_scriptlets opengl
%files opengl
%{_libdir}/libOpenGL.so.0*

%ldconfig_scriptlets gles
%files gles
%if 0%{?_without_mesa_glvnd_default}
%{_libdir}/%{name}/libGLES*.so.*
%else
%{_libdir}/libGLES*.so.*
%endif

%files glx
%if 0%{?_without_mesa_glvnd_default}
%{_libdir}/%{name}/libGL.so.*
%{_libdir}/%{name}/libGLX.so.*
%else
%{_libdir}/libGL.so.*
%{_libdir}/libGLX.so.*
%endif

%files egl
%dir %{_sysconfdir}/glvnd/egl_vendor.d/
%dir %{_datadir}/glvnd/egl_vendor.d/
%dir %{_sysconfdir}/egl/
%dir %{_sysconfdir}/egl/egl_external_platform.d/
%dir %{_datadir}/egl/
%dir %{_datadir}/egl/egl_external_platform.d/
%if 0%{?_without_mesa_glvnd_default}
%{_libdir}/%{name}/libEGL*.so.*
%else
%{_libdir}/libEGL*.so.*
%endif

%files core-devel
%dir %{_includedir}/glvnd/
%{_includedir}/glvnd/*.h
%{_libdir}/pkgconfig/libglvnd.pc

%files devel
%dir %{_includedir}/EGL/
%dir %{_includedir}/GL/
%dir %{_includedir}/GLES/
%dir %{_includedir}/GLES2/
%dir %{_includedir}/GLES3/
%dir %{_includedir}/KHR/
%{_includedir}/EGL/*.h
%{_includedir}/GL/*.h
%{_includedir}/GLES/*.h
%{_includedir}/GLES2/*.h
%{_includedir}/GLES3/*.h
%{_includedir}/KHR/*.h
%{_libdir}/lib*.so
%if 0%{?_without_mesa_glvnd_default}
%{_libdir}/%{name}/lib*.so
%endif
%{_libdir}/pkgconfig/gl*.pc
%{_libdir}/pkgconfig/egl.pc
%{_libdir}/pkgconfig/opengl.pc


%changelog
* Sun Nov 21 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 1.3.2-2
- fixed dependency.

* Sat Mar 27 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 1.3.2-1
- initial build for Vine Linux.
- moved Fedora's %%changelog to ChangeLog.fedora.
