2
0

FindSDL2.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #.rst:
  2. # FindSDL2
  3. # --------
  4. #
  5. # Locate SDL2 library
  6. #
  7. # This module defines
  8. #
  9. # ::
  10. #
  11. # SDL2_LIBRARY, the name of the library to link against
  12. # SDL2_FOUND, if false, do not try to link to SDL
  13. # SDL2_INCLUDE_DIR, where to find SDL.h
  14. # SDL2_VERSION_STRING, human-readable string containing the version of SDL
  15. #
  16. #
  17. #
  18. # This module responds to the flag:
  19. #
  20. # ::
  21. #
  22. # SDL2_BUILDING_LIBRARY
  23. # If this is defined, then no SDL2_main will be linked in because
  24. # only applications need main().
  25. # Otherwise, it is assumed you are building an application and this
  26. # module will attempt to locate and set the proper link flags
  27. # as part of the returned SDL2_LIBRARY variable.
  28. #
  29. #
  30. #
  31. # Don't forget to include SDLmain.h and SDLmain.m your project for the
  32. # OS X framework based version. (Other versions link to -lSDL2main which
  33. # this module will try to find on your behalf.) Also for OS X, this
  34. # module will automatically add the -framework Cocoa on your behalf.
  35. #
  36. #
  37. #
  38. # Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your
  39. # configuration and no SDL2_LIBRARY, it means CMake did not find your SDL
  40. # library (SDL2.dll, libSDL2.so, SDL.framework, etc). Set
  41. # SDL2_LIBRARY_TEMP to point to your SDL library, and configure again.
  42. # Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this
  43. # value as appropriate. These values are used to generate the final
  44. # SDL2_LIBRARY variable, but when these values are unset, SDL2_LIBRARY
  45. # does not get created.
  46. #
  47. #
  48. #
  49. # $SDL2DIR is an environment variable that would correspond to the
  50. # ./configure --prefix=$SDL2DIR used in building SDL. l.e.galup 9-20-02
  51. #
  52. # Modified by Eric Wing. Added code to assist with automated building
  53. # by using environmental variables and providing a more
  54. # controlled/consistent search behavior. Added new modifications to
  55. # recognize OS X frameworks and additional Unix paths (FreeBSD, etc).
  56. # Also corrected the header search path to follow "proper" SDL
  57. # guidelines. Added a search for SDLmain which is needed by some
  58. # platforms. Added a search for threads which is needed by some
  59. # platforms. Added needed compile switches for MinGW.
  60. #
  61. # On OSX, this will prefer the Framework version (if found) over others.
  62. # People will have to manually change the cache values of SDL2_LIBRARY to
  63. # override this selection or set the CMake environment
  64. # CMAKE_INCLUDE_PATH to modify the search paths.
  65. #
  66. #=============================================================================
  67. # Copyright 2003-2009 Kitware, Inc.
  68. # Copyright 2012 Benjamin Eikel
  69. #
  70. # Distributed under the OSI-approved BSD License (the "License");
  71. # see accompanying file kitware license.txt for details.
  72. #
  73. # This software is distributed WITHOUT ANY WARRANTY; without even the
  74. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  75. # See the License for more information.
  76. #=============================================================================
  77. # (To distribute this file outside of CMake, substitute the full
  78. # License text for the above reference.)
  79. if (NOT WIN32)
  80. find_package(PkgConfig)
  81. if (PKG_CONFIG_FOUND)
  82. pkg_check_modules(_SDL2 sdl2)
  83. set(SDL2_VERSION_STRING ${_SDL2_VERSION})
  84. endif ()
  85. endif ()
  86. find_path(SDL2_INCLUDE_DIR
  87. SDL.h
  88. HINTS
  89. ${_SDL2_INCLUDEDIR}
  90. ENV SDL2DIR
  91. PATH_SUFFIXES
  92. SDL2
  93. include/SDL2
  94. include
  95. )
  96. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  97. set(VC_LIB_PATH_SUFFIX lib/x64)
  98. else()
  99. set(VC_LIB_PATH_SUFFIX lib/x86)
  100. endif()
  101. find_library(SDL2_LIBRARY_TEMP
  102. NAMES
  103. SDL2
  104. HINTS
  105. ${_SDL2_LIBDIR}
  106. ENV SDL2DIR
  107. PATH_SUFFIXES
  108. lib
  109. ${VC_LIB_PATH_SUFFIX}
  110. )
  111. if(NOT SDL2_BUILDING_LIBRARY)
  112. if(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
  113. # Non-OS X framework versions expect you to also dynamically link to
  114. # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms
  115. # seem to provide SDLmain for compatibility even though they don't
  116. # necessarily need it.
  117. find_library(SDL2MAIN_LIBRARY
  118. NAMES
  119. SDL2main
  120. HINTS
  121. ENV SDL2DIR
  122. PATH_SUFFIXES
  123. lib
  124. ${VC_LIB_PATH_SUFFIX}
  125. PATHS
  126. /sw
  127. /opt/local
  128. /opt/csw
  129. /opt
  130. )
  131. endif()
  132. endif()
  133. # SDL may require threads on your system.
  134. # The Apple build may not need an explicit flag because one of the
  135. # frameworks may already provide it.
  136. # But for non-OSX systems, I will use the CMake Threads package.
  137. if(NOT APPLE)
  138. find_package(Threads)
  139. endif()
  140. # MinGW needs an additional library, mwindows
  141. # It's total link flags should look like -lmingw32 -lSDLmain -lSDL -lmwindows
  142. # (Actually on second look, I think it only needs one of the m* libraries.)
  143. if(MINGW)
  144. set(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
  145. endif()
  146. if(SDL2_LIBRARY_TEMP)
  147. # For SDLmain
  148. if(SDL2MAIN_LIBRARY AND NOT SDL2_BUILDING_LIBRARY)
  149. list(FIND SDL2_LIBRARY_TEMP "${SDL2MAIN_LIBRARY}" _SDL2_MAIN_INDEX)
  150. if(_SDL2_MAIN_INDEX EQUAL -1)
  151. set(SDL2_LIBRARY_TEMP "${SDL2MAIN_LIBRARY}" ${SDL2_LIBRARY_TEMP})
  152. endif()
  153. unset(_SDL2_MAIN_INDEX)
  154. endif()
  155. # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa.
  156. # CMake doesn't display the -framework Cocoa string in the UI even
  157. # though it actually is there if I modify a pre-used variable.
  158. # I think it has something to do with the CACHE STRING.
  159. # So I use a temporary variable until the end so I can set the
  160. # "real" variable in one-shot.
  161. if(APPLE)
  162. set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
  163. endif()
  164. # For threads, as mentioned Apple doesn't need this.
  165. # In fact, there seems to be a problem if I used the Threads package
  166. # and try using this line, so I'm just skipping it entirely for OS X.
  167. if(NOT APPLE)
  168. set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
  169. endif()
  170. # For MinGW library
  171. if(MINGW)
  172. set(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
  173. endif()
  174. # Set the final string here so the GUI reflects the final state.
  175. set(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found")
  176. # Set the temp variable to INTERNAL so it is not seen in the CMake GUI
  177. set(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
  178. endif()
  179. if(SDL2_INCLUDE_DIR AND EXISTS "${SDL2_INCLUDE_DIR}/SDL_version.h" AND NOT SDL2_VERSION_STRING)
  180. file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$")
  181. file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$")
  182. file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$")
  183. string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MAJOR "${SDL2_VERSION_MAJOR_LINE}")
  184. string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MINOR "${SDL2_VERSION_MINOR_LINE}")
  185. string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_PATCH "${SDL2_VERSION_PATCH_LINE}")
  186. set(SDL2_VERSION_STRING ${SDL2_VERSION_MAJOR}.${SDL2_VERSION_MINOR}.${SDL2_VERSION_PATCH})
  187. unset(SDL2_VERSION_MAJOR_LINE)
  188. unset(SDL2_VERSION_MINOR_LINE)
  189. unset(SDL2_VERSION_PATCH_LINE)
  190. unset(SDL2_VERSION_MAJOR)
  191. unset(SDL2_VERSION_MINOR)
  192. unset(SDL2_VERSION_PATCH)
  193. endif()
  194. set(SDL2_LIBRARIES ${SDL2_LIBRARY})
  195. set(SDL2_INCLUDE_DIRS ${SDL2_INCLUDE_DIR})
  196. include(FindPackageHandleStandardArgs)
  197. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2
  198. REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR
  199. VERSION_VAR SDL2_VERSION_STRING)