FindSDL.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #
  7. # Locate SDL library
  8. #
  9. # This module defines
  10. #
  11. # ::
  12. #
  13. # SDL_LIBRARY, the name of the library to link against
  14. # SDL_FOUND, if false, do not try to link to SDL
  15. # SDL_INCLUDE_DIR, where to find SDL.h
  16. # SDL_VERSION_STRING, human-readable string containing the version of SDL
  17. #
  18. #
  19. #
  20. # This module responds to the flag:
  21. #
  22. # ::
  23. #
  24. # SDL_BUILDING_LIBRARY
  25. # If this is defined, then no SDL_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 SDL_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 SDL_LIBRARY_TEMP in your
  41. # configuration and no SDL_LIBRARY, it means CMake did not find your SDL
  42. # library (SDL.dll, libsdl.so, SDL.framework, etc). Set
  43. # SDL_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. # SDL_LIBRARY variable, but when these values are unset, SDL_LIBRARY
  47. # does not get created.
  48. #
  49. #
  50. #
  51. # $SDLDIR is an environment variable that would correspond to the
  52. # ./configure --prefix=$SDLDIR 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 SDL_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. find_path(SDL_INCLUDE_DIR SDL.h
  73. HINTS
  74. ENV SDLDIR
  75. PATH_SUFFIXES SDL SDL12 SDL11
  76. # path suffixes to search inside ENV{SDLDIR}
  77. include/SDL include/SDL12 include/SDL11 include
  78. )
  79. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  80. set(VC_LIB_PATH_SUFFIX lib/x64)
  81. else()
  82. set(VC_LIB_PATH_SUFFIX lib/x86)
  83. endif()
  84. # SDL-1.1 is the name used by FreeBSD ports...
  85. # don't confuse it for the version number.
  86. find_library(SDL_LIBRARY_TEMP
  87. NAMES SDL SDL-1.1
  88. HINTS
  89. ENV SDLDIR
  90. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  91. )
  92. # Hide this cache variable from the user, it's an internal implementation
  93. # detail. The documented library variable for the user is SDL_LIBRARY
  94. # which is derived from SDL_LIBRARY_TEMP further below.
  95. set_property(CACHE SDL_LIBRARY_TEMP PROPERTY TYPE INTERNAL)
  96. if(NOT SDL_BUILDING_LIBRARY)
  97. if(NOT SDL_INCLUDE_DIR MATCHES ".framework")
  98. # Non-OS X framework versions expect you to also dynamically link to
  99. # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms
  100. # seem to provide SDLmain for compatibility even though they don't
  101. # necessarily need it.
  102. find_library(SDLMAIN_LIBRARY
  103. NAMES SDLmain SDLmain-1.1
  104. HINTS
  105. ENV SDLDIR
  106. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  107. PATHS
  108. /sw
  109. /opt/local
  110. /opt/csw
  111. /opt
  112. )
  113. endif()
  114. endif()
  115. # SDL may require threads on your system.
  116. # The Apple build may not need an explicit flag because one of the
  117. # frameworks may already provide it.
  118. # But for non-OSX systems, I will use the CMake Threads package.
  119. if(NOT APPLE)
  120. find_package(Threads)
  121. endif()
  122. # MinGW needs an additional link flag, -mwindows
  123. # It's total link flags should look like -lmingw32 -lSDLmain -lSDL -mwindows
  124. if(MINGW)
  125. set(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "link flags for MinGW")
  126. endif()
  127. if(SDL_LIBRARY_TEMP)
  128. # For SDLmain
  129. if(SDLMAIN_LIBRARY AND NOT SDL_BUILDING_LIBRARY)
  130. list(FIND SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" _SDL_MAIN_INDEX)
  131. if(_SDL_MAIN_INDEX EQUAL -1)
  132. set(SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" ${SDL_LIBRARY_TEMP})
  133. endif()
  134. unset(_SDL_MAIN_INDEX)
  135. endif()
  136. # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa.
  137. # CMake doesn't display the -framework Cocoa string in the UI even
  138. # though it actually is there if I modify a pre-used variable.
  139. # I think it has something to do with the CACHE STRING.
  140. # So I use a temporary variable until the end so I can set the
  141. # "real" variable in one-shot.
  142. if(APPLE)
  143. set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} "-framework Cocoa")
  144. endif()
  145. # For threads, as mentioned Apple doesn't need this.
  146. # In fact, there seems to be a problem if I used the Threads package
  147. # and try using this line, so I'm just skipping it entirely for OS X.
  148. if(NOT APPLE)
  149. set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
  150. endif()
  151. # For MinGW library
  152. if(MINGW)
  153. set(SDL_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL_LIBRARY_TEMP})
  154. endif()
  155. # Set the final string here so the GUI reflects the final state.
  156. set(SDL_LIBRARY ${SDL_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found")
  157. endif()
  158. if(SDL_INCLUDE_DIR AND EXISTS "${SDL_INCLUDE_DIR}/SDL_version.h")
  159. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$")
  160. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$")
  161. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$")
  162. string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MAJOR "${SDL_VERSION_MAJOR_LINE}")
  163. string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MINOR "${SDL_VERSION_MINOR_LINE}")
  164. string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_VERSION_PATCH "${SDL_VERSION_PATCH_LINE}")
  165. set(SDL_VERSION_STRING ${SDL_VERSION_MAJOR}.${SDL_VERSION_MINOR}.${SDL_VERSION_PATCH})
  166. unset(SDL_VERSION_MAJOR_LINE)
  167. unset(SDL_VERSION_MINOR_LINE)
  168. unset(SDL_VERSION_PATCH_LINE)
  169. unset(SDL_VERSION_MAJOR)
  170. unset(SDL_VERSION_MINOR)
  171. unset(SDL_VERSION_PATCH)
  172. endif()
  173. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  174. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL
  175. REQUIRED_VARS SDL_LIBRARY SDL_INCLUDE_DIR
  176. VERSION_VAR SDL_VERSION_STRING)