1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 FindModule for OpenGL and OpenGL Utility Library (GLU).
10 .. versionchanged:: 3.2
11 X11 is no longer added as a dependency on Unix/Linux systems.
13 .. versionadded:: 3.10
14 GLVND support on Linux. See the :ref:`Linux Specific` section below.
19 .. versionadded:: 3.10
21 This module respects several optional COMPONENTS:
24 The EGL interface between OpenGL, OpenGL ES and the underlying windowing system.
27 An extension to X that interfaces OpenGL, OpenGL ES with X window system.
30 The cross platform API for 3D graphics.
33 .. versionadded:: 3.27
35 A subset of OpenGL API for embedded systems with limited capabilities.
38 .. versionadded:: 3.27
40 A subset of OpenGL API for embedded systems with more capabilities.
47 This module defines the :prop_tgt:`IMPORTED` targets:
50 Defined to the platform-specific OpenGL libraries if the system has OpenGL.
52 Defined if the system has OpenGL Utility Library (GLU).
54 .. versionadded:: 3.10
55 Additionally, the following GLVND-specific library targets are defined:
58 Defined to libOpenGL if the system is GLVND-based.
60 Defined if the system has OpenGL Extension to the X Window System (GLX).
62 Defined if the system has EGL.
64 .. versionadded:: 3.27
66 Defined if the system has GLES2.
68 .. versionadded:: 3.27
70 Defined if the system has GLES3.
75 This module sets the following variables:
78 True, if the system has OpenGL and all components are found.
79 ``OPENGL_XMESA_FOUND``
80 True, if the system has XMESA.
82 True, if the system has GLU.
83 ``OpenGL_OpenGL_FOUND``
84 True, if the system has an OpenGL library.
86 True, if the system has GLX.
88 True, if the system has EGL.
90 Defined if the system has GLES2.
92 Defined if the system has GLES3.
93 ``OPENGL_INCLUDE_DIR``
94 Path to the OpenGL include directory.
95 The ``OPENGL_INCLUDE_DIRS`` variable is preferred.
96 ``OPENGL_EGL_INCLUDE_DIRS``
97 Path to the EGL include directory.
99 Paths to the OpenGL library, windowing system libraries, and GLU libraries.
100 On Linux, this assumes GLX and is never correct for EGL-based targets.
101 Clients are encouraged to use the ``OpenGL::*`` import targets instead.
102 ``OPENGL_INCLUDE_DIRS``
103 .. versionadded:: 3.29
105 Paths to the OpenGL include directories.
107 .. versionadded:: 3.10
108 Variables for GLVND-specific libraries ``OpenGL``, ``EGL`` and ``GLX``.
113 The following cache variables may also be set:
115 ``OPENGL_egl_LIBRARY``
116 Path to the EGL library.
117 ``OPENGL_glu_LIBRARY``
118 Path to the GLU library.
119 ``OPENGL_glx_LIBRARY``
120 Path to the GLVND 'GLX' library.
121 ``OPENGL_opengl_LIBRARY``
122 Path to the GLVND 'OpenGL' library
123 ``OPENGL_gl_LIBRARY``
124 Path to the OpenGL library. New code should prefer the ``OpenGL::*`` import
126 ``OPENGL_gles2_LIBRARY``
127 .. versionadded:: 3.27
129 Path to the OpenGL GLES2 library.
130 ``OPENGL_gles3_LIBRARY``
131 .. versionadded:: 3.27
133 Path to the OpenGL GLES3 library.
135 ``OPENGL_GLU_INCLUDE_DIR``
136 .. versionadded:: 3.29
138 Path to the OpenGL GLU include directory.
140 .. versionadded:: 3.10
141 Variables for GLVND-specific libraries ``OpenGL``, ``EGL`` and ``GLX``.
143 .. _`Linux Specific`:
148 Some Linux systems utilize GLVND as a new ABI for OpenGL. GLVND separates
149 context libraries from OpenGL itself; OpenGL lives in "libOpenGL", and
150 contexts are defined in "libGLX" or "libEGL". GLVND is currently the only way
151 to get OpenGL 3+ functionality via EGL in a manner portable across vendors.
152 Projects may use GLVND explicitly with target ``OpenGL::OpenGL`` and either
153 ``OpenGL::GLX`` or ``OpenGL::EGL``.
155 Projects may use the ``OpenGL::GL`` target (or ``OPENGL_LIBRARIES`` variable)
156 to use legacy GL interfaces. These will use the legacy GL library located
157 by ``OPENGL_gl_LIBRARY``, if available. If ``OPENGL_gl_LIBRARY`` is empty or
158 not found and GLVND is available, the ``OpenGL::GL`` target will use GLVND
159 ``OpenGL::OpenGL`` and ``OpenGL::GLX`` (and the ``OPENGL_LIBRARIES``
160 variable will use the corresponding libraries). Thus, for non-EGL-based
161 Linux targets, the ``OpenGL::GL`` target is most portable.
163 A ``OpenGL_GL_PREFERENCE`` variable may be set to specify the preferred way
164 to provide legacy GL interfaces in case multiple choices are available.
165 The value may be one of:
168 If the GLVND OpenGL and GLX libraries are available, prefer them.
169 This forces ``OPENGL_gl_LIBRARY`` to be empty.
171 .. versionchanged:: 3.11
172 This is the default, unless policy :policy:`CMP0072` is set to ``OLD``
173 and no components are requested (since components
174 correspond to GLVND libraries).
177 Prefer to use the legacy libGL library, if available.
179 For EGL targets the client must rely on GLVND support on the user's system.
180 Linking should use the ``OpenGL::OpenGL OpenGL::EGL`` targets. Using GLES*
181 libraries is theoretically possible in place of ``OpenGL::OpenGL``, but this
182 module does not currently support that; contributions welcome.
184 ``OPENGL_egl_LIBRARY`` and ``OPENGL_EGL_INCLUDE_DIRS`` are defined in the case of
185 GLVND. For non-GLVND Linux and other systems these are left undefined.
190 On macOS this module defaults to using the macOS-native framework
191 version of OpenGL. To use the X11 version of OpenGL on macOS, one
192 can disable searching of frameworks. For example:
194 .. code-block:: cmake
197 if(APPLE AND X11_FOUND)
198 set(CMAKE_FIND_FRAMEWORK NEVER)
200 unset(CMAKE_FIND_FRAMEWORK)
205 An end user building this project may need to point CMake at their
206 X11 installation, e.g., with ``-DOpenGL_ROOT=/opt/X11``.
208 #]=======================================================================]
210 set(_OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
212 # Provide OPENGL_USE_<C> variables for each component.
213 foreach(component ${OpenGL_FIND_COMPONENTS})
214 string(TOUPPER ${component} _COMPONENT)
215 set(OPENGL_USE_${_COMPONENT} 1)
218 set(_OpenGL_CACHE_VARS)
223 set (OPENGL_gl_LIBRARY import32 CACHE STRING "OpenGL library for win32")
224 set (OPENGL_glu_LIBRARY import32 CACHE STRING "GLU library for win32")
226 set (OPENGL_gl_LIBRARY opengl32 CACHE STRING "OpenGL library for win32")
227 set (OPENGL_glu_LIBRARY glu32 CACHE STRING "GLU library for win32")
230 list(APPEND _OpenGL_CACHE_VARS
235 # The OpenGL.framework provides both gl and glu in OpenGL
236 # XQuartz provides libgl and libglu
237 find_library(OPENGL_gl_LIBRARY NAMES OpenGL GL DOC
239 find_library(OPENGL_glu_LIBRARY NAMES OpenGL GLU DOC
240 "OpenGL GLU library")
241 find_path(OPENGL_INCLUDE_DIR NAMES OpenGL/gl.h GL/gl.h DOC
242 "Include for OpenGL")
243 find_path(OPENGL_GLU_INCLUDE_DIR NAMES OpenGL/glu.h GL/glu.h DOC
244 "Include for the OpenGL GLU library")
245 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
247 list(APPEND _OpenGL_CACHE_VARS
249 OPENGL_GLU_INCLUDE_DIR
254 if (CMAKE_ANDROID_NDK)
255 set(_OPENGL_INCLUDE_PATH ${CMAKE_ANDROID_NDK}/sysroot/usr/include)
256 set(_OPENGL_LIB_PATH ${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}/arch-${CMAKE_ANDROID_ARCH}/usr/lib)
257 elseif (CMAKE_SYSTEM_NAME MATCHES "HP-UX")
258 # Handle HP-UX cases where we only want to find OpenGL in either hpux64
259 # or hpux32 depending on if we're doing a 64 bit build.
260 if(CMAKE_SIZEOF_VOID_P EQUAL 4)
262 /opt/graphics/OpenGL/lib/hpux32/)
265 /opt/graphics/OpenGL/lib/hpux64/
266 /opt/graphics/OpenGL/lib/pa20_64)
268 elseif(CMAKE_SYSTEM_NAME STREQUAL Haiku)
270 /boot/develop/lib/x86)
271 set(_OPENGL_INCLUDE_PATH
272 /boot/develop/headers/os/opengl)
273 elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
274 # CMake doesn't support arbitrary globs in search paths.
275 file(GLOB _OPENGL_LIB_PATH
276 # The NVidia driver installation tool on Linux installs libraries to a
277 # `nvidia-<version>` subdirectory.
279 "/usr/lib32/nvidia-*")
282 # The first line below is to make sure that the proper headers
283 # are used on a Linux machine with the NVidia drivers installed.
284 # They replace Mesa with NVidia's own library but normally do not
285 # install headers and that causes the linking to
286 # fail since the compiler finds the Mesa headers but NVidia's library.
287 # Make sure the NVIDIA directory comes BEFORE the others.
288 # - Atanas Georgiev <atanas@cs.columbia.edu>
289 find_path(OPENGL_INCLUDE_DIR GL/gl.h
290 /usr/share/doc/NVIDIA_GLX-1.0/include
291 /usr/openwin/share/include
292 /opt/graphics/OpenGL/include
293 ${_OPENGL_INCLUDE_PATH}
295 find_path(OPENGL_GLX_INCLUDE_DIR GL/glx.h ${_OPENGL_INCLUDE_PATH})
296 find_path(OPENGL_EGL_INCLUDE_DIR EGL/egl.h ${_OPENGL_INCLUDE_PATH})
297 find_path(OPENGL_GLES2_INCLUDE_DIR GLES2/gl2.h ${_OPENGL_INCLUDE_PATH})
298 find_path(OPENGL_GLES3_INCLUDE_DIR GLES3/gl3.h ${_OPENGL_INCLUDE_PATH})
299 find_path(OPENGL_xmesa_INCLUDE_DIR GL/xmesa.h
300 /usr/share/doc/NVIDIA_GLX-1.0/include
301 /usr/openwin/share/include
302 /opt/graphics/OpenGL/include
305 find_path(OPENGL_GLU_INCLUDE_DIR GL/glu.h ${_OPENGL_INCLUDE_PATH})
307 list(APPEND _OpenGL_CACHE_VARS
309 OPENGL_GLX_INCLUDE_DIR
310 OPENGL_EGL_INCLUDE_DIR
311 OPENGL_GLES2_INCLUDE_DIR
312 OPENGL_GLES3_INCLUDE_DIR
313 OPENGL_xmesa_INCLUDE_DIR
314 OPENGL_GLU_INCLUDE_DIR
317 # Search for the GLVND libraries. We do this regardless of COMPONENTS; we'll
318 # take into account the COMPONENTS logic later.
319 find_library(OPENGL_opengl_LIBRARY
321 PATHS ${_OPENGL_LIB_PATH}
324 find_library(OPENGL_glx_LIBRARY
326 PATHS ${_OPENGL_LIB_PATH}
327 PATH_SUFFIXES libglvnd
330 find_library(OPENGL_egl_LIBRARY
332 PATHS ${_OPENGL_LIB_PATH}
333 PATH_SUFFIXES libglvnd
336 find_library(OPENGL_gles2_LIBRARY
338 PATHS ${_OPENGL_LIB_PATH}
341 find_library(OPENGL_gles3_LIBRARY
343 GLESv2 # mesa provides only libGLESv2
344 PATHS ${_OPENGL_LIB_PATH}
347 find_library(OPENGL_glu_LIBRARY
349 PATHS ${OPENGL_gl_LIBRARY}
350 /opt/graphics/OpenGL/lib
355 list(APPEND _OpenGL_CACHE_VARS
356 OPENGL_opengl_LIBRARY
364 set(_OpenGL_GL_POLICY_WARN 0)
365 if(NOT DEFINED OpenGL_GL_PREFERENCE)
366 set(OpenGL_GL_PREFERENCE "")
368 if(NOT OpenGL_GL_PREFERENCE STREQUAL "")
369 # A preference has been explicitly specified.
370 if(NOT OpenGL_GL_PREFERENCE MATCHES "^(GLVND|LEGACY)$")
372 "OpenGL_GL_PREFERENCE value '${OpenGL_GL_PREFERENCE}' not recognized. "
373 "Allowed values are 'GLVND' and 'LEGACY'."
376 elseif(OpenGL_FIND_COMPONENTS)
377 # No preference was explicitly specified, but the caller did request
378 # at least one GLVND component. Prefer GLVND for legacy GL.
379 set(OpenGL_GL_PREFERENCE "GLVND")
381 # No preference was explicitly specified and no GLVND components were
382 # requested. Use a policy to choose the default.
383 cmake_policy(GET CMP0072 _OpenGL_GL_POLICY)
384 if("x${_OpenGL_GL_POLICY}x" STREQUAL "xNEWx")
385 set(OpenGL_GL_PREFERENCE "GLVND")
387 set(OpenGL_GL_PREFERENCE "LEGACY")
388 if("x${_OpenGL_GL_POLICY}x" STREQUAL "xx")
389 set(_OpenGL_GL_POLICY_WARN 1)
392 unset(_OpenGL_GL_POLICY)
395 if("x${OpenGL_GL_PREFERENCE}x" STREQUAL "xGLVNDx" AND OPENGL_opengl_LIBRARY AND OPENGL_glx_LIBRARY)
396 # We can provide legacy GL using GLVND libraries.
397 # Do not use any legacy GL library.
398 set(OPENGL_gl_LIBRARY "")
400 # We cannot provide legacy GL using GLVND libraries.
401 # Search for the legacy GL library.
402 find_library(OPENGL_gl_LIBRARY
404 PATHS /opt/graphics/OpenGL/lib
408 PATH_SUFFIXES libglvnd
410 list(APPEND _OpenGL_CACHE_VARS OPENGL_gl_LIBRARY)
413 if(_OpenGL_GL_POLICY_WARN AND OPENGL_gl_LIBRARY AND OPENGL_opengl_LIBRARY AND OPENGL_glx_LIBRARY)
414 cmake_policy(GET_WARNING CMP0072 _cmp0072_warning)
415 message(AUTHOR_WARNING
416 "${_cmp0072_warning}\n"
417 "FindOpenGL found both a legacy GL library:\n"
418 " OPENGL_gl_LIBRARY: ${OPENGL_gl_LIBRARY}\n"
419 "and GLVND libraries for OpenGL and GLX:\n"
420 " OPENGL_opengl_LIBRARY: ${OPENGL_opengl_LIBRARY}\n"
421 " OPENGL_glx_LIBRARY: ${OPENGL_glx_LIBRARY}\n"
422 "OpenGL_GL_PREFERENCE has not been set to \"GLVND\" or \"LEGACY\", so for "
423 "compatibility with CMake 3.10 and below the legacy GL library will be used."
426 unset(_OpenGL_GL_POLICY_WARN)
428 # FPHSA cannot handle "this OR that is required", so we conditionally set what
429 # it must look for. First clear any previous config we might have done:
430 set(_OpenGL_REQUIRED_VARS)
432 # now we append the libraries as appropriate. The complicated logic
433 # basically comes down to "use libOpenGL when we can, and add in specific
434 # context mechanisms when requested, or we need them to preserve the previous
435 # default where glx is always available."
436 if((NOT OPENGL_USE_EGL AND
437 NOT OPENGL_opengl_LIBRARY AND
438 OPENGL_glx_LIBRARY AND
439 NOT OPENGL_gl_LIBRARY) OR
440 (NOT OPENGL_USE_EGL AND
441 NOT OPENGL_USE_GLES3 AND
442 NOT OPENGL_USE_GLES2 AND
443 NOT OPENGL_glx_LIBRARY AND
444 NOT OPENGL_gl_LIBRARY) OR
445 (NOT OPENGL_USE_EGL AND
446 OPENGL_opengl_LIBRARY AND
447 OPENGL_glx_LIBRARY) OR
448 (NOT OPENGL_USE_GLES3 AND
449 NOT OPENGL_USE_GLES2 AND
451 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_opengl_LIBRARY)
454 # GLVND GLX library. Preferred when available.
455 if((NOT OPENGL_USE_OPENGL AND
456 NOT OPENGL_USE_GLX AND
457 NOT OPENGL_USE_EGL AND
458 NOT OPENGL_USE_GLES3 AND
459 NOT OPENGL_USE_GLES2 AND
460 NOT OPENGL_glx_LIBRARY AND
461 NOT OPENGL_gl_LIBRARY) OR
463 NOT OPENGL_USE_EGL AND
464 NOT OPENGL_USE_GLES3 AND
465 NOT OPENGL_USE_GLES2 AND
466 NOT OPENGL_glx_LIBRARY AND
467 NOT OPENGL_gl_LIBRARY) OR
468 (NOT OPENGL_USE_EGL AND
469 NOT OPENGL_USE_GLES3 AND
470 NOT OPENGL_USE_GLES2 AND
471 OPENGL_opengl_LIBRARY AND
472 OPENGL_glx_LIBRARY) OR
473 (OPENGL_USE_GLX AND OPENGL_USE_EGL))
474 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_glx_LIBRARY)
479 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_egl_LIBRARY)
482 # GLVND GLES2 library.
484 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_gles2_LIBRARY)
487 # GLVND GLES3 library.
489 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_gles3_LIBRARY)
492 # Old-style "libGL" library: used as a fallback when GLVND isn't available.
493 if((NOT OPENGL_USE_EGL AND
494 NOT OPENGL_opengl_LIBRARY AND
495 OPENGL_glx_LIBRARY AND
496 OPENGL_gl_LIBRARY) OR
497 (NOT OPENGL_USE_EGL AND
498 NOT OPENGL_glx_LIBRARY AND
500 list(PREPEND _OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
503 # We always need the 'gl.h' include dir.
505 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_EGL_INCLUDE_DIR)
507 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
510 unset(_OPENGL_INCLUDE_PATH)
511 unset(_OPENGL_LIB_PATH)
513 find_library(OPENGL_glu_LIBRARY
515 PATHS ${OPENGL_gl_LIBRARY}
516 /opt/graphics/OpenGL/lib
522 if(OPENGL_xmesa_INCLUDE_DIR)
523 set( OPENGL_XMESA_FOUND "YES" )
525 set( OPENGL_XMESA_FOUND "NO" )
528 if(OPENGL_glu_LIBRARY AND (WIN32 OR OPENGL_GLU_INCLUDE_DIR))
529 set( OPENGL_GLU_FOUND "YES" )
531 set( OPENGL_GLU_FOUND "NO" )
534 # OpenGL_OpenGL_FOUND is a bit unique in that it is okay if /either/ libOpenGL
536 # Using libGL with libEGL is never okay, though; we handle that case later.
537 if(NOT OPENGL_opengl_LIBRARY AND NOT OPENGL_gl_LIBRARY)
538 set(OpenGL_OpenGL_FOUND FALSE)
540 set(OpenGL_OpenGL_FOUND TRUE)
543 if(OPENGL_glx_LIBRARY AND OPENGL_GLX_INCLUDE_DIR)
544 set(OpenGL_GLX_FOUND TRUE)
546 set(OpenGL_GLX_FOUND FALSE)
549 if(OPENGL_egl_LIBRARY AND OPENGL_EGL_INCLUDE_DIR)
550 set(OpenGL_EGL_FOUND TRUE)
552 set(OpenGL_EGL_FOUND FALSE)
555 if(OPENGL_gles2_LIBRARY AND OPENGL_GLES2_INCLUDE_DIR)
556 set(OpenGL_GLES2_FOUND TRUE)
558 set(OpenGL_GLES2_FOUND FALSE)
561 if(OPENGL_gles3_LIBRARY AND OPENGL_GLES3_INCLUDE_DIR)
562 set(OpenGL_GLES3_FOUND TRUE)
564 set(OpenGL_GLES3_FOUND FALSE)
567 # User-visible names should be plural.
568 if(OPENGL_EGL_INCLUDE_DIR)
569 set(OPENGL_EGL_INCLUDE_DIRS ${OPENGL_EGL_INCLUDE_DIR})
572 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
573 if (CMAKE_FIND_PACKAGE_NAME STREQUAL "GLU")
574 # FindGLU include()'s this module. It's an old pattern, but rather than
575 # trying to suppress this from outside the module (which is then sensitive to
576 # the contents, detect the case in this module and suppress it explicitly.
577 set(FPHSA_NAME_MISMATCHED 1)
579 FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenGL REQUIRED_VARS ${_OpenGL_REQUIRED_VARS}
581 unset(FPHSA_NAME_MISMATCHED)
582 unset(_OpenGL_REQUIRED_VARS)
586 set(OPENGL_INCLUDE_DIRS ${OPENGL_INCLUDE_DIR})
588 # ::OpenGL is a GLVND library, and thus Linux-only: we don't bother checking
589 # for a framework version of this library.
590 if(OPENGL_opengl_LIBRARY AND NOT TARGET OpenGL::OpenGL)
591 if(IS_ABSOLUTE "${OPENGL_opengl_LIBRARY}")
592 add_library(OpenGL::OpenGL UNKNOWN IMPORTED)
593 set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LOCATION
594 "${OPENGL_opengl_LIBRARY}")
596 add_library(OpenGL::OpenGL INTERFACE IMPORTED)
597 set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LIBNAME
598 "${OPENGL_opengl_LIBRARY}")
600 set_target_properties(OpenGL::OpenGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
601 "${OPENGL_INCLUDE_DIR}")
602 set(_OpenGL_EGL_IMPL OpenGL::OpenGL)
605 # ::GLX is a GLVND library, and thus Linux-only: we don't bother checking
606 # for a framework version of this library.
607 if(OpenGL_GLX_FOUND AND NOT TARGET OpenGL::GLX AND TARGET OpenGL::OpenGL)
608 if(IS_ABSOLUTE "${OPENGL_glx_LIBRARY}")
609 add_library(OpenGL::GLX UNKNOWN IMPORTED)
610 set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LOCATION
611 "${OPENGL_glx_LIBRARY}")
613 add_library(OpenGL::GLX INTERFACE IMPORTED)
614 set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LIBNAME
615 "${OPENGL_glx_LIBRARY}")
617 set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_LINK_LIBRARIES
619 set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
620 "${OPENGL_GLX_INCLUDE_DIR}")
621 list(APPEND OPENGL_INCLUDE_DIRS ${OPENGL_GLX_INCLUDE_DIR})
624 # ::GLES2 is a GLVND library, and thus Linux-only: we don't bother checking
625 # for a framework version of this library.
626 if(OpenGL_GLES2_FOUND AND NOT TARGET OpenGL::GLES2)
629 if(NOT OPENGL_gles2_LIBRARY)
630 add_library(OpenGL::GLES2 INTERFACE IMPORTED)
632 if(IS_ABSOLUTE "${OPENGL_gles2_LIBRARY}")
633 add_library(OpenGL::GLES2 UNKNOWN IMPORTED)
634 set_target_properties(OpenGL::GLES2 PROPERTIES
635 IMPORTED_LOCATION "${OPENGL_gles2_LIBRARY}"
638 add_library(OpenGL::GLES2 INTERFACE IMPORTED)
639 set_target_properties(OpenGL::GLES2 PROPERTIES
640 IMPORTED_LIBNAME "${OPENGL_gles2_LIBRARY}"
645 # Attach target properties
646 set_target_properties(OpenGL::GLES2
648 INTERFACE_INCLUDE_DIRECTORIES
649 "${OPENGL_GLES2_INCLUDE_DIR}"
651 list(APPEND OPENGL_INCLUDE_DIRS ${OPENGL_GLES2_INCLUDE_DIR})
653 if (OPENGL_USE_GLES2)
654 set(_OpenGL_EGL_IMPL OpenGL::GLES2)
659 # ::GLES3 is a GLVND library, and thus Linux-only: we don't bother checking
660 # for a framework version of this library.
661 if(OpenGL_GLES3_FOUND AND NOT TARGET OpenGL::GLES3)
664 if(NOT OPENGL_gles3_LIBRARY)
665 add_library(OpenGL::GLES3 INTERFACE IMPORTED)
667 if(IS_ABSOLUTE "${OPENGL_gles3_LIBRARY}")
668 add_library(OpenGL::GLES3 UNKNOWN IMPORTED)
669 set_target_properties(OpenGL::GLES3 PROPERTIES
670 IMPORTED_LOCATION "${OPENGL_gles3_LIBRARY}"
673 add_library(OpenGL::GLES3 INTERFACE IMPORTED)
674 set_target_properties(OpenGL::GLES3 PROPERTIES
675 IMPORTED_LIBNAME "${OPENGL_gles3_LIBRARY}"
680 # Attach target properties
681 set_target_properties(OpenGL::GLES3 PROPERTIES
682 INTERFACE_INCLUDE_DIRECTORIES
683 "${OPENGL_GLES3_INCLUDE_DIR}"
685 list(APPEND OPENGL_INCLUDE_DIRS ${OPENGL_GLES3_INCLUDE_DIR})
687 if (OPENGL_USE_GLES3)
688 set(_OpenGL_EGL_IMPL OpenGL::GLES3)
693 if(OPENGL_gl_LIBRARY AND NOT TARGET OpenGL::GL)
694 # A legacy GL library is available, so use it for the legacy GL target.
695 if(IS_ABSOLUTE "${OPENGL_gl_LIBRARY}")
696 add_library(OpenGL::GL UNKNOWN IMPORTED)
697 set_target_properties(OpenGL::GL PROPERTIES
698 IMPORTED_LOCATION "${OPENGL_gl_LIBRARY}")
700 add_library(OpenGL::GL INTERFACE IMPORTED)
701 set_target_properties(OpenGL::GL PROPERTIES
702 IMPORTED_LIBNAME "${OPENGL_gl_LIBRARY}")
704 set_target_properties(OpenGL::GL PROPERTIES
705 INTERFACE_INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}")
706 elseif(NOT TARGET OpenGL::GL AND TARGET OpenGL::OpenGL AND TARGET OpenGL::GLX)
707 # A legacy GL library is not available, but we can provide the legacy GL
708 # target using GLVND OpenGL+GLX.
709 add_library(OpenGL::GL INTERFACE IMPORTED)
710 set_target_properties(OpenGL::GL PROPERTIES INTERFACE_LINK_LIBRARIES
712 set_property(TARGET OpenGL::GL APPEND PROPERTY INTERFACE_LINK_LIBRARIES
714 set_target_properties(OpenGL::GL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
715 "${OPENGL_INCLUDE_DIR}")
718 # ::EGL is a GLVND library, and thus Linux-only: we don't bother checking
719 # for a framework version of this library.
720 # Note we test whether _OpenGL_EGL_IMPL is set. Based on the OpenGL implementation,
721 # _OpenGL_EGL_IMPL will be one of OpenGL::OpenGL, OpenGL::GLES2, OpenGL::GLES3
722 if(_OpenGL_EGL_IMPL AND OpenGL_EGL_FOUND AND NOT TARGET OpenGL::EGL)
723 if(IS_ABSOLUTE "${OPENGL_egl_LIBRARY}")
724 add_library(OpenGL::EGL UNKNOWN IMPORTED)
725 set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LOCATION
726 "${OPENGL_egl_LIBRARY}")
728 add_library(OpenGL::EGL INTERFACE IMPORTED)
729 set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LIBNAME
730 "${OPENGL_egl_LIBRARY}")
732 set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_LINK_LIBRARIES
733 "${_OpenGL_EGL_IMPL}")
734 # Note that EGL's include directory is different from OpenGL/GLX's!
735 set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
736 "${OPENGL_EGL_INCLUDE_DIR}")
737 list(APPEND OPENGL_INCLUDE_DIRS ${OPENGL_EGL_INCLUDE_DIR})
740 if(OPENGL_GLU_FOUND AND NOT TARGET OpenGL::GLU)
741 if(IS_ABSOLUTE "${OPENGL_glu_LIBRARY}")
742 add_library(OpenGL::GLU UNKNOWN IMPORTED)
743 set_target_properties(OpenGL::GLU PROPERTIES
744 IMPORTED_LOCATION "${OPENGL_glu_LIBRARY}")
746 add_library(OpenGL::GLU INTERFACE IMPORTED)
747 set_target_properties(OpenGL::GLU PROPERTIES
748 IMPORTED_LIBNAME "${OPENGL_glu_LIBRARY}")
750 set_target_properties(OpenGL::GLU PROPERTIES
751 INTERFACE_LINK_LIBRARIES OpenGL::GL)
752 # Note that GLU's include directory may be different from OpenGL's!
753 set_target_properties(OpenGL::GLU PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
754 "${OPENGL_GLU_INCLUDE_DIR}")
755 list(APPEND OPENGL_INCLUDE_DIRS ${OPENGL_GLU_INCLUDE_DIR})
758 # OPENGL_LIBRARIES mirrors OpenGL::GL's logic ...
759 if(OPENGL_gl_LIBRARY)
760 set(OPENGL_LIBRARIES ${OPENGL_gl_LIBRARY})
761 elseif(TARGET OpenGL::OpenGL AND TARGET OpenGL::GLX)
762 set(OPENGL_LIBRARIES ${OPENGL_opengl_LIBRARY} ${OPENGL_glx_LIBRARY})
764 set(OPENGL_LIBRARIES "")
766 # ... and also includes GLU, if available.
767 if(TARGET OpenGL::GLU)
768 list(APPEND OPENGL_LIBRARIES ${OPENGL_glu_LIBRARY})
772 list(REMOVE_DUPLICATES OPENGL_INCLUDE_DIRS)
774 # This deprecated setting is for backward compatibility with CMake1.4
775 set(OPENGL_LIBRARY ${OPENGL_LIBRARIES})
776 # This deprecated setting is for backward compatibility with CMake1.4
777 set(OPENGL_INCLUDE_PATH ${OPENGL_INCLUDE_DIR})
779 mark_as_advanced(${_OpenGL_CACHE_VARS})
780 unset(_OpenGL_CACHE_VARS)