%global srcname mesa
%global _description These drivers contains video acceleration codecs for decoding/encoding H.264 and H.265 \
algorithms and decoding only VC1 algorithm.
%ifnarch s390x
%global with_hardware 1
%global with_radeonsi 1
%global with_vmware 1
%global with_vulkan_hw 0
%global with_vdpau 1
%global with_va 1
%if !0%{?rhel}
%global with_r300 1
%global with_r600 1
%global with_nine 0
#%%if 0%%{?with_vulkan_hw}
%global with_nvk %{with_vulkan_hw}
#%%endif
%global with_opencl 0
%endif
#%%global base_vulkan %%{?with_vulkan_hw:,amd}%%{!?with_vulkan_hw:%%{nil}}
%endif

#%%ifnarch %%{ix86}
%if !0%{?rhel}
%global with_teflon 0
%endif
#%%endif

%ifarch %{ix86} x86_64
%global with_crocus 0
%global with_i915   0
%global with_iris   0
%global with_xa     0
%global with_intel_clc 0
#%%global intel_platform_vulkan %%{?with_vulkan_hw:,intel,intel_hasvk}%%{!?with_vulkan_hw:%%{nil}}
%endif
#%%ifarch x86_64
#%%if !0%%{?with_vulkan_hw}
%global with_intel_vk_rt 0
#%%endif
#%%endif

%ifarch aarch64 x86_64 %{ix86}
%global with_kmsro     0
%if !0%{?rhel}
%global with_lima      0
%global with_vc4       0
%global with_etnaviv   0
%global with_tegra     0
%endif
%global with_freedreno 0
%global with_panfrost  0
%global with_v3d       0
%global with_xa        0
#%%global extra_platform_vulkan %%{?with_vulkan_hw:,broadcom,freedreno,panfrost,imagination-experimental}%%{!?with_vulkan_hw:%%{nil}}
%endif

%if !0%{?rhel}
%global with_libunwind 1
%global with_lmsensors 1
%endif

%ifarch %{valgrind_arches}
%bcond_without valgrind
%else
%bcond_with valgrind
%endif

%define gittag 14bc2e2d395

#%%global vulkan_drivers swrast%%{?base_vulkan}%%{?intel_platform_vulkan}%%{?extra_platform_vulkan}%%{?with_nvk:,nouveau}

Name:           %{srcname}-freeworld
Summary:        Mesa graphics libraries
%global ver 25.1.0-devel
Release:        0.276%{?gittag:~git%{gittag}}%{?dist}
License:        MIT AND BSD-3-Clause AND SGI-B-2.0
URL:            http://www.mesa3d.org
Version:        %{lua:ver = string.gsub(rpm.expand("%{ver}"), "-", "~"); print(ver)}
Source0:        https://mesa.freedesktop.org/archive/%{srcname}-%{ver}%{?gittag:-%{gittag}}.tar.xz
# src/gallium/auxiliary/postprocess/pp_mlaa* have an ... interestingly worded license.
# Source1 contains email correspondence clarifying the license terms.
# Fedora opts to ignore the optional part of clause 2 and treat that code as 2 clause BSD.
Source1:        Mesa-MLAA-License-Clarification-Email.txt
Source2:        org.mesa3d.vaapi.freeworld.metainfo.xml
Source3:        org.mesa3d.vdpau.freeworld.metainfo.xml

