From f922f1e740c7966e185846b127390091240e3246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 16 Jun 2018 23:06:54 +0200 Subject: [PATCH] CMake: prefer GLVND if on CMake 3.11. This *did not* make things simpler. At all. Ugh. Can I follow Apple and deprecate GL too? --- CMakeLists.txt | 1 + modules/FindMagnum.cmake | 80 +++++++++++++++++++++++++++++- src/Magnum/GL/CMakeLists.txt | 20 +++++++- src/Magnum/Platform/CMakeLists.txt | 42 +++++++++++++--- 4 files changed, 133 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f593e64f..b017dbf37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -214,6 +214,7 @@ endif() # Check dependencies if(NOT TARGET_GLES OR TARGET_DESKTOP_GLES) + set(OpenGL_GL_PREFERENCE GLVND) # since CMake 3.11 find_package(OpenGL REQUIRED) elseif(TARGET_GLES2) find_package(OpenGLES2 REQUIRED) diff --git a/modules/FindMagnum.cmake b/modules/FindMagnum.cmake index a6dfb1d37..63c647d2e 100644 --- a/modules/FindMagnum.cmake +++ b/modules/FindMagnum.cmake @@ -593,6 +593,27 @@ foreach(_component ${Magnum_FIND_COMPONENTS}) set_property(TARGET Magnum::${_component} APPEND PROPERTY INTERFACE_LINK_LIBRARIES GLFW::GLFW) + # With GLVND (since CMake 3.11) we need to explicitly link to + # GLX/EGL because libOpenGL doesn't provide it. For EGL we have + # our own EGL find module, which makes things simpler. The + # upstream FindOpenGL is anything but simple. Also can't use + # OpenGL_OpenGL_FOUND, because that one is set also if GLVND is + # *not* found. WTF. + if(MAGNUM_TARGET_GL) + if(CORRADE_TARGET_UNIX AND NOT CORRADE_TARGET_APPLE AND (NOT MAGNUM_TARGET_GLES OR MAGNUM_TARGET_DESKTOP_GLES)) + set(OpenGL_GL_PREFERENCE GLVND) + find_package(OpenGL) + if(OPENGL_opengl_LIBRARY) + set_property(TARGET Magnum::${_component} APPEND + PROPERTY INTERFACE_LINK_LIBRARIES OpenGL::GLX) + endif() + elseif(MAGNUM_TARGET_GLES AND NOT MAGNUM_TARGET_DESKTOP_GLES AND NOT CORRADE_TARGET_EMSCRIPTEN) + find_package(EGL) + set_property(TARGET Magnum::${_component} APPEND + PROPERTY INTERFACE_LINK_LIBRARIES EGL::EGL) + endif() + endif() + # GLUT application dependencies elseif(_component STREQUAL GlutApplication) find_package(GLUT) @@ -601,12 +622,45 @@ foreach(_component ${Magnum_FIND_COMPONENTS}) set_property(TARGET Magnum::${_component} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${GLUT_glut_LIBRARY}) + # With GLVND (since CMake 3.11) we need to explicitly link to + # GLX because libOpenGL doesn't provide it. Also can't use + # OpenGL_OpenGL_FOUND, because that one is set also if GLVND is + # *not* found. WTF. I don't think GLUT works with EGL, so not + # handling that. + set(OpenGL_GL_PREFERENCE GLVND) + find_package(OpenGL) + if(OPENGL_opengl_LIBRARY) + set_property(TARGET Magnum::${_component} APPEND PROPERTY + INTERFACE_LINK_LIBRARIES OpenGL::GLX) + endif() + # SDL2 application dependencies elseif(_component STREQUAL Sdl2Application) find_package(SDL2) set_property(TARGET Magnum::${_component} APPEND PROPERTY INTERFACE_LINK_LIBRARIES SDL2::SDL2) + # With GLVND (since CMake 3.11) we need to explicitly link to + # GLX/EGL because libOpenGL doesn't provide it. For EGL we have + # our own EGL find module, which makes things simpler. The + # upstream FindOpenGL is anything but simple. Also can't use + # OpenGL_OpenGL_FOUND, because that one is set also if GLVND is + # *not* found. WTF. + if(MAGNUM_TARGET_GL) + if(CORRADE_TARGET_UNIX AND NOT CORRADE_TARGET_APPLE AND (NOT MAGNUM_TARGET_GLES OR MAGNUM_TARGET_DESKTOP_GLES)) + set(OpenGL_GL_PREFERENCE GLVND) + find_package(OpenGL) + if(OPENGL_opengl_LIBRARY) + set_property(TARGET Magnum::${_component} APPEND + PROPERTY INTERFACE_LINK_LIBRARIES OpenGL::GLX) + endif() + elseif(MAGNUM_TARGET_GLES AND NOT MAGNUM_TARGET_DESKTOP_GLES AND NOT CORRADE_TARGET_EMSCRIPTEN) + find_package(EGL) + set_property(TARGET Magnum::${_component} APPEND + PROPERTY INTERFACE_LINK_LIBRARIES EGL::EGL) + endif() + endif() + # (Windowless) GLX application dependencies elseif(_component STREQUAL GlxApplication OR _component STREQUAL WindowlessGlxApplication) find_package(X11) @@ -615,6 +669,17 @@ foreach(_component ${Magnum_FIND_COMPONENTS}) set_property(TARGET Magnum::${_component} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${X11_LIBRARIES}) + # With GLVND (since CMake 3.11) we need to explicitly link to + # GLX because libOpenGL doesn't provide it. Also can't use + # OpenGL_OpenGL_FOUND, because that one is set also if GLVND is + # *not* found. WTF. + set(OpenGL_GL_PREFERENCE GLVND) + find_package(OpenGL) + if(OPENGL_opengl_LIBRARY) + set_property(TARGET Magnum::${_component} APPEND PROPERTY + INTERFACE_LINK_LIBRARIES OpenGL::GLX) + endif() + # Windowless CGL application has no additional dependencies # Windowless EGL application dependencies @@ -689,9 +754,20 @@ foreach(_component ${Magnum_FIND_COMPONENTS}) INTERFACE_INCLUDE_DIRECTORIES ${MAGNUM_INCLUDE_DIR}/MagnumExternal/OpenGL) if(NOT MAGNUM_TARGET_GLES OR MAGNUM_TARGET_DESKTOP_GLES) + # If the GLVND library (CMake 3.11+) was found, link to the + # imported target. Otherwise (and also on all systems except + # Linux) link to the classic libGL. Can't use + # OpenGL_OpenGL_FOUND, because that one is set also if GLVND is + # *not* found. WTF. + set(OpenGL_GL_PREFERENCE GLVND) find_package(OpenGL REQUIRED) - set_property(TARGET Magnum::${_component} APPEND PROPERTY - INTERFACE_LINK_LIBRARIES ${OPENGL_gl_LIBRARY}) + if(OPENGL_opengl_LIBRARY) + set_property(TARGET Magnum::${_component} APPEND PROPERTY + INTERFACE_LINK_LIBRARIES OpenGL::OpenGL) + else() + set_property(TARGET Magnum::${_component} APPEND PROPERTY + INTERFACE_LINK_LIBRARIES ${OPENGL_gl_LIBRARY}) + endif() elseif(MAGNUM_TARGET_GLES2) find_package(OpenGLES2 REQUIRED) set_property(TARGET Magnum::${_component} APPEND PROPERTY diff --git a/src/Magnum/GL/CMakeLists.txt b/src/Magnum/GL/CMakeLists.txt index bff17f6a3..aa1c32b71 100644 --- a/src/Magnum/GL/CMakeLists.txt +++ b/src/Magnum/GL/CMakeLists.txt @@ -217,7 +217,15 @@ target_include_directories(MagnumGL PUBLIC ${PROJECT_SOURCE_DIR}/src/MagnumExternal/OpenGL) target_link_libraries(MagnumGL PUBLIC Magnum) if(NOT TARGET_GLES OR TARGET_DESKTOP_GLES) - target_link_libraries(MagnumGL PUBLIC ${OPENGL_gl_LIBRARY}) + # If the GLVND library (CMake 3.11+) was found, link to the imported + # target. Otherwise (and also on all systems except Linux) link to the + # classic libGL. Can't use OpenGL_OpenGL_FOUND, because that one is set + # also if GLVND is *not* found. WTF. + if(OPENGL_opengl_LIBRARY) + target_link_libraries(MagnumGL PUBLIC OpenGL::OpenGL) + else() + target_link_libraries(MagnumGL PUBLIC ${OPENGL_gl_LIBRARY}) + endif() elseif(TARGET_GLES2) target_link_libraries(MagnumGL PUBLIC OpenGLES2::OpenGLES2) else() @@ -286,7 +294,15 @@ if(BUILD_TESTS) target_link_libraries(MagnumGLTestLib PUBLIC Magnum) if(NOT TARGET_GLES OR TARGET_DESKTOP_GLES) - target_link_libraries(MagnumGLTestLib PUBLIC ${OPENGL_gl_LIBRARY}) + # If the GLVND library (CMake 3.11+) was found, link to the imported + # target. Otherwise (and also on all systems except Linux) link to the + # classic libGL. Can't use OpenGL_OpenGL_FOUND, because that one is set + # also if GLVND is *not* found. WTF. + if(OPENGL_opengl_LIBRARY) + target_link_libraries(MagnumGLTestLib PUBLIC OpenGL::OpenGL) + else() + target_link_libraries(MagnumGLTestLib PUBLIC ${OPENGL_gl_LIBRARY}) + endif() elseif(TARGET_GLES2) target_link_libraries(MagnumGLTestLib PUBLIC OpenGLES2::OpenGLES2) else() diff --git a/src/Magnum/Platform/CMakeLists.txt b/src/Magnum/Platform/CMakeLists.txt index 90d628952..79a0a8e44 100644 --- a/src/Magnum/Platform/CMakeLists.txt +++ b/src/Magnum/Platform/CMakeLists.txt @@ -51,9 +51,18 @@ if((WITH_GLFWAPPLICATION OR WITH_GLUTAPPLICATION OR WITH_SDL2APPLICATION) AND TA elseif(CORRADE_TARGET_UNIX AND (NOT MAGNUM_TARGET_GLES OR MAGNUM_TARGET_DESKTOP_GLES)) set(NEED_GLXCONTEXT 1) set(MagnumSomeContext_OBJECTS $) + # If the GLVND library (CMake 3.11+) was found and linked to, we need + # to link to GLX explicitly. Otherwise (and also on all systems except + # Linux) the transitive dependency to classic GL lib from MagnumGL is + # enough. Can't use OpenGL_OpenGL_FOUND, because that one is set also + # if GLVND is *not* found. WTF. + if(OPENGL_opengl_LIBRARY) + set(MagnumSomeContext_LIBRARY OpenGL::GLX) + endif() elseif(MAGNUM_TARGET_GLES AND NOT CORRADE_TARGET_EMSCRIPTEN) set(NEED_EGLCONTEXT 1) set(MagnumSomeContext_OBJECTS $) + # We're linking to EGL explicitly, no need to bother with GLVND there endif() endif() @@ -127,7 +136,10 @@ if(WITH_GLFWAPPLICATION) # linked to the executable and not to any intermediate shared lib target_link_libraries(MagnumGlfwApplication PUBLIC Magnum GLFW::GLFW) if(TARGET_GL) - target_link_libraries(MagnumGlfwApplication PUBLIC MagnumGL) + target_link_libraries(MagnumGlfwApplication PUBLIC + MagnumGL + # need to link to GLX explicitly if using GLVND (CMake 3.11+) + ${MagnumSomeContext_LIBRARY}) endif() install(FILES ${MagnumGlfwApplication_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/Platform) @@ -161,7 +173,11 @@ if(WITH_GLUTAPPLICATION) # Assuming that PIC is not needed because the Application lib is always # linked to the executable and not to any intermediate shared lib target_include_directories(MagnumGlutApplication PUBLIC ${GLUT_INCLUDE_DIR}) - target_link_libraries(MagnumGlutApplication PUBLIC MagnumGL ${GLUT_glut_LIBRARY}) + target_link_libraries(MagnumGlutApplication PUBLIC + MagnumGL + ${GLUT_glut_LIBRARY} + # need to link to GLX explicitly if using GLVND (CMake 3.11+) + ${MagnumSomeContext_LIBRARY}) install(FILES ${MagnumGlutApplication_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/Platform) install(TARGETS MagnumGlutApplication @@ -196,7 +212,10 @@ if(WITH_SDL2APPLICATION) # linked to the executable and not to any intermediate shared lib target_link_libraries(MagnumSdl2Application PUBLIC Magnum SDL2::SDL2) if(TARGET_GL) - target_link_libraries(MagnumSdl2Application PUBLIC MagnumGL) + target_link_libraries(MagnumSdl2Application PUBLIC + MagnumGL + # need to link to GLX explicitly if using GLVND (CMake 3.11+) + ${MagnumSomeContext_LIBRARY}) endif() install(FILES ${MagnumSdl2Application_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/Platform) @@ -252,7 +271,11 @@ if(WITH_GLXAPPLICATION) # Assuming that PIC is not needed because the Application lib is always # linked to the executable and not to any intermediate shared lib target_include_directories(MagnumGlxApplication PUBLIC ${X11_INCLUDE_DIR}) - target_link_libraries(MagnumGlxApplication PUBLIC MagnumGL ${X11_LIBRARIES}) + target_link_libraries(MagnumGlxApplication PUBLIC + MagnumGL + ${X11_LIBRARIES} + # need to link to GLX explicitly if using GLVND (CMake 3.11+) + ${MagnumSomeContext_LIBRARY}) install(FILES ${MagnumGlxApplication_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/Platform) install(TARGETS MagnumGlxApplication @@ -368,7 +391,11 @@ if(WITH_WINDOWLESSGLXAPPLICATION) set_target_properties(MagnumWindowlessGlxApplication PROPERTIES POSITION_INDEPENDENT_CODE ON) endif() target_include_directories(MagnumWindowlessGlxApplication PUBLIC ${X11_INCLUDE_DIR}) - target_link_libraries(MagnumWindowlessGlxApplication PUBLIC MagnumGL ${X11_LIBRARIES}) + target_link_libraries(MagnumWindowlessGlxApplication PUBLIC + MagnumGL + ${X11_LIBRARIES} + # need to link to GLX explicitly if using GLVND (CMake 3.11+) + ${MagnumSomeContext_LIBRARY}) install(FILES ${MagnumWindowlessGlxApplication_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/Platform) install(TARGETS MagnumWindowlessGlxApplication @@ -701,7 +728,10 @@ if(NEED_GLXCONTEXT OR WITH_GLXCONTEXT) set_target_properties(MagnumGlxContext PROPERTIES POSITION_INDEPENDENT_CODE ON) endif() target_include_directories(MagnumGlxContext PUBLIC ${X11_INCLUDE_DIR}) - target_link_libraries(MagnumGlxContext PUBLIC MagnumGL ${X11_LIBRARIES}) + target_link_libraries(MagnumGlxContext PUBLIC + MagnumGL + ${X11_LIBRARIES} + ${MagnumSomeContext_LIBRARY}) install(TARGETS MagnumGlxContext RUNTIME DESTINATION ${MAGNUM_BINARY_INSTALL_DIR}