FindSDL.cmake 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. FindSDL
  5. -------
  6. Locate the SDL library
  7. Imported targets
  8. ^^^^^^^^^^^^^^^^
  9. .. versionadded:: 3.19
  10. This module defines the following :prop_tgt:`IMPORTED` target:
  11. ``SDL::SDL``
  12. The SDL library, if found
  13. Result variables
  14. ^^^^^^^^^^^^^^^^
  15. This module will set the following variables in your project:
  16. ``SDL_INCLUDE_DIRS``
  17. where to find SDL.h
  18. ``SDL_LIBRARIES``
  19. the name of the library to link against
  20. ``SDL_FOUND``
  21. if false, do not try to link to SDL
  22. ``SDL_VERSION``
  23. the human-readable string containing the version of SDL if found
  24. ``SDL_VERSION_MAJOR``
  25. SDL major version
  26. ``SDL_VERSION_MINOR``
  27. SDL minor version
  28. ``SDL_VERSION_PATCH``
  29. SDL patch version
  30. .. versionadded:: 3.19
  31. Added the ``SDL_INCLUDE_DIRS``, ``SDL_LIBRARIES`` and ``SDL_VERSION[_<PART>]``
  32. variables.
  33. Cache variables
  34. ^^^^^^^^^^^^^^^
  35. These variables may optionally be set to help this module find the correct files:
  36. ``SDL_INCLUDE_DIR``
  37. where to find SDL.h
  38. ``SDL_LIBRARY``
  39. the name of the library to link against
  40. Variables for locating SDL
  41. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  42. This module responds to the flag:
  43. ``SDL_BUILDING_LIBRARY``
  44. If this is defined, then no SDL_main will be linked in because
  45. only applications need main().
  46. Otherwise, it is assumed you are building an application and this
  47. module will attempt to locate and set the proper link flags
  48. as part of the returned SDL_LIBRARY variable.
  49. Obsolete variables
  50. ^^^^^^^^^^^^^^^^^^
  51. .. deprecated:: 3.19
  52. These variables are obsolete and provided for backwards compatibility:
  53. ``SDL_VERSION_STRING``
  54. the human-readable string containing the version of SDL if found.
  55. Identical to SDL_VERSION
  56. Don't forget to include SDLmain.h and SDLmain.m your project for the
  57. OS X framework based version. (Other versions link to -lSDLmain which
  58. this module will try to find on your behalf.) Also for OS X, this
  59. module will automatically add the -framework Cocoa on your behalf.
  60. Additional Note: If you see an empty SDL_LIBRARY_TEMP in your
  61. configuration and no SDL_LIBRARY, it means CMake did not find your SDL
  62. library (SDL.dll, libsdl.so, SDL.framework, etc). Set
  63. SDL_LIBRARY_TEMP to point to your SDL library, and configure again.
  64. Similarly, if you see an empty SDLMAIN_LIBRARY, you should set this
  65. value as appropriate. These values are used to generate the final
  66. SDL_LIBRARY variable, but when these values are unset, SDL_LIBRARY
  67. does not get created.
  68. $SDLDIR is an environment variable that would correspond to the
  69. ./configure --prefix=$SDLDIR used in building SDL. l.e.galup 9-20-02
  70. On OSX, this will prefer the Framework version (if found) over others.
  71. People will have to manually change the cache values of SDL_LIBRARY to
  72. override this selection or set the CMake environment
  73. CMAKE_INCLUDE_PATH to modify the search paths.
  74. Note that the header path has changed from SDL/SDL.h to just SDL.h
  75. This needed to change because "proper" SDL convention is #include
  76. "SDL.h", not <SDL/SDL.h>. This is done for portability reasons
  77. because not all systems place things in SDL/ (see FreeBSD).
  78. #]=======================================================================]
  79. cmake_policy(PUSH)
  80. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  81. find_path(SDL_INCLUDE_DIR SDL.h
  82. HINTS
  83. ENV SDLDIR
  84. PATH_SUFFIXES SDL SDL12 SDL11
  85. # path suffixes to search inside ENV{SDLDIR}
  86. include/SDL include/SDL12 include/SDL11 include
  87. )
  88. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  89. set(VC_LIB_PATH_SUFFIX lib/x64)
  90. else()
  91. set(VC_LIB_PATH_SUFFIX lib/x86)
  92. endif()
  93. # SDL-1.1 is the name used by FreeBSD ports...
  94. # don't confuse it for the version number.
  95. find_library(SDL_LIBRARY_TEMP
  96. NAMES SDL SDL-1.1
  97. HINTS
  98. ENV SDLDIR
  99. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  100. )
  101. # Hide this cache variable from the user, it's an internal implementation
  102. # detail. The documented library variable for the user is SDL_LIBRARY
  103. # which is derived from SDL_LIBRARY_TEMP further below.
  104. set_property(CACHE SDL_LIBRARY_TEMP PROPERTY TYPE INTERNAL)
  105. if(NOT SDL_BUILDING_LIBRARY)
  106. if(NOT SDL_INCLUDE_DIR MATCHES ".framework")
  107. # Non-OS X framework versions expect you to also dynamically link to
  108. # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms
  109. # seem to provide SDLmain for compatibility even though they don't
  110. # necessarily need it.
  111. find_library(SDLMAIN_LIBRARY
  112. NAMES SDLmain SDLmain-1.1
  113. HINTS
  114. ENV SDLDIR
  115. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  116. PATHS
  117. /opt
  118. )
  119. endif()
  120. endif()
  121. # SDL may require threads on your system.
  122. # The Apple build may not need an explicit flag because one of the
  123. # frameworks may already provide it.
  124. # But for non-OSX systems, I will use the CMake Threads package.
  125. if(NOT APPLE)
  126. find_package(Threads)
  127. endif()
  128. # MinGW needs an additional link flag, -mwindows
  129. # It's total link flags should look like -lmingw32 -lSDLmain -lSDL -mwindows
  130. if(MINGW)
  131. set(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "link flags for MinGW")
  132. endif()
  133. if(SDL_LIBRARY_TEMP)
  134. # For SDLmain
  135. if(SDLMAIN_LIBRARY AND NOT SDL_BUILDING_LIBRARY)
  136. list(FIND SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" _SDL_MAIN_INDEX)
  137. if(_SDL_MAIN_INDEX EQUAL -1)
  138. set(SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" ${SDL_LIBRARY_TEMP})
  139. endif()
  140. unset(_SDL_MAIN_INDEX)
  141. endif()
  142. # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa.
  143. # CMake doesn't display the -framework Cocoa string in the UI even
  144. # though it actually is there if I modify a preused variable.
  145. # I think it has something to do with the CACHE STRING.
  146. # So I use a temporary variable until the end so I can set the
  147. # "real" variable in one-shot.
  148. if(APPLE)
  149. set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} "-framework Cocoa")
  150. endif()
  151. # For threads, as mentioned Apple doesn't need this.
  152. # In fact, there seems to be a problem if I used the Threads package
  153. # and try using this line, so I'm just skipping it entirely for OS X.
  154. if(NOT APPLE)
  155. set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
  156. endif()
  157. # For MinGW library
  158. if(MINGW)
  159. set(SDL_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL_LIBRARY_TEMP})
  160. endif()
  161. # Set the final string here so the GUI reflects the final state.
  162. set(SDL_LIBRARY ${SDL_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found")
  163. endif()
  164. if(SDL_INCLUDE_DIR AND EXISTS "${SDL_INCLUDE_DIR}/SDL_version.h")
  165. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$")
  166. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$")
  167. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$")
  168. string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MAJOR "${SDL_VERSION_MAJOR_LINE}")
  169. string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MINOR "${SDL_VERSION_MINOR_LINE}")
  170. string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_VERSION_PATCH "${SDL_VERSION_PATCH_LINE}")
  171. unset(SDL_VERSION_MAJOR_LINE)
  172. unset(SDL_VERSION_MINOR_LINE)
  173. unset(SDL_VERSION_PATCH_LINE)
  174. set(SDL_VERSION ${SDL_VERSION_MAJOR}.${SDL_VERSION_MINOR}.${SDL_VERSION_PATCH})
  175. set(SDL_VERSION_STRING ${SDL_VERSION})
  176. endif()
  177. include(FindPackageHandleStandardArgs)
  178. find_package_handle_standard_args(SDL
  179. REQUIRED_VARS SDL_LIBRARY SDL_INCLUDE_DIR
  180. VERSION_VAR SDL_VERSION_STRING)
  181. if(SDL_FOUND)
  182. set(SDL_LIBRARIES ${SDL_LIBRARY})
  183. set(SDL_INCLUDE_DIRS ${SDL_INCLUDE_DIR})
  184. if(NOT TARGET SDL::SDL)
  185. add_library(SDL::SDL INTERFACE IMPORTED)
  186. set_target_properties(SDL::SDL PROPERTIES
  187. INTERFACE_INCLUDE_DIRECTORIES "${SDL_INCLUDE_DIR}"
  188. INTERFACE_LINK_LIBRARIES "${SDL_LIBRARY}")
  189. endif()
  190. endif()
  191. cmake_policy(POP)