FindSDL_sound.cmake 13 KB

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