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