[GNU/Linux] [Debian 12] CMake fails to find OpenGL when MESA has been built without GLX support

Hello there!

CMake (version included in Debian 12 and also latest stable cmake version 3.29.3) fails to find OpenGL when doing find_package(OpenGL REQUIRED) if MESA is built without GLX support:
This is the error I get:

CMake Error at /usr/local/lib/python3.11/dist-packages/cmake/data/share/cmake-3.29/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find OpenGL (missing: OPENGL_glx_LIBRARY)
Call Stack (most recent call first):
/usr/local/lib/python3.11/dist-packages/cmake/data/share/cmake-3.29/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
/usr/local/lib/python3.11/dist-packages/cmake/data/share/cmake-3.29/Modules/FindOpenGL.cmake:579 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
cmake/ncine_imported_targets.cmake:210 (find_package)
CMakeLists.txt:73 (include)

According to this doc, CMake should find the GLVND OpenGL implementation without any problems using find_package(OpenGL REQUIRED)https://cmake.org/cmake/help/latest/policy/CMP0072.html

This is how my MESA build is configured:

meson setup -Dbuildtype=release -Dglx=disabled -Dplatforms=wayland \
-Dvulkan-drivers=intel -Dgallium-drivers=iris,kmsro,zink -Dglvnd=enabled ..

GLVND is in fact found by CMake if I do:

FIND_LIBRARY(OpenGL_LIBRARY OpenGL)
SET(EXTRA_LIBS ${OpenGL_LIBRARY})

…and CMake also links properly against GLVND OpenGL implementation if I do this:

target_link_libraries(${NCINE_APP} PUBLIC ${OpenGL_LIBRARY})

Any idea on WHY find_package(OpenGL REQUIRED) fails???

Many, many projects I use tend to do that, and I have to manually modify their CMake files and hack-in a solution, but I am almost sure this is on CMake.

Also, it seems that doing this works:

set(OPENGL_USE_OPENGL ON)
find_package(OpenGL REQUIRED)

It seems to be because of this:

…but that contradicts CMake docs in that set(OPENGL_USE_OPENGL ON) shouldn’t be needed when GLVND is installed because GLVND whould be preferred instead of legacy GLX.