BuildRequires:  meson >= 1.3.0
BuildRequires:  gcc
BuildRequires:  gcc-c++
BuildRequires:  gettext
%if 0%{?with_hardware}
BuildRequires:  kernel-headers
%endif
# We only check for the minimum version of pkgconfig(libdrm) needed so that the
# SRPMs for each arch still have the same build dependencies. See:
# https://bugzilla.redhat.com/show_bug.cgi?id=1859515
BuildRequires:  pkgconfig(libdrm) >= 2.4.122
%if 0%{?with_libunwind}
BuildRequires:  pkgconfig(libunwind)
%endif
BuildRequires:  pkgconfig(expat)
BuildRequires:  pkgconfig(zlib) >= 1.2.3
BuildRequires:  pkgconfig(libzstd)
BuildRequires:  pkgconfig(libselinux)
BuildRequires:  pkgconfig(wayland-scanner)
BuildRequires:  pkgconfig(wayland-protocols) >= 1.34
BuildRequires:  pkgconfig(wayland-client) >= 1.11
BuildRequires:  pkgconfig(wayland-server) >= 1.11
BuildRequires:  pkgconfig(wayland-egl-backend) >= 3
BuildRequires:  pkgconfig(x11)
BuildRequires:  pkgconfig(xext)
BuildRequires:  pkgconfig(xdamage) >= 1.1
BuildRequires:  pkgconfig(xfixes)
BuildRequires:  pkgconfig(xcb-glx) >= 1.8.1
BuildRequires:  pkgconfig(xxf86vm)
BuildRequires:  pkgconfig(xcb)
BuildRequires:  pkgconfig(x11-xcb)
BuildRequires:  pkgconfig(xcb-dri2) >= 1.8
BuildRequires:  pkgconfig(xcb-dri3)
BuildRequires:  pkgconfig(xcb-present)
BuildRequires:  pkgconfig(xcb-sync)
BuildRequires:  pkgconfig(xshmfence) >= 1.1
BuildRequires:  pkgconfig(dri2proto) >= 2.8
BuildRequires:  pkgconfig(glproto) >= 1.4.14
BuildRequires:  pkgconfig(xcb-xfixes)
BuildRequires:  pkgconfig(xcb-randr)
BuildRequires:  pkgconfig(xrandr) >= 1.3
BuildRequires:  bison
BuildRequires:  flex
%if 0%{?with_lmsensors}
BuildRequires:  lm_sensors-devel
%endif
%if 0%{?with_vdpau}
BuildRequires:  pkgconfig(vdpau) >= 1.1
%endif
%if 0%{?with_va}
BuildRequires:  pkgconfig(libva) >= 0.38.0
%endif
BuildRequires:  pkgconfig(libelf)
BuildRequires:  pkgconfig(libglvnd) >= 1.3.2
BuildRequires:  llvm-devel >= 7.0.0
%if 0%{?with_teflon}
BuildRequires:  flatbuffers-devel
BuildRequires:  flatbuffers-compiler
BuildRequires:  xtensor-devel
%endif
%if 0%{?with_opencl} || 0%{?with_nvk} || 0%{?with_intel_clc}
BuildRequires:  rust-packaging
%endif
%ifarch %{ix86} x86_64
BuildRequires:  clang-devel
BuildRequires:  bindgen
BuildRequires:  pkgconfig(libclc)
BuildRequires:  pkgconfig(SPIRV-Tools)
BuildRequires:  pkgconfig(LLVMSPIRVLib)
%endif
%if 0%{?with_nvk}
BuildRequires:  cbindgen
BuildRequires:  (crate(paste) >= 1.0.14 with crate(paste) < 2)
BuildRequires:  (crate(proc-macro2) >= 1.0.56 with crate(proc-macro2) < 2)
BuildRequires:  (crate(quote) >= 1.0.25 with crate(quote) < 2)
BuildRequires:  (crate(syn/clone-impls) >= 2.0.15 with crate(syn/clone-impls) < 3)
BuildRequires:  (crate(unicode-ident) >= 1.0.6 with crate(unicode-ident) < 2)
%endif
%if %{with valgrind}
BuildRequires:  pkgconfig(valgrind)
%endif
BuildRequires:  python3-devel
BuildRequires:  python3-mako
%if 0%{?with_intel_clc}
BuildRequires:  python3-ply
%endif
BuildRequires:  python3-pycparser
BuildRequires:  python3-pyyaml
BuildRequires:  vulkan-headers
BuildRequires:  glslang
%if 0%{?with_vulkan_hw}
BuildRequires:  pkgconfig(vulkan)
%endif

