FindSDL2.cmake 7.2 KB

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