FindSDL2.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # Copyright 2019 Amine Ben Hassouna <[email protected]>
  4. # Copyright 2000-2019 Kitware, Inc. and Contributors
  5. # All rights reserved.
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. # * Neither the name of Kitware, Inc. nor the names of Contributors
  15. # may be used to endorse or promote products derived from this
  16. # software without specific prior written permission.
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #[=======================================================================[.rst:
  29. FindSDL2
  30. --------
  31. Locate SDL2 library
  32. This module defines the following 'IMPORTED' targets:
  33. ::
  34. SDL2::SDL2
  35. The SDL2 library, if found.
  36. Libraries should link to SDL2::SDL2
  37. SDL2::SDL2main
  38. The SDL2main library, if found.
  39. Applications should link to SDL2::SDL2main instead of SDL2::SDL2
  40. This module will set the following variables in your project:
  41. ::
  42. SDL2_LIBRARIES, the name of the library to link against
  43. SDL2_INCLUDE_DIRS, where to find SDL.h
  44. SDL2_FOUND, if false, do not try to link to SDL2
  45. SDL2MAIN_FOUND, if false, do not try to link to SDL2main
  46. SDL2_VERSION_STRING, human-readable string containing the version of SDL2
  47. This module responds to the following cache variables:
  48. ::
  49. SDL2_PATH
  50. Set a custom SDL2 Library path (default: empty)
  51. SDL2_NO_DEFAULT_PATH
  52. Disable search SDL2 Library in default path.
  53. If SDL2_PATH (default: ON)
  54. Else (default: OFF)
  55. SDL2_INCLUDE_DIR
  56. SDL2 headers path.
  57. SDL2_LIBRARY
  58. SDL2 Library (.dll, .so, .a, etc) path.
  59. SDL2MAIN_LIBRARY
  60. SDL2main Library (.a) path.
  61. SDL2_BUILDING_LIBRARY
  62. This flag is useful only when linking to SDL2_LIBRARIES instead of
  63. SDL2::SDL2main. It is required only when building a library that links to
  64. SDL2_LIBRARIES, because only applications need main() (No need to also
  65. link to SDL2main).
  66. If this flag is defined, then no SDL2main will be added to SDL2_LIBRARIES
  67. and no SDL2::SDL2main target will be created.
  68. Don't forget to include SDLmain.h and SDLmain.m in your project for the
  69. OS X framework based version. (Other versions link to -lSDL2main which
  70. this module will try to find on your behalf.) Also for OS X, this
  71. module will automatically add the -framework Cocoa on your behalf.
  72. Additional Note: If you see an empty SDL2_LIBRARY in your project
  73. configuration, it means CMake did not find your SDL2 library
  74. (SDL2.dll, libsdl2.so, SDL2.framework, etc). Set SDL2_LIBRARY to point
  75. to your SDL2 library, and configure again. Similarly, if you see an
  76. empty SDL2MAIN_LIBRARY, you should set this value as appropriate. These
  77. values are used to generate the final SDL2_LIBRARIES variable and the
  78. SDL2::SDL2 and SDL2::SDL2main targets, but when these values are unset,
  79. SDL2_LIBRARIES, SDL2::SDL2 and SDL2::SDL2main does not get created.
  80. $SDL2DIR is an environment variable that would correspond to the
  81. ./configure --prefix=$SDL2DIR used in building SDL2. l.e.galup 9-20-02
  82. Created by Amine Ben Hassouna:
  83. Adapt FindSDL.cmake to SDL2 (FindSDL2.cmake).
  84. Add cache variables for more flexibility:
  85. SDL2_PATH, SDL2_NO_DEFAULT_PATH (for details, see doc above).
  86. Mark 'Threads' as a required dependency for non-OSX systems.
  87. Modernize the FindSDL2.cmake module by creating specific targets:
  88. SDL2::SDL2 and SDL2::SDL2main (for details, see doc above).
  89. Original FindSDL.cmake module:
  90. Modified by Eric Wing. Added code to assist with automated building
  91. by using environmental variables and providing a more
  92. controlled/consistent search behavior. Added new modifications to
  93. recognize OS X frameworks and additional Unix paths (FreeBSD, etc).
  94. Also corrected the header search path to follow "proper" SDL
  95. guidelines. Added a search for SDLmain which is needed by some
  96. platforms. Added a search for threads which is needed by some
  97. platforms. Added needed compile switches for MinGW.
  98. On OSX, this will prefer the Framework version (if found) over others.
  99. People will have to manually change the cache value of SDL2_LIBRARY to
  100. override this selection or set the SDL2_PATH variable or the CMake
  101. environment CMAKE_INCLUDE_PATH to modify the search paths.
  102. Note that the header path has changed from SDL/SDL.h to just SDL.h
  103. This needed to change because "proper" SDL convention is #include
  104. "SDL.h", not <SDL/SDL.h>. This is done for portability reasons
  105. because not all systems place things in SDL/ (see FreeBSD).
  106. #]=======================================================================]
  107. # Define options for searching SDL2 Library in a custom path
  108. set(SDL2_PATH "" CACHE STRING "Custom SDL2 Library path")
  109. set(_SDL2_NO_DEFAULT_PATH OFF)
  110. if(SDL2_PATH)
  111. set(_SDL2_NO_DEFAULT_PATH ON)
  112. endif()
  113. set(SDL2_NO_DEFAULT_PATH ${_SDL2_NO_DEFAULT_PATH}
  114. CACHE BOOL "Disable search SDL2 Library in default path")
  115. unset(_SDL2_NO_DEFAULT_PATH)
  116. set(SDL2_NO_DEFAULT_PATH_CMD)
  117. if(SDL2_NO_DEFAULT_PATH)
  118. set(SDL2_NO_DEFAULT_PATH_CMD NO_DEFAULT_PATH)
  119. endif()
  120. # Search for the SDL2 include directory
  121. find_path(SDL2_INCLUDE_DIR SDL.h
  122. HINTS
  123. ENV SDL2DIR
  124. ${SDL2_NO_DEFAULT_PATH_CMD}
  125. PATH_SUFFIXES SDL2
  126. # path suffixes to search inside ENV{SDL2DIR}
  127. include/SDL2 include
  128. PATHS ${SDL2_PATH}
  129. DOC "Where the SDL2 headers can be found"
  130. )
  131. set(SDL2_INCLUDE_DIRS "${SDL2_INCLUDE_DIR}")
  132. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  133. set(VC_LIB_PATH_SUFFIX lib/x64)
  134. else()
  135. set(VC_LIB_PATH_SUFFIX lib/x86)
  136. endif()
  137. # SDL-2.0 is the name used by FreeBSD ports...
  138. # don't confuse it for the version number.
  139. find_library(SDL2_LIBRARY
  140. NAMES SDL2 SDL-2.0
  141. HINTS
  142. ENV SDL2DIR
  143. ${SDL2_NO_DEFAULT_PATH_CMD}
  144. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  145. PATHS ${SDL2_PATH}
  146. DOC "Where the SDL2 Library can be found"
  147. )
  148. set(SDL2_LIBRARIES "${SDL2_LIBRARY}")
  149. if(NOT SDL2_BUILDING_LIBRARY)
  150. if(NOT SDL2_INCLUDE_DIR MATCHES ".framework")
  151. # Non-OS X framework versions expect you to also dynamically link to
  152. # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
  153. # seem to provide SDL2main for compatibility even though they don't
  154. # necessarily need it.
  155. if(SDL2_PATH)
  156. set(SDL2MAIN_LIBRARY_PATHS "${SDL2_PATH}")
  157. endif()
  158. if(NOT SDL2_NO_DEFAULT_PATH)
  159. set(SDL2MAIN_LIBRARY_PATHS
  160. /sw
  161. /opt/local
  162. /opt/csw
  163. /opt
  164. "${SDL2MAIN_LIBRARY_PATHS}"
  165. )
  166. endif()
  167. find_library(SDL2MAIN_LIBRARY
  168. NAMES SDL2main
  169. HINTS
  170. ENV SDL2DIR
  171. ${SDL2_NO_DEFAULT_PATH_CMD}
  172. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  173. PATHS ${SDL2MAIN_LIBRARY_PATHS}
  174. DOC "Where the SDL2main library can be found"
  175. )
  176. unset(SDL2MAIN_LIBRARY_PATHS)
  177. endif()
  178. endif()
  179. # SDL2 may require threads on your system.
  180. # The Apple build may not need an explicit flag because one of the
  181. # frameworks may already provide it.
  182. # But for non-OSX systems, I will use the CMake Threads package.
  183. if(NOT APPLE)
  184. find_package(Threads QUIET)
  185. if(NOT Threads_FOUND)
  186. set(SDL2_THREADS_NOT_FOUND "Could NOT find Threads (Threads is required by SDL2).")
  187. if(SDL2_FIND_REQUIRED)
  188. message(FATAL_ERROR ${SDL2_THREADS_NOT_FOUND})
  189. else()
  190. if(NOT SDL2_FIND_QUIETLY)
  191. message(STATUS ${SDL2_THREADS_NOT_FOUND})
  192. endif()
  193. return()
  194. endif()
  195. unset(SDL2_THREADS_NOT_FOUND)
  196. endif()
  197. endif()
  198. # MinGW needs an additional link flag, -mwindows
  199. # It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -mwindows
  200. if(MINGW)
  201. set(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "link flags for MinGW")
  202. endif()
  203. if(SDL2_LIBRARY)
  204. # For SDL2main
  205. if(SDL2MAIN_LIBRARY AND NOT SDL2_BUILDING_LIBRARY)
  206. list(FIND SDL2_LIBRARIES "${SDL2MAIN_LIBRARY}" _SDL2_MAIN_INDEX)
  207. if(_SDL2_MAIN_INDEX EQUAL -1)
  208. set(SDL2_LIBRARIES "${SDL2MAIN_LIBRARY}" ${SDL2_LIBRARIES})
  209. endif()
  210. unset(_SDL2_MAIN_INDEX)
  211. endif()
  212. # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
  213. # CMake doesn't display the -framework Cocoa string in the UI even
  214. # though it actually is there if I modify a pre-used variable.
  215. # I think it has something to do with the CACHE STRING.
  216. # So I use a temporary variable until the end so I can set the
  217. # "real" variable in one-shot.
  218. if(APPLE_MACOS)
  219. set(SDL2_LIBRARIES ${SDL2_LIBRARIES} -framework Cocoa)
  220. endif()
  221. # For threads, as mentioned Apple doesn't need this.
  222. # In fact, there seems to be a problem if I used the Threads package
  223. # and try using this line, so I'm just skipping it entirely for OS X.
  224. if(NOT APPLE)
  225. set(SDL2_LIBRARIES ${SDL2_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
  226. endif()
  227. # For MinGW library
  228. if(MINGW)
  229. set(SDL2_LIBRARIES ${MINGW32_LIBRARY} ${SDL2_LIBRARIES})
  230. endif()
  231. endif()
  232. # Read SDL2 version
  233. if(SDL2_INCLUDE_DIR AND EXISTS "${SDL2_INCLUDE_DIR}/SDL_version.h")
  234. file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$")
  235. file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$")
  236. file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$")
  237. string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MAJOR "${SDL2_VERSION_MAJOR_LINE}")
  238. string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MINOR "${SDL2_VERSION_MINOR_LINE}")
  239. string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_PATCH "${SDL2_VERSION_PATCH_LINE}")
  240. set(SDL2_VERSION_STRING ${SDL2_VERSION_MAJOR}.${SDL2_VERSION_MINOR}.${SDL2_VERSION_PATCH})
  241. unset(SDL2_VERSION_MAJOR_LINE)
  242. unset(SDL2_VERSION_MINOR_LINE)
  243. unset(SDL2_VERSION_PATCH_LINE)
  244. unset(SDL2_VERSION_MAJOR)
  245. unset(SDL2_VERSION_MINOR)
  246. unset(SDL2_VERSION_PATCH)
  247. endif()
  248. include(FindPackageHandleStandardArgs)
  249. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2
  250. REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR
  251. VERSION_VAR SDL2_VERSION_STRING)
  252. mark_as_advanced(SDL2_PATH
  253. SDL2_NO_DEFAULT_PATH
  254. SDL2_LIBRARY
  255. SDL2MAIN_LIBRARY
  256. SDL2_INCLUDE_DIR
  257. SDL2_BUILDING_LIBRARY)
  258. # SDL2:: targets (SDL2::SDL2 and SDL2::SDL2main)
  259. if(SDL2_FOUND)
  260. # SDL2::SDL2 target
  261. if(SDL2_LIBRARY AND NOT TARGET SDL2::SDL2)
  262. add_library(SDL2::SDL2 UNKNOWN IMPORTED)
  263. set_target_properties(SDL2::SDL2 PROPERTIES
  264. IMPORTED_LOCATION "${SDL2_LIBRARY}"
  265. INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}")
  266. if(APPLE)
  267. # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
  268. # For more details, please see above.
  269. if (APPLE_MACOS)
  270. set_property(TARGET SDL2::SDL2 APPEND PROPERTY
  271. INTERFACE_LINK_OPTIONS -framework Cocoa)
  272. elseif (APPLE_IOS)
  273. target_link_libraries(SDL2::SDL2 INTERFACE
  274. "-framework AudioToolbox"
  275. "-framework AVFoundation"
  276. "-framework CoreAudio"
  277. "-framework CoreBluetooth"
  278. "-framework CoreFoundation"
  279. "-framework CoreGraphics"
  280. "-framework CoreMotion"
  281. "-framework CoreVideo"
  282. "-framework GameController"
  283. "-framework IOKit"
  284. "-framework Metal"
  285. "-framework OpenGLES"
  286. "-framework QuartzCore"
  287. "-framework UIKit"
  288. "-weak_framework CoreHaptics"
  289. )
  290. endif()
  291. else()
  292. # For threads, as mentioned Apple doesn't need this.
  293. # For more details, please see above.
  294. set_property(TARGET SDL2::SDL2 APPEND PROPERTY
  295. INTERFACE_LINK_LIBRARIES Threads::Threads)
  296. endif()
  297. endif()
  298. # SDL2::SDL2main target
  299. # Applications should link to SDL2::SDL2main instead of SDL2::SDL2
  300. # For more details, please see above.
  301. if(NOT SDL2_BUILDING_LIBRARY AND NOT TARGET SDL2::SDL2main)
  302. if(SDL2_INCLUDE_DIR MATCHES ".framework" OR NOT SDL2MAIN_LIBRARY)
  303. add_library(SDL2::SDL2main INTERFACE IMPORTED)
  304. set_property(TARGET SDL2::SDL2main PROPERTY
  305. INTERFACE_LINK_LIBRARIES SDL2::SDL2)
  306. elseif(SDL2MAIN_LIBRARY)
  307. # MinGW requires that the mingw32 library is specified before the
  308. # libSDL2main.a static library when linking.
  309. # The SDL2::SDL2mainInternal target is used internally to make sure that
  310. # CMake respects this condition.
  311. add_library(SDL2::SDL2mainInternal UNKNOWN IMPORTED)
  312. set_property(TARGET SDL2::SDL2mainInternal PROPERTY
  313. IMPORTED_LOCATION "${SDL2MAIN_LIBRARY}")
  314. set_property(TARGET SDL2::SDL2mainInternal PROPERTY
  315. INTERFACE_LINK_LIBRARIES SDL2::SDL2)
  316. add_library(SDL2::SDL2main INTERFACE IMPORTED)
  317. if(MINGW)
  318. # MinGW needs an additional link flag '-mwindows' and link to mingw32
  319. set_property(TARGET SDL2::SDL2main PROPERTY
  320. INTERFACE_LINK_LIBRARIES "mingw32" "-mwindows")
  321. endif()
  322. set_property(TARGET SDL2::SDL2main APPEND PROPERTY
  323. INTERFACE_LINK_LIBRARIES SDL2::SDL2mainInternal)
  324. endif()
  325. endif()
  326. endif()