FindOpenGL.cmake 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. # IMPORTED Targets
  10. # ^^^^^^^^^^^^^^^^
  11. #
  12. # This module defines the :prop_tgt:`IMPORTED` targets:
  13. #
  14. # ``OpenGL::GL``
  15. # Defined if the system has OpenGL.
  16. # ``OpenGL::GLU``
  17. # Defined if the system has GLU.
  18. #
  19. # Result Variables
  20. # ^^^^^^^^^^^^^^^^
  21. #
  22. # This module sets the following variables:
  23. #
  24. # ``OPENGL_FOUND``
  25. # True, if the system has OpenGL.
  26. # ``OPENGL_XMESA_FOUND``
  27. # True, if the system has XMESA.
  28. # ``OPENGL_GLU_FOUND``
  29. # True, if the system has GLU.
  30. # ``OPENGL_INCLUDE_DIR``
  31. # Path to the OpenGL include directory.
  32. # ``OPENGL_LIBRARIES``
  33. # Paths to the OpenGL and GLU libraries.
  34. #
  35. # If you want to use just GL you can use these values:
  36. #
  37. # ``OPENGL_gl_LIBRARY``
  38. # Path to the OpenGL library.
  39. # ``OPENGL_glu_LIBRARY``
  40. # Path to the GLU library.
  41. #
  42. # OSX Specific
  43. # ^^^^^^^^^^^^
  44. #
  45. # On OSX default to using the framework version of OpenGL. People will
  46. # have to change the cache values of OPENGL_glu_LIBRARY and
  47. # OPENGL_gl_LIBRARY to use OpenGL with X11 on OSX.
  48. set(_OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)
  49. if (CYGWIN)
  50. find_path(OPENGL_INCLUDE_DIR GL/gl.h )
  51. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
  52. find_library(OPENGL_gl_LIBRARY opengl32 )
  53. find_library(OPENGL_glu_LIBRARY glu32 )
  54. elseif (WIN32)
  55. if(BORLAND)
  56. set (OPENGL_gl_LIBRARY import32 CACHE STRING "OpenGL library for win32")
  57. set (OPENGL_glu_LIBRARY import32 CACHE STRING "GLU library for win32")
  58. else()
  59. set (OPENGL_gl_LIBRARY opengl32 CACHE STRING "OpenGL library for win32")
  60. set (OPENGL_glu_LIBRARY glu32 CACHE STRING "GLU library for win32")
  61. endif()
  62. elseif (APPLE)
  63. # The OpenGL.framework provides both gl and glu
  64. find_library(OPENGL_gl_LIBRARY OpenGL DOC "OpenGL library for OS X")
  65. find_library(OPENGL_glu_LIBRARY OpenGL DOC
  66. "GLU library for OS X (usually same as OpenGL library)")
  67. find_path(OPENGL_INCLUDE_DIR OpenGL/gl.h DOC "Include for OpenGL on OS X")
  68. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
  69. else()
  70. if (CMAKE_SYSTEM_NAME MATCHES "HP-UX")
  71. # Handle HP-UX cases where we only want to find OpenGL in either hpux64
  72. # or hpux32 depending on if we're doing a 64 bit build.
  73. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  74. set(_OPENGL_LIB_PATH
  75. /opt/graphics/OpenGL/lib/hpux32/)
  76. else()
  77. set(_OPENGL_LIB_PATH
  78. /opt/graphics/OpenGL/lib/hpux64/
  79. /opt/graphics/OpenGL/lib/pa20_64)
  80. endif()
  81. elseif(CMAKE_SYSTEM_NAME STREQUAL Haiku)
  82. set(_OPENGL_LIB_PATH
  83. /boot/develop/lib/x86)
  84. set(_OPENGL_INCLUDE_PATH
  85. /boot/develop/headers/os/opengl)
  86. endif()
  87. # The first line below is to make sure that the proper headers
  88. # are used on a Linux machine with the NVidia drivers installed.
  89. # They replace Mesa with NVidia's own library but normally do not
  90. # install headers and that causes the linking to
  91. # fail since the compiler finds the Mesa headers but NVidia's library.
  92. # Make sure the NVIDIA directory comes BEFORE the others.
  93. # - Atanas Georgiev <[email protected]>
  94. find_path(OPENGL_INCLUDE_DIR GL/gl.h
  95. /usr/share/doc/NVIDIA_GLX-1.0/include
  96. /usr/openwin/share/include
  97. /opt/graphics/OpenGL/include /usr/X11R6/include
  98. ${_OPENGL_INCLUDE_PATH}
  99. )
  100. list(APPEND _OpenGL_REQUIRED_VARS OPENGL_INCLUDE_DIR)
  101. find_path(OPENGL_xmesa_INCLUDE_DIR GL/xmesa.h
  102. /usr/share/doc/NVIDIA_GLX-1.0/include
  103. /usr/openwin/share/include
  104. /opt/graphics/OpenGL/include /usr/X11R6/include
  105. )
  106. find_library(OPENGL_gl_LIBRARY
  107. NAMES GL MesaGL
  108. PATHS /opt/graphics/OpenGL/lib
  109. /usr/openwin/lib
  110. /usr/shlib /usr/X11R6/lib
  111. ${_OPENGL_LIB_PATH}
  112. )
  113. unset(_OPENGL_INCLUDE_PATH)
  114. unset(_OPENGL_LIB_PATH)
  115. find_library(OPENGL_glu_LIBRARY
  116. NAMES GLU MesaGLU
  117. PATHS ${OPENGL_gl_LIBRARY}
  118. /opt/graphics/OpenGL/lib
  119. /usr/openwin/lib
  120. /usr/shlib /usr/X11R6/lib
  121. )
  122. endif ()
  123. if(OPENGL_gl_LIBRARY)
  124. if(OPENGL_xmesa_INCLUDE_DIR)
  125. set( OPENGL_XMESA_FOUND "YES" )
  126. else()
  127. set( OPENGL_XMESA_FOUND "NO" )
  128. endif()
  129. set( OPENGL_LIBRARIES ${OPENGL_gl_LIBRARY} ${OPENGL_LIBRARIES})
  130. if(OPENGL_glu_LIBRARY)
  131. set( OPENGL_GLU_FOUND "YES" )
  132. if(NOT "${OPENGL_glu_LIBRARY}" STREQUAL "${OPENGL_gl_LIBRARY}")
  133. set( OPENGL_LIBRARIES ${OPENGL_glu_LIBRARY} ${OPENGL_LIBRARIES} )
  134. endif()
  135. else()
  136. set( OPENGL_GLU_FOUND "NO" )
  137. endif()
  138. # This deprecated setting is for backward compatibility with CMake1.4
  139. set (OPENGL_LIBRARY ${OPENGL_LIBRARIES})
  140. endif()
  141. # This deprecated setting is for backward compatibility with CMake1.4
  142. set(OPENGL_INCLUDE_PATH ${OPENGL_INCLUDE_DIR})
  143. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  144. FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenGL REQUIRED_VARS ${_OpenGL_REQUIRED_VARS})
  145. unset(_OpenGL_REQUIRED_VARS)
  146. # OpenGL:: targets
  147. if(OPENGL_FOUND)
  148. if(NOT TARGET OpenGL::GL)
  149. if(IS_ABSOLUTE "${OPENGL_gl_LIBRARY}")
  150. add_library(OpenGL::GL UNKNOWN IMPORTED)
  151. if(OPENGL_gl_LIBRARY MATCHES "/([^/]+)\\.framework$")
  152. set(_gl_fw "${OPENGL_gl_LIBRARY}/${CMAKE_MATCH_1}")
  153. if(EXISTS "${_gl_fw}.tbd")
  154. string(APPEND _gl_fw ".tbd")
  155. endif()
  156. set_target_properties(OpenGL::GL PROPERTIES
  157. IMPORTED_LOCATION "${_gl_fw}")
  158. else()
  159. set_target_properties(OpenGL::GL PROPERTIES
  160. IMPORTED_LOCATION "${OPENGL_gl_LIBRARY}")
  161. endif()
  162. else()
  163. add_library(OpenGL::GL INTERFACE IMPORTED)
  164. set_target_properties(OpenGL::GL PROPERTIES
  165. IMPORTED_LIBNAME "${OPENGL_gl_LIBRARY}")
  166. endif()
  167. set_target_properties(OpenGL::GL PROPERTIES
  168. INTERFACE_INCLUDE_DIRECTORIES "${OPENGL_INCLUDE_DIR}")
  169. endif()
  170. if(OPENGL_GLU_FOUND AND NOT TARGET OpenGL::GLU)
  171. if(IS_ABSOLUTE "${OPENGL_glu_LIBRARY}")
  172. add_library(OpenGL::GLU UNKNOWN IMPORTED)
  173. if(OPENGL_glu_LIBRARY MATCHES "/([^/]+)\\.framework$")
  174. set(_glu_fw "${OPENGL_glu_LIBRARY}/${CMAKE_MATCH_1}")
  175. if(EXISTS "${_glu_fw}.tbd")
  176. string(APPEND _glu_fw ".tbd")
  177. endif()
  178. set_target_properties(OpenGL::GLU PROPERTIES
  179. IMPORTED_LOCATION "${_glu_fw}")
  180. else()
  181. set_target_properties(OpenGL::GLU PROPERTIES
  182. IMPORTED_LOCATION "${OPENGL_glu_LIBRARY}")
  183. endif()
  184. else()
  185. add_library(OpenGL::GLU INTERFACE IMPORTED)
  186. set_target_properties(OpenGL::GLU PROPERTIES
  187. IMPORTED_LIBNAME "${OPENGL_glu_LIBRARY}")
  188. endif()
  189. set_target_properties(OpenGL::GLU PROPERTIES
  190. INTERFACE_LINK_LIBRARIES OpenGL::GL)
  191. endif()
  192. endif()
  193. mark_as_advanced(
  194. OPENGL_INCLUDE_DIR
  195. OPENGL_xmesa_INCLUDE_DIR
  196. OPENGL_glu_LIBRARY
  197. OPENGL_gl_LIBRARY
  198. )