FindOpenGL.cmake 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. 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 OpenGL Utility Library (GLU).
  20. ``OpenGL::GLX``
  21. Defined if the system has OpenGL Extension to the X Window System (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. set(_OpenGL_CACHE_VARS)
  108. if (CYGWIN)
  109. find_path(OPENGL_INCLUDE_DIR GL/gl.h )
  110. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
  111. find_library(OPENGL_gl_LIBRARY opengl32 )
  112. find_library(OPENGL_glu_LIBRARY glu32 )
  113. list(APPEND _OpenGL_CACHE_VARS
  114. OPENGL_INCLUDE_DIR
  115. OPENGL_gl_LIBRARY
  116. OPENGL_glu_LIBRARY
  117. )
  118. elseif (WIN32)
  119. if(BORLAND)
  120. set (OPENGL_gl_LIBRARY import32 CACHE STRING "OpenGL library for win32")
  121. set (OPENGL_glu_LIBRARY import32 CACHE STRING "GLU library for win32")
  122. else()
  123. set (OPENGL_gl_LIBRARY opengl32 CACHE STRING "OpenGL library for win32")
  124. set (OPENGL_glu_LIBRARY glu32 CACHE STRING "GLU library for win32")
  125. endif()
  126. list(APPEND _OpenGL_CACHE_VARS
  127. OPENGL_gl_LIBRARY
  128. OPENGL_glu_LIBRARY
  129. )
  130. elseif (APPLE)
  131. # The OpenGL.framework provides both gl and glu
  132. find_library(OPENGL_gl_LIBRARY OpenGL DOC "OpenGL library for OS X")
  133. find_library(OPENGL_glu_LIBRARY OpenGL DOC
  134. "GLU library for OS X (usually same as OpenGL library)")
  135. find_path(OPENGL_INCLUDE_DIR OpenGL/gl.h DOC "Include for OpenGL on OS X")
  136. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
  137. list(APPEND _OpenGL_CACHE_VARS
  138. OPENGL_INCLUDE_DIR
  139. OPENGL_gl_LIBRARY
  140. OPENGL_glu_LIBRARY
  141. )
  142. else()
  143. if (CMAKE_SYSTEM_NAME MATCHES "HP-UX")
  144. # Handle HP-UX cases where we only want to find OpenGL in either hpux64
  145. # or hpux32 depending on if we're doing a 64 bit build.
  146. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  147. set(_OPENGL_LIB_PATH
  148. /opt/graphics/OpenGL/lib/hpux32/)
  149. else()
  150. set(_OPENGL_LIB_PATH
  151. /opt/graphics/OpenGL/lib/hpux64/
  152. /opt/graphics/OpenGL/lib/pa20_64)
  153. endif()
  154. elseif(CMAKE_SYSTEM_NAME STREQUAL Haiku)
  155. set(_OPENGL_LIB_PATH
  156. /boot/develop/lib/x86)
  157. set(_OPENGL_INCLUDE_PATH
  158. /boot/develop/headers/os/opengl)
  159. endif()
  160. # The first line below is to make sure that the proper headers
  161. # are used on a Linux machine with the NVidia drivers installed.
  162. # They replace Mesa with NVidia's own library but normally do not
  163. # install headers and that causes the linking to
  164. # fail since the compiler finds the Mesa headers but NVidia's library.
  165. # Make sure the NVIDIA directory comes BEFORE the others.
  166. # - Atanas Georgiev <[email protected]>
  167. find_path(OPENGL_INCLUDE_DIR GL/gl.h
  168. /usr/share/doc/NVIDIA_GLX-1.0/include
  169. /usr/openwin/share/include
  170. /opt/graphics/OpenGL/include
  171. ${_OPENGL_INCLUDE_PATH}
  172. )
  173. find_path(OPENGL_GLX_INCLUDE_DIR GL/glx.h ${_OPENGL_INCLUDE_PATH})
  174. find_path(OPENGL_EGL_INCLUDE_DIR EGL/egl.h ${_OPENGL_INCLUDE_PATH})
  175. find_path(OPENGL_xmesa_INCLUDE_DIR GL/xmesa.h
  176. /usr/share/doc/NVIDIA_GLX-1.0/include
  177. /usr/openwin/share/include
  178. /opt/graphics/OpenGL/include
  179. )
  180. list(APPEND _OpenGL_CACHE_VARS
  181. OPENGL_INCLUDE_DIR
  182. OPENGL_GLX_INCLUDE_DIR
  183. OPENGL_EGL_INCLUDE_DIR
  184. OPENGL_xmesa_INCLUDE_DIR
  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. PATH_SUFFIXES libglvnd
  196. )
  197. find_library(OPENGL_egl_LIBRARY
  198. NAMES EGL
  199. PATHS ${_OPENGL_LIB_PATH}
  200. PATH_SUFFIXES libglvnd
  201. )
  202. find_library(OPENGL_glu_LIBRARY
  203. NAMES GLU MesaGLU
  204. PATHS ${OPENGL_gl_LIBRARY}
  205. /opt/graphics/OpenGL/lib
  206. /usr/openwin/lib
  207. /usr/shlib
  208. )
  209. list(APPEND _OpenGL_CACHE_VARS
  210. OPENGL_opengl_LIBRARY
  211. OPENGL_glx_LIBRARY
  212. OPENGL_egl_LIBRARY
  213. OPENGL_glu_LIBRARY
  214. )
  215. set(_OpenGL_GL_POLICY_WARN 0)
  216. if(NOT DEFINED OpenGL_GL_PREFERENCE)
  217. set(OpenGL_GL_PREFERENCE "")
  218. endif()
  219. if(NOT OpenGL_GL_PREFERENCE STREQUAL "")
  220. # A preference has been explicitly specified.
  221. if(NOT OpenGL_GL_PREFERENCE MATCHES "^(GLVND|LEGACY)$")
  222. message(FATAL_ERROR
  223. "OpenGL_GL_PREFERENCE value '${OpenGL_GL_PREFERENCE}' not recognized. "
  224. "Allowed values are 'GLVND' and 'LEGACY'."
  225. )
  226. endif()
  227. elseif(OpenGL_FIND_COMPONENTS)
  228. # No preference was explicitly specified, but the caller did request
  229. # at least one GLVND component. Prefer GLVND for legacy GL.
  230. set(OpenGL_GL_PREFERENCE "GLVND")
  231. else()
  232. # No preference was explicitly specified and no GLVND components were
  233. # requested. Use a policy to choose the default.
  234. cmake_policy(GET CMP0072 _OpenGL_GL_POLICY)
  235. if("x${_OpenGL_GL_POLICY}x" STREQUAL "xNEWx")
  236. set(OpenGL_GL_PREFERENCE "GLVND")
  237. else()
  238. set(OpenGL_GL_PREFERENCE "LEGACY")
  239. if("x${_OpenGL_GL_POLICY}x" STREQUAL "xx")
  240. set(_OpenGL_GL_POLICY_WARN 1)
  241. endif()
  242. endif()
  243. unset(_OpenGL_GL_POLICY)
  244. endif()
  245. if("x${OpenGL_GL_PREFERENCE}x" STREQUAL "xGLVNDx" AND OPENGL_opengl_LIBRARY AND OPENGL_glx_LIBRARY)
  246. # We can provide legacy GL using GLVND libraries.
  247. # Do not use any legacy GL library.
  248. set(OPENGL_gl_LIBRARY "")
  249. else()
  250. # We cannot provide legacy GL using GLVND libraries.
  251. # Search for the legacy GL library.
  252. find_library(OPENGL_gl_LIBRARY
  253. NAMES GL MesaGL
  254. PATHS /opt/graphics/OpenGL/lib
  255. /usr/openwin/lib
  256. /usr/shlib
  257. ${_OPENGL_LIB_PATH}
  258. PATH_SUFFIXES libglvnd
  259. )
  260. list(APPEND _OpenGL_CACHE_VARS OPENGL_gl_LIBRARY)
  261. endif()
  262. if(_OpenGL_GL_POLICY_WARN AND OPENGL_gl_LIBRARY AND OPENGL_opengl_LIBRARY AND OPENGL_glx_LIBRARY)
  263. cmake_policy(GET_WARNING CMP0072 _cmp0072_warning)
  264. message(AUTHOR_WARNING
  265. "${_cmp0072_warning}\n"
  266. "FindOpenGL found both a legacy GL library:\n"
  267. " OPENGL_gl_LIBRARY: ${OPENGL_gl_LIBRARY}\n"
  268. "and GLVND libraries for OpenGL and GLX:\n"
  269. " OPENGL_opengl_LIBRARY: ${OPENGL_opengl_LIBRARY}\n"
  270. " OPENGL_glx_LIBRARY: ${OPENGL_glx_LIBRARY}\n"
  271. "OpenGL_GL_PREFERENCE has not been set to \"GLVND\" or \"LEGACY\", so for "
  272. "compatibility with CMake 3.10 and below the legacy GL library will be used."
  273. )
  274. endif()
  275. unset(_OpenGL_GL_POLICY_WARN)
  276. # FPHSA cannot handle "this OR that is required", so we conditionally set what
  277. # it must look for. First clear any previous config we might have done:
  278. set(_OpenGL_REQUIRED_VARS)
  279. # now we append the libraries as appropriate. The complicated logic
  280. # basically comes down to "use libOpenGL when we can, and add in specific
  281. # context mechanisms when requested, or we need them to preserve the previous
  282. # default where glx is always available."
  283. if((NOT OPENGL_USE_EGL AND
  284. NOT OPENGL_opengl_LIBRARY AND
  285. OPENGL_glx_LIBRARY AND
  286. NOT OPENGL_gl_LIBRARY) OR
  287. (NOT OPENGL_USE_EGL AND
  288. NOT OPENGL_glx_LIBRARY AND
  289. NOT OPENGL_gl_LIBRARY) OR
  290. (NOT OPENGL_USE_EGL AND
  291. OPENGL_opengl_LIBRARY AND
  292. OPENGL_glx_LIBRARY) OR
  293. ( OPENGL_USE_EGL))
  294. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_opengl_LIBRARY)
  295. endif()
  296. # GLVND GLX library. Preferred when available.
  297. if((NOT OPENGL_USE_OPENGL AND
  298. NOT OPENGL_USE_GLX AND
  299. NOT OPENGL_USE_EGL AND
  300. NOT OPENGL_glx_LIBRARY AND
  301. NOT OPENGL_gl_LIBRARY) OR
  302. ( OPENGL_USE_GLX AND
  303. NOT OPENGL_USE_EGL AND
  304. NOT OPENGL_glx_LIBRARY AND
  305. NOT OPENGL_gl_LIBRARY) OR
  306. (NOT OPENGL_USE_EGL AND
  307. OPENGL_opengl_LIBRARY AND
  308. OPENGL_glx_LIBRARY) OR
  309. (OPENGL_USE_GLX AND OPENGL_USE_EGL))
  310. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_glx_LIBRARY)
  311. endif()
  312. # GLVND EGL library.
  313. if(OPENGL_USE_EGL)
  314. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_egl_LIBRARY)
  315. endif()
  316. # Old-style "libGL" library: used as a fallback when GLVND isn't available.
  317. if((NOT OPENGL_USE_EGL AND
  318. NOT OPENGL_opengl_LIBRARY AND
  319. OPENGL_glx_LIBRARY AND
  320. OPENGL_gl_LIBRARY) OR
  321. (NOT OPENGL_USE_EGL AND
  322. NOT OPENGL_glx_LIBRARY AND
  323. OPENGL_gl_LIBRARY))
  324. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
  325. endif()
  326. # We always need the 'gl.h' include dir.
  327. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
  328. unset(_OPENGL_INCLUDE_PATH)
  329. unset(_OPENGL_LIB_PATH)
  330. find_library(OPENGL_glu_LIBRARY
  331. NAMES GLU MesaGLU
  332. PATHS ${OPENGL_gl_LIBRARY}
  333. /opt/graphics/OpenGL/lib
  334. /usr/openwin/lib
  335. /usr/shlib
  336. )
  337. endif ()
  338. if(OPENGL_xmesa_INCLUDE_DIR)
  339. set( OPENGL_XMESA_FOUND "YES" )
  340. else()
  341. set( OPENGL_XMESA_FOUND "NO" )
  342. endif()
  343. if(OPENGL_glu_LIBRARY)
  344. set( OPENGL_GLU_FOUND "YES" )
  345. else()
  346. set( OPENGL_GLU_FOUND "NO" )
  347. endif()
  348. # OpenGL_OpenGL_FOUND is a bit unique in that it is okay if /either/ libOpenGL
  349. # or libGL is found.
  350. # Using libGL with libEGL is never okay, though; we handle that case later.
  351. if(NOT OPENGL_opengl_LIBRARY AND NOT OPENGL_gl_LIBRARY)
  352. set(OpenGL_OpenGL_FOUND FALSE)
  353. else()
  354. set(OpenGL_OpenGL_FOUND TRUE)
  355. endif()
  356. if(OPENGL_glx_LIBRARY AND OPENGL_GLX_INCLUDE_DIR)
  357. set(OpenGL_GLX_FOUND TRUE)
  358. else()
  359. set(OpenGL_GLX_FOUND FALSE)
  360. endif()
  361. if(OPENGL_egl_LIBRARY AND OPENGL_EGL_INCLUDE_DIR)
  362. set(OpenGL_EGL_FOUND TRUE)
  363. else()
  364. set(OpenGL_EGL_FOUND FALSE)
  365. endif()
  366. # User-visible names should be plural.
  367. if(OPENGL_EGL_INCLUDE_DIR)
  368. set(OPENGL_EGL_INCLUDE_DIRS ${OPENGL_EGL_INCLUDE_DIR})
  369. endif()
  370. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  371. if (CMAKE_FIND_PACKAGE_NAME STREQUAL "GLU")
  372. # FindGLU include()'s this module. It's an old pattern, but rather than
  373. # trying to suppress this from outside the module (which is then sensitive to
  374. # the contents, detect the case in this module and suppress it explicitly.
  375. set(FPHSA_NAME_MISMATCHED 1)
  376. endif ()
  377. FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenGL REQUIRED_VARS ${_OpenGL_REQUIRED_VARS}
  378. HANDLE_COMPONENTS)
  379. unset(FPHSA_NAME_MISMATCHED)
  380. unset(_OpenGL_REQUIRED_VARS)
  381. # OpenGL:: targets
  382. if(OPENGL_FOUND)
  383. # ::OpenGL is a GLVND library, and thus Linux-only: we don't bother checking
  384. # for a framework version of this library.
  385. if(OPENGL_opengl_LIBRARY AND NOT TARGET OpenGL::OpenGL)
  386. if(IS_ABSOLUTE "${OPENGL_opengl_LIBRARY}")
  387. add_library(OpenGL::OpenGL UNKNOWN IMPORTED)
  388. set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LOCATION
  389. "${OPENGL_opengl_LIBRARY}")
  390. else()
  391. add_library(OpenGL::OpenGL INTERFACE IMPORTED)
  392. set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LIBNAME
  393. "${OPENGL_opengl_LIBRARY}")
  394. endif()
  395. set_target_properties(OpenGL::OpenGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  396. "${OPENGL_INCLUDE_DIR}")
  397. endif()
  398. # ::GLX is a GLVND library, and thus Linux-only: we don't bother checking
  399. # for a framework version of this library.
  400. if(OpenGL_GLX_FOUND AND NOT TARGET OpenGL::GLX)
  401. if(IS_ABSOLUTE "${OPENGL_glx_LIBRARY}")
  402. add_library(OpenGL::GLX UNKNOWN IMPORTED)
  403. set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LOCATION
  404. "${OPENGL_glx_LIBRARY}")
  405. else()
  406. add_library(OpenGL::GLX INTERFACE IMPORTED)
  407. set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LIBNAME
  408. "${OPENGL_glx_LIBRARY}")
  409. endif()
  410. set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_LINK_LIBRARIES
  411. OpenGL::OpenGL)
  412. set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  413. "${OPENGL_GLX_INCLUDE_DIR}")
  414. endif()
  415. if(OPENGL_gl_LIBRARY AND NOT TARGET OpenGL::GL)
  416. # A legacy GL library is available, so use it for the legacy GL target.
  417. if(IS_ABSOLUTE "${OPENGL_gl_LIBRARY}")
  418. add_library(OpenGL::GL UNKNOWN IMPORTED)
  419. if(OPENGL_gl_LIBRARY MATCHES "/([^/]+)\\.framework$")
  420. set(_gl_fw "${OPENGL_gl_LIBRARY}/${CMAKE_MATCH_1}")
  421. if(EXISTS "${_gl_fw}.tbd")
  422. string(APPEND _gl_fw ".tbd")
  423. endif()
  424. set_target_properties(OpenGL::GL PROPERTIES
  425. IMPORTED_LOCATION "${_gl_fw}")
  426. else()
  427. set_target_properties(OpenGL::GL PROPERTIES
  428. IMPORTED_LOCATION "${OPENGL_gl_LIBRARY}")
  429. endif()
  430. else()
  431. add_library(OpenGL::GL INTERFACE IMPORTED)
  432. set_target_properties(OpenGL::GL PROPERTIES
  433. IMPORTED_LIBNAME "${OPENGL_gl_LIBRARY}")
  434. endif()
  435. set_target_properties(OpenGL::GL PROPERTIES
  436. INTERFACE_INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}")
  437. elseif(NOT TARGET OpenGL::GL AND TARGET OpenGL::OpenGL AND TARGET OpenGL::GLX)
  438. # A legacy GL library is not available, but we can provide the legacy GL
  439. # target using GLVND OpenGL+GLX.
  440. add_library(OpenGL::GL INTERFACE IMPORTED)
  441. set_target_properties(OpenGL::GL PROPERTIES INTERFACE_LINK_LIBRARIES
  442. OpenGL::OpenGL)
  443. set_property(TARGET OpenGL::GL APPEND PROPERTY INTERFACE_LINK_LIBRARIES
  444. OpenGL::GLX)
  445. set_target_properties(OpenGL::GL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  446. "${OPENGL_INCLUDE_DIR}")
  447. endif()
  448. # ::EGL is a GLVND library, and thus Linux-only: we don't bother checking
  449. # for a framework version of this library.
  450. # Note we test for OpenGL::OpenGL as a target. When this module is updated to
  451. # support GLES, we would additionally want to check for the hypothetical GLES
  452. # target and enable EGL if either ::GLES or ::OpenGL is created.
  453. if(TARGET OpenGL::OpenGL AND OpenGL_EGL_FOUND AND NOT TARGET OpenGL::EGL)
  454. if(IS_ABSOLUTE "${OPENGL_egl_LIBRARY}")
  455. add_library(OpenGL::EGL UNKNOWN IMPORTED)
  456. set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LOCATION
  457. "${OPENGL_egl_LIBRARY}")
  458. else()
  459. add_library(OpenGL::EGL INTERFACE IMPORTED)
  460. set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LIBNAME
  461. "${OPENGL_egl_LIBRARY}")
  462. endif()
  463. set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_LINK_LIBRARIES
  464. OpenGL::OpenGL)
  465. # Note that EGL's include directory is different from OpenGL/GLX's!
  466. set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  467. "${OPENGL_EGL_INCLUDE_DIR}")
  468. endif()
  469. if(OPENGL_GLU_FOUND AND NOT TARGET OpenGL::GLU)
  470. if(IS_ABSOLUTE "${OPENGL_glu_LIBRARY}")
  471. add_library(OpenGL::GLU UNKNOWN IMPORTED)
  472. if(OPENGL_glu_LIBRARY MATCHES "/([^/]+)\\.framework$")
  473. set(_glu_fw "${OPENGL_glu_LIBRARY}/${CMAKE_MATCH_1}")
  474. if(EXISTS "${_glu_fw}.tbd")
  475. string(APPEND _glu_fw ".tbd")
  476. endif()
  477. set_target_properties(OpenGL::GLU PROPERTIES
  478. IMPORTED_LOCATION "${_glu_fw}")
  479. else()
  480. set_target_properties(OpenGL::GLU PROPERTIES
  481. IMPORTED_LOCATION "${OPENGL_glu_LIBRARY}")
  482. endif()
  483. else()
  484. add_library(OpenGL::GLU INTERFACE IMPORTED)
  485. set_target_properties(OpenGL::GLU PROPERTIES
  486. IMPORTED_LIBNAME "${OPENGL_glu_LIBRARY}")
  487. endif()
  488. set_target_properties(OpenGL::GLU PROPERTIES
  489. INTERFACE_LINK_LIBRARIES OpenGL::GL)
  490. endif()
  491. # OPENGL_LIBRARIES mirrors OpenGL::GL's logic ...
  492. if(OPENGL_gl_LIBRARY)
  493. set(OPENGL_LIBRARIES ${OPENGL_gl_LIBRARY})
  494. elseif(TARGET OpenGL::OpenGL AND TARGET OpenGL::GLX)
  495. set(OPENGL_LIBRARIES ${OPENGL_opengl_LIBRARY} ${OPENGL_glx_LIBRARY})
  496. else()
  497. set(OPENGL_LIBRARIES "")
  498. endif()
  499. # ... and also includes GLU, if available.
  500. if(TARGET OpenGL::GLU)
  501. list(APPEND OPENGL_LIBRARIES ${OPENGL_glu_LIBRARY})
  502. endif()
  503. endif()
  504. # This deprecated setting is for backward compatibility with CMake1.4
  505. set(OPENGL_LIBRARY ${OPENGL_LIBRARIES})
  506. # This deprecated setting is for backward compatibility with CMake1.4
  507. set(OPENGL_INCLUDE_PATH ${OPENGL_INCLUDE_DIR})
  508. mark_as_advanced(${_OpenGL_CACHE_VARS})
  509. unset(_OpenGL_CACHE_VARS)