FindSDL_sound.cmake 13 KB

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