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).
13 This module respects several optional COMPONENTS: ``EGL``, ``GLX``, and
14 ``OpenGL``. There are corresponding import targets for each of these flags.
19 This module defines the :prop_tgt:`IMPORTED` targets:
22 Defined to the platform-specific OpenGL libraries if the system has OpenGL.
24 Defined to libOpenGL if the system is GLVND-based.
26 Defined if the system has OpenGL Utility Library (GLU).
28 Defined if the system has OpenGL Extension to the X Window System (GLX).
30 Defined if the system has EGL.
35 This module sets the following variables:
38 True, if the system has OpenGL and all components are found.
39 ``OPENGL_XMESA_FOUND``
40 True, if the system has XMESA.
42 True, if the system has GLU.
43 ``OpenGL_OpenGL_FOUND``
44 True, if the system has an OpenGL library.
46 True, if the system has GLX.
48 True, if the system has EGL.
49 ``OPENGL_INCLUDE_DIR``
50 Path to the OpenGL include directory.
51 ``OPENGL_EGL_INCLUDE_DIRS``
52 Path to the EGL include directory.
54 Paths to the OpenGL library, windowing system libraries, and GLU libraries.
55 On Linux, this assumes GLX and is never correct for EGL-based targets.
56 Clients are encouraged to use the ``OpenGL::*`` import targets instead.
61 The following cache variables may also be set:
63 ``OPENGL_egl_LIBRARY``
64 Path to the EGL library.
65 ``OPENGL_glu_LIBRARY``
66 Path to the GLU library.
67 ``OPENGL_glx_LIBRARY``
68 Path to the GLVND 'GLX' library.
69 ``OPENGL_opengl_LIBRARY``
70 Path to the GLVND 'OpenGL' library
72 Path to the OpenGL library. New code should prefer the ``OpenGL::*`` import
78 Some Linux systems utilize GLVND as a new ABI for OpenGL. GLVND separates
79 context libraries from OpenGL itself; OpenGL lives in "libOpenGL", and
80 contexts are defined in "libGLX" or "libEGL". GLVND is currently the only way
81 to get OpenGL 3+ functionality via EGL in a manner portable across vendors.
82 Projects may use GLVND explicitly with target ``OpenGL::OpenGL`` and either
83 ``OpenGL::GLX`` or ``OpenGL::EGL``.
85 Projects may use the ``OpenGL::GL`` target (or ``OPENGL_LIBRARIES`` variable)
86 to use legacy GL interfaces. These will use the legacy GL library located
87 by ``OPENGL_gl_LIBRARY``, if available. If ``OPENGL_gl_LIBRARY`` is empty or
88 not found and GLVND is available, the ``OpenGL::GL`` target will use GLVND
89 ``OpenGL::OpenGL`` and ``OpenGL::GLX`` (and the ``OPENGL_LIBRARIES``
90 variable will use the corresponding libraries). Thus, for non-EGL-based
91 Linux targets, the ``OpenGL::GL`` target is most portable.
93 A ``OpenGL_GL_PREFERENCE`` variable may be set to specify the preferred way
94 to provide legacy GL interfaces in case multiple choices are available.
95 The value may be one of:
98 If the GLVND OpenGL and GLX libraries are available, prefer them.
99 This forces ``OPENGL_gl_LIBRARY`` to be empty.
100 This is the default if components were requested (since components
101 correspond to GLVND libraries) or if policy :policy:`CMP0072` is
105 Prefer to use the legacy libGL library, if available.
106 This is the default if no components were requested and
107 policy :policy:`CMP0072` is not set to ``NEW``.
109 For EGL targets the client must rely on GLVND support on the user's system.
110 Linking should use the ``OpenGL::OpenGL OpenGL::EGL`` targets. Using GLES*
111 libraries is theoretically possible in place of ``OpenGL::OpenGL``, but this
112 module does not currently support that; contributions welcome.
114 ``OPENGL_egl_LIBRARY`` and ``OPENGL_EGL_INCLUDE_DIRS`` are defined in the case of
115 GLVND. For non-GLVND Linux and other systems these are left undefined.
120 On OSX FindOpenGL defaults to using the framework version of OpenGL. People
121 will have to change the cache values of OPENGL_glu_LIBRARY and
122 OPENGL_gl_LIBRARY to use OpenGL with X11 on OSX.
123 #]=======================================================================]
125 set(_OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
127 # Provide OPENGL_USE_<C> variables for each component.
128 foreach(component ${OpenGL_FIND_COMPONENTS})
129 string(TOUPPER ${component} _COMPONENT)
130 set(OPENGL_USE_${_COMPONENT} 1)
133 set(_OpenGL_CACHE_VARS)
136 find_path(OPENGL_INCLUDE_DIR GL/gl.h )
137 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
139 find_library(OPENGL_gl_LIBRARY opengl32 )
140 find_library(OPENGL_glu_LIBRARY glu32 )
142 list(APPEND _OpenGL_CACHE_VARS
150 set (OPENGL_gl_LIBRARY import32 CACHE STRING "OpenGL library for win32")
151 set (OPENGL_glu_LIBRARY import32 CACHE STRING "GLU library for win32")
153 set (OPENGL_gl_LIBRARY opengl32 CACHE STRING "OpenGL library for win32")
154 set (OPENGL_glu_LIBRARY glu32 CACHE STRING "GLU library for win32")
157 list(APPEND _OpenGL_CACHE_VARS
162 # The OpenGL.framework provides both gl and glu
163 find_library(OPENGL_gl_LIBRARY OpenGL DOC "OpenGL library for OS X")
164 find_library(OPENGL_glu_LIBRARY OpenGL DOC
165 "GLU library for OS X (usually same as OpenGL library)")
166 find_path(OPENGL_INCLUDE_DIR OpenGL/gl.h DOC "Include for OpenGL on OS X")
167 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
169 list(APPEND _OpenGL_CACHE_VARS
175 if (CMAKE_SYSTEM_NAME MATCHES "HP-UX")
176 # Handle HP-UX cases where we only want to find OpenGL in either hpux64
177 # or hpux32 depending on if we're doing a 64 bit build.
178 if(CMAKE_SIZEOF_VOID_P EQUAL 4)
180 /opt/graphics/OpenGL/lib/hpux32/)
183 /opt/graphics/OpenGL/lib/hpux64/
184 /opt/graphics/OpenGL/lib/pa20_64)
186 elseif(CMAKE_SYSTEM_NAME STREQUAL Haiku)
188 /boot/develop/lib/x86)
189 set(_OPENGL_INCLUDE_PATH
190 /boot/develop/headers/os/opengl)
193 # The first line below is to make sure that the proper headers
194 # are used on a Linux machine with the NVidia drivers installed.
195 # They replace Mesa with NVidia's own library but normally do not
196 # install headers and that causes the linking to
197 # fail since the compiler finds the Mesa headers but NVidia's library.
198 # Make sure the NVIDIA directory comes BEFORE the others.
199 # - Atanas Georgiev <atanas@cs.columbia.edu>
200 find_path(OPENGL_INCLUDE_DIR GL/gl.h
201 /usr/share/doc/NVIDIA_GLX-1.0/include
202 /usr/openwin/share/include
203 /opt/graphics/OpenGL/include
204 ${_OPENGL_INCLUDE_PATH}
206 find_path(OPENGL_GLX_INCLUDE_DIR GL/glx.h ${_OPENGL_INCLUDE_PATH})
207 find_path(OPENGL_EGL_INCLUDE_DIR EGL/egl.h ${_OPENGL_INCLUDE_PATH})
208 find_path(OPENGL_xmesa_INCLUDE_DIR GL/xmesa.h
209 /usr/share/doc/NVIDIA_GLX-1.0/include
210 /usr/openwin/share/include
211 /opt/graphics/OpenGL/include
213 list(APPEND _OpenGL_CACHE_VARS
215 OPENGL_GLX_INCLUDE_DIR
216 OPENGL_EGL_INCLUDE_DIR
217 OPENGL_xmesa_INCLUDE_DIR
220 # Search for the GLVND libraries. We do this regardless of COMPONENTS; we'll
221 # take into account the COMPONENTS logic later.
222 find_library(OPENGL_opengl_LIBRARY
224 PATHS ${_OPENGL_LIB_PATH}
227 find_library(OPENGL_glx_LIBRARY
229 PATHS ${_OPENGL_LIB_PATH}
230 PATH_SUFFIXES libglvnd
233 find_library(OPENGL_egl_LIBRARY
235 PATHS ${_OPENGL_LIB_PATH}
236 PATH_SUFFIXES libglvnd
239 find_library(OPENGL_glu_LIBRARY
241 PATHS ${OPENGL_gl_LIBRARY}
242 /opt/graphics/OpenGL/lib
247 list(APPEND _OpenGL_CACHE_VARS
248 OPENGL_opengl_LIBRARY
254 set(_OpenGL_GL_POLICY_WARN 0)
255 if(NOT DEFINED OpenGL_GL_PREFERENCE)
256 set(OpenGL_GL_PREFERENCE "")
258 if(NOT OpenGL_GL_PREFERENCE STREQUAL "")
259 # A preference has been explicitly specified.
260 if(NOT OpenGL_GL_PREFERENCE MATCHES "^(GLVND|LEGACY)$")
262 "OpenGL_GL_PREFERENCE value '${OpenGL_GL_PREFERENCE}' not recognized. "
263 "Allowed values are 'GLVND' and 'LEGACY'."
266 elseif(OpenGL_FIND_COMPONENTS)
267 # No preference was explicitly specified, but the caller did request
268 # at least one GLVND component. Prefer GLVND for legacy GL.
269 set(OpenGL_GL_PREFERENCE "GLVND")
271 # No preference was explicitly specified and no GLVND components were
272 # requested. Use a policy to choose the default.
273 cmake_policy(GET CMP0072 _OpenGL_GL_POLICY)
274 if("x${_OpenGL_GL_POLICY}x" STREQUAL "xNEWx")
275 set(OpenGL_GL_PREFERENCE "GLVND")
277 set(OpenGL_GL_PREFERENCE "LEGACY")
278 if("x${_OpenGL_GL_POLICY}x" STREQUAL "xx")
279 set(_OpenGL_GL_POLICY_WARN 1)
282 unset(_OpenGL_GL_POLICY)
285 if("x${OpenGL_GL_PREFERENCE}x" STREQUAL "xGLVNDx" AND OPENGL_opengl_LIBRARY AND OPENGL_glx_LIBRARY)
286 # We can provide legacy GL using GLVND libraries.
287 # Do not use any legacy GL library.
288 set(OPENGL_gl_LIBRARY "")
290 # We cannot provide legacy GL using GLVND libraries.
291 # Search for the legacy GL library.
292 find_library(OPENGL_gl_LIBRARY
294 PATHS /opt/graphics/OpenGL/lib
298 PATH_SUFFIXES libglvnd
300 list(APPEND _OpenGL_CACHE_VARS OPENGL_gl_LIBRARY)
303 if(_OpenGL_GL_POLICY_WARN AND OPENGL_gl_LIBRARY AND OPENGL_opengl_LIBRARY AND OPENGL_glx_LIBRARY)
304 cmake_policy(GET_WARNING CMP0072 _cmp0072_warning)
305 message(AUTHOR_WARNING
306 "${_cmp0072_warning}\n"
307 "FindOpenGL found both a legacy GL library:\n"
308 " OPENGL_gl_LIBRARY: ${OPENGL_gl_LIBRARY}\n"
309 "and GLVND libraries for OpenGL and GLX:\n"
310 " OPENGL_opengl_LIBRARY: ${OPENGL_opengl_LIBRARY}\n"
311 " OPENGL_glx_LIBRARY: ${OPENGL_glx_LIBRARY}\n"
312 "OpenGL_GL_PREFERENCE has not been set to \"GLVND\" or \"LEGACY\", so for "
313 "compatibility with CMake 3.10 and below the legacy GL library will be used."
316 unset(_OpenGL_GL_POLICY_WARN)
318 # FPHSA cannot handle "this OR that is required", so we conditionally set what
319 # it must look for. First clear any previous config we might have done:
320 set(_OpenGL_REQUIRED_VARS)
322 # now we append the libraries as appropriate. The complicated logic
323 # basically comes down to "use libOpenGL when we can, and add in specific
324 # context mechanisms when requested, or we need them to preserve the previous
325 # default where glx is always available."
326 if((NOT OPENGL_USE_EGL AND
327 NOT OPENGL_opengl_LIBRARY AND
328 OPENGL_glx_LIBRARY AND
329 NOT OPENGL_gl_LIBRARY) OR
330 (NOT OPENGL_USE_EGL AND
331 NOT OPENGL_glx_LIBRARY AND
332 NOT OPENGL_gl_LIBRARY) OR
333 (NOT OPENGL_USE_EGL AND
334 OPENGL_opengl_LIBRARY AND
335 OPENGL_glx_LIBRARY) OR
337 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_opengl_LIBRARY)
340 # GLVND GLX library. Preferred when available.
341 if((NOT OPENGL_USE_OPENGL AND
342 NOT OPENGL_USE_GLX AND
343 NOT OPENGL_USE_EGL AND
344 NOT OPENGL_glx_LIBRARY AND
345 NOT OPENGL_gl_LIBRARY) OR
347 NOT OPENGL_USE_EGL AND
348 NOT OPENGL_glx_LIBRARY AND
349 NOT OPENGL_gl_LIBRARY) OR
350 (NOT OPENGL_USE_EGL AND
351 OPENGL_opengl_LIBRARY AND
352 OPENGL_glx_LIBRARY) OR
353 (OPENGL_USE_GLX AND OPENGL_USE_EGL))
354 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_glx_LIBRARY)
359 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_egl_LIBRARY)
362 # Old-style "libGL" library: used as a fallback when GLVND isn't available.
363 if((NOT OPENGL_USE_EGL AND
364 NOT OPENGL_opengl_LIBRARY AND
365 OPENGL_glx_LIBRARY AND
366 OPENGL_gl_LIBRARY) OR
367 (NOT OPENGL_USE_EGL AND
368 NOT OPENGL_glx_LIBRARY AND
370 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
373 # We always need the 'gl.h' include dir.
374 list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
376 unset(_OPENGL_INCLUDE_PATH)
377 unset(_OPENGL_LIB_PATH)
379 find_library(OPENGL_glu_LIBRARY
381 PATHS ${OPENGL_gl_LIBRARY}
382 /opt/graphics/OpenGL/lib
388 if(OPENGL_xmesa_INCLUDE_DIR)
389 set( OPENGL_XMESA_FOUND "YES" )
391 set( OPENGL_XMESA_FOUND "NO" )
394 if(OPENGL_glu_LIBRARY)
395 set( OPENGL_GLU_FOUND "YES" )
397 set( OPENGL_GLU_FOUND "NO" )
400 # OpenGL_OpenGL_FOUND is a bit unique in that it is okay if /either/ libOpenGL
402 # Using libGL with libEGL is never okay, though; we handle that case later.
403 if(NOT OPENGL_opengl_LIBRARY AND NOT OPENGL_gl_LIBRARY)
404 set(OpenGL_OpenGL_FOUND FALSE)
406 set(OpenGL_OpenGL_FOUND TRUE)
409 if(OPENGL_glx_LIBRARY AND OPENGL_GLX_INCLUDE_DIR)
410 set(OpenGL_GLX_FOUND TRUE)
412 set(OpenGL_GLX_FOUND FALSE)
415 if(OPENGL_egl_LIBRARY AND OPENGL_EGL_INCLUDE_DIR)
416 set(OpenGL_EGL_FOUND TRUE)
418 set(OpenGL_EGL_FOUND FALSE)
421 # User-visible names should be plural.
422 if(OPENGL_EGL_INCLUDE_DIR)
423 set(OPENGL_EGL_INCLUDE_DIRS ${OPENGL_EGL_INCLUDE_DIR})
426 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
427 FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenGL REQUIRED_VARS ${_OpenGL_REQUIRED_VARS}
429 unset(_OpenGL_REQUIRED_VARS)
433 # ::OpenGL is a GLVND library, and thus Linux-only: we don't bother checking
434 # for a framework version of this library.
435 if(OPENGL_opengl_LIBRARY AND NOT TARGET OpenGL::OpenGL)
436 if(IS_ABSOLUTE "${OPENGL_opengl_LIBRARY}")
437 add_library(OpenGL::OpenGL UNKNOWN IMPORTED)
438 set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LOCATION
439 "${OPENGL_opengl_LIBRARY}")
441 add_library(OpenGL::OpenGL INTERFACE IMPORTED)
442 set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LIBNAME
443 "${OPENGL_opengl_LIBRARY}")
445 set_target_properties(OpenGL::OpenGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
446 "${OPENGL_INCLUDE_DIR}")
449 # ::GLX is a GLVND library, and thus Linux-only: we don't bother checking
450 # for a framework version of this library.
451 if(OpenGL_GLX_FOUND AND NOT TARGET OpenGL::GLX)
452 if(IS_ABSOLUTE "${OPENGL_glx_LIBRARY}")
453 add_library(OpenGL::GLX UNKNOWN IMPORTED)
454 set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LOCATION
455 "${OPENGL_glx_LIBRARY}")
457 add_library(OpenGL::GLX INTERFACE IMPORTED)
458 set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LIBNAME
459 "${OPENGL_glx_LIBRARY}")
461 set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_LINK_LIBRARIES
463 set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
464 "${OPENGL_GLX_INCLUDE_DIR}")
467 if(OPENGL_gl_LIBRARY AND NOT TARGET OpenGL::GL)
468 # A legacy GL library is available, so use it for the legacy GL target.
469 if(IS_ABSOLUTE "${OPENGL_gl_LIBRARY}")
470 add_library(OpenGL::GL UNKNOWN IMPORTED)
471 if(OPENGL_gl_LIBRARY MATCHES "/([^/]+)\\.framework$")
472 set(_gl_fw "${OPENGL_gl_LIBRARY}/${CMAKE_MATCH_1}")
473 if(EXISTS "${_gl_fw}.tbd")
474 string(APPEND _gl_fw ".tbd")
476 set_target_properties(OpenGL::GL PROPERTIES
477 IMPORTED_LOCATION "${_gl_fw}")
479 set_target_properties(OpenGL::GL PROPERTIES
480 IMPORTED_LOCATION "${OPENGL_gl_LIBRARY}")
483 add_library(OpenGL::GL INTERFACE IMPORTED)
484 set_target_properties(OpenGL::GL PROPERTIES
485 IMPORTED_LIBNAME "${OPENGL_gl_LIBRARY}")
487 set_target_properties(OpenGL::GL PROPERTIES
488 INTERFACE_INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}")
489 elseif(NOT TARGET OpenGL::GL AND TARGET OpenGL::OpenGL AND TARGET OpenGL::GLX)
490 # A legacy GL library is not available, but we can provide the legacy GL
491 # target using GLVND OpenGL+GLX.
492 add_library(OpenGL::GL INTERFACE IMPORTED)
493 set_target_properties(OpenGL::GL PROPERTIES INTERFACE_LINK_LIBRARIES
495 set_property(TARGET OpenGL::GL APPEND PROPERTY INTERFACE_LINK_LIBRARIES
497 set_target_properties(OpenGL::GL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
498 "${OPENGL_INCLUDE_DIR}")
501 # ::EGL is a GLVND library, and thus Linux-only: we don't bother checking
502 # for a framework version of this library.
503 # Note we test for OpenGL::OpenGL as a target. When this module is updated to
504 # support GLES, we would additionally want to check for the hypothetical GLES
505 # target and enable EGL if either ::GLES or ::OpenGL is created.
506 if(TARGET OpenGL::OpenGL AND OpenGL_EGL_FOUND AND NOT TARGET OpenGL::EGL)
507 if(IS_ABSOLUTE "${OPENGL_egl_LIBRARY}")
508 add_library(OpenGL::EGL UNKNOWN IMPORTED)
509 set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LOCATION
510 "${OPENGL_egl_LIBRARY}")
512 add_library(OpenGL::EGL INTERFACE IMPORTED)
513 set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LIBNAME
514 "${OPENGL_egl_LIBRARY}")
516 set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_LINK_LIBRARIES
518 # Note that EGL's include directory is different from OpenGL/GLX's!
519 set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
520 "${OPENGL_EGL_INCLUDE_DIR}")
523 if(OPENGL_GLU_FOUND AND NOT TARGET OpenGL::GLU)
524 if(IS_ABSOLUTE "${OPENGL_glu_LIBRARY}")
525 add_library(OpenGL::GLU UNKNOWN IMPORTED)
526 if(OPENGL_glu_LIBRARY MATCHES "/([^/]+)\\.framework$")
527 set(_glu_fw "${OPENGL_glu_LIBRARY}/${CMAKE_MATCH_1}")
528 if(EXISTS "${_glu_fw}.tbd")
529 string(APPEND _glu_fw ".tbd")
531 set_target_properties(OpenGL::GLU PROPERTIES
532 IMPORTED_LOCATION "${_glu_fw}")
534 set_target_properties(OpenGL::GLU PROPERTIES
535 IMPORTED_LOCATION "${OPENGL_glu_LIBRARY}")
538 add_library(OpenGL::GLU INTERFACE IMPORTED)
539 set_target_properties(OpenGL::GLU PROPERTIES
540 IMPORTED_LIBNAME "${OPENGL_glu_LIBRARY}")
542 set_target_properties(OpenGL::GLU PROPERTIES
543 INTERFACE_LINK_LIBRARIES OpenGL::GL)
546 # OPENGL_LIBRARIES mirrors OpenGL::GL's logic ...
547 if(OPENGL_gl_LIBRARY)
548 set(OPENGL_LIBRARIES ${OPENGL_gl_LIBRARY})
549 elseif(TARGET OpenGL::OpenGL AND TARGET OpenGL::GLX)
550 set(OPENGL_LIBRARIES ${OPENGL_opengl_LIBRARY} ${OPENGL_glx_LIBRARY})
552 set(OPENGL_LIBRARIES "")
554 # ... and also includes GLU, if available.
555 if(TARGET OpenGL::GLU)
556 list(APPEND OPENGL_LIBRARIES ${OPENGL_glu_LIBRARY})
560 # This deprecated setting is for backward compatibility with CMake1.4
561 set(OPENGL_LIBRARY ${OPENGL_LIBRARIES})
562 # This deprecated setting is for backward compatibility with CMake1.4
563 set(OPENGL_INCLUDE_PATH ${OPENGL_INCLUDE_DIR})
565 mark_as_advanced(${_OpenGL_CACHE_VARS})
566 unset(_OpenGL_CACHE_VARS)