%description
%{_description}

%if 0%{?with_va}
%package        -n %{srcname}-va-drivers-freeworld
Summary:        Mesa-based VA-API drivers
Requires:       %{srcname}-filesystem%{?_isa} = %{?epoch:%{epoch}:}%{version}

%description    -n %{srcname}-va-drivers-freeworld
%{_description}
%endif

%if 0%{?with_vdpau}
%package        -n %{srcname}-vdpau-drivers-freeworld
Summary:        Mesa-based VDPAU drivers
Requires:       %{srcname}-filesystem%{?_isa} = %{?epoch:%{epoch}:}%{version}
Conflicts:      %{srcname}-vdpau-drivers%{?_isa}

%description 	-n %{srcname}-vdpau-drivers-freeworld
%{_description}
%endif
%prep
%autosetup -n %{srcname}-%{ver} -p1
cp %{SOURCE1} docs/

%build
# ensure standard Rust compiler flags are set
export RUSTFLAGS="%build_rustflags"

# We've gotten a report that enabling LTO for mesa breaks some games. See
# https://bugzilla.redhat.com/show_bug.cgi?id=1862771 for details.
# Disable LTO for now
%define _lto_cflags %{nil}
%if %{with optimize}
%define __cflags_arch_x86_64 -march=x86-64-v3
%endif
#-fno-plt

%meson \
  --libdir=%{_libdir}/dri-freeworld \
  -Dvdpau-libs-path=%{_libdir}/vdpau \
  -Ddri-drivers-path=%{_libdir}/dri-freeworld \
  -Dva-libs-path=%{_libdir}/dri-freeworld \
  -Dplatforms=x11,wayland \
  -Dosmesa=false \
%if 0%{?with_hardware}
  -Dgallium-drivers=swrast,virgl,nouveau%{?with_r300:,r300}%{?with_crocus:,crocus}%{?with_i915:,i915}%{?with_iris:,iris}%{?with_vmware:,svga}%{?with_radeonsi:,radeonsi}%{?with_r600:,r600}%{?with_freedreno:,freedreno}%{?with_etnaviv:,etnaviv}%{?with_tegra:,tegra}%{?with_vc4:,vc4}%{?with_v3d:,v3d}%{?with_lima:,lima}%{?with_panfrost:,panfrost}%{?with_vulkan_hw:,zink} \
%else
  -Dgallium-drivers=swrast,virgl \
%endif
  -Dgallium-vdpau=%{?with_vdpau:enabled}%{!?with_vdpau:disabled} \
  -Dgallium-va=%{?with_va:enabled}%{!?with_va:disabled} \
  -Dgallium-xa=%{!?with_xa:enabled}%{?with_xa:disabled} \
  -Dgallium-nine=%{!?with_nine:true}%{?with_nine:false} \
  -Dteflon=%{!?with_teflon:true}%{?with_teflon:false} \
  -Dgallium-opencl=%{!?with_opencl:icd}%{?with_opencl:disabled} \
%if 0%{?with_opencl}
  -Dgallium-rusticl=true \
%endif
  -Dvideo-codecs=h264dec,h264enc,h265dec,h265enc,vc1dec,av1dec,av1enc,vp9dec \
  -Dvulkan-drivers=%{?vulkan_drivers} \
  -Dvulkan-layers=device-select \
  -Dshared-glapi=enabled \
  -Dgles1=enabled \
  -Dgles2=enabled \
  -Dopengl=true \
  -Dgbm=disabled \
  -Dglx=dri \
  -Degl=disabled \
  -Dglvnd=disabled \
%if 0%{?with_intel_clc}
  -Dintel-clc=enabled \
