FindSDL_sound.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. /usr/local/include
  84. /usr/include
  85. /sw/include/SDL # Fink
  86. /sw/include
  87. /opt/local/include/SDL # DarwinPorts
  88. /opt/local/include
  89. /opt/csw/include/SDL # Blastwave
  90. /opt/csw/include
  91. /opt/include/SDL
  92. /opt/include
  93. )
  94. FIND_LIBRARY(SDL_SOUND_LIBRARY
  95. NAMES SDL_sound
  96. HINTS
  97. $ENV{SDLSOUNDDIR}/lib
  98. $ENV{SDLSOUNDDIR}
  99. $ENV{SDLDIR}/lib
  100. $ENV{SDLDIR}
  101. PATHS
  102. /usr/local/lib
  103. /usr/lib
  104. /sw/lib
  105. /opt/local/lib
  106. /opt/csw/lib
  107. /opt/lib
  108. )
  109. SET(SDL_SOUND_FOUND "NO")
  110. IF(SDL_FOUND AND SDL_SOUND_INCLUDE_DIR AND SDL_SOUND_LIBRARY)
  111. # CMake is giving me problems using TRY_COMPILE with the CMAKE_FLAGS
  112. # for the :STRING syntax if I have multiple values contained in a
  113. # single variable. This is a problem for the SDL_LIBRARY variable
  114. # because it does just that. When I feed this variable to the command,
  115. # only the first value gets the appropriate modifier (e.g. -I) and
  116. # the rest get dropped.
  117. # To get multiple single variables to work, I must separate them with a "\;"
  118. # I could go back and modify the FindSDL.cmake module, but that's kind of painful.
  119. # The solution would be to try something like:
  120. # SET(SDL_TRY_COMPILE_LIBRARY_LIST "${SDL_TRY_COMPILE_LIBRARY_LIST}\;${CMAKE_THREAD_LIBS_INIT}")
  121. # Instead, it was suggested on the mailing list to write a temporary CMakeLists.txt
  122. # with a temporary test project and invoke that with TRY_COMPILE.
  123. # See message thread "Figuring out dependencies for a library in order to build"
  124. # 2005-07-16
  125. # TRY_COMPILE(
  126. # MY_RESULT
  127. # ${CMAKE_BINARY_DIR}
  128. # ${PROJECT_SOURCE_DIR}/DetermineSoundLibs.c
  129. # CMAKE_FLAGS
  130. # -DINCLUDE_DIRECTORIES:STRING=${SDL_INCLUDE_DIR}\;${SDL_SOUND_INCLUDE_DIR}
  131. # -DLINK_LIBRARIES:STRING=${SDL_SOUND_LIBRARY}\;${SDL_LIBRARY}
  132. # OUTPUT_VARIABLE MY_OUTPUT
  133. # )
  134. # To minimize external dependencies, create a sdlsound test program
  135. # which will be used to figure out if additional link dependencies are
  136. # required for the link phase.
  137. FILE(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/DetermineSoundLibs.c
  138. "#include \"SDL_sound.h\"
  139. #include \"SDL.h\"
  140. int main(int argc, char* argv[])
  141. {
  142. Sound_AudioInfo desired;
  143. Sound_Sample* sample;
  144. SDL_Init(0);
  145. Sound_Init();
  146. /* This doesn't actually have to work, but Init() is a no-op
  147. * for some of the decoders, so this should force more symbols
  148. * to be pulled in.
  149. */
  150. sample = Sound_NewSampleFromFile(argv[1], &desired, 4096);
  151. Sound_Quit();
  152. SDL_Quit();
  153. return 0;
  154. }"
  155. )
  156. # Calling
  157. # TARGET_LINK_LIBRARIES(DetermineSoundLibs "${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
  158. # causes problems when SDL_LIBRARY looks like
  159. # /Library/Frameworks/SDL.framework;-framework Cocoa
  160. # The ;-framework Cocoa seems to be confusing CMake once the OS X
  161. # framework support was added. I was told that breaking up the list
  162. # would fix the problem.
  163. SET(TMP_TRY_LIBS)
  164. FOREACH(lib ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
  165. SET(TMP_TRY_LIBS "${TMP_TRY_LIBS} \"${lib}\"")
  166. ENDFOREACH(lib)
  167. # MESSAGE("TMP_TRY_LIBS ${TMP_TRY_LIBS}")
  168. # Write the CMakeLists.txt and test project
  169. # Weird, this is still sketchy. If I don't quote the variables
  170. # in the TARGET_LINK_LIBRARIES, I seem to loose everything
  171. # in the SDL_LIBRARY string after the "-framework".
  172. # But if I quote the stuff in INCLUDE_DIRECTORIES, it doesn't work.
  173. FILE(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/CMakeLists.txt
  174. "PROJECT(DetermineSoundLibs)
  175. INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
  176. ADD_EXECUTABLE(DetermineSoundLibs DetermineSoundLibs.c)
  177. TARGET_LINK_LIBRARIES(DetermineSoundLibs ${TMP_TRY_LIBS})"
  178. )
  179. TRY_COMPILE(
  180. MY_RESULT
  181. ${PROJECT_BINARY_DIR}/CMakeTmp
  182. ${PROJECT_BINARY_DIR}/CMakeTmp
  183. DetermineSoundLibs
  184. OUTPUT_VARIABLE MY_OUTPUT
  185. )
  186. # MESSAGE("${MY_RESULT}")
  187. # MESSAGE(${MY_OUTPUT})
  188. IF(NOT MY_RESULT)
  189. # I expect that MPGLIB, VOC, WAV, AIFF, and SHN are compiled in statically.
  190. # I think Timidity is also compiled in statically.
  191. # I've never had to explcitly link against Quicktime, so I'll skip that for now.
  192. SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY})
  193. # Find MikMod
  194. IF("${MY_OUTPUT}" MATCHES "MikMod_")
  195. FIND_LIBRARY(MIKMOD_LIBRARY
  196. NAMES libmikmod-coreaudio mikmod
  197. PATHS
  198. $ENV{MIKMODDIR}/lib
  199. $ENV{MIKMODDIR}
  200. $ENV{SDLSOUNDDIR}/lib
  201. $ENV{SDLSOUNDDIR}
  202. $ENV{SDLDIR}/lib
  203. $ENV{SDLDIR}
  204. /usr/local/lib
  205. /usr/lib
  206. /sw/lib
  207. /opt/local/lib
  208. /opt/csw/lib
  209. /opt/lib
  210. )
  211. IF(MIKMOD_LIBRARY)
  212. SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MIKMOD_LIBRARY})
  213. ENDIF(MIKMOD_LIBRARY)
  214. ENDIF("${MY_OUTPUT}" MATCHES "MikMod_")
  215. # Find ModPlug
  216. IF("${MY_OUTPUT}" MATCHES "MODPLUG_")
  217. FIND_LIBRARY(MODPLUG_LIBRARY
  218. NAMES modplug
  219. PATHS
  220. $ENV{MODPLUGDIR}/lib
  221. $ENV{MODPLUGDIR}
  222. $ENV{SDLSOUNDDIR}/lib
  223. $ENV{SDLSOUNDDIR}
  224. $ENV{SDLDIR}/lib
  225. $ENV{SDLDIR}
  226. /usr/local/lib
  227. /usr/lib
  228. /sw/lib
  229. /opt/local/lib
  230. /opt/csw/lib
  231. /opt/lib
  232. )
  233. IF(MODPLUG_LIBRARY)
  234. SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MODPLUG_LIBRARY})
  235. ENDIF(MODPLUG_LIBRARY)
  236. ENDIF("${MY_OUTPUT}" MATCHES "MODPLUG_")
  237. # Find Ogg and Vorbis
  238. IF("${MY_OUTPUT}" MATCHES "ov_")
  239. FIND_LIBRARY(VORBIS_LIBRARY
  240. NAMES vorbis Vorbis VORBIS
  241. PATHS
  242. $ENV{VORBISDIR}/lib
  243. $ENV{VORBISDIR}
  244. $ENV{OGGDIR}/lib
  245. $ENV{OGGDIR}
  246. $ENV{SDLSOUNDDIR}/lib
  247. $ENV{SDLSOUNDDIR}
  248. $ENV{SDLDIR}/lib
  249. $ENV{SDLDIR}
  250. /usr/local/lib
  251. /usr/lib
  252. /sw/lib
  253. /opt/local/lib
  254. /opt/csw/lib
  255. /opt/lib
  256. )
  257. IF(VORBIS_LIBRARY)
  258. SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBIS_LIBRARY})
  259. ENDIF(VORBIS_LIBRARY)
  260. FIND_LIBRARY(OGG_LIBRARY
  261. NAMES ogg Ogg OGG
  262. PATHS
  263. $ENV{OGGDIR}/lib
  264. $ENV{OGGDIR}
  265. $ENV{VORBISDIR}/lib
  266. $ENV{VORBISDIR}
  267. $ENV{SDLSOUNDDIR}/lib
  268. $ENV{SDLSOUNDDIR}
  269. $ENV{SDLDIR}/lib
  270. $ENV{SDLDIR}
  271. /usr/local/lib
  272. /usr/lib
  273. /sw/lib
  274. /opt/local/lib
  275. /opt/csw/lib
  276. /opt/lib
  277. )
  278. IF(OGG_LIBRARY)
  279. SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
  280. ENDIF(OGG_LIBRARY)
  281. ENDIF("${MY_OUTPUT}" MATCHES "ov_")
  282. # Find SMPEG
  283. IF("${MY_OUTPUT}" MATCHES "SMPEG_")
  284. FIND_LIBRARY(SMPEG_LIBRARY
  285. NAMES smpeg SMPEG Smpeg SMpeg
  286. PATHS
  287. $ENV{SMPEGDIR}/lib
  288. $ENV{SMPEGDIR}
  289. $ENV{SDLSOUNDDIR}/lib
  290. $ENV{SDLSOUNDDIR}
  291. $ENV{SDLDIR}/lib
  292. $ENV{SDLDIR}
  293. /usr/local/lib
  294. /usr/lib
  295. /sw/lib
  296. /opt/local/lib
  297. /opt/csw/lib
  298. /opt/lib
  299. )
  300. IF(SMPEG_LIBRARY)
  301. SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SMPEG_LIBRARY})
  302. ENDIF(SMPEG_LIBRARY)
  303. ENDIF("${MY_OUTPUT}" MATCHES "SMPEG_")
  304. # Find FLAC
  305. IF("${MY_OUTPUT}" MATCHES "FLAC_")
  306. FIND_LIBRARY(FLAC_LIBRARY
  307. NAMES flac FLAC
  308. PATHS
  309. $ENV{FLACDIR}/lib
  310. $ENV{FLACDIR}
  311. $ENV{SDLSOUNDDIR}/lib
  312. $ENV{SDLSOUNDDIR}
  313. $ENV{SDLDIR}/lib
  314. $ENV{SDLDIR}
  315. /usr/local/lib
  316. /usr/lib
  317. /sw/lib
  318. /opt/local/lib
  319. /opt/csw/lib
  320. /opt/lib
  321. )
  322. IF(FLAC_LIBRARY)
  323. SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${FLAC_LIBRARY})
  324. ENDIF(FLAC_LIBRARY)
  325. ENDIF("${MY_OUTPUT}" MATCHES "FLAC_")
  326. # Hmmm...Speex seems to depend on Ogg. This might be a problem if
  327. # the TRY_COMPILE attempt gets blocked at SPEEX before it can pull
  328. # in the Ogg symbols. I'm not sure if I should duplicate the ogg stuff
  329. # above for here or if two ogg entries will screw up things.
  330. IF("${MY_OUTPUT}" MATCHES "speex_")
  331. FIND_LIBRARY(SPEEX_LIBRARY
  332. NAMES speex SPEEX
  333. PATHS
  334. $ENV{SPEEXDIR}/lib
  335. $ENV{SPEEXDIR}
  336. $ENV{SDLSOUNDDIR}/lib
  337. $ENV{SDLSOUNDDIR}
  338. $ENV{SDLDIR}/lib
  339. $ENV{SDLDIR}
  340. /usr/local/lib
  341. /usr/lib
  342. /sw/lib
  343. /opt/local/lib
  344. /opt/csw/lib
  345. /opt/lib
  346. )
  347. IF(SPEEX_LIBRARY)
  348. SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SPEEX_LIBRARY})
  349. ENDIF(SPEEX_LIBRARY)
  350. # Find OGG (needed for Speex)
  351. # We might have already found Ogg for Vorbis, so skip it if so.
  352. IF(NOT OGG_LIBRARY)
  353. FIND_LIBRARY(OGG_LIBRARY
  354. NAMES ogg Ogg OGG
  355. PATHS
  356. $ENV{OGGDIR}/lib
  357. $ENV{OGGDIR}
  358. $ENV{VORBISDIR}/lib
  359. $ENV{VORBISDIR}
  360. $ENV{SPEEXDIR}/lib
  361. $ENV{SPEEXDIR}
  362. $ENV{SDLSOUNDDIR}/lib
  363. $ENV{SDLSOUNDDIR}
  364. $ENV{SDLDIR}/lib
  365. $ENV{SDLDIR}
  366. /usr/local/lib
  367. /usr/lib
  368. /sw/lib
  369. /opt/local/lib
  370. /opt/csw/lib
  371. /opt/lib
  372. )
  373. IF(OGG_LIBRARY)
  374. SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
  375. ENDIF(OGG_LIBRARY)
  376. ENDIF(NOT OGG_LIBRARY)
  377. ENDIF("${MY_OUTPUT}" MATCHES "speex_")
  378. ELSE(NOT MY_RESULT)
  379. SET(SDL_SOUND_LIBRARIES "${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARY}" CACHE INTERNAL "SDL_sound and dependent libraries")
  380. ENDIF(NOT MY_RESULT)
  381. SET(SDL_SOUND_LIBRARIES "${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARIES_TMP}" CACHE INTERNAL "SDL_sound and dependent libraries")
  382. SET(SDL_SOUND_FOUND "YES")
  383. ENDIF(SDL_FOUND AND SDL_SOUND_INCLUDE_DIR AND SDL_SOUND_LIBRARY)
  384. # MESSAGE("SDL_SOUND_LIBRARIES is ${SDL_SOUND_LIBRARIES}")