FindSDL.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #.rst:
  2. # FindSDL
  3. # -------
  4. #
  5. # Locate SDL library
  6. #
  7. # This module defines
  8. #
  9. # ::
  10. #
  11. # SDL_LIBRARY, the name of the library to link against
  12. # SDL_FOUND, if false, do not try to link to SDL
  13. # SDL_INCLUDE_DIR, where to find SDL.h
  14. # SDL_VERSION_STRING, human-readable string containing the version of SDL
  15. #
  16. #
  17. #
  18. # This module responds to the flag:
  19. #
  20. # ::
  21. #
  22. # SDL_BUILDING_LIBRARY
  23. # If this is defined, then no SDL_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 SDL_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 -lSDLmain 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 SDL_LIBRARY_TEMP in your
  39. # configuration and no SDL_LIBRARY, it means CMake did not find your SDL
  40. # library (SDL.dll, libsdl.so, SDL.framework, etc). Set
  41. # SDL_LIBRARY_TEMP to point to your SDL library, and configure again.
  42. # Similarly, if you see an empty SDLMAIN_LIBRARY, you should set this
  43. # value as appropriate. These values are used to generate the final
  44. # SDL_LIBRARY variable, but when these values are unset, SDL_LIBRARY
  45. # does not get created.
  46. #
  47. #
  48. #
  49. # $SDLDIR is an environment variable that would correspond to the
  50. # ./configure --prefix=$SDLDIR 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 SDL_LIBRARY to
  63. # override this selection or set the CMake environment
  64. # CMAKE_INCLUDE_PATH to modify the search paths.
  65. #
  66. # Note that the header path has changed from SDL/SDL.h to just SDL.h
  67. # This needed to change because "proper" SDL convention is #include
  68. # "SDL.h", not <SDL/SDL.h>. This is done for portability reasons
  69. # because not all systems place things in SDL/ (see FreeBSD).
  70. #=============================================================================
  71. # Copyright 2003-2009 Kitware, Inc.
  72. # Copyright 2012 Benjamin Eikel
  73. #
  74. # Distributed under the OSI-approved BSD License (the "License");
  75. # see accompanying file Copyright.txt for details.
  76. #
  77. # This software is distributed WITHOUT ANY WARRANTY; without even the
  78. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  79. # See the License for more information.
  80. #=============================================================================
  81. # (To distribute this file outside of CMake, substitute the full
  82. # License text for the above reference.)
  83. find_path(SDL_INCLUDE_DIR SDL.h
  84. HINTS
  85. ENV SDLDIR
  86. PATH_SUFFIXES SDL SDL12 SDL11
  87. # path suffixes to search inside ENV{SDLDIR}
  88. include/SDL include/SDL12 include/SDL11 include
  89. )
  90. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  91. set(VC_LIB_PATH_SUFFIX lib/x64)
  92. else()
  93. set(VC_LIB_PATH_SUFFIX lib/x86)
  94. endif()
  95. # SDL-1.1 is the name used by FreeBSD ports...
  96. # don't confuse it for the version number.
  97. find_library(SDL_LIBRARY_TEMP
  98. NAMES SDL SDL-1.1
  99. HINTS
  100. ENV SDLDIR
  101. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  102. )
  103. if(NOT SDL_BUILDING_LIBRARY)
  104. if(NOT SDL_INCLUDE_DIR MATCHES ".framework")
  105. # Non-OS X framework versions expect you to also dynamically link to
  106. # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms
  107. # seem to provide SDLmain for compatibility even though they don't
  108. # necessarily need it.
  109. find_library(SDLMAIN_LIBRARY
  110. NAMES SDLmain SDLmain-1.1
  111. HINTS
  112. ENV SDLDIR
  113. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  114. PATHS
  115. /sw
  116. /opt/local
  117. /opt/csw
  118. /opt
  119. )
  120. endif()
  121. endif()
  122. # SDL may require threads on your system.
  123. # The Apple build may not need an explicit flag because one of the
  124. # frameworks may already provide it.
  125. # But for non-OSX systems, I will use the CMake Threads package.
  126. if(NOT APPLE)
  127. find_package(Threads)
  128. endif()
  129. # MinGW needs an additional library, mwindows
  130. # It's total link flags should look like -lmingw32 -lSDLmain -lSDL -lmwindows
  131. # (Actually on second look, I think it only needs one of the m* libraries.)
  132. if(MINGW)
  133. set(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
  134. endif()
  135. if(SDL_LIBRARY_TEMP)
  136. # For SDLmain
  137. if(SDLMAIN_LIBRARY AND NOT SDL_BUILDING_LIBRARY)
  138. list(FIND SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" _SDL_MAIN_INDEX)
  139. if(_SDL_MAIN_INDEX EQUAL -1)
  140. set(SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" ${SDL_LIBRARY_TEMP})
  141. endif()
  142. unset(_SDL_MAIN_INDEX)
  143. endif()
  144. # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa.
  145. # CMake doesn't display the -framework Cocoa string in the UI even
  146. # though it actually is there if I modify a pre-used variable.
  147. # I think it has something to do with the CACHE STRING.
  148. # So I use a temporary variable until the end so I can set the
  149. # "real" variable in one-shot.
  150. if(APPLE)
  151. set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} "-framework Cocoa")
  152. endif()
  153. # For threads, as mentioned Apple doesn't need this.
  154. # In fact, there seems to be a problem if I used the Threads package
  155. # and try using this line, so I'm just skipping it entirely for OS X.
  156. if(NOT APPLE)
  157. set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
  158. endif()
  159. # For MinGW library
  160. if(MINGW)
  161. set(SDL_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL_LIBRARY_TEMP})
  162. endif()
  163. # Set the final string here so the GUI reflects the final state.
  164. set(SDL_LIBRARY ${SDL_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found")
  165. # Set the temp variable to INTERNAL so it is not seen in the CMake GUI
  166. set(SDL_LIBRARY_TEMP "${SDL_LIBRARY_TEMP}" CACHE INTERNAL "")
  167. endif()
  168. if(SDL_INCLUDE_DIR AND EXISTS "${SDL_INCLUDE_DIR}/SDL_version.h")
  169. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$")
  170. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$")
  171. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$")
  172. string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MAJOR "${SDL_VERSION_MAJOR_LINE}")
  173. string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MINOR "${SDL_VERSION_MINOR_LINE}")
  174. string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_VERSION_PATCH "${SDL_VERSION_PATCH_LINE}")
  175. set(SDL_VERSION_STRING ${SDL_VERSION_MAJOR}.${SDL_VERSION_MINOR}.${SDL_VERSION_PATCH})
  176. unset(SDL_VERSION_MAJOR_LINE)
  177. unset(SDL_VERSION_MINOR_LINE)
  178. unset(SDL_VERSION_PATCH_LINE)
  179. unset(SDL_VERSION_MAJOR)
  180. unset(SDL_VERSION_MINOR)
  181. unset(SDL_VERSION_PATCH)
  182. endif()
  183. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  184. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL
  185. REQUIRED_VARS SDL_LIBRARY SDL_INCLUDE_DIR
  186. VERSION_VAR SDL_VERSION_STRING)