FindOpenGL.cmake 19 KB

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