FindOpenGL.cmake 25 KB

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