FindSDL2.cmake 7.6 KB

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