FindSDL_sound.cmake 13 KB

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