FindOpenGL.cmake 20 KB

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