FindSDL_sound.cmake 13 KB

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