From 19f9ba10ad746c8100a55a9db4a31768316f5246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 28 Sep 2023 10:22:00 +0200 Subject: [PATCH] CMake: set a CMake policy instead of setting OpenGL_GL_PREFERENCE. The policy makes it work for all find_package(OpenGL) calls, possibly even in subprojects, without having to copypaste the same OpenGL_GL_PREFERENCE override code to each. --- CMakeLists.txt | 13 ++++++------- modules/FindMagnum.cmake | 20 ++++++++++++-------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 68d516da2..9298ae150 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,12 @@ endif() if(POLICY CMP0144) cmake_policy(SET CMP0144 NEW) endif() +# Prefer GLVND when finding OpenGL. If this causes problems (known to fail with +# NVidia drivers in Debian Buster, reported on 2019-04-09), users can override +# this by setting OpenGL_GL_PREFERENCE to LEGACY. +if(POLICY CMP0072) + cmake_policy(SET CMP0072 NEW) +endif() # Superprojects can use just set(MAGNUM_WITH_BLAH ON) without FORCE CACHE on # 3.13+ if(POLICY CMP0077) @@ -563,13 +569,6 @@ endif() # Check dependencies if(MAGNUM_WITH_GL) if(NOT MAGNUM_TARGET_GLES OR (MAGNUM_TARGET_GLES AND NOT MAGNUM_TARGET_EGL AND NOT CORRADE_TARGET_IOS)) - # OpenGL library preference. Prefer to use GLVND, since that's the - # better approach nowadays, but allow the users to override it from - # outside in case it is broken for some reason (Nvidia drivers in - # Debian's testing (Buster) -- reported on 2019-04-09). - if(NOT CMAKE_VERSION VERSION_LESS 3.10 AND NOT OpenGL_GL_PREFERENCE) - set(OpenGL_GL_PREFERENCE GLVND) - endif() find_package(OpenGL REQUIRED) elseif(MAGNUM_TARGET_GLES2) find_package(OpenGLES2 REQUIRED) diff --git a/modules/FindMagnum.cmake b/modules/FindMagnum.cmake index 756e94ebf..afd12518c 100644 --- a/modules/FindMagnum.cmake +++ b/modules/FindMagnum.cmake @@ -227,6 +227,15 @@ # DEALINGS IN THE SOFTWARE. # +# CMake policies used by FindMagnum are popped again at the end. +cmake_policy(PUSH) +# Prefer GLVND when finding OpenGL. If this causes problems (known to fail with +# NVidia drivers in Debian Buster, reported on 2019-04-09), users can override +# this by setting OpenGL_GL_PREFERENCE to LEGACY. +if(POLICY CMP0072) + cmake_policy(SET CMP0072 NEW) +endif() + # Corrade library dependencies set(_MAGNUM_CORRADE_DEPENDENCIES ) foreach(_magnum_component ${Magnum_FIND_COMPONENTS}) @@ -309,14 +318,6 @@ if(MAGNUM_BUILD_DEPRECATED) endif() endif() -# OpenGL library preference. Prefer to use GLVND, since that's the better -# approach nowadays, but allow the users to override it from outside in case -# it is broken for some reason (Nvidia drivers in Debian's testing (Buster) -- -# reported on 2019-04-09). -if(NOT CMAKE_VERSION VERSION_LESS 3.10 AND NOT OpenGL_GL_PREFERENCE) - set(OpenGL_GL_PREFERENCE GLVND) -endif() - # Base Magnum library if(NOT TARGET Magnum::Magnum) add_library(Magnum::Magnum UNKNOWN IMPORTED) @@ -1292,3 +1293,6 @@ if(MAGNUM_PLUGINS_RELEASE_DIR) set(MAGNUM_PLUGINS_SCENECONVERTER_RELEASE_DIR ${MAGNUM_PLUGINS_RELEASE_DIR}/sceneconverters) set(MAGNUM_PLUGINS_AUDIOIMPORTER_RELEASE_DIR ${MAGNUM_PLUGINS_RELEASE_DIR}/audioimporters) endif() + +# Resets CMake policies set at the top of the file to not affect other code. +cmake_policy(POP)