FindSDL_sound.cmake 13 KB

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