%endif
  -Dintel-rt=%{!?with_intel_vk_rt:enabled}%{?with_intel_vk_rt:disabled} \
  -Dmicrosoft-clc=disabled \
  -Dllvm=enabled \
  -Dshared-llvm=enabled \
  -Dvalgrind=%{?with_valgrind:enabled}%{!?with_valgrind:disabled} \
  -Dbuild-tests=false \
  -Dselinux=true \
%if !0%{?with_libunwind}
  -Dlibunwind=disabled \
%endif
%if !0%{?with_lmsensors}
  -Dlmsensors=disabled \
%endif
  -Dandroid-libbacktrace=disabled \
%ifarch %{ix86}
  -Dglx-read-only-text=true \
%endif
  %{nil}
%meson_build

%install
%meson_install

# install Appdata files
mkdir -p %{buildroot}%{_metainfodir}
install -pm 0644 %{SOURCE2} %{buildroot}%{_metainfodir}
install -pm 0644 %{SOURCE3} %{buildroot}%{_metainfodir}

# libvdpau opens the versioned name, don't bother including the unversioned
rm -vf %{buildroot}%{_libdir}/vdpau/*.so
# likewise glvnd
rm -vf %{buildroot}%{_libdir}/libGLX_mesa.so
rm -vf %{buildroot}%{_libdir}/libEGL_mesa.so
# XXX can we just not build this
rm -vf %{buildroot}%{_libdir}/libGLES*

# glvnd needs a default provider for indirect rendering where it cannot
# determine the vendor
ln -s %{_libdir}/libGLX_mesa.so.0 %{buildroot}%{_libdir}/libGLX_system.so.0

# this keeps breaking, check it early.  note that the exit from eu-ftr is odd.
pushd %{buildroot}%{_libdir}
for i in libOSMesa*.so libGL.so ; do
    eu-findtextrel $i && exit 1
done
popd

# strip unneeded files from va-api and vdpau
rm -rf %{buildroot}%{_datadir}/{drirc.d,glvnd,vulkan}
rm -rf %{buildroot}%{_libdir}{,/dri-freeworld}/{d3d,EGL,gallium-pipe,libGLX,pkgconfig}
rm -rf %{buildroot}%{_includedir}/{d3dadapter,EGL,GL,KHR}
rm -fr %{buildroot}%{_sysconfdir}/OpenGL
rm -fr %{buildroot}%{_libdir}{,/dri-freeworld}/libGL.so*
rm -fr %{buildroot}%{_libdir}/libgallium-*.so
rm -fr %{buildroot}%{_libdir}{,/dri-freeworld}/libglapi.so*
rm -fr %{buildroot}%{_libdir}/libOSMesa.so*
rm -fr %{buildroot}%{_libdir}/pkgconfig/osmesa.pc
rm -fr %{buildroot}%{_libdir}/libgbm.so*
rm -fr %{buildroot}%{_includedir}/gbm.h
rm -fr %{buildroot}%{_libdir}{,/dri-freeworld}/libxatracker.so*
rm -fr %{buildroot}%{_includedir}/xa_*.h
rm -fr %{buildroot}%{_libdir}/libMesaOpenCL.so*
rm -fr %{buildroot}%{_libdir}/dri/*_dri.so
rm -fr %{buildroot}%{_libdir}/libvulkan*.so
rm -fr %{buildroot}%{_libdir}{,/dri-freeworld}/libVkLayer_MESA_device_select.so
rm -fr %{buildroot}%{_includedir}/GLES*
rm -fr %{buildroot}%{_libdir}/dri-freeworld/libGLES*
rm -fr %{buildroot}%{_prefix}/lib%{_libdir}/dri-freeworld/libGLES*

# ld.so.conf.d file
install -m 0755 -d  %{buildroot}%{_sysconfdir}/ld.so.conf.d/
echo -e "%{_libdir}/dri-freeworld/ \n" > %{buildroot}%{_sysconfdir}/ld.so.conf.d/%{name}-%{_lib}.conf

%if 0%{?with_va}
%post -n %{srcname}-va-drivers-freeworld -p /sbin/ldconfig
%postun -n %{srcname}-va-drivers-freeworld -p /sbin/ldconfig
%files -n %{srcname}-va-drivers-freeworld
%config %{_sysconfdir}/ld.so.conf.d/%{name}-%{_lib}.conf
%{_libdir}/dri-freeworld/libgallium-%{ver}.so
%{_libdir}/dri-freeworld/nouveau_drv_video.so
%if 0%{?with_r600}
%{_libdir}/dri-freeworld/r600_drv_video.so
%endif
%if 0%{?with_radeonsi}
%{_libdir}/dri-freeworld/radeonsi_drv_video.so
%endif
%{_libdir}/dri-freeworld/virtio_gpu_drv_video.so
%{_metainfodir}/org.mesa3d.vaapi.freeworld.metainfo.xml
%license docs/license.rst
%endif

%if 0%{?with_vdpau}
%files -n %{srcname}-vdpau-drivers-freeworld
%{_libdir}/vdpau/libvdpau_nouveau.so.1*
%if 0%{?with_r600}
%{_libdir}/vdpau/libvdpau_r600.so.1*
%endif
%if 0%{?with_radeonsi}
%{_libdir}/vdpau/libvdpau_radeonsi.so.1*
%endif
%{_libdir}/vdpau/libvdpau_virtio_gpu.so.1*
%{_metainfodir}/org.mesa3d.vdpau.freeworld.metainfo.xml
%license docs/license.rst
%endif

%changelog
* Thu Feb 27 2025 Martin Naď - 25.1.0~devel-0.276~git14bc2e2d395
- update to git 14bc2e2d395

* Wed Feb 19 2025 Martin Naď - 25.1.0~devel-0.275~git6b20b065848
- update to git 6b20b065848

* Fri Feb 14 2025 Martin Naď - 25.1.0~devel-0.274~git94093f58fb9
- update to git 94093f58fb9

* Mon Feb 10 2025 Martin Naď - 25.1.0~devel-0.273~git3a1d79bdec6
- update to git 3a1d79bdec6

* Mon Nov 04 2024 Martin Naď - 24.3.0~devel-0.272~git94cf47e43f1
- update to git 94cf47e43f1

* Sat Oct 26 2024 Martin Naď - 24.3.0~devel-0.271~git13db5fad278
- update to git 13db5fad278

* Wed Oct 16 2024 Martin Naď - 24.3.0~devel-0.270~gitad13268c728
- update to git ad13268c728

* Sun Oct 13 2024 Martin Naď - 24.3.0~devel-0.269~gitc7b126f6273
- update to git c7b126f6273

* Mon Oct 07 2024 Martin Naď - 24.3.0~devel-0.268~git5bd825a4b8f
- update to git 5bd825a4b8f

* Wed Oct 02 2024 Martin Naď - 24.3.0~devel-0.267~gitb7c66f4f890
- update to git b7c66f4f890

* Mon Sep 30 2024 Martin Naď - 24.3.0~devel-0.266~git5fdc82d5f11
- update to git 5fdc82d5f11

* Sun Sep 29 2024 Martin Naď - 24.3.0~devel-0.265~gitcd40732b25c
- update to git cd40732b25c

* Fri Sep 27 2024 Martin Naď - 24.3.0~devel-0.264~git246051ebc68
- update to git 246051ebc68

* Wed Sep 25 2024 Martin Naď - 24.3.0~devel-0.263~git2193d87277c
- rebuilt

* Wed Sep 25 2024 Martin Naď - 24.3.0~devel-0.262~git2193d87277c
- update to git 2193d87277c

* Tue Sep 24 2024 Martin Naď - 24.2.3-0.261~gitdb3da0d302a
- update to git db3da0d302a

* Tue Sep 24 2024 Martin Naď - 23.2.1-0.260~git94ed71dad16
- update to git 94ed71dad16

* Sun Sep 22 2024 Martin Naď
- update to git ebd2c2e668e