FindSDL_sound.cmake 12 KB

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