FindSDL_sound.cmake 12 KB

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