FindOpenGL.cmake 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 /usr/X11R6/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 /usr/X11R6/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 /usr/X11R6/lib
  192. ${_OPENGL_LIB_PATH}
  193. )
  194. find_library(OPENGL_glx_LIBRARY
  195. NAMES GLX
  196. PATHS /usr/X11R6/lib
  197. ${_OPENGL_LIB_PATH}
  198. )
  199. find_library(OPENGL_egl_LIBRARY
  200. NAMES EGL
  201. PATHS ${_OPENGL_LIB_PATH}
  202. )
  203. find_library(OPENGL_glu_LIBRARY
  204. NAMES GLU MesaGLU
  205. PATHS ${OPENGL_gl_LIBRARY}
  206. /opt/graphics/OpenGL/lib
  207. /usr/openwin/lib
  208. /usr/shlib /usr/X11R6/lib
  209. )
  210. set(_OpenGL_GL_POLICY_WARN 0)
  211. if(NOT DEFINED OpenGL_GL_PREFERENCE)
  212. set(OpenGL_GL_PREFERENCE "")
  213. endif()
  214. if(NOT OpenGL_GL_PREFERENCE STREQUAL "")
  215. # A preference has been explicitly specified.
  216. if(NOT OpenGL_GL_PREFERENCE MATCHES "^(GLVND|LEGACY)$")
  217. message(FATAL_ERROR
  218. "OpenGL_GL_PREFERENCE value '${OpenGL_GL_PREFERENCE}' not recognized. "
  219. "Allowed values are 'GLVND' and 'LEGACY'."
  220. )
  221. endif()
  222. elseif(OpenGL_FIND_COMPONENTS)
  223. # No preference was explicitly specified, but the caller did request
  224. # at least one GLVND component. Prefer GLVND for legacy GL.
  225. set(OpenGL_GL_PREFERENCE "GLVND")
  226. else()
  227. # No preference was explicitly specified and no GLVND components were
  228. # requested. Use a policy to choose the default.
  229. cmake_policy(GET CMP0072 _OpenGL_GL_POLICY)
  230. if("x${_OpenGL_GL_POLICY}x" STREQUAL "xNEWx")
  231. set(OpenGL_GL_PREFERENCE "GLVND")
  232. else()
  233. set(OpenGL_GL_PREFERENCE "LEGACY")
  234. if("x${_OpenGL_GL_POLICY}x" STREQUAL "xx")
  235. set(_OpenGL_GL_POLICY_WARN 1)
  236. endif()
  237. endif()
  238. unset(_OpenGL_GL_POLICY)
  239. endif()
  240. if("x${OpenGL_GL_PREFERENCE}x" STREQUAL "xGLVNDx" AND OPENGL_opengl_LIBRARY AND OPENGL_glx_LIBRARY)
  241. # We can provide legacy GL using GLVND libraries.
  242. # Do not use any legacy GL library.
  243. set(OPENGL_gl_LIBRARY "")
  244. else()
  245. # We cannot provide legacy GL using GLVND libraries.
  246. # Search for the legacy GL library.
  247. find_library(OPENGL_gl_LIBRARY
  248. NAMES GL MesaGL
  249. PATHS /opt/graphics/OpenGL/lib
  250. /usr/openwin/lib
  251. /usr/shlib /usr/X11R6/lib
  252. ${_OPENGL_LIB_PATH}
  253. )
  254. endif()
  255. if(_OpenGL_GL_POLICY_WARN AND OPENGL_gl_LIBRARY AND OPENGL_opengl_LIBRARY AND OPENGL_glx_LIBRARY)
  256. message(AUTHOR_WARNING
  257. "Policy CMP0072 is not set: FindOpenGL prefers GLVND by default when available. "
  258. "Run \"cmake --help-policy CMP0072\" for policy details. "
  259. "Use the cmake_policy command to set the policy and suppress this warning."
  260. "\n"
  261. "FindOpenGL found both a legacy GL library:\n"
  262. " OPENGL_gl_LIBRARY: ${OPENGL_gl_LIBRARY}\n"
  263. "and GLVND libraries for OpenGL and GLX:\n"
  264. " OPENGL_opengl_LIBRARY: ${OPENGL_opengl_LIBRARY}\n"
  265. " OPENGL_glx_LIBRARY: ${OPENGL_glx_LIBRARY}\n"
  266. "OpenGL_GL_PREFERENCE has not been set to \"GLVND\" or \"LEGACY\", so for "
  267. "compatibility with CMake 3.10 and below the legacy GL library will be used."
  268. )
  269. endif()
  270. unset(_OpenGL_GL_POLICY_WARN)
  271. # FPHSA cannot handle "this OR that is required", so we conditionally set what
  272. # it must look for. First clear any previous config we might have done:
  273. set(_OpenGL_REQUIRED_VARS)
  274. # now we append the libraries as appropriate. The complicated logic
  275. # basically comes down to "use libOpenGL when we can, and add in specific
  276. # context mechanisms when requested, or we need them to preserve the previous
  277. # default where glx is always available."
  278. if((NOT OPENGL_USE_EGL AND
  279. NOT OPENGL_opengl_LIBRARY AND
  280. OPENGL_glx_LIBRARY AND
  281. NOT OPENGL_gl_LIBRARY) OR
  282. (NOT OPENGL_USE_EGL AND
  283. NOT OPENGL_glx_LIBRARY AND
  284. NOT OPENGL_gl_LIBRARY) OR
  285. (NOT OPENGL_USE_EGL AND
  286. OPENGL_opengl_LIBRARY AND
  287. OPENGL_glx_LIBRARY) OR
  288. ( OPENGL_USE_EGL))
  289. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_opengl_LIBRARY)
  290. endif()
  291. # GLVND GLX library. Preferred when available.
  292. if((NOT OPENGL_USE_OPENGL AND
  293. NOT OPENGL_USE_GLX AND
  294. NOT OPENGL_USE_EGL AND
  295. NOT OPENGL_glx_LIBRARY AND
  296. NOT OPENGL_gl_LIBRARY) OR
  297. ( OPENGL_USE_GLX AND
  298. NOT OPENGL_USE_EGL AND
  299. NOT OPENGL_glx_LIBRARY AND
  300. NOT OPENGL_gl_LIBRARY) OR
  301. (NOT OPENGL_USE_EGL AND
  302. OPENGL_opengl_LIBRARY AND
  303. OPENGL_glx_LIBRARY) OR
  304. (OPENGL_USE_GLX AND OPENGL_USE_EGL))
  305. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_glx_LIBRARY)
  306. endif()
  307. # GLVND EGL library.
  308. if(OPENGL_USE_EGL)
  309. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_egl_LIBRARY)
  310. endif()
  311. # Old-style "libGL" library: used as a fallback when GLVND isn't available.
  312. if((NOT OPENGL_USE_EGL AND
  313. NOT OPENGL_opengl_LIBRARY AND
  314. OPENGL_glx_LIBRARY AND
  315. OPENGL_gl_LIBRARY) OR
  316. (NOT OPENGL_USE_EGL AND
  317. NOT OPENGL_glx_LIBRARY AND
  318. OPENGL_gl_LIBRARY))
  319. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
  320. endif()
  321. # We always need the 'gl.h' include dir.
  322. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
  323. unset(_OPENGL_INCLUDE_PATH)
  324. unset(_OPENGL_LIB_PATH)
  325. find_library(OPENGL_glu_LIBRARY
  326. NAMES GLU MesaGLU
  327. PATHS ${OPENGL_gl_LIBRARY}
  328. /opt/graphics/OpenGL/lib
  329. /usr/openwin/lib
  330. /usr/shlib /usr/X11R6/lib
  331. )
  332. endif ()
  333. if(OPENGL_xmesa_INCLUDE_DIR)
  334. set( OPENGL_XMESA_FOUND "YES" )
  335. else()
  336. set( OPENGL_XMESA_FOUND "NO" )
  337. endif()
  338. if(OPENGL_glu_LIBRARY)
  339. set( OPENGL_GLU_FOUND "YES" )
  340. else()
  341. set( OPENGL_GLU_FOUND "NO" )
  342. endif()
  343. # OpenGL_OpenGL_FOUND is a bit unique in that it is okay if /either/ libOpenGL
  344. # or libGL is found.
  345. # Using libGL with libEGL is never okay, though; we handle that case later.
  346. if(NOT OPENGL_opengl_LIBRARY AND NOT OPENGL_gl_LIBRARY)
  347. set(OpenGL_OpenGL_FOUND FALSE)
  348. else()
  349. set(OpenGL_OpenGL_FOUND TRUE)
  350. endif()
  351. if(OPENGL_glx_LIBRARY AND OPENGL_GLX_INCLUDE_DIR)
  352. set(OpenGL_GLX_FOUND TRUE)
  353. else()
  354. set(OpenGL_GLX_FOUND FALSE)
  355. endif()
  356. if(OPENGL_egl_LIBRARY AND OPENGL_EGL_INCLUDE_DIR)
  357. set(OpenGL_EGL_FOUND TRUE)
  358. else()
  359. set(OpenGL_EGL_FOUND FALSE)
  360. endif()
  361. # User-visible names should be plural.
  362. if(OPENGL_EGL_INCLUDE_DIR)
  363. set(OPENGL_EGL_INCLUDE_DIRS ${OPENGL_EGL_INCLUDE_DIR})
  364. endif()
  365. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  366. FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenGL REQUIRED_VARS ${_OpenGL_REQUIRED_VARS}
  367. HANDLE_COMPONENTS)
  368. unset(_OpenGL_REQUIRED_VARS)
  369. # OpenGL:: targets
  370. if(OPENGL_FOUND)
  371. # ::OpenGL is a GLVND library, and thus Linux-only: we don't bother checking
  372. # for a framework version of this library.
  373. if(OPENGL_opengl_LIBRARY AND NOT TARGET OpenGL::OpenGL)
  374. if(IS_ABSOLUTE "${OPENGL_opengl_LIBRARY}")
  375. add_library(OpenGL::OpenGL UNKNOWN IMPORTED)
  376. set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LOCATION
  377. "${OPENGL_opengl_LIBRARY}")
  378. else()
  379. add_library(OpenGL::OpenGL INTERFACE IMPORTED)
  380. set_target_properties(OpenGL::OpenGL PROPERTIES IMPORTED_LIBNAME
  381. "${OPENGL_opengl_LIBRARY}")
  382. endif()
  383. set_target_properties(OpenGL::OpenGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  384. "${OPENGL_INCLUDE_DIR}")
  385. endif()
  386. # ::GLX is a GLVND library, and thus Linux-only: we don't bother checking
  387. # for a framework version of this library.
  388. if(OpenGL_GLX_FOUND AND NOT TARGET OpenGL::GLX)
  389. if(IS_ABSOLUTE "${OPENGL_glx_LIBRARY}")
  390. add_library(OpenGL::GLX UNKNOWN IMPORTED)
  391. set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LOCATION
  392. "${OPENGL_glx_LIBRARY}")
  393. else()
  394. add_library(OpenGL::GLX INTERFACE IMPORTED)
  395. set_target_properties(OpenGL::GLX PROPERTIES IMPORTED_LIBNAME
  396. "${OPENGL_glx_LIBRARY}")
  397. endif()
  398. set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_LINK_LIBRARIES
  399. OpenGL::OpenGL)
  400. set_target_properties(OpenGL::GLX PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  401. "${OPENGL_GLX_INCLUDE_DIR}")
  402. endif()
  403. if(OPENGL_gl_LIBRARY AND NOT TARGET OpenGL::GL)
  404. # A legacy GL library is available, so use it for the legacy GL target.
  405. if(IS_ABSOLUTE "${OPENGL_gl_LIBRARY}")
  406. add_library(OpenGL::GL UNKNOWN IMPORTED)
  407. if(OPENGL_gl_LIBRARY MATCHES "/([^/]+)\\.framework$")
  408. set(_gl_fw "${OPENGL_gl_LIBRARY}/${CMAKE_MATCH_1}")
  409. if(EXISTS "${_gl_fw}.tbd")
  410. string(APPEND _gl_fw ".tbd")
  411. endif()
  412. set_target_properties(OpenGL::GL PROPERTIES
  413. IMPORTED_LOCATION "${_gl_fw}")
  414. else()
  415. set_target_properties(OpenGL::GL PROPERTIES
  416. IMPORTED_LOCATION "${OPENGL_gl_LIBRARY}")
  417. endif()
  418. else()
  419. add_library(OpenGL::GL INTERFACE IMPORTED)
  420. set_target_properties(OpenGL::GL PROPERTIES
  421. IMPORTED_LIBNAME "${OPENGL_gl_LIBRARY}")
  422. endif()
  423. set_target_properties(OpenGL::GL PROPERTIES
  424. INTERFACE_INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}")
  425. elseif(NOT TARGET OpenGL::GL AND TARGET OpenGL::OpenGL AND TARGET OpenGL::GLX)
  426. # A legacy GL library is not available, but we can provide the legacy GL
  427. # target using GLVND OpenGL+GLX.
  428. add_library(OpenGL::GL INTERFACE IMPORTED)
  429. set_target_properties(OpenGL::GL PROPERTIES INTERFACE_LINK_LIBRARIES
  430. OpenGL::OpenGL)
  431. set_property(TARGET OpenGL::GL APPEND PROPERTY INTERFACE_LINK_LIBRARIES
  432. OpenGL::GLX)
  433. set_target_properties(OpenGL::GL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  434. "${OPENGL_INCLUDE_DIR}")
  435. endif()
  436. # ::EGL is a GLVND library, and thus Linux-only: we don't bother checking
  437. # for a framework version of this library.
  438. # Note we test for OpenGL::OpenGL as a target. When this module is updated to
  439. # support GLES, we would additionally want to check for the hypothetical GLES
  440. # target and enable EGL if either ::GLES or ::OpenGL is created.
  441. if(TARGET OpenGL::OpenGL AND OpenGL_EGL_FOUND AND NOT TARGET OpenGL::EGL)
  442. if(IS_ABSOLUTE "${OPENGL_egl_LIBRARY}")
  443. add_library(OpenGL::EGL UNKNOWN IMPORTED)
  444. set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LOCATION
  445. "${OPENGL_egl_LIBRARY}")
  446. else()
  447. add_library(OpenGL::EGL INTERFACE IMPORTED)
  448. set_target_properties(OpenGL::EGL PROPERTIES IMPORTED_LIBNAME
  449. "${OPENGL_egl_LIBRARY}")
  450. endif()
  451. set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_LINK_LIBRARIES
  452. OpenGL::OpenGL)
  453. # Note that EGL's include directory is different from OpenGL/GLX's!
  454. set_target_properties(OpenGL::EGL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  455. "${OPENGL_EGL_INCLUDE_DIR}")
  456. endif()
  457. if(OPENGL_GLU_FOUND AND NOT TARGET OpenGL::GLU)
  458. if(IS_ABSOLUTE "${OPENGL_glu_LIBRARY}")
  459. add_library(OpenGL::GLU UNKNOWN IMPORTED)
  460. if(OPENGL_glu_LIBRARY MATCHES "/([^/]+)\\.framework$")
  461. set(_glu_fw "${OPENGL_glu_LIBRARY}/${CMAKE_MATCH_1}")
  462. if(EXISTS "${_glu_fw}.tbd")
  463. string(APPEND _glu_fw ".tbd")
  464. endif()
  465. set_target_properties(OpenGL::GLU PROPERTIES
  466. IMPORTED_LOCATION "${_glu_fw}")
  467. else()
  468. set_target_properties(OpenGL::GLU PROPERTIES
  469. IMPORTED_LOCATION "${OPENGL_glu_LIBRARY}")
  470. endif()
  471. else()
  472. add_library(OpenGL::GLU INTERFACE IMPORTED)
  473. set_target_properties(OpenGL::GLU PROPERTIES
  474. IMPORTED_LIBNAME "${OPENGL_glu_LIBRARY}")
  475. endif()
  476. set_target_properties(OpenGL::GLU PROPERTIES
  477. INTERFACE_LINK_LIBRARIES OpenGL::GL)
  478. endif()
  479. # OPENGL_LIBRARIES mirrors OpenGL::GL's logic ...
  480. if(OPENGL_gl_LIBRARY)
  481. set(OPENGL_LIBRARIES ${OPENGL_gl_LIBRARY})
  482. elseif(TARGET OpenGL::OpenGL AND TARGET OpenGL::GLX)
  483. set(OPENGL_LIBRARIES ${OPENGL_opengl_LIBRARY} ${OPENGL_glx_LIBRARY})
  484. else()
  485. set(OPENGL_LIBRARIES "")
  486. endif()
  487. # ... and also includes GLU, if available.
  488. if(TARGET OpenGL::GLU)
  489. list(APPEND OPENGL_LIBRARIES ${OPENGL_glu_LIBRARY})
  490. endif()
  491. endif()
  492. # This deprecated setting is for backward compatibility with CMake1.4
  493. set(OPENGL_LIBRARY ${OPENGL_LIBRARIES})
  494. # This deprecated setting is for backward compatibility with CMake1.4
  495. set(OPENGL_INCLUDE_PATH ${OPENGL_INCLUDE_DIR})
  496. mark_as_advanced(
  497. OPENGL_INCLUDE_DIR
  498. OPENGL_xmesa_INCLUDE_DIR
  499. OPENGL_egl_LIBRARY
  500. OPENGL_glu_LIBRARY
  501. OPENGL_glx_LIBRARY
  502. OPENGL_gl_LIBRARY
  503. OPENGL_opengl_LIBRARY
  504. OPENGL_EGL_INCLUDE_DIR
  505. OPENGL_GLX_INCLUDE_DIR
  506. )