FindGLEW.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # PrusaSlicer specifics:
  4. # This file is backported from CMake 3.15 distribution to behave uniformly
  5. # across all versions of CMake. It explicitly adds GLEW_STATIC complile
  6. # definition to static targets which is needed to prevent link errors.
  7. #[=======================================================================[.rst:
  8. FindGLEW
  9. --------
  10. Find the OpenGL Extension Wrangler Library (GLEW)
  11. Input Variables
  12. ^^^^^^^^^^^^^^^
  13. The following variables may be set to influence this module’s behavior:
  14. ``GLEW_USE_STATIC_LIBS``
  15. to find and create :prop_tgt:`IMPORTED` target for static linkage.
  16. ``GLEW_VERBOSE``
  17. to output a detailed log of this module.
  18. Imported Targets
  19. ^^^^^^^^^^^^^^^^
  20. This module defines the following :ref:`Imported Targets <Imported Targets>`:
  21. ``GLEW::glew``
  22. The GLEW shared library.
  23. ``GLEW::glew_s``
  24. The GLEW static library, if ``GLEW_USE_STATIC_LIBS`` is set to ``TRUE``.
  25. ``GLEW::GLEW``
  26. Duplicates either ``GLEW::glew`` or ``GLEW::glew_s`` based on availability.
  27. Result Variables
  28. ^^^^^^^^^^^^^^^^
  29. This module defines the following variables:
  30. ``GLEW_INCLUDE_DIRS``
  31. include directories for GLEW
  32. ``GLEW_LIBRARIES``
  33. libraries to link against GLEW
  34. ``GLEW_SHARED_LIBRARIES``
  35. libraries to link against shared GLEW
  36. ``GLEW_STATIC_LIBRARIES``
  37. libraries to link against static GLEW
  38. ``GLEW_FOUND``
  39. true if GLEW has been found and can be used
  40. ``GLEW_VERSION``
  41. GLEW version
  42. ``GLEW_VERSION_MAJOR``
  43. GLEW major version
  44. ``GLEW_VERSION_MINOR``
  45. GLEW minor version
  46. ``GLEW_VERSION_MICRO``
  47. GLEW micro version
  48. #]=======================================================================]
  49. include(FindPackageHandleStandardArgs)
  50. find_package(GLEW CONFIG QUIET)
  51. if(GLEW_FOUND)
  52. find_package_handle_standard_args(GLEW DEFAULT_MSG GLEW_CONFIG)
  53. return()
  54. endif()
  55. if(GLEW_VERBOSE)
  56. message(STATUS "FindGLEW: did not find GLEW CMake config file. Searching for libraries.")
  57. endif()
  58. if(APPLE)
  59. find_package(OpenGL QUIET)
  60. if(OpenGL_FOUND)
  61. if(GLEW_VERBOSE)
  62. message(STATUS "FindGLEW: Found OpenGL Framework.")
  63. message(STATUS "FindGLEW: OPENGL_LIBRARIES: ${OPENGL_LIBRARIES}")
  64. endif()
  65. else()
  66. if(GLEW_VERBOSE)
  67. message(STATUS "FindGLEW: could not find GLEW library.")
  68. endif()
  69. return()
  70. endif()
  71. endif()
  72. function(__glew_set_find_library_suffix shared_or_static)
  73. if((UNIX AND NOT APPLE) AND "${shared_or_static}" MATCHES "SHARED")
  74. set(CMAKE_FIND_LIBRARY_SUFFIXES ".so" PARENT_SCOPE)
  75. elseif((UNIX AND NOT APPLE) AND "${shared_or_static}" MATCHES "STATIC")
  76. set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" PARENT_SCOPE)
  77. elseif(APPLE AND "${shared_or_static}" MATCHES "SHARED")
  78. set(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib;.so" PARENT_SCOPE)
  79. elseif(APPLE AND "${shared_or_static}" MATCHES "STATIC")
  80. set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" PARENT_SCOPE)
  81. elseif(WIN32 AND "${shared_or_static}" MATCHES "SHARED")
  82. set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib" PARENT_SCOPE)
  83. elseif(WIN32 AND "${shared_or_static}" MATCHES "STATIC")
  84. set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib;.a;.dll.a" PARENT_SCOPE)
  85. endif()
  86. if(GLEW_VERBOSE)
  87. message(STATUS "FindGLEW: CMAKE_FIND_LIBRARY_SUFFIXES for ${shared_or_static}: ${CMAKE_FIND_LIBRARY_SUFFIXES}")
  88. endif()
  89. endfunction()
  90. if(GLEW_VERBOSE)
  91. if(DEFINED GLEW_USE_STATIC_LIBS)
  92. message(STATUS "FindGLEW: GLEW_USE_STATIC_LIBS: ${GLEW_USE_STATIC_LIBS}.")
  93. else()
  94. message(STATUS "FindGLEW: GLEW_USE_STATIC_LIBS is undefined. Treated as FALSE.")
  95. endif()
  96. endif()
  97. find_path(GLEW_INCLUDE_DIR GL/glew.h)
  98. mark_as_advanced(GLEW_INCLUDE_DIR)
  99. set(GLEW_INCLUDE_DIRS ${GLEW_INCLUDE_DIR})
  100. if(GLEW_VERBOSE)
  101. message(STATUS "FindGLEW: GLEW_INCLUDE_DIR: ${GLEW_INCLUDE_DIR}")
  102. message(STATUS "FindGLEW: GLEW_INCLUDE_DIRS: ${GLEW_INCLUDE_DIRS}")
  103. endif()
  104. if("${CMAKE_GENERATOR_PLATFORM}" MATCHES "x64" OR "${CMAKE_GENERATOR}" MATCHES "Win64")
  105. set(_arch "x64")
  106. else()
  107. set(_arch "Win32")
  108. endif()
  109. set(__GLEW_CURRENT_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  110. __glew_set_find_library_suffix(SHARED)
  111. find_library(GLEW_SHARED_LIBRARY_RELEASE
  112. NAMES GLEW glew glew32
  113. PATH_SUFFIXES lib lib64 libx32 lib/Release/${_arch}
  114. PATHS ENV GLEW_ROOT)
  115. find_library(GLEW_SHARED_LIBRARY_DEBUG
  116. NAMES GLEWd glewd glew32d
  117. PATH_SUFFIXES lib lib64
  118. PATHS ENV GLEW_ROOT)
  119. __glew_set_find_library_suffix(STATIC)
  120. find_library(GLEW_STATIC_LIBRARY_RELEASE
  121. NAMES GLEW glew glew32s
  122. PATH_SUFFIXES lib lib64 libx32 lib/Release/${_arch}
  123. PATHS ENV GLEW_ROOT)
  124. find_library(GLEW_STATIC_LIBRARY_DEBUG
  125. NAMES GLEWds glewd glewds glew32ds
  126. PATH_SUFFIXES lib lib64
  127. PATHS ENV GLEW_ROOT)
  128. set(CMAKE_FIND_LIBRARY_SUFFIXES ${__GLEW_CURRENT_FIND_LIBRARY_SUFFIXES})
  129. unset(__GLEW_CURRENT_FIND_LIBRARY_SUFFIXES)
  130. include(SelectLibraryConfigurations)
  131. select_library_configurations(GLEW_SHARED)
  132. select_library_configurations(GLEW_STATIC)
  133. if(NOT GLEW_USE_STATIC_LIBS)
  134. set(GLEW_LIBRARIES ${GLEW_SHARED_LIBRARY})
  135. else()
  136. set(GLEW_LIBRARIES ${GLEW_STATIC_LIBRARY})
  137. endif()
  138. if(GLEW_VERBOSE)
  139. message(STATUS "FindGLEW: GLEW_SHARED_LIBRARY_RELEASE: ${GLEW_SHARED_LIBRARY_RELEASE}")
  140. message(STATUS "FindGLEW: GLEW_STATIC_LIBRARY_RELEASE: ${GLEW_STATIC_LIBRARY_RELEASE}")
  141. message(STATUS "FindGLEW: GLEW_SHARED_LIBRARY_DEBUG: ${GLEW_SHARED_LIBRARY_DEBUG}")
  142. message(STATUS "FindGLEW: GLEW_STATIC_LIBRARY_DEBUG: ${GLEW_STATIC_LIBRARY_DEBUG}")
  143. message(STATUS "FindGLEW: GLEW_SHARED_LIBRARY: ${GLEW_SHARED_LIBRARY}")
  144. message(STATUS "FindGLEW: GLEW_STATIC_LIBRARY: ${GLEW_STATIC_LIBRARY}")
  145. message(STATUS "FindGLEW: GLEW_LIBRARIES: ${GLEW_LIBRARIES}")
  146. endif()
  147. # Read version from GL/glew.h file
  148. if(EXISTS "${GLEW_INCLUDE_DIR}/GL/glew.h")
  149. file(STRINGS "${GLEW_INCLUDE_DIR}/GL/glew.h" _contents REGEX "^VERSION_.+ [0-9]+")
  150. if(_contents)
  151. string(REGEX REPLACE ".*VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" GLEW_VERSION_MAJOR "${_contents}")
  152. string(REGEX REPLACE ".*VERSION_MINOR[ \t]+([0-9]+).*" "\\1" GLEW_VERSION_MINOR "${_contents}")
  153. string(REGEX REPLACE ".*VERSION_MICRO[ \t]+([0-9]+).*" "\\1" GLEW_VERSION_MICRO "${_contents}")
  154. set(GLEW_VERSION "${GLEW_VERSION_MAJOR}.${GLEW_VERSION_MINOR}.${GLEW_VERSION_MICRO}")
  155. endif()
  156. endif()
  157. if(GLEW_VERBOSE)
  158. message(STATUS "FindGLEW: GLEW_VERSION_MAJOR: ${GLEW_VERSION_MAJOR}")
  159. message(STATUS "FindGLEW: GLEW_VERSION_MINOR: ${GLEW_VERSION_MINOR}")
  160. message(STATUS "FindGLEW: GLEW_VERSION_MICRO: ${GLEW_VERSION_MICRO}")
  161. message(STATUS "FindGLEW: GLEW_VERSION: ${GLEW_VERSION}")
  162. endif()
  163. find_package_handle_standard_args(GLEW
  164. REQUIRED_VARS GLEW_INCLUDE_DIRS GLEW_LIBRARIES
  165. VERSION_VAR GLEW_VERSION)
  166. if(NOT GLEW_FOUND)
  167. if(GLEW_VERBOSE)
  168. message(STATUS "FindGLEW: could not find GLEW library.")
  169. endif()
  170. return()
  171. endif()
  172. if(NOT TARGET GLEW::glew AND NOT GLEW_USE_STATIC_LIBS)
  173. if(GLEW_VERBOSE)
  174. message(STATUS "FindGLEW: Creating GLEW::glew imported target.")
  175. endif()
  176. add_library(GLEW::glew UNKNOWN IMPORTED)
  177. set_target_properties(GLEW::glew
  178. PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${GLEW_INCLUDE_DIRS}")
  179. if(APPLE)
  180. set_target_properties(GLEW::glew
  181. PROPERTIES INTERFACE_LINK_LIBRARIES OpenGL::GL)
  182. endif()
  183. if(GLEW_SHARED_LIBRARY_RELEASE)
  184. set_property(TARGET GLEW::glew
  185. APPEND
  186. PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
  187. set_target_properties(GLEW::glew
  188. PROPERTIES IMPORTED_LOCATION_RELEASE "${GLEW_SHARED_LIBRARY_RELEASE}")
  189. endif()
  190. if(GLEW_SHARED_LIBRARY_DEBUG)
  191. set_property(TARGET GLEW::glew
  192. APPEND
  193. PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
  194. set_target_properties(GLEW::glew
  195. PROPERTIES IMPORTED_LOCATION_DEBUG "${GLEW_SHARED_LIBRARY_DEBUG}")
  196. endif()
  197. elseif(NOT TARGET GLEW::glew_s AND GLEW_USE_STATIC_LIBS)
  198. if(GLEW_VERBOSE)
  199. message(STATUS "FindGLEW: Creating GLEW::glew_s imported target.")
  200. endif()
  201. add_library(GLEW::glew_s UNKNOWN IMPORTED)
  202. set_target_properties(GLEW::glew_s
  203. PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${GLEW_INCLUDE_DIRS}")
  204. set_target_properties(GLEW::glew_s PROPERTIES INTERFACE_COMPILE_DEFINITIONS GLEW_STATIC)
  205. if(APPLE)
  206. set_target_properties(GLEW::glew_s
  207. PROPERTIES INTERFACE_LINK_LIBRARIES OpenGL::GL)
  208. endif()
  209. if(GLEW_STATIC_LIBRARY_RELEASE)
  210. set_property(TARGET GLEW::glew_s
  211. APPEND
  212. PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
  213. set_target_properties(GLEW::glew_s
  214. PROPERTIES IMPORTED_LOCATION_RELEASE "${GLEW_STATIC_LIBRARY_RELEASE}")
  215. endif()
  216. if(GLEW_STATIC_LIBRARY_DEBUG)
  217. set_property(TARGET GLEW::glew_s
  218. APPEND
  219. PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
  220. set_target_properties(GLEW::glew_s
  221. PROPERTIES IMPORTED_LOCATION_DEBUG "${GLEW_STATIC_LIBRARY_DEBUG}")
  222. endif()
  223. endif()
  224. if(NOT TARGET GLEW::GLEW)
  225. if(GLEW_VERBOSE)
  226. message(STATUS "FindGLEW: Creating GLEW::GLEW imported target.")
  227. endif()
  228. add_library(GLEW::GLEW UNKNOWN IMPORTED)
  229. set_target_properties(GLEW::GLEW
  230. PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${GLEW_INCLUDE_DIRS}")
  231. if(APPLE)
  232. set_target_properties(GLEW::GLEW
  233. PROPERTIES INTERFACE_LINK_LIBRARIES OpenGL::GL)
  234. endif()
  235. if(TARGET GLEW::glew)
  236. if(GLEW_SHARED_LIBRARY_RELEASE)
  237. set_property(TARGET GLEW::GLEW
  238. APPEND
  239. PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
  240. set_target_properties(GLEW::GLEW
  241. PROPERTIES IMPORTED_LOCATION_RELEASE "${GLEW_SHARED_LIBRARY_RELEASE}")
  242. endif()
  243. if(GLEW_SHARED_LIBRARY_DEBUG)
  244. set_property(TARGET GLEW::GLEW
  245. APPEND
  246. PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
  247. set_target_properties(GLEW::GLEW
  248. PROPERTIES IMPORTED_LOCATION_DEBUG "${GLEW_SHARED_LIBRARY_DEBUG}")
  249. endif()
  250. elseif(TARGET GLEW::glew_s)
  251. if(GLEW_STATIC_LIBRARY_RELEASE)
  252. set_property(TARGET GLEW::GLEW
  253. APPEND
  254. PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
  255. set_target_properties(GLEW::GLEW
  256. PROPERTIES IMPORTED_LOCATION_RELEASE "${GLEW_STATIC_LIBRARY_RELEASE}"
  257. INTERFACE_COMPILE_DEFINITIONS GLEW_STATIC)
  258. endif()
  259. if(GLEW_STATIC_LIBRARY_DEBUG AND GLEW_USE_STATIC_LIBS)
  260. set_property(TARGET GLEW::GLEW
  261. APPEND
  262. PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
  263. set_target_properties(GLEW::GLEW
  264. PROPERTIES IMPORTED_LOCATION_DEBUG "${GLEW_STATIC_LIBRARY_DEBUG}"
  265. INTERFACE_COMPILE_DEFINITIONS GLEW_STATIC)
  266. endif()
  267. elseif(GLEW_VERBOSE)
  268. message(WARNING "FindGLEW: no `GLEW::glew` or `GLEW::glew_s` target was created. Something went wrong in FindGLEW target creation.")
  269. endif()
  270. endif()