FindSDL2.cmake 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # This module defines
  2. # SDL2_LIBRARY, the name of the library to link against
  3. # SDL2_FOUND, if false, do not try to link to SDL2
  4. # SDL2_INCLUDE_DIR, where to find SDL.h
  5. # SDL2_DLL_DIR, where to find SDL2.dll if it exists
  6. #
  7. # This module responds to the the flag:
  8. # SDL2_BUILDING_LIBRARY
  9. # If this is defined, then no SDL2main will be linked in because
  10. # only applications need main().
  11. # Otherwise, it is assumed you are building an application and this
  12. # module will attempt to locate and set the the proper link flags
  13. # as part of the returned SDL2_LIBRARY variable.
  14. #
  15. # Don't forget to include SDLmain.h and SDLmain.m your project for the
  16. # OS X framework based version. (Other versions link to -lSDL2main which
  17. # this module will try to find on your behalf.) Also for OS X, this
  18. # module will automatically add the -framework Cocoa on your behalf.
  19. #
  20. #
  21. # Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
  22. # and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
  23. # (SDL2.dll, libsdl2.so, SDL2.framework, etc).
  24. # Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
  25. # Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
  26. # as appropriate. These values are used to generate the final SDL2_LIBRARY
  27. # variable, but when these values are unset, SDL2_LIBRARY does not get created.
  28. #
  29. #
  30. # $SDL2DIR is an environment variable that would
  31. # correspond to the ./configure --prefix=$SDL2DIR
  32. # used in building SDL2.
  33. # l.e.galup 9-20-02
  34. #
  35. # Modified by Eric Wing.
  36. # Added code to assist with automated building by using environmental variables
  37. # and providing a more controlled/consistent search behavior.
  38. # Added new modifications to recognize OS X frameworks and
  39. # additional Unix paths (FreeBSD, etc).
  40. # Also corrected the header search path to follow "proper" SDL guidelines.
  41. # Added a search for SDL2main which is needed by some platforms.
  42. # Added a search for threads which is needed by some platforms.
  43. # Added needed compile switches for MinGW.
  44. #
  45. # On OSX, this will prefer the Framework version (if found) over others.
  46. # People will have to manually change the cache values of
  47. # SDL2_LIBRARY to override this selection or set the CMake environment
  48. # CMAKE_INCLUDE_PATH to modify the search paths.
  49. #
  50. # Note that the header path has changed from SDL2/SDL.h to just SDL.h
  51. # This needed to change because "proper" SDL convention
  52. # is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
  53. # reasons because not all systems place things in SDL2/ (see FreeBSD).
  54. #=============================================================================
  55. # Copyright 2003-2009 Kitware, Inc.
  56. #
  57. # Distributed under the OSI-approved BSD License (the "License").
  58. #
  59. # This software is distributed WITHOUT ANY WARRANTY; without even the
  60. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  61. # See the License for more information.
  62. #=============================================================================
  63. # CMake - Cross Platform Makefile Generator
  64. # Copyright 2000-2016 Kitware, Inc.
  65. # Copyright 2000-2011 Insight Software Consortium
  66. # All rights reserved.
  67. #
  68. # Redistribution and use in source and binary forms, with or without
  69. # modification, are permitted provided that the following conditions
  70. # are met:
  71. #
  72. # * Redistributions of source code must retain the above copyright
  73. # notice, this list of conditions and the following disclaimer.
  74. #
  75. # * Redistributions in binary form must reproduce the above copyright
  76. # notice, this list of conditions and the following disclaimer in the
  77. # documentation and/or other materials provided with the distribution.
  78. #
  79. # * Neither the names of Kitware, Inc., the Insight Software Consortium,
  80. # nor the names of their contributors may be used to endorse or promote
  81. # products derived from this software without specific prior written
  82. # permission.
  83. #
  84. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  85. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  86. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  87. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  88. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  89. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  90. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  91. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  92. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  93. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  94. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  95. #
  96. # ------------------------------------------------------------------------------
  97. #
  98. # The above copyright and license notice applies to distributions of
  99. # CMake in source and binary form. Some source files contain additional
  100. # notices of original copyright by their contributors; see each source
  101. # for details. Third-party software packages supplied with CMake under
  102. # compatible licenses provide their own copyright notices documented in
  103. # corresponding subdirectories.
  104. #
  105. # ------------------------------------------------------------------------------
  106. #
  107. # CMake was initially developed by Kitware with the following sponsorship:
  108. #
  109. # * National Library of Medicine at the National Institutes of Health
  110. # as part of the Insight Segmentation and Registration Toolkit (ITK).
  111. #
  112. # * US National Labs (Los Alamos, Livermore, Sandia) ASC Parallel
  113. # Visualization Initiative.
  114. #
  115. # * National Alliance for Medical Image Computing (NAMIC) is funded by the
  116. # National Institutes of Health through the NIH Roadmap for Medical Research,
  117. # Grant U54 EB005149.
  118. #
  119. # * Kitware, Inc.
  120. #
  121. message("<FindSDL2.cmake>")
  122. SET(SDL2_SEARCH_PATHS
  123. ~/Library/Frameworks
  124. /Library/Frameworks
  125. /usr/local
  126. /usr
  127. /sw # Fink
  128. /opt/local # DarwinPorts
  129. /opt/csw # Blastwave
  130. /opt
  131. ${SDL2_PATH}
  132. )
  133. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  134. set(VC_LIB_PATH_SUFFIX lib/x64)
  135. else()
  136. set(VC_LIB_PATH_SUFFIX lib/x86)
  137. endif()
  138. FIND_LIBRARY(SDL2_LIBRARY_TEMP
  139. NAMES SDL2
  140. HINTS
  141. $ENV{SDL2DIR}
  142. PATH_SUFFIXES lib64 lib ${VC_LIB_PATH_SUFFIX}
  143. PATHS ${SDL2_SEARCH_PATHS}
  144. )
  145. IF(SDL2_LIBRARY_TEMP)
  146. if(MSVC)
  147. get_filename_component(SDL2_DLL_DIR_TEMP ${SDL2_LIBRARY_TEMP} DIRECTORY)
  148. if(EXISTS ${SDL2_DLL_DIR_TEMP}/SDL2.dll)
  149. set(SDL2_DLL_DIR ${SDL2_DLL_DIR_TEMP})
  150. unset(SDL2_DLL_DIR_TEMP)
  151. endif()
  152. endif()
  153. FIND_PATH(SDL2_INCLUDE_DIR SDL.h
  154. HINTS
  155. $ENV{SDL2DIR}
  156. PATH_SUFFIXES include/SDL2 include
  157. PATHS ${SDL2_SEARCH_PATHS}
  158. )
  159. IF(NOT SDL2_BUILDING_LIBRARY)
  160. IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
  161. # Non-OS X framework versions expect you to also dynamically link to
  162. # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
  163. # seem to provide SDL2main for compatibility even though they don't
  164. # necessarily need it.
  165. FIND_LIBRARY(SDL2MAIN_LIBRARY
  166. NAMES SDL2main
  167. HINTS
  168. $ENV{SDL2DIR}
  169. PATH_SUFFIXES lib64 lib
  170. PATHS ${SDL2_SEARCH_PATHS}
  171. )
  172. ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
  173. ENDIF(NOT SDL2_BUILDING_LIBRARY)
  174. # SDL2 may require threads on your system.
  175. # The Apple build may not need an explicit flag because one of the
  176. # frameworks may already provide it.
  177. # But for non-OSX systems, I will use the CMake Threads package.
  178. IF(NOT APPLE)
  179. FIND_PACKAGE(Threads)
  180. ENDIF(NOT APPLE)
  181. # MinGW needs an additional library, mwindows
  182. # It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows
  183. # (Actually on second look, I think it only needs one of the m* libraries.)
  184. IF(MINGW)
  185. SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
  186. ENDIF(MINGW)
  187. # For SDL2main
  188. IF(NOT SDL2_BUILDING_LIBRARY)
  189. IF(SDL2MAIN_LIBRARY)
  190. SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
  191. ENDIF(SDL2MAIN_LIBRARY)
  192. ENDIF(NOT SDL2_BUILDING_LIBRARY)
  193. # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
  194. # CMake doesn't display the -framework Cocoa string in the UI even
  195. # though it actually is there if I modify a pre-used variable.
  196. # I think it has something to do with the CACHE STRING.
  197. # So I use a temporary variable until the end so I can set the
  198. # "real" variable in one-shot.
  199. IF(APPLE)
  200. SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
  201. ENDIF(APPLE)
  202. # For threads, as mentioned Apple doesn't need this.
  203. # In fact, there seems to be a problem if I used the Threads package
  204. # and try using this line, so I'm just skipping it entirely for OS X.
  205. IF(NOT APPLE)
  206. SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
  207. ENDIF(NOT APPLE)
  208. # For MinGW library
  209. IF(MINGW)
  210. SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
  211. ENDIF(MINGW)
  212. # Set the final string here so the GUI reflects the final state.
  213. SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
  214. # Unset the temp variable to INTERNAL so it is not seen in the CMake GUI
  215. UNSET(SDL2_LIBRARY_TEMP)
  216. ENDIF(SDL2_LIBRARY_TEMP)
  217. message("</FindSDL2.cmake>")
  218. INCLUDE(FindPackageHandleStandardArgs)
  219. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)