FindOpenGL.cmake 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #.rst:
  4. # FindOpenGL
  5. # ----------
  6. #
  7. # FindModule for OpenGL and GLU.
  8. #
  9. # Optional COMPONENTS
  10. # ^^^^^^^^^^^^^^^^^^^
  11. #
  12. # This module respects several optional COMPONENTS: ``EGL``, ``GLX``, and
  13. # ``OpenGL``. There are corresponding import targets for each of these flags.
  14. #
  15. # IMPORTED Targets
  16. # ^^^^^^^^^^^^^^^^
  17. #
  18. # This module defines the :prop_tgt:`IMPORTED` targets:
  19. #
  20. # ``OpenGL::GL``
  21. # Defined to the platform-specific OpenGL libraries if the system has OpenGL.
  22. # ``OpenGL::OpenGL``
  23. # Defined to libOpenGL if the system is GLVND-based.
  24. # ``OpenGL::GLU``
  25. # Defined if the system has GLU.
  26. # ``OpenGL::GLX``
  27. # Defined if the system has GLX.
  28. # ``OpenGL::EGL``
  29. # Defined if the system has EGL.
  30. #
  31. # Result Variables
  32. # ^^^^^^^^^^^^^^^^
  33. #
  34. # This module sets the following variables:
  35. #
  36. # ``OPENGL_FOUND``
  37. # True, if the system has OpenGL and all components are found.
  38. # ``OPENGL_XMESA_FOUND``
  39. # True, if the system has XMESA.
  40. # ``OPENGL_GLU_FOUND``
  41. # True, if the system has GLU.
  42. # ``OpenGL_OpenGL_FOUND``
  43. # True, if the system has an OpenGL library.
  44. # ``OpenGL_GLX_FOUND``
  45. # True, if the system has GLX.
  46. # ``OpenGL_EGL_FOUND``
  47. # True, if the system has EGL.
  48. # ``OPENGL_INCLUDE_DIR``
  49. # Path to the OpenGL include directory.
  50. # ``OPENGL_EGL_INCLUDE_DIRS``
  51. # Path to the EGL include directory.
  52. # ``OPENGL_LIBRARIES``
  53. # Paths to the OpenGL library, windowing system libraries, and GLU libraries.
  54. # On Linux, this assumes GLX and is never correct for EGL-based targets.
  55. # Clients are encouraged to use the ``OpenGL::*`` import targets instead.
  56. #
  57. # Cache variables
  58. # ^^^^^^^^^^^^^^^
  59. #
  60. # The following cache variables may also be set:
  61. #
  62. # ``OPENGL_egl_LIBRARY``
  63. # Path to the EGL library.
  64. # ``OPENGL_glu_LIBRARY``
  65. # Path to the GLU library.
  66. # ``OPENGL_glx_LIBRARY``
  67. # Path to the GLVND 'GLX' library.
  68. # ``OPENGL_opengl_LIBRARY``
  69. # Path to the GLVND 'OpenGL' library
  70. # ``OPENGL_gl_LIBRARY``
  71. # Path to the OpenGL library. New code should prefer the ``OpenGL::*`` import
  72. # targets.
  73. #
  74. # Linux-specific
  75. # ^^^^^^^^^^^^^^
  76. #
  77. # Some Linux systems utilize GLVND as a new ABI for OpenGL. GLVND separates
  78. # context libraries from OpenGL itself; OpenGL lives in "libOpenGL", and
  79. # contexts are defined in "libGLX" or "libEGL". GLVND is currently the only way
  80. # to get OpenGL 3+ functionality via EGL in a manner portable across vendors.
  81. # Projects may use GLVND explicitly with target ``OpenGL::OpenGL`` and either
  82. # ``OpenGL::GLX`` or ``OpenGL::EGL``.
  83. #
  84. # Projects may use the ``OpenGL::GL`` target (or ``OPENGL_LIBRARIES`` variable)
  85. # to use legacy GL interfaces. These will use the legacy GL library located
  86. # by ``OPENGL_gl_LIBRARY``, if available. If ``OPENGL_gl_LIBRARY`` is empty or
  87. # not found and GLVND is available, the ``OpenGL::GL`` target will use GLVND
  88. # ``OpenGL::OpenGL`` and ``OpenGL::GLX`` (and the ``OPENGL_LIBRARIES``
  89. # variable will use the corresponding libraries). Thus, for non-EGL-based
  90. # Linux targets, the ``OpenGL::GL`` target is most portable.
  91. #
  92. # A ``OpenGL_GL_PREFERENCE`` variable may be set to specify the preferred way
  93. # to provide legacy GL interfaces in case multiple choices are available.
  94. # The value may be one of:
  95. #
  96. # ``GLVND``
  97. # If the GLVND OpenGL and GLX libraries are available, prefer them.
  98. # This forces ``OPENGL_gl_LIBRARY`` to be empty.
  99. # This is the default if components were requested (since components
  100. # correspond to GLVND libraries) or if policy :policy:`CMP0072` is
  101. # set to ``NEW``.
  102. #
  103. # ``LEGACY``
  104. # Prefer to use the legacy libGL library, if available.
  105. # This is the default if no components were requested and
  106. # policy :policy:`CMP0072` is not set to ``NEW``.
  107. #
  108. # For EGL targets the client must rely on GLVND support on the user's system.
  109. # Linking should use the ``OpenGL::OpenGL OpenGL::EGL`` targets. Using GLES*
  110. # libraries is theoretically possible in place of ``OpenGL::OpenGL``, but this
  111. # module does not currently support that; contributions welcome.
  112. #
  113. # ``OPENGL_egl_LIBRARY`` and ``OPENGL_EGL_INCLUDE_DIRS`` are defined in the case of
  114. # GLVND. For non-GLVND Linux and other systems these are left undefined.
  115. #
  116. # macOS-Specific
  117. # ^^^^^^^^^^^^^^
  118. #
  119. # On OSX FindOpenGL defaults to using the framework version of OpenGL. People
  120. # will have to change the cache values of OPENGL_glu_LIBRARY and
  121. # OPENGL_gl_LIBRARY to use OpenGL with X11 on OSX.
  122. set(_OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
  123. # Provide OPENGL_USE_<C> variables for each component.
  124. foreach(component ${OpenGL_FIND_COMPONENTS})
  125. string(TOUPPER ${component} _COMPONENT)
  126. set(OPENGL_USE_${_COMPONENT} 1)
  127. endforeach()
  128. if (CYGWIN)
  129. find_path(OPENGL_INCLUDE_DIR GL/gl.h )
  130. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
  131. find_library(OPENGL_gl_LIBRARY opengl32 )
  132. find_library(OPENGL_glu_LIBRARY glu32 )
  133. elseif (WIN32)
  134. if(BORLAND)
  135. set (OPENGL_gl_LIBRARY import32 CACHE STRING "OpenGL library for win32")
  136. set (OPENGL_glu_LIBRARY import32 CACHE STRING "GLU library for win32")
  137. else()
  138. set (OPENGL_gl_LIBRARY opengl32 CACHE STRING "OpenGL library for win32")
  139. set (OPENGL_glu_LIBRARY glu32 CACHE STRING "GLU library for win32")
  140. endif()
  141. elseif (APPLE)
  142. # The OpenGL.framework provides both gl and glu
  143. find_library(OPENGL_gl_LIBRARY OpenGL DOC "OpenGL library for OS X")
  144. find_library(OPENGL_glu_LIBRARY OpenGL DOC
  145. "GLU library for OS X (usually same as OpenGL library)")
  146. find_path(OPENGL_INCLUDE_DIR OpenGL/gl.h DOC "Include for OpenGL on OS X")
  147. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
  148. else()
  149. if (CMAKE_SYSTEM_NAME MATCHES "HP-UX")
  150. # Handle HP-UX cases where we only want to find OpenGL in either hpux64
  151. # or hpux32 depending on if we're doing a 64 bit build.
  152. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  153. set(_OPENGL_LIB_PATH
  154. /opt/graphics/OpenGL/lib/hpux32/)
  155. else()
  156. set(_OPENGL_LIB_PATH
  157. /opt/graphics/OpenGL/lib/hpux64/
  158. /opt/graphics/OpenGL/lib/pa20_64)
  159. endif()
  160. elseif(CMAKE_SYSTEM_NAME STREQUAL Haiku)
  161. set(_OPENGL_LIB_PATH
  162. /boot/develop/lib/x86)
  163. set(_OPENGL_INCLUDE_PATH
  164. /boot/develop/headers/os/opengl)
  165. endif()
  166. # The first line below is to make sure that the proper headers
  167. # are used on a Linux machine with the NVidia drivers installed.
  168. # They replace Mesa with NVidia's own library but normally do not
  169. # install headers and that causes the linking to
  170. # fail since the compiler finds the Mesa headers but NVidia's library.
  171. # Make sure the NVIDIA directory comes BEFORE the others.
  172. # - Atanas Georgiev <atanas@cs.columbia.edu>
  173. find_path(OPENGL_INCLUDE_DIR GL/gl.h
  174. /usr/share/doc/NVIDIA_GLX-1.0/include
  175. /usr/openwin/share/include
  176. /opt/graphics/OpenGL/include
  177. ${_OPENGL_INCLUDE_PATH}
  178. )
  179. find_path(OPENGL_GLX_INCLUDE_DIR GL/glx.h ${_OPENGL_INCLUDE_PATH})
  180. find_path(OPENGL_EGL_INCLUDE_DIR EGL/egl.h ${_OPENGL_INCLUDE_PATH})
  181. find_path(OPENGL_xmesa_INCLUDE_DIR GL/xmesa.h
  182. /usr/share/doc/NVIDIA_GLX-1.0/include
  183. /usr/openwin/share/include
  184. /opt/graphics/OpenGL/include
  185. )
  186. # Search for the GLVND libraries. We do this regardless of COMPONENTS; we'll
  187. # take into account the COMPONENTS logic later.
  188. find_library(OPENGL_opengl_LIBRARY
  189. NAMES OpenGL
  190. PATHS ${_OPENGL_LIB_PATH}
  191. )
  192. find_library(OPENGL_glx_LIBRARY
  193. NAMES GLX
  194. PATHS ${_OPENGL_LIB_PATH}
  195. )
  196. find_library(OPENGL_egl_LIBRARY
  197. NAMES EGL
  198. PATHS ${_OPENGL_LIB_PATH}
  199. )
  200. find_library(OPENGL_glu_LIBRARY
  201. NAMES GLU MesaGLU
  202. PATHS ${OPENGL_gl_LIBRARY}
  203. /opt/graphics/OpenGL/lib
  204. /usr/openwin/lib
  205. /usr/shlib
  206. )
  207. set(_OpenGL_GL_POLICY_WARN 0)
  208. if(NOT DEFINED OpenGL_GL_PREFERENCE)
  209. set(OpenGL_GL_PREFERENCE "")
  210. endif()
  211. if(NOT OpenGL_GL_PREFERENCE STREQUAL "")
  212. # A preference has been explicitly specified.
  213. if(NOT OpenGL_GL_PREFERENCE MATCHES "^(GLVND|LEGACY)$")
  214. message(FATAL_ERROR
  215. "OpenGL_GL_PREFERENCE value '${OpenGL_GL_PREFERENCE}' not recognized. "
  216. "Allowed values are 'GLVND' and 'LEGACY'."
  217. )
  218. endif()
  219. elseif(OpenGL_FIND_COMPONENTS)
  220. # No preference was explicitly specified, but the caller did request
  221. # at least one GLVND component. Prefer GLVND for legacy GL.
  222. set(OpenGL_GL_PREFERENCE "GLVND")
  223. else()
  224. # No preference was explicitly specified and no GLVND components were
  225. # requested. Use a policy to choose the default.
  226. cmake_policy(GET CMP0072 _OpenGL_GL_POLICY)
  227. if("x${_OpenGL_GL_POLICY}x" STREQUAL "xNEWx")
  228. set(OpenGL_GL_PREFERENCE "GLVND")
  229. else()
  230. set(OpenGL_GL_PREFERENCE "LEGACY")
  231. if("x${_OpenGL_GL_POLICY}x" STREQUAL "xx")
  232. set(_OpenGL_GL_POLICY_WARN 1)
  233. endif()
  234. endif()
  235. unset(_OpenGL_GL_POLICY)
  236. endif()
  237. if("x${OpenGL_GL_PREFERENCE}x" STREQUAL "xGLVNDx" AND OPENGL_opengl_LIBRARY AND OPENGL_glx_LIBRARY)
  238. # We can provide legacy GL using GLVND libraries.
  239. # Do not use any legacy GL library.
  240. set(OPENGL_gl_LIBRARY "")
  241. else()
  242. # We cannot provide legacy GL using GLVND libraries.
  243. # Search for the legacy GL library.
  244. find_library(OPENGL_gl_LIBRARY
  245. NAMES GL MesaGL
  246. PATHS /opt/graphics/OpenGL/lib
  247. /usr/openwin/lib
  248. /usr/shlib
  249. ${_OPENGL_LIB_PATH}
  250. )
  251. endif()
  252. if(_OpenGL_GL_POLICY_WARN AND OPENGL_gl_LIBRARY AND OPENGL_opengl_LIBRARY AND OPENGL_glx_LIBRARY)
  253. message(AUTHOR_WARNING
  254. "Policy CMP0072 is not set: FindOpenGL prefers GLVND by default when available. "
  255. "Run \"cmake --help-policy CMP0072\" for policy details. "
  256. "Use the cmake_policy command to set the policy and suppress this warning."
  257. "\n"
  258. "FindOpenGL found both a legacy GL library:\n"
  259. " OPENGL_gl_LIBRARY: ${OPENGL_gl_LIBRARY}\n"
  260. "and GLVND libraries for OpenGL and GLX:\n"
  261. " OPENGL_opengl_LIBRARY: ${OPENGL_opengl_LIBRARY}\n"
  262. " OPENGL_glx_LIBRARY: ${OPENGL_glx_LIBRARY}\n"
  263. "OpenGL_GL_PREFERENCE has not been set to \"GLVND\" or \"LEGACY\", so for "
  264. "compatibility with CMake 3.10 and below the legacy GL library will be used."
  265. )
  266. endif()
  267. unset(_OpenGL_GL_POLICY_WARN)
  268. # FPHSA cannot handle "this OR that is required", so we conditionally set what
  269. # it must look for. First clear any previous config we might have done:
  270. set(_OpenGL_REQUIRED_VARS)
  271. # now we append the libraries as appropriate. The complicated logic
  272. # basically comes down to "use libOpenGL when we can, and add in specific
  273. # context mechanisms when requested, or we need them to preserve the previous
  274. # default where glx is always available."
  275. if((NOT OPENGL_USE_EGL AND
  276. NOT OPENGL_opengl_LIBRARY AND
  277. OPENGL_glx_LIBRARY AND
  278. NOT OPENGL_gl_LIBRARY) OR
  279. (NOT OPENGL_USE_EGL AND
  280. NOT OPENGL_glx_LIBRARY AND
  281. NOT OPENGL_gl_LIBRARY) OR
  282. (NOT OPENGL_USE_EGL AND
  283. OPENGL_opengl_LIBRARY AND
  284. OPENGL_glx_LIBRARY) OR
  285. ( OPENGL_USE_EGL))
  286. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_opengl_LIBRARY)
  287. endif()
  288. # GLVND GLX library. Preferred when available.
  289. if((NOT OPENGL_USE_OPENGL AND
  290. NOT OPENGL_USE_GLX AND
  291. NOT OPENGL_USE_EGL AND
  292. NOT OPENGL_glx_LIBRARY AND
  293. NOT OPENGL_gl_LIBRARY) OR
  294. ( OPENGL_USE_GLX AND
  295. NOT OPENGL_USE_EGL AND
  296. NOT OPENGL_glx_LIBRARY AND
  297. NOT OPENGL_gl_LIBRARY) OR
  298. (NOT OPENGL_USE_EGL AND
  299. OPENGL_opengl_LIBRARY AND
  300. OPENGL_glx_LIBRARY) OR
  301. (OPENGL_USE_GLX AND OPENGL_USE_EGL))
  302. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_glx_LIBRARY)
  303. endif()
  304. # GLVND EGL library.
  305. if(OPENGL_USE_EGL)
  306. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_egl_LIBRARY)
  307. endif()
  308. # Old-style "libGL" library: used as a fallback when GLVND isn't available.
  309. if((NOT OPENGL_USE_EGL AND
  310. NOT OPENGL_opengl_LIBRARY AND
  311. OPENGL_glx_LIBRARY AND
  312. OPENGL_gl_LIBRARY) OR
  313. (NOT OPENGL_USE_EGL AND
  314. NOT OPENGL_glx_LIBRARY AND
  315. OPENGL_gl_LIBRARY))
  316. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
  317. endif()
  318. # We always need the 'gl.h' include dir.
  319. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
  320. unset(_OPENGL_INCLUDE_PATH)
  321. unset(_OPENGL_LIB_PATH)
  322. find_library(OPENGL_glu_LIBRARY
  323. NAMES GLU MesaGLU
  324. PATHS ${OPENGL_gl_LIBRARY}
  325. /opt/graphics/OpenGL/lib
  326. /usr/openwin/lib
  327. /usr/shlib
  328. )
  329. endif ()
  330. if(OPENGL_xmesa_INCLUDE_DIR)
  331. set( OPENGL_XMESA_FOUND "YES" )
  332. else()
  333. set( OPENGL_XMESA_FOUND "NO" )
  334. endif()
  335. if(OPENGL_glu_LIBRARY)
  336. set( OPENGL_GLU_FOUND "YES" )
  337. else()
  338. set( OPENGL_GLU_FOUND "NO" )
  339. endif()
  340. # OpenGL_OpenGL_FOUND is a bit unique in that it is okay if /either/ libOpenGL
  341. # or libGL is found.
  342. # Using libGL with libEGL is never okay, though; we handle that case later.
  343. if(NOT OPENGL_opengl_LIBRARY AND NOT OPENGL_gl_LIBRARY)
  344. set(OpenGL_OpenGL_FOUND FALSE)
  345. else()
  346. set(OpenGL_OpenGL_FOUND TRUE)
  347. endif()
  348. if(OPENGL_glx_LIBRARY AND OPENGL_GLX_INCLUDE_DIR)
  349. set(OpenGL_GLX_FOUND TRUE)
  350. else()
  351. set(OpenGL_GLX_FOUND FALSE)
  352. endif()
  353. if(OPENGL_egl_LIBRARY AND OPENGL_EGL_INCLUDE_DIR)
  354. set(OpenGL_EGL_FOUND TRUE)
  355. else()
  356. set(OpenGL_EGL_FOUND FALSE)
  357. endif()
  358. # User-visible names should be plural.
  359. if(OPENGL_EGL_INCLUDE_DIR)
  360. set(OPENGL_EGL_INCLUDE_DIRS ${OPENGL_EGL_INCLUDE_DIR})
  361. endif()
  362. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  363. FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenGL REQUIRED_VARS ${_OpenGL_REQUIRED_VARS}
  364. HANDLE_COMPONENTS)
  365. unset(_OpenGL_REQUIRED_VARS)
  366. # OpenGL:: targets
  367. if(OPENGL_FOUND)
  368. # ::OpenGL is a GLVND library, and thus Linux-only: we don't bother checking
  369. # for a framework version of this library.
  370. if(OPENGL_opengl_LIBRARY AND NOT TARGET OpenGL::OpenGL)
  371. if(IS_ABSOLUTE "${OPENGL_opengl_LIBRARY}")
  372. add_library(OpenGL::OpenGL UNKNOWN IMPORTED)
  373. set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LOCATION
  374. "${OPENGL_opengl_LIBRARY}")
  375. else()
  376. add_library(OpenGL::OpenGL INTERFACE IMPORTED)
  377. set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LIBNAME
  378. "${OPENGL_opengl_LIBRARY}")
  379. endif()
  380. set_target_properties(OpenGL::OpenGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  381. "${OPENGL_INCLUDE_DIR}")
  382. endif()
  383. # ::GLX is a GLVND library, and thus Linux-only: we don't bother checking
  384. # for a framework version of this library.
  385. if(OpenGL_GLX_FOUND AND NOT TARGET OpenGL::GLX)
  386. if(IS_ABSOLUTE "${OPENGL_glx_LIBRARY}")
  387. add_library(OpenGL::GLX UNKNOWN IMPORTED)
  388. set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LOCATION
  389. "${OPENGL_glx_LIBRARY}")
  390. else()
  391. add_library(OpenGL::GLX INTERFACE IMPORTED)
  392. set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LIBNAME
  393. "${OPENGL_glx_LIBRARY}")
  394. endif()
  395. set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_LINK_LIBRARIES
  396. OpenGL::OpenGL)
  397. set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  398. "${OPENGL_GLX_INCLUDE_DIR}")
  399. endif()
  400. if(OPENGL_gl_LIBRARY AND NOT TARGET OpenGL::GL)
  401. # A legacy GL library is available, so use it for the legacy GL target.
  402. if(IS_ABSOLUTE "${OPENGL_gl_LIBRARY}")
  403. add_library(OpenGL::GL UNKNOWN IMPORTED)
  404. if(OPENGL_gl_LIBRARY MATCHES "/([^/]+)\\.framework$")
  405. set(_gl_fw "${OPENGL_gl_LIBRARY}/${CMAKE_MATCH_1}")
  406. if(EXISTS "${_gl_fw}.tbd")
  407. string(APPEND _gl_fw ".tbd")
  408. endif()
  409. set_target_properties(OpenGL::GL PROPERTIES
  410. IMPORTED_LOCATION "${_gl_fw}")
  411. else()
  412. set_target_properties(OpenGL::GL PROPERTIES
  413. IMPORTED_LOCATION "${OPENGL_gl_LIBRARY}")
  414. endif()
  415. else()
  416. add_library(OpenGL::GL INTERFACE IMPORTED)
  417. set_target_properties(OpenGL::GL PROPERTIES
  418. IMPORTED_LIBNAME "${OPENGL_gl_LIBRARY}")
  419. endif()
  420. set_target_properties(OpenGL::GL PROPERTIES
  421. INTERFACE_INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}")
  422. elseif(NOT TARGET OpenGL::GL AND TARGET OpenGL::OpenGL AND TARGET OpenGL::GLX)
  423. # A legacy GL library is not available, but we can provide the legacy GL
  424. # target using GLVND OpenGL+GLX.
  425. add_library(OpenGL::GL INTERFACE IMPORTED)
  426. set_target_properties(OpenGL::GL PROPERTIES INTERFACE_LINK_LIBRARIES
  427. OpenGL::OpenGL)
  428. set_property(TARGET OpenGL::GL APPEND PROPERTY INTERFACE_LINK_LIBRARIES
  429. OpenGL::GLX)
  430. set_target_properties(OpenGL::GL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  431. "${OPENGL_INCLUDE_DIR}")
  432. endif()
  433. # ::EGL is a GLVND library, and thus Linux-only: we don't bother checking
  434. # for a framework version of this library.
  435. # Note we test for OpenGL::OpenGL as a target. When this module is updated to
  436. # support GLES, we would additionally want to check for the hypothetical GLES
  437. # target and enable EGL if either ::GLES or ::OpenGL is created.
  438. if(TARGET OpenGL::OpenGL AND OpenGL_EGL_FOUND AND NOT TARGET OpenGL::EGL)
  439. if(IS_ABSOLUTE "${OPENGL_egl_LIBRARY}")
  440. add_library(OpenGL::EGL UNKNOWN IMPORTED)
  441. set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LOCATION
  442. "${OPENGL_egl_LIBRARY}")
  443. else()
  444. add_library(OpenGL::EGL INTERFACE IMPORTED)
  445. set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LIBNAME
  446. "${OPENGL_egl_LIBRARY}")
  447. endif()
  448. set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_LINK_LIBRARIES
  449. OpenGL::OpenGL)
  450. # Note that EGL's include directory is different from OpenGL/GLX's!
  451. set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  452. "${OPENGL_EGL_INCLUDE_DIR}")
  453. endif()
  454. if(OPENGL_GLU_FOUND AND NOT TARGET OpenGL::GLU)
  455. if(IS_ABSOLUTE "${OPENGL_glu_LIBRARY}")
  456. add_library(OpenGL::GLU UNKNOWN IMPORTED)
  457. if(OPENGL_glu_LIBRARY MATCHES "/([^/]+)\\.framework$")
  458. set(_glu_fw "${OPENGL_glu_LIBRARY}/${CMAKE_MATCH_1}")
  459. if(EXISTS "${_glu_fw}.tbd")
  460. string(APPEND _glu_fw ".tbd")
  461. endif()
  462. set_target_properties(OpenGL::GLU PROPERTIES
  463. IMPORTED_LOCATION "${_glu_fw}")
  464. else()
  465. set_target_properties(OpenGL::GLU PROPERTIES
  466. IMPORTED_LOCATION "${OPENGL_glu_LIBRARY}")
  467. endif()
  468. else()
  469. add_library(OpenGL::GLU INTERFACE IMPORTED)
  470. set_target_properties(OpenGL::GLU PROPERTIES
  471. IMPORTED_LIBNAME "${OPENGL_glu_LIBRARY}")
  472. endif()
  473. set_target_properties(OpenGL::GLU PROPERTIES
  474. INTERFACE_LINK_LIBRARIES OpenGL::GL)
  475. endif()
  476. # OPENGL_LIBRARIES mirrors OpenGL::GL's logic ...
  477. if(OPENGL_gl_LIBRARY)
  478. set(OPENGL_LIBRARIES ${OPENGL_gl_LIBRARY})
  479. elseif(TARGET OpenGL::OpenGL AND TARGET OpenGL::GLX)
  480. set(OPENGL_LIBRARIES ${OPENGL_opengl_LIBRARY} ${OPENGL_glx_LIBRARY})
  481. else()
  482. set(OPENGL_LIBRARIES "")
  483. endif()
  484. # ... and also includes GLU, if available.
  485. if(TARGET OpenGL::GLU)
  486. list(APPEND OPENGL_LIBRARIES ${OPENGL_glu_LIBRARY})
  487. endif()
  488. endif()
  489. # This deprecated setting is for backward compatibility with CMake1.4
  490. set(OPENGL_LIBRARY ${OPENGL_LIBRARIES})
  491. # This deprecated setting is for backward compatibility with CMake1.4
  492. set(OPENGL_INCLUDE_PATH ${OPENGL_INCLUDE_DIR})
  493. mark_as_advanced(
  494. OPENGL_INCLUDE_DIR
  495. OPENGL_xmesa_INCLUDE_DIR
  496. OPENGL_egl_LIBRARY
  497. OPENGL_glu_LIBRARY
  498. OPENGL_glx_LIBRARY
  499. OPENGL_gl_LIBRARY
  500. OPENGL_opengl_LIBRARY
  501. OPENGL_EGL_INCLUDE_DIR
  502. OPENGL_GLX_INCLUDE_DIR
  503. )