FindSDL2.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #
  6. # This module responds to the the flag:
  7. # SDL2_BUILDING_LIBRARY
  8. # If this is defined, then no SDL2main will be linked in because
  9. # only applications need main().
  10. # Otherwise, it is assumed you are building an application and this
  11. # module will attempt to locate and set the the proper link flags
  12. # as part of the returned SDL2_LIBRARY variable.
  13. #
  14. # Don't forget to include SDLmain.h and SDLmain.m your project for the
  15. # OS X framework based version. (Other versions link to -lSDL2main which
  16. # this module will try to find on your behalf.) Also for OS X, this
  17. # module will automatically add the -framework Cocoa on your behalf.
  18. #
  19. #
  20. # Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
  21. # and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
  22. # (SDL2.dll, libsdl2.so, SDL2.framework, etc).
  23. # Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
  24. # Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
  25. # as appropriate. These values are used to generate the final SDL2_LIBRARY
  26. # variable, but when these values are unset, SDL2_LIBRARY does not get created.
  27. #
  28. #
  29. # $SDL2DIR is an environment variable that would
  30. # correspond to the ./configure --prefix=$SDL2DIR
  31. # used in building SDL2.
  32. # l.e.galup 9-20-02
  33. #
  34. # Modified by Eric Wing.
  35. # Added code to assist with automated building by using environmental variables
  36. # and providing a more controlled/consistent search behavior.
  37. # Added new modifications to recognize OS X frameworks and
  38. # additional Unix paths (FreeBSD, etc).
  39. # Also corrected the header search path to follow "proper" SDL guidelines.
  40. # Added a search for SDL2main which is needed by some platforms.
  41. # Added a search for threads which is needed by some platforms.
  42. # Added needed compile switches for MinGW.
  43. #
  44. # On OSX, this will prefer the Framework version (if found) over others.
  45. # People will have to manually change the cache values of
  46. # SDL2_LIBRARY to override this selection or set the CMake environment
  47. # CMAKE_INCLUDE_PATH to modify the search paths.
  48. #
  49. # Note that the header path has changed from SDL2/SDL.h to just SDL.h
  50. # This needed to change because "proper" SDL convention
  51. # is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
  52. # reasons because not all systems place things in SDL2/ (see FreeBSD).
  53. #=============================================================================
  54. # Copyright 2003-2009 Kitware, Inc.
  55. #
  56. # Distributed under the OSI-approved BSD License (the "License");
  57. # see accompanying file Copyright.txt for details.
  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. # (To distribute this file outside of CMake, substitute the full
  64. # License text for the above reference.)
  65. SET(SDL2_SEARCH_PATHS
  66. ~/Library/Frameworks
  67. /Library/Frameworks
  68. /usr/local
  69. /usr
  70. /sw # Fink
  71. /opt/local # DarwinPorts
  72. /opt/csw # Blastwave
  73. /opt
  74. ${SDL2_PATH}
  75. )
  76. FIND_PATH(SDL2_INCLUDE_DIR SDL.h
  77. HINTS
  78. $ENV{SDL2DIR}
  79. PATH_SUFFIXES include/SDL2 include
  80. PATHS ${SDL2_SEARCH_PATHS}
  81. )
  82. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  83. set(PATH_SUFFIXES lib64 lib/x64 lib)
  84. else()
  85. set(PATH_SUFFIXES lib/x86 lib)
  86. endif()
  87. FIND_LIBRARY(SDL2_LIBRARY_TEMP
  88. NAMES SDL2
  89. HINTS
  90. $ENV{SDL2DIR}
  91. PATH_SUFFIXES ${PATH_SUFFIXES}
  92. PATHS ${SDL2_SEARCH_PATHS}
  93. )
  94. IF(NOT SDL2_BUILDING_LIBRARY)
  95. IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
  96. # Non-OS X framework versions expect you to also dynamically link to
  97. # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
  98. # seem to provide SDL2main for compatibility even though they don't
  99. # necessarily need it.
  100. FIND_LIBRARY(SDL2MAIN_LIBRARY
  101. NAMES SDL2main
  102. HINTS
  103. $ENV{SDL2DIR}
  104. PATH_SUFFIXES ${PATH_SUFFIXES}
  105. PATHS ${SDL2_SEARCH_PATHS}
  106. )
  107. ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
  108. ENDIF(NOT SDL2_BUILDING_LIBRARY)
  109. # SDL2 may require threads on your system.
  110. # The Apple build may not need an explicit flag because one of the
  111. # frameworks may already provide it.
  112. # But for non-OSX systems, I will use the CMake Threads package.
  113. IF(NOT APPLE)
  114. FIND_PACKAGE(Threads)
  115. ENDIF(NOT APPLE)
  116. # MinGW needs an additional link flag, -mwindows
  117. # It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -mwindows
  118. IF(MINGW)
  119. SET(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "mwindows for MinGW")
  120. ENDIF(MINGW)
  121. IF(SDL2_LIBRARY_TEMP)
  122. # For SDL2main
  123. IF(NOT SDL2_BUILDING_LIBRARY)
  124. IF(SDL2MAIN_LIBRARY)
  125. SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
  126. ENDIF(SDL2MAIN_LIBRARY)
  127. ENDIF(NOT SDL2_BUILDING_LIBRARY)
  128. # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
  129. # CMake doesn't display the -framework Cocoa string in the UI even
  130. # though it actually is there if I modify a pre-used variable.
  131. # I think it has something to do with the CACHE STRING.
  132. # So I use a temporary variable until the end so I can set the
  133. # "real" variable in one-shot.
  134. IF(APPLE)
  135. SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
  136. ENDIF(APPLE)
  137. # For threads, as mentioned Apple doesn't need this.
  138. # In fact, there seems to be a problem if I used the Threads package
  139. # and try using this line, so I'm just skipping it entirely for OS X.
  140. IF(NOT APPLE)
  141. SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
  142. ENDIF(NOT APPLE)
  143. # For MinGW library
  144. IF(MINGW)
  145. SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
  146. ENDIF(MINGW)
  147. # Set the final string here so the GUI reflects the final state.
  148. SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
  149. # Set the temp variable to INTERNAL so it is not seen in the CMake GUI
  150. SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
  151. ENDIF(SDL2_LIBRARY_TEMP)
  152. INCLUDE(FindPackageHandleStandardArgs)
  153. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)