9
3

FindSDL2.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # - Find SDL2
  2. # Find the SDL2 headers and libraries
  3. #
  4. # SDL2::SDL2 - Imported target to use for building a library
  5. # SDL2::SDL2main - Imported interface target to use if you want SDL and SDLmain.
  6. # SDL2_FOUND - True if SDL2 was found.
  7. # SDL2_DYNAMIC - If we found a DLL version of SDL (meaning you might want to copy a DLL from SDL2::SDL2)
  8. #
  9. # Original Author:
  10. # 2015 Ryan Pavlik <ryan.pavlik@gmail.com> <abiryan@ryand.net>
  11. #
  12. # Copyright Sensics, Inc. 2015.
  13. # Distributed under the Boost Software License, Version 1.0.
  14. # (See accompanying file LICENSE_1_0.txt or copy at
  15. # http://www.boost.org/LICENSE_1_0.txt)
  16. # Set up architectures (for windows) and prefixes (for mingw builds)
  17. if(WIN32)
  18. if(MINGW)
  19. include(MinGWSearchPathExtras OPTIONAL)
  20. if(MINGWSEARCH_TARGET_TRIPLE)
  21. set(SDL2_PREFIX ${MINGWSEARCH_TARGET_TRIPLE})
  22. endif()
  23. endif()
  24. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  25. set(SDL2_LIB_PATH_SUFFIX lib/x64)
  26. if(NOT MSVC AND NOT SDL2_PREFIX)
  27. set(SDL2_PREFIX x86_64-w64-mingw32)
  28. endif()
  29. else()
  30. set(SDL2_LIB_PATH_SUFFIX lib/x86)
  31. if(NOT MSVC AND NOT SDL2_PREFIX)
  32. set(SDL2_PREFIX i686-w64-mingw32)
  33. endif()
  34. endif()
  35. endif()
  36. if(SDL2_PREFIX)
  37. set(SDL2_ORIGPREFIXPATH ${CMAKE_PREFIX_PATH})
  38. if(SDL2_ROOT_DIR)
  39. list(APPEND CMAKE_PREFIX_PATH "${SDL2_ROOT_DIR}")
  40. endif()
  41. if(CMAKE_PREFIX_PATH)
  42. foreach(_prefix ${CMAKE_PREFIX_PATH})
  43. list(APPEND CMAKE_PREFIX_PATH "${_prefix}/${SDL2_PREFIX}")
  44. endforeach()
  45. endif()
  46. if(MINGWSEARCH_PREFIXES)
  47. list(APPEND CMAKE_PREFIX_PATH ${MINGWSEARCH_PREFIXES})
  48. endif()
  49. endif()
  50. # Invoke pkgconfig for hints
  51. find_package(PkgConfig QUIET)
  52. set(SDL2_INCLUDE_HINTS)
  53. set(SDL2_LIB_HINTS)
  54. if(PKG_CONFIG_FOUND)
  55. pkg_search_module(SDL2PC QUIET sdl2)
  56. if(SDL2PC_INCLUDE_DIRS)
  57. set(SDL2_INCLUDE_HINTS ${SDL2PC_INCLUDE_DIRS})
  58. endif()
  59. if(SDL2PC_LIBRARY_DIRS)
  60. set(SDL2_LIB_HINTS ${SDL2PC_LIBRARY_DIRS})
  61. endif()
  62. endif()
  63. include(FindPackageHandleStandardArgs)
  64. find_library(SDL2_LIBRARY
  65. NAMES
  66. SDL2
  67. HINTS
  68. ${SDL2_LIB_HINTS}
  69. PATHS
  70. ${SDL2_ROOT_DIR}
  71. ENV SDL2DIR
  72. PATH_SUFFIXES lib SDL2 ${SDL2_LIB_PATH_SUFFIX})
  73. set(_sdl2_framework FALSE)
  74. # Some special-casing if we've found/been given a framework.
  75. # Handles whether we're given the library inside the framework or the framework itself.
  76. if(APPLE AND "${SDL2_LIBRARY}" MATCHES "(/[^/]+)*.framework(/.*)?$")
  77. set(_sdl2_framework TRUE)
  78. set(SDL2_FRAMEWORK "${SDL2_LIBRARY}")
  79. # Move up in the directory tree as required to get the framework directory.
  80. while("${SDL2_FRAMEWORK}" MATCHES "(/[^/]+)*.framework(/.*)$" AND NOT "${SDL2_FRAMEWORK}" MATCHES "(/[^/]+)*.framework$")
  81. get_filename_component(SDL2_FRAMEWORK "${SDL2_FRAMEWORK}" DIRECTORY)
  82. endwhile()
  83. if("${SDL2_FRAMEWORK}" MATCHES "(/[^/]+)*.framework$")
  84. set(SDL2_FRAMEWORK_NAME ${CMAKE_MATCH_1})
  85. # If we found a framework, do a search for the header ahead of time that will be more likely to get the framework header.
  86. find_path(SDL2_INCLUDE_DIR
  87. NAMES
  88. SDL_haptic.h # this file was introduced with SDL2
  89. HINTS
  90. "${SDL2_FRAMEWORK}/Headers/")
  91. else()
  92. # For some reason we couldn't get the framework directory itself.
  93. # Shouldn't happen, but might if something is weird.
  94. unset(SDL2_FRAMEWORK)
  95. endif()
  96. endif()
  97. find_path(SDL2_INCLUDE_DIR
  98. NAMES
  99. SDL_haptic.h # this file was introduced with SDL2
  100. HINTS
  101. ${SDL2_INCLUDE_HINTS}
  102. PATHS
  103. ${SDL2_ROOT_DIR}
  104. ENV SDL2DIR
  105. PATH_SUFFIXES include include/sdl2 include/SDL2 SDL2)
  106. if(WIN32 AND SDL2_LIBRARY)
  107. find_file(SDL2_RUNTIME_LIBRARY
  108. NAMES
  109. SDL2.dll
  110. libSDL2.dll
  111. HINTS
  112. ${SDL2_LIB_HINTS}
  113. PATHS
  114. ${SDL2_ROOT_DIR}
  115. ENV SDL2DIR
  116. PATH_SUFFIXES bin lib ${SDL2_LIB_PATH_SUFFIX})
  117. endif()
  118. if(WIN32 OR ANDROID OR IOS OR (APPLE AND NOT _sdl2_framework))
  119. set(SDL2_EXTRA_REQUIRED SDL2_SDLMAIN_LIBRARY)
  120. find_library(SDL2_SDLMAIN_LIBRARY
  121. NAMES
  122. SDL2main
  123. PATHS
  124. ${SDL2_ROOT_DIR}
  125. ENV SDL2DIR
  126. PATH_SUFFIXES lib ${SDL2_LIB_PATH_SUFFIX})
  127. endif()
  128. if(MINGW AND NOT SDL2PC_FOUND)
  129. find_library(SDL2_MINGW_LIBRARY mingw32)
  130. find_library(SDL2_MWINDOWS_LIBRARY mwindows)
  131. endif()
  132. if(SDL2_PREFIX)
  133. # Restore things the way they used to be.
  134. set(CMAKE_PREFIX_PATH ${SDL2_ORIGPREFIXPATH})
  135. endif()
  136. # handle the QUIETLY and REQUIRED arguments and set QUATLIB_FOUND to TRUE if
  137. # all listed variables are TRUE
  138. include(FindPackageHandleStandardArgs)
  139. find_package_handle_standard_args(SDL2
  140. DEFAULT_MSG
  141. SDL2_LIBRARY
  142. SDL2_INCLUDE_DIR
  143. ${SDL2_EXTRA_REQUIRED})
  144. if(SDL2_FOUND)
  145. if(NOT TARGET SDL2::SDL2)
  146. # Create SDL2::SDL2
  147. if(WIN32 AND SDL2_RUNTIME_LIBRARY)
  148. set(SDL2_DYNAMIC TRUE)
  149. add_library(SDL2::SDL2 SHARED IMPORTED)
  150. set_target_properties(SDL2::SDL2
  151. PROPERTIES
  152. IMPORTED_IMPLIB "${SDL2_LIBRARY}"
  153. IMPORTED_LOCATION "${SDL2_RUNTIME_LIBRARY}"
  154. INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
  155. )
  156. else()
  157. add_library(SDL2::SDL2 UNKNOWN IMPORTED)
  158. if(SDL2_FRAMEWORK AND SDL2_FRAMEWORK_NAME)
  159. # Handle the case that SDL2 is a framework and we were able to decompose it above.
  160. set_target_properties(SDL2::SDL2 PROPERTIES
  161. IMPORTED_LOCATION "${SDL2_FRAMEWORK}/${SDL2_FRAMEWORK_NAME}")
  162. elseif(_sdl2_framework AND SDL2_LIBRARY MATCHES "(/[^/]+)*.framework$")
  163. # Handle the case that SDL2 is a framework and SDL_LIBRARY is just the framework itself.
  164. # This takes the basename of the framework, without the extension,
  165. # and sets it (as a child of the framework) as the imported location for the target.
  166. # This is the library symlink inside of the framework.
  167. set_target_properties(SDL2::SDL2 PROPERTIES
  168. IMPORTED_LOCATION "${SDL2_LIBRARY}/${CMAKE_MATCH_1}")
  169. else()
  170. # Handle non-frameworks (including non-Mac), as well as the case that we're given the library inside of the framework
  171. set_target_properties(SDL2::SDL2 PROPERTIES
  172. IMPORTED_LOCATION "${SDL2_LIBRARY}")
  173. endif()
  174. set_target_properties(SDL2::SDL2
  175. PROPERTIES
  176. INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
  177. )
  178. endif()
  179. if(APPLE)
  180. # Need Cocoa here, is always a framework
  181. find_library(SDL2_COCOA_LIBRARY Cocoa)
  182. list(APPEND SDL2_EXTRA_REQUIRED SDL2_COCOA_LIBRARY)
  183. if(SDL2_COCOA_LIBRARY)
  184. set_target_properties(SDL2::SDL2 PROPERTIES
  185. IMPORTED_LINK_INTERFACE_LIBRARIES ${SDL2_COCOA_LIBRARY})
  186. endif()
  187. endif()
  188. # Compute what to do with SDL2main
  189. set(SDL2MAIN_LIBRARIES SDL2::SDL2)
  190. add_library(SDL2::SDL2main INTERFACE IMPORTED)
  191. if(SDL2_SDLMAIN_LIBRARY)
  192. add_library(SDL2::SDL2main_real STATIC IMPORTED)
  193. set_target_properties(SDL2::SDL2main_real
  194. PROPERTIES
  195. IMPORTED_LOCATION "${SDL2_SDLMAIN_LIBRARY}")
  196. set(SDL2MAIN_LIBRARIES SDL2::SDL2main_real ${SDL2MAIN_LIBRARIES})
  197. endif()
  198. if(MINGW)
  199. # MinGW requires some additional libraries to appear earlier in the link line.
  200. if(SDL2PC_LIBRARIES)
  201. # Use pkgconfig-suggested extra libraries if available.
  202. list(REMOVE_ITEM SDL2PC_LIBRARIES SDL2main SDL2)
  203. set(SDL2MAIN_LIBRARIES ${SDL2PC_LIBRARIES} ${SDL2MAIN_LIBRARIES})
  204. else()
  205. # fall back to extra libraries specified in pkg-config in
  206. # an official binary distro of SDL2 for MinGW I downloaded
  207. if(SDL2_MINGW_LIBRARY)
  208. set(SDL2MAIN_LIBRARIES ${SDL2_MINGW_LIBRARY} ${SDL2MAIN_LIBRARIES})
  209. endif()
  210. if(SDL2_MWINDOWS_LIBRARY)
  211. set(SDL2MAIN_LIBRARIES ${SDL2_MWINDOWS_LIBRARY} ${SDL2MAIN_LIBRARIES})
  212. endif()
  213. endif()
  214. set_target_properties(SDL2::SDL2main
  215. PROPERTIES
  216. INTERFACE_COMPILE_DEFINITIONS "main=SDL_main")
  217. endif()
  218. set_target_properties(SDL2::SDL2main
  219. PROPERTIES
  220. INTERFACE_LINK_LIBRARIES "${SDL2MAIN_LIBRARIES}")
  221. endif()
  222. mark_as_advanced(SDL2_ROOT_DIR)
  223. endif()
  224. mark_as_advanced(SDL2_LIBRARY
  225. SDL2_RUNTIME_LIBRARY
  226. SDL2_INCLUDE_DIR
  227. SDL2_SDLMAIN_LIBRARY
  228. SDL2_COCOA_LIBRARY
  229. SDL2_MINGW_LIBRARY
  230. SDL2_MWINDOWS_LIBRARY)