FindOpenGL.cmake 26 KB

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