FindSDL_sound.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #.rst:
  2. # FindSDL_sound
  3. # -------------
  4. #
  5. # Locates the SDL_sound library
  6. #
  7. #
  8. #
  9. # This module depends on SDL being found and must be called AFTER
  10. # FindSDL.cmake is called.
  11. #
  12. # This module defines
  13. #
  14. # ::
  15. #
  16. # SDL_SOUND_INCLUDE_DIR, where to find SDL_sound.h
  17. # SDL_SOUND_FOUND, if false, do not try to link to SDL_sound
  18. # SDL_SOUND_LIBRARIES, this contains the list of libraries that you need
  19. # to link against. This is a read-only variable and is marked INTERNAL.
  20. # SDL_SOUND_EXTRAS, this is an optional variable for you to add your own
  21. # flags to SDL_SOUND_LIBRARIES. This is prepended to SDL_SOUND_LIBRARIES.
  22. # This is available mostly for cases this module failed to anticipate for
  23. # and you must add additional flags. This is marked as ADVANCED.
  24. # SDL_SOUND_VERSION_STRING, human-readable string containing the
  25. # version of SDL_sound
  26. #
  27. #
  28. #
  29. # This module also defines (but you shouldn't need to use directly)
  30. #
  31. # ::
  32. #
  33. # SDL_SOUND_LIBRARY, the name of just the SDL_sound library you would link
  34. # against. Use SDL_SOUND_LIBRARIES for you link instructions and not this one.
  35. #
  36. # And might define the following as needed
  37. #
  38. # ::
  39. #
  40. # MIKMOD_LIBRARY
  41. # MODPLUG_LIBRARY
  42. # OGG_LIBRARY
  43. # VORBIS_LIBRARY
  44. # SMPEG_LIBRARY
  45. # FLAC_LIBRARY
  46. # SPEEX_LIBRARY
  47. #
  48. #
  49. #
  50. # Typically, you should not use these variables directly, and you should
  51. # use SDL_SOUND_LIBRARIES which contains SDL_SOUND_LIBRARY and the other
  52. # audio libraries (if needed) to successfully compile on your system.
  53. #
  54. # Created by Eric Wing. This module is a bit more complicated than the
  55. # other FindSDL* family modules. The reason is that SDL_sound can be
  56. # compiled in a large variety of different ways which are independent of
  57. # platform. SDL_sound may dynamically link against other 3rd party
  58. # libraries to get additional codec support, such as Ogg Vorbis, SMPEG,
  59. # ModPlug, MikMod, FLAC, Speex, and potentially others. Under some
  60. # circumstances which I don't fully understand, there seems to be a
  61. # requirement that dependent libraries of libraries you use must also be
  62. # explicitly linked against in order to successfully compile. SDL_sound
  63. # does not currently have any system in place to know how it was
  64. # compiled. So this CMake module does the hard work in trying to
  65. # discover which 3rd party libraries are required for building (if any).
  66. # This module uses a brute force approach to create a test program that
  67. # uses SDL_sound, and then tries to build it. If the build fails, it
  68. # parses the error output for known symbol names to figure out which
  69. # libraries are needed.
  70. #
  71. # Responds to the $SDLDIR and $SDLSOUNDDIR environmental variable that
  72. # would correspond to the ./configure --prefix=$SDLDIR used in building
  73. # SDL.
  74. #
  75. # On OSX, this will prefer the Framework version (if found) over others.
  76. # People will have to manually change the cache values of SDL_LIBRARY to
  77. # override this selectionor set the CMake environment CMAKE_INCLUDE_PATH
  78. # to modify the search paths.
  79. #=============================================================================
  80. # Copyright 2005-2009 Kitware, Inc.
  81. # Copyright 2012 Benjamin Eikel
  82. #
  83. # Distributed under the OSI-approved BSD License (the "License");
  84. # see accompanying file Copyright.txt for details.
  85. #
  86. # This software is distributed WITHOUT ANY WARRANTY; without even the
  87. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  88. # See the License for more information.
  89. #=============================================================================
  90. # (To distribute this file outside of CMake, substitute the full
  91. # License text for the above reference.)
  92. set(SDL_SOUND_EXTRAS "" CACHE STRING "SDL_sound extra flags")
  93. mark_as_advanced(SDL_SOUND_EXTRAS)
  94. # Find SDL_sound.h
  95. find_path(SDL_SOUND_INCLUDE_DIR SDL_sound.h
  96. HINTS
  97. ENV SDLSOUNDDIR
  98. ENV SDLDIR
  99. PATH_SUFFIXES SDL
  100. # path suffixes to search inside ENV{SDLDIR}
  101. include/SDL include/SDL12 include/SDL11 include
  102. )
  103. find_library(SDL_SOUND_LIBRARY
  104. NAMES SDL_sound
  105. HINTS
  106. ENV SDLSOUNDDIR
  107. ENV SDLDIR
  108. PATH_SUFFIXES lib VisualC/win32lib
  109. )
  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()
  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. "cmake_minimum_required(VERSION ${CMAKE_VERSION})
  175. project(DetermineSoundLibs)
  176. include_directories(${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
  177. add_executable(DetermineSoundLibs DetermineSoundLibs.c)
  178. target_link_libraries(DetermineSoundLibs ${TMP_TRY_LIBS})"
  179. )
  180. try_compile(
  181. MY_RESULT
  182. ${PROJECT_BINARY_DIR}/CMakeTmp
  183. ${PROJECT_BINARY_DIR}/CMakeTmp
  184. DetermineSoundLibs
  185. OUTPUT_VARIABLE MY_OUTPUT
  186. )
  187. # message("${MY_RESULT}")
  188. # message(${MY_OUTPUT})
  189. if(NOT MY_RESULT)
  190. # I expect that MPGLIB, VOC, WAV, AIFF, and SHN are compiled in statically.
  191. # I think Timidity is also compiled in statically.
  192. # I've never had to explcitly link against Quicktime, so I'll skip that for now.
  193. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY})
  194. # Find MikMod
  195. if("${MY_OUTPUT}" MATCHES "MikMod_")
  196. find_library(MIKMOD_LIBRARY
  197. NAMES libmikmod-coreaudio mikmod
  198. PATHS
  199. ENV MIKMODDIR
  200. ENV SDLSOUNDDIR
  201. ENV SDLDIR
  202. /sw
  203. /opt/local
  204. /opt/csw
  205. /opt
  206. PATH_SUFFIXES
  207. lib
  208. )
  209. if(MIKMOD_LIBRARY)
  210. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MIKMOD_LIBRARY})
  211. endif(MIKMOD_LIBRARY)
  212. endif("${MY_OUTPUT}" MATCHES "MikMod_")
  213. # Find ModPlug
  214. if("${MY_OUTPUT}" MATCHES "MODPLUG_")
  215. find_library(MODPLUG_LIBRARY
  216. NAMES modplug
  217. PATHS
  218. ENV MODPLUGDIR
  219. ENV SDLSOUNDDIR
  220. ENV SDLDIR
  221. /sw
  222. /opt/local
  223. /opt/csw
  224. /opt
  225. PATH_SUFFIXES
  226. lib
  227. )
  228. if(MODPLUG_LIBRARY)
  229. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MODPLUG_LIBRARY})
  230. endif()
  231. endif()
  232. # Find Ogg and Vorbis
  233. if("${MY_OUTPUT}" MATCHES "ov_")
  234. find_library(VORBIS_LIBRARY
  235. NAMES vorbis Vorbis VORBIS
  236. PATHS
  237. ENV VORBISDIR
  238. ENV OGGDIR
  239. ENV SDLSOUNDDIR
  240. ENV SDLDIR
  241. /sw
  242. /opt/local
  243. /opt/csw
  244. /opt
  245. PATH_SUFFIXES
  246. lib
  247. )
  248. if(VORBIS_LIBRARY)
  249. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBIS_LIBRARY})
  250. endif()
  251. find_library(OGG_LIBRARY
  252. NAMES ogg Ogg OGG
  253. PATHS
  254. ENV OGGDIR
  255. ENV VORBISDIR
  256. ENV SDLSOUNDDIR
  257. ENV SDLDIR
  258. /sw
  259. /opt/local
  260. /opt/csw
  261. /opt
  262. PATH_SUFFIXES
  263. 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
  275. ENV SDLSOUNDDIR
  276. ENV SDLDIR
  277. /sw
  278. /opt/local
  279. /opt/csw
  280. /opt
  281. PATH_SUFFIXES
  282. lib
  283. )
  284. if(SMPEG_LIBRARY)
  285. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SMPEG_LIBRARY})
  286. endif()
  287. endif()
  288. # Find FLAC
  289. if("${MY_OUTPUT}" MATCHES "FLAC_")
  290. find_library(FLAC_LIBRARY
  291. NAMES flac FLAC
  292. PATHS
  293. ENV FLACDIR
  294. ENV SDLSOUNDDIR
  295. ENV SDLDIR
  296. /sw
  297. /opt/local
  298. /opt/csw
  299. /opt
  300. PATH_SUFFIXES
  301. lib
  302. )
  303. if(FLAC_LIBRARY)
  304. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${FLAC_LIBRARY})
  305. endif()
  306. endif()
  307. # Hmmm...Speex seems to depend on Ogg. This might be a problem if
  308. # the TRY_COMPILE attempt gets blocked at SPEEX before it can pull
  309. # in the Ogg symbols. I'm not sure if I should duplicate the ogg stuff
  310. # above for here or if two ogg entries will screw up things.
  311. if("${MY_OUTPUT}" MATCHES "speex_")
  312. find_library(SPEEX_LIBRARY
  313. NAMES speex SPEEX
  314. PATHS
  315. ENV SPEEXDIR
  316. ENV SDLSOUNDDIR
  317. ENV SDLDIR
  318. /sw
  319. /opt/local
  320. /opt/csw
  321. /opt
  322. PATH_SUFFIXES
  323. lib
  324. )
  325. if(SPEEX_LIBRARY)
  326. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SPEEX_LIBRARY})
  327. endif()
  328. # Find OGG (needed for Speex)
  329. # We might have already found Ogg for Vorbis, so skip it if so.
  330. if(NOT OGG_LIBRARY)
  331. find_library(OGG_LIBRARY
  332. NAMES ogg Ogg OGG
  333. PATHS
  334. ENV OGGDIR
  335. ENV VORBISDIR
  336. ENV SPEEXDIR
  337. ENV SDLSOUNDDIR
  338. ENV SDLDIR
  339. /sw
  340. /opt/local
  341. /opt/csw
  342. /opt
  343. PATH_SUFFIXES lib
  344. )
  345. if(OGG_LIBRARY)
  346. set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
  347. endif()
  348. endif()
  349. endif()
  350. else()
  351. set(SDL_SOUND_LIBRARIES "${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARY}" CACHE INTERNAL "SDL_sound and dependent libraries")
  352. endif()
  353. set(SDL_SOUND_LIBRARIES "${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARIES_TMP}" CACHE INTERNAL "SDL_sound and dependent libraries")
  354. endif()
  355. if(SDL_SOUND_INCLUDE_DIR AND EXISTS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h")
  356. file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SOUND_VER_MAJOR[ \t]+[0-9]+$")
  357. file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MINOR_LINE REGEX "^#define[ \t]+SOUND_VER_MINOR[ \t]+[0-9]+$")
  358. file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_PATCH_LINE REGEX "^#define[ \t]+SOUND_VER_PATCH[ \t]+[0-9]+$")
  359. string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MAJOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MAJOR "${SDL_SOUND_VERSION_MAJOR_LINE}")
  360. string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MINOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MINOR "${SDL_SOUND_VERSION_MINOR_LINE}")
  361. string(REGEX REPLACE "^#define[ \t]+SOUND_VER_PATCH[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_PATCH "${SDL_SOUND_VERSION_PATCH_LINE}")
  362. set(SDL_SOUND_VERSION_STRING ${SDL_SOUND_VERSION_MAJOR}.${SDL_SOUND_VERSION_MINOR}.${SDL_SOUND_VERSION_PATCH})
  363. unset(SDL_SOUND_VERSION_MAJOR_LINE)
  364. unset(SDL_SOUND_VERSION_MINOR_LINE)
  365. unset(SDL_SOUND_VERSION_PATCH_LINE)
  366. unset(SDL_SOUND_VERSION_MAJOR)
  367. unset(SDL_SOUND_VERSION_MINOR)
  368. unset(SDL_SOUND_VERSION_PATCH)
  369. endif()
  370. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  371. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL_sound
  372. REQUIRED_VARS SDL_SOUND_LIBRARY SDL_SOUND_INCLUDE_DIR
  373. VERSION_VAR SDL_SOUND_VERSION_STRING)