FindSDL2.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # This module defines
  2. # SDL2::SDL2, a target providing SDL2 itself
  3. # SDL2::SDL2main, a target providing an entry point for applications
  4. # SDL2_LIBRARY, the name of the library to link against
  5. # SDL2_FOUND, if false, do not try to link to SDL2
  6. # SDL2_INCLUDE_DIR, where to find SDL.h
  7. #
  8. # This module responds to the the flag:
  9. # SDL2_BUILDING_LIBRARY
  10. # If this is defined, then no SDL2::SDL2main target will be created because
  11. # only applications need main().
  12. # Otherwise, it is assumed you are building an application and this
  13. # module will attempt to locate and set the the proper link flags
  14. # as part of the SDL2::SDL2main target.
  15. #
  16. # Don't forget to include SDLmain.h and SDLmain.m your project for the
  17. # OS X framework based version. (Other versions link to -lSDL2main which
  18. # this module will try to find on your behalf.) Also for OS X, this
  19. # module will automatically add the -framework Cocoa on your behalf.
  20. #
  21. #
  22. # Additional Note: If you see an empty SDL2_LIBRARY in your configuration, it
  23. # means CMake did not find your SDL2 library (SDL2.dll, libsdl2.so,
  24. # SDL2.framework, etc).
  25. # Set SDL2_LIBRARY to point to your SDL2 library, and configure again.
  26. # Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
  27. # as appropriate.
  28. #
  29. #
  30. # Modified by Dominic Clark.
  31. # Added modern CMake targets to match those SDL2 itself uses.
  32. #
  33. # $SDL2DIR is an environment variable that would
  34. # correspond to the ./configure --prefix=$SDL2DIR
  35. # used in building SDL2.
  36. # l.e.galup 9-20-02
  37. #
  38. # Modified by Eric Wing.
  39. # Added code to assist with automated building by using environmental variables
  40. # and providing a more controlled/consistent search behavior.
  41. # Added new modifications to recognize OS X frameworks and
  42. # additional Unix paths (FreeBSD, etc).
  43. # Also corrected the header search path to follow "proper" SDL guidelines.
  44. # Added a search for SDL2main which is needed by some platforms.
  45. # Added a search for threads which is needed by some platforms.
  46. # Added needed compile switches for MinGW.
  47. #
  48. # On OSX, this will prefer the Framework version (if found) over others.
  49. # People will have to manually change the cache values of
  50. # SDL2_LIBRARY to override this selection or set the CMake environment
  51. # CMAKE_INCLUDE_PATH to modify the search paths.
  52. #
  53. # Note that the header path has changed from SDL2/SDL.h to just SDL.h
  54. # This needed to change because "proper" SDL convention
  55. # is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
  56. # reasons because not all systems place things in SDL2/ (see FreeBSD).
  57. #=============================================================================
  58. # Copyright 2003-2009 Kitware, Inc.
  59. #
  60. # Distributed under the OSI-approved BSD License (the "License");
  61. # see accompanying file COPYING-CMAKE-SCRIPTS for details.
  62. #
  63. # This software is distributed WITHOUT ANY WARRANTY; without even the
  64. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  65. # See the License for more information.
  66. #=============================================================================
  67. # (To distribute this file outside of CMake, substitute the full
  68. # License text for the above reference.)
  69. # message("<FindSDL2.cmake>")
  70. SET(SDL2_SEARCH_PATHS
  71. ~/Library/Frameworks
  72. /Library/Frameworks
  73. /usr/local
  74. /usr
  75. /sw # Fink
  76. /opt/local # DarwinPorts
  77. /opt/csw # Blastwave
  78. /opt
  79. ${SDL2_PATH}
  80. )
  81. FIND_PATH(SDL2_INCLUDE_DIR SDL.h
  82. HINTS
  83. $ENV{SDL2DIR}
  84. PATH_SUFFIXES SDL2 include/SDL2 include
  85. PATHS ${SDL2_SEARCH_PATHS}
  86. )
  87. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  88. set(PATH_SUFFIXES lib64 lib/x64 lib)
  89. else()
  90. set(PATH_SUFFIXES lib/x86 lib)
  91. endif()
  92. FIND_LIBRARY(SDL2_LIBRARY
  93. NAMES SDL2
  94. HINTS
  95. $ENV{SDL2DIR}
  96. PATH_SUFFIXES ${PATH_SUFFIXES}
  97. PATHS ${SDL2_SEARCH_PATHS}
  98. )
  99. IF(NOT SDL2_BUILDING_LIBRARY)
  100. IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
  101. # Non-OS X framework versions expect you to also dynamically link to
  102. # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
  103. # seem to provide SDL2main for compatibility even though they don't
  104. # necessarily need it.
  105. FIND_LIBRARY(SDL2MAIN_LIBRARY
  106. NAMES SDL2main
  107. HINTS
  108. $ENV{SDL2DIR}
  109. PATH_SUFFIXES ${PATH_SUFFIXES}
  110. PATHS ${SDL2_SEARCH_PATHS}
  111. )
  112. ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
  113. ENDIF(NOT SDL2_BUILDING_LIBRARY)
  114. # SDL2 may require threads on your system.
  115. # The Apple build may not need an explicit flag because one of the
  116. # frameworks may already provide it.
  117. # But for non-OSX systems, I will use the CMake Threads package.
  118. IF(NOT APPLE)
  119. FIND_PACKAGE(Threads)
  120. ENDIF(NOT APPLE)
  121. # MinGW needs an additional link flag, -mwindows
  122. # It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -mwindows
  123. IF(MINGW)
  124. SET(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "mwindows for MinGW")
  125. ENDIF(MINGW)
  126. IF(SDL2_LIBRARY)
  127. ADD_LIBRARY(SDL2::SDL2 UNKNOWN IMPORTED)
  128. SET_TARGET_PROPERTIES(SDL2::SDL2 PROPERTIES
  129. IMPORTED_LOCATION "${SDL2_LIBRARY}"
  130. INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
  131. )
  132. # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
  133. IF(APPLE)
  134. SET_PROPERTY(TARGET SDL2::SDL2 APPEND PROPERTY
  135. INTERFACE_LINK_OPTIONS "-framework Cocoa"
  136. )
  137. ENDIF(APPLE)
  138. # For threads, as mentioned Apple doesn't need this.
  139. # In fact, there seems to be a problem if I used the Threads package
  140. # and try using this line, so I'm just skipping it entirely for OS X.
  141. IF(NOT APPLE AND Threads_FOUND)
  142. SET_PROPERTY(TARGET SDL2::SDL2 APPEND PROPERTY
  143. INTERFACE_LINK_LIBRARIES "Threads::Threads"
  144. )
  145. ENDIF(NOT APPLE AND Threads_FOUND)
  146. ENDIF(SDL2_LIBRARY)
  147. IF(NOT SDL2_BUILDING_LIBRARY AND SDL2MAIN_LIBRARY)
  148. ADD_LIBRARY(SDL2::SDL2main STATIC IMPORTED)
  149. SET_TARGET_PROPERTIES(SDL2::SDL2main PROPERTIES
  150. IMPORTED_LOCATION "${SDL2MAIN_LIBRARY}"
  151. INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
  152. )
  153. IF(MINGW)
  154. SET_PROPERTY(TARGET SDL2::SDL2main APPEND PROPERTY
  155. INTERFACE_LINK_OPTIONS "${MINGW32_LIBRARY}"
  156. )
  157. ENDIF(MINGW)
  158. ENDIF(NOT SDL2_BUILDING_LIBRARY AND SDL2MAIN_LIBRARY)
  159. # message("</FindSDL2.cmake>")
  160. INCLUDE(FindPackageHandleStandardArgs)
  161. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)