|
| 1 | +# Copyright (C) 2022 The Qt Company Ltd. |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +# We can't create the same interface imported target multiple times, CMake will complain if we do |
| 5 | +# that. This can happen if the find_package call is done in multiple different subdirectories. |
| 6 | +if(TARGET WrapOpenGL::WrapOpenGL) |
| 7 | + set(WrapOpenGL_FOUND ON) |
| 8 | + return() |
| 9 | +endif() |
| 10 | + |
| 11 | +set(WrapOpenGL_FOUND OFF) |
| 12 | + |
| 13 | +find_package(OpenGL ${WrapOpenGL_FIND_VERSION}) |
| 14 | + |
| 15 | +if (OpenGL_FOUND) |
| 16 | + set(WrapOpenGL_FOUND ON) |
| 17 | + |
| 18 | + add_library(WrapOpenGL::WrapOpenGL INTERFACE IMPORTED) |
| 19 | + if(APPLE) |
| 20 | + # CMake 3.27 and older: |
| 21 | + # On Darwin platforms FindOpenGL sets IMPORTED_LOCATION to the absolute path of the library |
| 22 | + # within the framework. This ends up as an absolute path link flag, which we don't want, |
| 23 | + # because that makes our .prl files un-relocatable. |
| 24 | + # Extract the framework path instead, and use that in INTERFACE_LINK_LIBRARIES, |
| 25 | + # which CMake ends up transforming into a relocatable -framework flag. |
| 26 | + # See https://gitlab.kitware.com/cmake/cmake/-/issues/20871 for details. |
| 27 | + # |
| 28 | + # CMake 3.28 and above: |
| 29 | + # IMPORTED_LOCATION is the absolute path the the OpenGL.framework folder. |
| 30 | + get_target_property(__opengl_fw_lib_path OpenGL::GL IMPORTED_LOCATION) |
| 31 | + if(__opengl_fw_lib_path AND NOT __opengl_fw_lib_path MATCHES "/([^/]+)\\.framework$") |
| 32 | + get_filename_component(__opengl_fw_path "${__opengl_fw_lib_path}" DIRECTORY) |
| 33 | + endif() |
| 34 | + |
| 35 | + if(NOT __opengl_fw_path) |
| 36 | + # Just a safety measure in case if no OpenGL::GL target exists. |
| 37 | + set(__opengl_fw_path "-framework OpenGL") |
| 38 | + endif() |
| 39 | + |
| 40 | + target_link_libraries(WrapOpenGL::WrapOpenGL INTERFACE ${__opengl_fw_path}) |
| 41 | + else() |
| 42 | + target_link_libraries(WrapOpenGL::WrapOpenGL INTERFACE OpenGL::GL) |
| 43 | + endif() |
| 44 | +elseif(UNIX AND NOT APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "Integrity") |
| 45 | + # Requesting only the OpenGL component ensures CMake does not mark the package as |
| 46 | + # not found if neither GLX nor libGL are available. This allows finding OpenGL |
| 47 | + # on an X11-less Linux system. |
| 48 | + find_package(OpenGL ${WrapOpenGL_FIND_VERSION} COMPONENTS OpenGL) |
| 49 | + if (OpenGL_FOUND) |
| 50 | + set(WrapOpenGL_FOUND ON) |
| 51 | + add_library(WrapOpenGL::WrapOpenGL INTERFACE IMPORTED) |
| 52 | + target_link_libraries(WrapOpenGL::WrapOpenGL INTERFACE OpenGL::OpenGL) |
| 53 | + endif() |
| 54 | +endif() |
| 55 | + |
| 56 | +include(FindPackageHandleStandardArgs) |
| 57 | +find_package_handle_standard_args(WrapOpenGL DEFAULT_MSG WrapOpenGL_FOUND) |
0 commit comments