FindSDL.cmake 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindSDL
  5. -------
  6. Locate SDL library
  7. This module defines
  8. ::
  9. SDL_LIBRARY, the name of the library to link against
  10. SDL_FOUND, if false, do not try to link to SDL
  11. SDL_INCLUDE_DIR, where to find SDL.h
  12. SDL_VERSION_STRING, human-readable string containing the version of SDL
  13. This module responds to the flag:
  14. ::
  15. SDL_BUILDING_LIBRARY
  16. If this is defined, then no SDL_main will be linked in because
  17. only applications need main().
  18. Otherwise, it is assumed you are building an application and this
  19. module will attempt to locate and set the proper link flags
  20. as part of the returned SDL_LIBRARY variable.
  21. Don't forget to include SDLmain.h and SDLmain.m your project for the
  22. OS X framework based version. (Other versions link to -lSDLmain which
  23. this module will try to find on your behalf.) Also for OS X, this
  24. module will automatically add the -framework Cocoa on your behalf.
  25. Additional Note: If you see an empty SDL_LIBRARY_TEMP in your
  26. configuration and no SDL_LIBRARY, it means CMake did not find your SDL
  27. library (SDL.dll, libsdl.so, SDL.framework, etc). Set
  28. SDL_LIBRARY_TEMP to point to your SDL library, and configure again.
  29. Similarly, if you see an empty SDLMAIN_LIBRARY, you should set this
  30. value as appropriate. These values are used to generate the final
  31. SDL_LIBRARY variable, but when these values are unset, SDL_LIBRARY
  32. does not get created.
  33. $SDLDIR is an environment variable that would correspond to the
  34. ./configure --prefix=$SDLDIR used in building SDL. l.e.galup 9-20-02
  35. Modified by Eric Wing. Added code to assist with automated building
  36. by using environmental variables and providing a more
  37. controlled/consistent search behavior. Added new modifications to
  38. recognize OS X frameworks and additional Unix paths (FreeBSD, etc).
  39. Also corrected the header search path to follow "proper" SDL
  40. guidelines. Added a search for SDLmain which is needed by some
  41. platforms. Added a search for threads which is needed by some
  42. platforms. Added needed compile switches for MinGW.
  43. On OSX, this will prefer the Framework version (if found) over others.
  44. People will have to manually change the cache values of SDL_LIBRARY to
  45. override this selection or set the CMake environment
  46. CMAKE_INCLUDE_PATH to modify the search paths.
  47. Note that the header path has changed from SDL/SDL.h to just SDL.h
  48. This needed to change because "proper" SDL convention is #include
  49. "SDL.h", not <SDL/SDL.h>. This is done for portability reasons
  50. because not all systems place things in SDL/ (see FreeBSD).
  51. #]=======================================================================]
  52. find_path(SDL_INCLUDE_DIR SDL.h
  53. HINTS
  54. ENV SDLDIR
  55. PATH_SUFFIXES SDL SDL12 SDL11
  56. # path suffixes to search inside ENV{SDLDIR}
  57. include/SDL include/SDL12 include/SDL11 include
  58. )
  59. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  60. set(VC_LIB_PATH_SUFFIX lib/x64)
  61. else()
  62. set(VC_LIB_PATH_SUFFIX lib/x86)
  63. endif()
  64. # SDL-1.1 is the name used by FreeBSD ports...
  65. # don't confuse it for the version number.
  66. find_library(SDL_LIBRARY_TEMP
  67. NAMES SDL SDL-1.1
  68. HINTS
  69. ENV SDLDIR
  70. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  71. )
  72. # Hide this cache variable from the user, it's an internal implementation
  73. # detail. The documented library variable for the user is SDL_LIBRARY
  74. # which is derived from SDL_LIBRARY_TEMP further below.
  75. set_property(CACHE SDL_LIBRARY_TEMP PROPERTY TYPE INTERNAL)
  76. if(NOT SDL_BUILDING_LIBRARY)
  77. if(NOT SDL_INCLUDE_DIR MATCHES ".framework")
  78. # Non-OS X framework versions expect you to also dynamically link to
  79. # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms
  80. # seem to provide SDLmain for compatibility even though they don't
  81. # necessarily need it.
  82. find_library(SDLMAIN_LIBRARY
  83. NAMES SDLmain SDLmain-1.1
  84. HINTS
  85. ENV SDLDIR
  86. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  87. PATHS
  88. /opt
  89. )
  90. endif()
  91. endif()
  92. # SDL may require threads on your system.
  93. # The Apple build may not need an explicit flag because one of the
  94. # frameworks may already provide it.
  95. # But for non-OSX systems, I will use the CMake Threads package.
  96. if(NOT APPLE)
  97. find_package(Threads)
  98. endif()
  99. # MinGW needs an additional link flag, -mwindows
  100. # It's total link flags should look like -lmingw32 -lSDLmain -lSDL -mwindows
  101. if(MINGW)
  102. set(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "link flags for MinGW")
  103. endif()
  104. if(SDL_LIBRARY_TEMP)
  105. # For SDLmain
  106. if(SDLMAIN_LIBRARY AND NOT SDL_BUILDING_LIBRARY)
  107. list(FIND SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" _SDL_MAIN_INDEX)
  108. if(_SDL_MAIN_INDEX EQUAL -1)
  109. set(SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" ${SDL_LIBRARY_TEMP})
  110. endif()
  111. unset(_SDL_MAIN_INDEX)
  112. endif()
  113. # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa.
  114. # CMake doesn't display the -framework Cocoa string in the UI even
  115. # though it actually is there if I modify a pre-used variable.
  116. # I think it has something to do with the CACHE STRING.
  117. # So I use a temporary variable until the end so I can set the
  118. # "real" variable in one-shot.
  119. if(APPLE)
  120. set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} "-framework Cocoa")
  121. endif()
  122. # For threads, as mentioned Apple doesn't need this.
  123. # In fact, there seems to be a problem if I used the Threads package
  124. # and try using this line, so I'm just skipping it entirely for OS X.
  125. if(NOT APPLE)
  126. set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
  127. endif()
  128. # For MinGW library
  129. if(MINGW)
  130. set(SDL_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL_LIBRARY_TEMP})
  131. endif()
  132. # Set the final string here so the GUI reflects the final state.
  133. set(SDL_LIBRARY ${SDL_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found")
  134. endif()
  135. if(SDL_INCLUDE_DIR AND EXISTS "${SDL_INCLUDE_DIR}/SDL_version.h")
  136. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$")
  137. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$")
  138. file(STRINGS "${SDL_INCLUDE_DIR}/SDL_version.h" SDL_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$")
  139. string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MAJOR "${SDL_VERSION_MAJOR_LINE}")
  140. string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_VERSION_MINOR "${SDL_VERSION_MINOR_LINE}")
  141. string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_VERSION_PATCH "${SDL_VERSION_PATCH_LINE}")
  142. set(SDL_VERSION_STRING ${SDL_VERSION_MAJOR}.${SDL_VERSION_MINOR}.${SDL_VERSION_PATCH})
  143. unset(SDL_VERSION_MAJOR_LINE)
  144. unset(SDL_VERSION_MINOR_LINE)
  145. unset(SDL_VERSION_PATCH_LINE)
  146. unset(SDL_VERSION_MAJOR)
  147. unset(SDL_VERSION_MINOR)
  148. unset(SDL_VERSION_PATCH)
  149. endif()
  150. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  151. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL
  152. REQUIRED_VARS SDL_LIBRARY SDL_INCLUDE_DIR
  153. VERSION_VAR SDL_VERSION_STRING)