FindSDL_sound.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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_sound
  5. -------------
  6. Locates the SDL_sound library
  7. This module depends on SDL being found and must be called AFTER
  8. FindSDL.cmake is called.
  9. This module defines
  10. ::
  11. SDL_SOUND_INCLUDE_DIR, where to find SDL_sound.h
  12. SDL_SOUND_FOUND, if false, do not try to link to SDL_sound
  13. SDL_SOUND_LIBRARIES, this contains the list of libraries that you need
  14. to link against.
  15. SDL_SOUND_EXTRAS, this is an optional variable for you to add your own
  16. flags to SDL_SOUND_LIBRARIES. This is prepended to SDL_SOUND_LIBRARIES.
  17. This is available mostly for cases this module failed to anticipate for
  18. and you must add additional flags. This is marked as ADVANCED.
  19. SDL_SOUND_VERSION_STRING, human-readable string containing the
  20. version of SDL_sound
  21. This module also defines (but you shouldn't need to use directly)
  22. ::
  23. SDL_SOUND_LIBRARY, the name of just the SDL_sound library you would link
  24. against. Use SDL_SOUND_LIBRARIES for you link instructions and not this one.
  25. And might define the following as needed
  26. ::
  27. MIKMOD_LIBRARY
  28. MODPLUG_LIBRARY
  29. OGG_LIBRARY
  30. VORBIS_LIBRARY
  31. SMPEG_LIBRARY
  32. FLAC_LIBRARY
  33. SPEEX_LIBRARY
  34. Typically, you should not use these variables directly, and you should
  35. use SDL_SOUND_LIBRARIES which contains SDL_SOUND_LIBRARY and the other
  36. audio libraries (if needed) to successfully compile on your system.
  37. Responds to the $SDLDIR and $SDLSOUNDDIR environmental variable that
  38. would correspond to the ./configure --prefix=$SDLDIR used in building
  39. SDL.
  40. On OSX, this will prefer the Framework version (if found) over others.
  41. People will have to manually change the cache values of SDL_LIBRARY to
  42. override this selectionor set the CMake environment CMAKE_INCLUDE_PATH
  43. to modify the search paths.
  44. #]=======================================================================]
  45. #[[
  46. This module is a bit more complicated than the
  47. other FindSDL* family modules. The reason is that SDL_sound can be
  48. compiled in a large variety of different ways which are independent of
  49. platform. SDL_sound may dynamically link against other 3rd party
  50. libraries to get additional codec support, such as Ogg Vorbis, SMPEG,
  51. ModPlug, MikMod, FLAC, Speex, and potentially others. Under some
  52. circumstances which I don't fully understand, there seems to be a
  53. requirement that dependent libraries of libraries you use must also be
  54. explicitly linked against in order to successfully compile. SDL_sound
  55. does not currently have any system in place to know how it was
  56. compiled. So this CMake module does the hard work in trying to
  57. discover which 3rd party libraries are required for building (if any).
  58. This module uses a brute force approach to create a test program that
  59. uses SDL_sound, and then tries to build it. If the build fails, it
  60. parses the error output for known symbol names to figure out which
  61. libraries are needed.
  62. #]]
  63. set(SDL_SOUND_EXTRAS "" CACHE STRING "SDL_sound extra flags")
  64. mark_as_advanced(SDL_SOUND_EXTRAS)
  65. # Find SDL_sound.h
  66. find_path(SDL_SOUND_INCLUDE_DIR SDL_sound.h
  67. HINTS
  68. ENV SDLSOUNDDIR
  69. ENV SDLDIR
  70. PATH_SUFFIXES SDL
  71. # path suffixes to search inside ENV{SDLDIR}
  72. include/SDL include/SDL12 include/SDL11 include
  73. )
  74. find_library(SDL_SOUND_LIBRARY
  75. NAMES SDL_sound
  76. HINTS
  77. ENV SDLSOUNDDIR
  78. ENV SDLDIR
  79. PATH_SUFFIXES lib VisualC/win32lib
  80. )
  81. if(SDL_FOUND AND SDL_SOUND_INCLUDE_DIR AND SDL_SOUND_LIBRARY)
  82. # CMake is giving me problems using TRY_COMPILE with the CMAKE_FLAGS
  83. # for the :STRING syntax if I have multiple values contained in a
  84. # single variable. This is a problem for the SDL_LIBRARY variable
  85. # because it does just that. When I feed this variable to the command,
  86. # only the first value gets the appropriate modifier (e.g. -I) and
  87. # the rest get dropped.
  88. # To get multiple single variables to work, I must separate them with a "\;"
  89. # I could go back and modify the FindSDL.cmake module, but that's kind of painful.
  90. # The solution would be to try something like:
  91. # string(APPEND SDL_TRY_COMPILE_LIBRARY_LIST "\;${CMAKE_THREAD_LIBS_INIT}")
  92. # Instead, it was suggested on the mailing list to write a temporary CMakeLists.txt
  93. # with a temporary test project and invoke that with TRY_COMPILE.
  94. # See message thread "Figuring out dependencies for a library in order to build"
  95. # 2005-07-16
  96. # try_compile(
  97. # MY_RESULT
  98. # ${CMAKE_BINARY_DIR}
  99. # ${PROJECT_SOURCE_DIR}/DetermineSoundLibs.c
  100. # CMAKE_FLAGS
  101. # -DINCLUDE_DIRECTORIES:STRING=${SDL_INCLUDE_DIR}\;${SDL_SOUND_INCLUDE_DIR}
  102. # -DLINK_LIBRARIES:STRING=${SDL_SOUND_LIBRARY}\;${SDL_LIBRARY}
  103. # OUTPUT_VARIABLE MY_OUTPUT
  104. # )
  105. # To minimize external dependencies, create a sdlsound test program
  106. # which will be used to figure out if additional link dependencies are
  107. # required for the link phase.
  108. file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/DetermineSoundLibs.c
  109. "#include \"SDL_sound.h\"
  110. #include \"SDL.h\"
  111. int main(int argc, char* argv[])
  112. {
  113. Sound_AudioInfo desired;
  114. Sound_Sample* sample;
  115. SDL_Init(0);
  116. Sound_Init();
  117. /* This doesn't actually have to work, but Init() is a no-op
  118. * for some of the decoders, so this should force more symbols
  119. * to be pulled in.
  120. */
  121. sample = Sound_NewSampleFromFile(argv[1], &desired, 4096);
  122. Sound_Quit();
  123. SDL_Quit();
  124. return 0;
  125. }"
  126. )
  127. # Calling
  128. # target_link_libraries(DetermineSoundLibs "${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
  129. # causes problems when SDL_LIBRARY looks like
  130. # /Library/Frameworks/SDL.framework;-framework Cocoa
  131. # The ;-framework Cocoa seems to be confusing CMake once the OS X
  132. # framework support was added. I was told that breaking up the list
  133. # would fix the problem.
  134. set(TMP_TRY_LIBS)
  135. foreach(lib ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
  136. string(APPEND TMP_TRY_LIBS " \"${lib}\"")
  137. endforeach()
  138. # Write the CMakeLists.txt and test project
  139. # Weird, this is still sketchy. If I don't quote the variables
  140. # in the TARGET_LINK_LIBRARIES, I seem to loose everything
  141. # in the SDL_LIBRARY string after the "-framework".
  142. # But if I quote the stuff in INCLUDE_DIRECTORIES, it doesn't work.
  143. file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/CMakeLists.txt
  144. "cmake_minimum_required(VERSION ${CMAKE_VERSION})
  145. project(DetermineSoundLibs)
  146. include_directories(${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
  147. add_executable(DetermineSoundLibs DetermineSoundLibs.c)
  148. target_link_libraries(DetermineSoundLibs ${TMP_TRY_LIBS})"
  149. )
  150. try_compile(
  151. MY_RESULT
  152. PROJECT DetermineSoundLibs
  153. SOURCE_DIR ${PROJECT_BINARY_DIR}/CMakeTmp
  154. BINARY_DIR ${PROJECT_BINARY_DIR}/CMakeTmp
  155. OUTPUT_VARIABLE MY_OUTPUT
  156. )
  157. if(NOT MY_RESULT)
  158. # I expect that MPGLIB, VOC, WAV, AIFF, and SHN are compiled in statically.
  159. # I think Timidity is also compiled in statically.
  160. # I've never had to explcitly link against Quicktime, so I'll skip that for now.
  161. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY})
  162. # Find MikMod
  163. if("${MY_OUTPUT}" MATCHES "MikMod_")
  164. find_library(MIKMOD_LIBRARY
  165. NAMES libmikmod-coreaudio mikmod
  166. PATHS
  167. ENV MIKMODDIR
  168. ENV SDLSOUNDDIR
  169. ENV SDLDIR
  170. /opt
  171. PATH_SUFFIXES
  172. lib
  173. )
  174. if(MIKMOD_LIBRARY)
  175. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MIKMOD_LIBRARY})
  176. endif(MIKMOD_LIBRARY)
  177. endif("${MY_OUTPUT}" MATCHES "MikMod_")
  178. # Find ModPlug
  179. if("${MY_OUTPUT}" MATCHES "MODPLUG_")
  180. find_library(MODPLUG_LIBRARY
  181. NAMES modplug
  182. PATHS
  183. ENV MODPLUGDIR
  184. ENV SDLSOUNDDIR
  185. ENV SDLDIR
  186. /opt
  187. PATH_SUFFIXES
  188. lib
  189. )
  190. if(MODPLUG_LIBRARY)
  191. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MODPLUG_LIBRARY})
  192. endif()
  193. endif()
  194. # Find Ogg and Vorbis
  195. if("${MY_OUTPUT}" MATCHES "ov_")
  196. find_library(VORBIS_LIBRARY
  197. NAMES vorbis Vorbis VORBIS
  198. PATHS
  199. ENV VORBISDIR
  200. ENV OGGDIR
  201. ENV SDLSOUNDDIR
  202. ENV SDLDIR
  203. /opt
  204. PATH_SUFFIXES
  205. lib
  206. )
  207. if(VORBIS_LIBRARY)
  208. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBIS_LIBRARY})
  209. endif()
  210. find_library(OGG_LIBRARY
  211. NAMES ogg Ogg OGG
  212. PATHS
  213. ENV OGGDIR
  214. ENV VORBISDIR
  215. ENV SDLSOUNDDIR
  216. ENV SDLDIR
  217. /opt
  218. PATH_SUFFIXES
  219. lib
  220. )
  221. if(OGG_LIBRARY)
  222. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
  223. endif()
  224. endif()
  225. # Find SMPEG
  226. if("${MY_OUTPUT}" MATCHES "SMPEG_")
  227. find_library(SMPEG_LIBRARY
  228. NAMES smpeg SMPEG Smpeg SMpeg
  229. PATHS
  230. ENV SMPEGDIR
  231. ENV SDLSOUNDDIR
  232. ENV SDLDIR
  233. /opt
  234. PATH_SUFFIXES
  235. lib
  236. )
  237. if(SMPEG_LIBRARY)
  238. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SMPEG_LIBRARY})
  239. endif()
  240. endif()
  241. # Find FLAC
  242. if("${MY_OUTPUT}" MATCHES "FLAC_")
  243. find_library(FLAC_LIBRARY
  244. NAMES flac FLAC
  245. PATHS
  246. ENV FLACDIR
  247. ENV SDLSOUNDDIR
  248. ENV SDLDIR
  249. /opt
  250. PATH_SUFFIXES
  251. lib
  252. )
  253. if(FLAC_LIBRARY)
  254. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${FLAC_LIBRARY})
  255. endif()
  256. endif()
  257. # Hmmm...Speex seems to depend on Ogg. This might be a problem if
  258. # the TRY_COMPILE attempt gets blocked at SPEEX before it can pull
  259. # in the Ogg symbols. I'm not sure if I should duplicate the ogg stuff
  260. # above for here or if two ogg entries will screw up things.
  261. if("${MY_OUTPUT}" MATCHES "speex_")
  262. find_library(SPEEX_LIBRARY
  263. NAMES speex SPEEX
  264. PATHS
  265. ENV SPEEXDIR
  266. ENV SDLSOUNDDIR
  267. ENV SDLDIR
  268. /opt
  269. PATH_SUFFIXES
  270. lib
  271. )
  272. if(SPEEX_LIBRARY)
  273. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SPEEX_LIBRARY})
  274. endif()
  275. # Find OGG (needed for Speex)
  276. # We might have already found Ogg for Vorbis, so skip it if so.
  277. if(NOT OGG_LIBRARY)
  278. find_library(OGG_LIBRARY
  279. NAMES ogg Ogg OGG
  280. PATHS
  281. ENV OGGDIR
  282. ENV VORBISDIR
  283. ENV SPEEXDIR
  284. ENV SDLSOUNDDIR
  285. ENV SDLDIR
  286. /opt
  287. PATH_SUFFIXES lib
  288. )
  289. if(OGG_LIBRARY)
  290. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
  291. endif()
  292. endif()
  293. endif()
  294. set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARIES_TMP})
  295. else()
  296. set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARY})
  297. endif()
  298. endif()
  299. if(SDL_SOUND_INCLUDE_DIR AND EXISTS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h")
  300. file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SOUND_VER_MAJOR[ \t]+[0-9]+$")
  301. file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MINOR_LINE REGEX "^#define[ \t]+SOUND_VER_MINOR[ \t]+[0-9]+$")
  302. file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_PATCH_LINE REGEX "^#define[ \t]+SOUND_VER_PATCH[ \t]+[0-9]+$")
  303. string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MAJOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MAJOR "${SDL_SOUND_VERSION_MAJOR_LINE}")
  304. string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MINOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MINOR "${SDL_SOUND_VERSION_MINOR_LINE}")
  305. string(REGEX REPLACE "^#define[ \t]+SOUND_VER_PATCH[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_PATCH "${SDL_SOUND_VERSION_PATCH_LINE}")
  306. set(SDL_SOUND_VERSION_STRING ${SDL_SOUND_VERSION_MAJOR}.${SDL_SOUND_VERSION_MINOR}.${SDL_SOUND_VERSION_PATCH})
  307. unset(SDL_SOUND_VERSION_MAJOR_LINE)
  308. unset(SDL_SOUND_VERSION_MINOR_LINE)
  309. unset(SDL_SOUND_VERSION_PATCH_LINE)
  310. unset(SDL_SOUND_VERSION_MAJOR)
  311. unset(SDL_SOUND_VERSION_MINOR)
  312. unset(SDL_SOUND_VERSION_PATCH)
  313. endif()
  314. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  315. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL_sound
  316. REQUIRED_VARS SDL_SOUND_LIBRARY SDL_SOUND_INCLUDE_DIR
  317. VERSION_VAR SDL_SOUND_VERSION_STRING)