74392dac3ecd177d2a2031739d157a615420b539
[platform/upstream/cmake.git] / Modules / FindOpenGL.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindOpenGL
6 ----------
7
8 FindModule for OpenGL and OpenGL Utility Library (GLU).
9
10 Optional COMPONENTS
11 ^^^^^^^^^^^^^^^^^^^
12
13 This module respects several optional COMPONENTS: ``EGL``, ``GLX``, and
14 ``OpenGL``.  There are corresponding import targets for each of these flags.
15
16 IMPORTED Targets
17 ^^^^^^^^^^^^^^^^
18
19 This module defines the :prop_tgt:`IMPORTED` targets:
20
21 ``OpenGL::GL``
22  Defined to the platform-specific OpenGL libraries if the system has OpenGL.
23 ``OpenGL::OpenGL``
24  Defined to libOpenGL if the system is GLVND-based.
25 ``OpenGL::GLU``
26  Defined if the system has OpenGL Utility Library (GLU).
27 ``OpenGL::GLX``
28  Defined if the system has OpenGL Extension to the X Window System (GLX).
29 ``OpenGL::EGL``
30  Defined if the system has EGL.
31
32 Result Variables
33 ^^^^^^^^^^^^^^^^
34
35 This module sets the following variables:
36
37 ``OPENGL_FOUND``
38  True, if the system has OpenGL and all components are found.
39 ``OPENGL_XMESA_FOUND``
40  True, if the system has XMESA.
41 ``OPENGL_GLU_FOUND``
42  True, if the system has GLU.
43 ``OpenGL_OpenGL_FOUND``
44  True, if the system has an OpenGL library.
45 ``OpenGL_GLX_FOUND``
46  True, if the system has GLX.
47 ``OpenGL_EGL_FOUND``
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.
53 ``OPENGL_LIBRARIES``
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.
57
58 Cache variables
59 ^^^^^^^^^^^^^^^
60
61 The following cache variables may also be set:
62
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
71 ``OPENGL_gl_LIBRARY``
72  Path to the OpenGL library.  New code should prefer the ``OpenGL::*`` import
73  targets.
74
75 Linux-specific
76 ^^^^^^^^^^^^^^
77
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``.
84
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.
92
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:
96
97 ``GLVND``
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
102  set to ``NEW``.
103
104 ``LEGACY``
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``.
108
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.
113
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.
116
117 macOS-Specific
118 ^^^^^^^^^^^^^^
119
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 #]=======================================================================]
124
125 set(_OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
126
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)
131 endforeach()
132
133 set(_OpenGL_CACHE_VARS)
134
135 if (CYGWIN)
136   find_path(OPENGL_INCLUDE_DIR GL/gl.h )
137   list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
138
139   find_library(OPENGL_gl_LIBRARY opengl32 )
140   find_library(OPENGL_glu_LIBRARY glu32 )
141
142   list(APPEND _OpenGL_CACHE_VARS
143     OPENGL_INCLUDE_DIR
144     OPENGL_gl_LIBRARY
145     OPENGL_glu_LIBRARY
146     )
147 elseif (WIN32)
148
149   if(BORLAND)
150     set (OPENGL_gl_LIBRARY import32 CACHE STRING "OpenGL library for win32")
151     set (OPENGL_glu_LIBRARY import32 CACHE STRING "GLU library for win32")
152   else()
153     set (OPENGL_gl_LIBRARY opengl32 CACHE STRING "OpenGL library for win32")
154     set (OPENGL_glu_LIBRARY glu32 CACHE STRING "GLU library for win32")
155   endif()
156
157   list(APPEND _OpenGL_CACHE_VARS
158     OPENGL_gl_LIBRARY
159     OPENGL_glu_LIBRARY
160     )
161 elseif (APPLE)
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)
168
169   list(APPEND _OpenGL_CACHE_VARS
170     OPENGL_INCLUDE_DIR
171     OPENGL_gl_LIBRARY
172     OPENGL_glu_LIBRARY
173     )
174 else()
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)
179       set(_OPENGL_LIB_PATH
180         /opt/graphics/OpenGL/lib/hpux32/)
181     else()
182       set(_OPENGL_LIB_PATH
183         /opt/graphics/OpenGL/lib/hpux64/
184         /opt/graphics/OpenGL/lib/pa20_64)
185     endif()
186   elseif(CMAKE_SYSTEM_NAME STREQUAL Haiku)
187     set(_OPENGL_LIB_PATH
188       /boot/develop/lib/x86)
189     set(_OPENGL_INCLUDE_PATH
190       /boot/develop/headers/os/opengl)
191   endif()
192
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}
205   )
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
212   )
213   list(APPEND _OpenGL_CACHE_VARS
214     OPENGL_INCLUDE_DIR
215     OPENGL_GLX_INCLUDE_DIR
216     OPENGL_EGL_INCLUDE_DIR
217     OPENGL_xmesa_INCLUDE_DIR
218     )
219
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
223     NAMES OpenGL
224     PATHS ${_OPENGL_LIB_PATH}
225   )
226
227   find_library(OPENGL_glx_LIBRARY
228     NAMES GLX
229     PATHS ${_OPENGL_LIB_PATH}
230     PATH_SUFFIXES libglvnd
231   )
232
233   find_library(OPENGL_egl_LIBRARY
234     NAMES EGL
235     PATHS ${_OPENGL_LIB_PATH}
236     PATH_SUFFIXES libglvnd
237   )
238
239   find_library(OPENGL_glu_LIBRARY
240     NAMES GLU MesaGLU
241     PATHS ${OPENGL_gl_LIBRARY}
242           /opt/graphics/OpenGL/lib
243           /usr/openwin/lib
244           /usr/shlib
245   )
246
247   list(APPEND _OpenGL_CACHE_VARS
248     OPENGL_opengl_LIBRARY
249     OPENGL_glx_LIBRARY
250     OPENGL_egl_LIBRARY
251     OPENGL_glu_LIBRARY
252     )
253
254   set(_OpenGL_GL_POLICY_WARN 0)
255   if(NOT DEFINED OpenGL_GL_PREFERENCE)
256     set(OpenGL_GL_PREFERENCE "")
257   endif()
258   if(NOT OpenGL_GL_PREFERENCE STREQUAL "")
259     # A preference has been explicitly specified.
260     if(NOT OpenGL_GL_PREFERENCE MATCHES "^(GLVND|LEGACY)$")
261       message(FATAL_ERROR
262         "OpenGL_GL_PREFERENCE value '${OpenGL_GL_PREFERENCE}' not recognized.  "
263         "Allowed values are 'GLVND' and 'LEGACY'."
264         )
265     endif()
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")
270   else()
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")
276     else()
277       set(OpenGL_GL_PREFERENCE "LEGACY")
278       if("x${_OpenGL_GL_POLICY}x" STREQUAL "xx")
279         set(_OpenGL_GL_POLICY_WARN 1)
280       endif()
281     endif()
282     unset(_OpenGL_GL_POLICY)
283   endif()
284
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 "")
289   else()
290     # We cannot provide legacy GL using GLVND libraries.
291     # Search for the legacy GL library.
292     find_library(OPENGL_gl_LIBRARY
293       NAMES GL MesaGL
294       PATHS /opt/graphics/OpenGL/lib
295             /usr/openwin/lib
296             /usr/shlib
297             ${_OPENGL_LIB_PATH}
298       PATH_SUFFIXES libglvnd
299       )
300     list(APPEND _OpenGL_CACHE_VARS OPENGL_gl_LIBRARY)
301   endif()
302
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."
314       )
315   endif()
316   unset(_OpenGL_GL_POLICY_WARN)
317
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)
321
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
336      (    OPENGL_USE_EGL))
337     list(APPEND _OpenGL_REQUIRED_VARS OPENGL_opengl_LIBRARY)
338   endif()
339
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
346      (    OPENGL_USE_GLX AND
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)
355   endif()
356
357   # GLVND EGL library.
358   if(OPENGL_USE_EGL)
359     list(APPEND _OpenGL_REQUIRED_VARS OPENGL_egl_LIBRARY)
360   endif()
361
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
369           OPENGL_gl_LIBRARY))
370     list(APPEND _OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
371   endif()
372
373   # We always need the 'gl.h' include dir.
374   list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
375
376   unset(_OPENGL_INCLUDE_PATH)
377   unset(_OPENGL_LIB_PATH)
378
379   find_library(OPENGL_glu_LIBRARY
380     NAMES GLU MesaGLU
381     PATHS ${OPENGL_gl_LIBRARY}
382           /opt/graphics/OpenGL/lib
383           /usr/openwin/lib
384           /usr/shlib
385   )
386 endif ()
387
388 if(OPENGL_xmesa_INCLUDE_DIR)
389   set( OPENGL_XMESA_FOUND "YES" )
390 else()
391   set( OPENGL_XMESA_FOUND "NO" )
392 endif()
393
394 if(OPENGL_glu_LIBRARY)
395   set( OPENGL_GLU_FOUND "YES" )
396 else()
397   set( OPENGL_GLU_FOUND "NO" )
398 endif()
399
400 # OpenGL_OpenGL_FOUND is a bit unique in that it is okay if /either/ libOpenGL
401 # or libGL is found.
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)
405 else()
406   set(OpenGL_OpenGL_FOUND TRUE)
407 endif()
408
409 if(OPENGL_glx_LIBRARY AND OPENGL_GLX_INCLUDE_DIR)
410   set(OpenGL_GLX_FOUND TRUE)
411 else()
412   set(OpenGL_GLX_FOUND FALSE)
413 endif()
414
415 if(OPENGL_egl_LIBRARY AND OPENGL_EGL_INCLUDE_DIR)
416   set(OpenGL_EGL_FOUND TRUE)
417 else()
418   set(OpenGL_EGL_FOUND FALSE)
419 endif()
420
421 # User-visible names should be plural.
422 if(OPENGL_EGL_INCLUDE_DIR)
423   set(OPENGL_EGL_INCLUDE_DIRS ${OPENGL_EGL_INCLUDE_DIR})
424 endif()
425
426 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
427 FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenGL REQUIRED_VARS ${_OpenGL_REQUIRED_VARS}
428                                   HANDLE_COMPONENTS)
429 unset(_OpenGL_REQUIRED_VARS)
430
431 # OpenGL:: targets
432 if(OPENGL_FOUND)
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}")
440     else()
441       add_library(OpenGL::OpenGL INTERFACE IMPORTED)
442       set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LIBNAME
443                             "${OPENGL_opengl_LIBRARY}")
444     endif()
445     set_target_properties(OpenGL::OpenGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
446                           "${OPENGL_INCLUDE_DIR}")
447   endif()
448
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}")
456     else()
457       add_library(OpenGL::GLX INTERFACE IMPORTED)
458       set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LIBNAME
459                             "${OPENGL_glx_LIBRARY}")
460     endif()
461     set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_LINK_LIBRARIES
462                           OpenGL::OpenGL)
463     set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
464                           "${OPENGL_GLX_INCLUDE_DIR}")
465   endif()
466
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")
475         endif()
476         set_target_properties(OpenGL::GL PROPERTIES
477           IMPORTED_LOCATION "${_gl_fw}")
478       else()
479         set_target_properties(OpenGL::GL PROPERTIES
480           IMPORTED_LOCATION "${OPENGL_gl_LIBRARY}")
481       endif()
482     else()
483       add_library(OpenGL::GL INTERFACE IMPORTED)
484       set_target_properties(OpenGL::GL PROPERTIES
485         IMPORTED_LIBNAME "${OPENGL_gl_LIBRARY}")
486     endif()
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
494                           OpenGL::OpenGL)
495     set_property(TARGET OpenGL::GL APPEND PROPERTY INTERFACE_LINK_LIBRARIES
496                  OpenGL::GLX)
497     set_target_properties(OpenGL::GL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
498                           "${OPENGL_INCLUDE_DIR}")
499   endif()
500
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}")
511     else()
512       add_library(OpenGL::EGL INTERFACE IMPORTED)
513       set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LIBNAME
514                             "${OPENGL_egl_LIBRARY}")
515     endif()
516     set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_LINK_LIBRARIES
517                           OpenGL::OpenGL)
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}")
521   endif()
522
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")
530         endif()
531         set_target_properties(OpenGL::GLU PROPERTIES
532           IMPORTED_LOCATION "${_glu_fw}")
533       else()
534         set_target_properties(OpenGL::GLU PROPERTIES
535           IMPORTED_LOCATION "${OPENGL_glu_LIBRARY}")
536       endif()
537     else()
538       add_library(OpenGL::GLU INTERFACE IMPORTED)
539       set_target_properties(OpenGL::GLU PROPERTIES
540         IMPORTED_LIBNAME "${OPENGL_glu_LIBRARY}")
541     endif()
542     set_target_properties(OpenGL::GLU PROPERTIES
543       INTERFACE_LINK_LIBRARIES OpenGL::GL)
544   endif()
545
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})
551   else()
552     set(OPENGL_LIBRARIES "")
553   endif()
554   # ... and also includes GLU, if available.
555   if(TARGET OpenGL::GLU)
556     list(APPEND OPENGL_LIBRARIES ${OPENGL_glu_LIBRARY})
557   endif()
558 endif()
559
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})
564
565 mark_as_advanced(${_OpenGL_CACHE_VARS})
566 unset(_OpenGL_CACHE_VARS)