1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Locates the SDL_sound library
12 This module depends on SDL being found and must be called AFTER
13 FindSDL.cmake is called.
19 SDL_SOUND_INCLUDE_DIR, where to find SDL_sound.h
20 SDL_SOUND_FOUND, if false, do not try to link to SDL_sound
21 SDL_SOUND_LIBRARIES, this contains the list of libraries that you need
23 SDL_SOUND_EXTRAS, this is an optional variable for you to add your own
24 flags to SDL_SOUND_LIBRARIES. This is prepended to SDL_SOUND_LIBRARIES.
25 This is available mostly for cases this module failed to anticipate for
26 and you must add additional flags. This is marked as ADVANCED.
27 SDL_SOUND_VERSION_STRING, human-readable string containing the
32 This module also defines (but you shouldn't need to use directly)
36 SDL_SOUND_LIBRARY, the name of just the SDL_sound library you would link
37 against. Use SDL_SOUND_LIBRARIES for you link instructions and not this one.
39 And might define the following as needed
53 Typically, you should not use these variables directly, and you should
54 use SDL_SOUND_LIBRARIES which contains SDL_SOUND_LIBRARY and the other
55 audio libraries (if needed) to successfully compile on your system.
57 Responds to the $SDLDIR and $SDLSOUNDDIR environmental variable that
58 would correspond to the ./configure --prefix=$SDLDIR used in building
61 On OSX, this will prefer the Framework version (if found) over others.
62 People will have to manually change the cache values of SDL_LIBRARY to
63 override this selectionor set the CMake environment CMAKE_INCLUDE_PATH
64 to modify the search paths.
65 #]=======================================================================]
69 This module is a bit more complicated than the
70 other FindSDL* family modules. The reason is that SDL_sound can be
71 compiled in a large variety of different ways which are independent of
72 platform. SDL_sound may dynamically link against other 3rd party
73 libraries to get additional codec support, such as Ogg Vorbis, SMPEG,
74 ModPlug, MikMod, FLAC, Speex, and potentially others. Under some
75 circumstances which I don't fully understand, there seems to be a
76 requirement that dependent libraries of libraries you use must also be
77 explicitly linked against in order to successfully compile. SDL_sound
78 does not currently have any system in place to know how it was
79 compiled. So this CMake module does the hard work in trying to
80 discover which 3rd party libraries are required for building (if any).
81 This module uses a brute force approach to create a test program that
82 uses SDL_sound, and then tries to build it. If the build fails, it
83 parses the error output for known symbol names to figure out which
87 set(SDL_SOUND_EXTRAS "" CACHE STRING "SDL_sound extra flags")
88 mark_as_advanced(SDL_SOUND_EXTRAS)
91 find_path(SDL_SOUND_INCLUDE_DIR SDL_sound.h
96 # path suffixes to search inside ENV{SDLDIR}
97 include/SDL include/SDL12 include/SDL11 include
100 find_library(SDL_SOUND_LIBRARY
105 PATH_SUFFIXES lib VisualC/win32lib
108 if(SDL_FOUND AND SDL_SOUND_INCLUDE_DIR AND SDL_SOUND_LIBRARY)
110 # CMake is giving me problems using TRY_COMPILE with the CMAKE_FLAGS
111 # for the :STRING syntax if I have multiple values contained in a
112 # single variable. This is a problem for the SDL_LIBRARY variable
113 # because it does just that. When I feed this variable to the command,
114 # only the first value gets the appropriate modifier (e.g. -I) and
115 # the rest get dropped.
116 # To get multiple single variables to work, I must separate them with a "\;"
117 # I could go back and modify the FindSDL.cmake module, but that's kind of painful.
118 # The solution would be to try something like:
119 # string(APPEND SDL_TRY_COMPILE_LIBRARY_LIST "\;${CMAKE_THREAD_LIBS_INIT}")
120 # Instead, it was suggested on the mailing list to write a temporary CMakeLists.txt
121 # with a temporary test project and invoke that with TRY_COMPILE.
122 # See message thread "Figuring out dependencies for a library in order to build"
126 # ${CMAKE_BINARY_DIR}
127 # ${PROJECT_SOURCE_DIR}/DetermineSoundLibs.c
129 # -DINCLUDE_DIRECTORIES:STRING=${SDL_INCLUDE_DIR}\;${SDL_SOUND_INCLUDE_DIR}
130 # -DLINK_LIBRARIES:STRING=${SDL_SOUND_LIBRARY}\;${SDL_LIBRARY}
131 # OUTPUT_VARIABLE MY_OUTPUT
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\"
140 int main(int argc, char* argv[])
142 Sound_AudioInfo desired;
143 Sound_Sample* sample;
148 /* This doesn't actually have to work, but Init() is a no-op
149 * for some of the decoders, so this should force more symbols
152 sample = Sound_NewSampleFromFile(argv[1], &desired, 4096);
161 # target_link_libraries(DetermineSoundLibs "${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
162 # causes problems when SDL_LIBRARY looks like
163 # /Library/Frameworks/SDL.framework;-framework Cocoa
164 # The ;-framework Cocoa seems to be confusing CMake once the OS X
165 # framework support was added. I was told that breaking up the list
166 # would fix the problem.
168 foreach(lib ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
169 string(APPEND TMP_TRY_LIBS " \"${lib}\"")
173 # Write the CMakeLists.txt and test project
174 # Weird, this is still sketchy. If I don't quote the variables
175 # in the TARGET_LINK_LIBRARIES, I seem to loose everything
176 # in the SDL_LIBRARY string after the "-framework".
177 # But if I quote the stuff in INCLUDE_DIRECTORIES, it doesn't work.
178 file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/CMakeLists.txt
179 "cmake_minimum_required(VERSION ${CMAKE_VERSION})
180 project(DetermineSoundLibs)
181 include_directories(${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
182 add_executable(DetermineSoundLibs DetermineSoundLibs.c)
183 target_link_libraries(DetermineSoundLibs ${TMP_TRY_LIBS})"
188 PROJECT DetermineSoundLibs
189 SOURCE_DIR ${PROJECT_BINARY_DIR}/CMakeTmp
190 BINARY_DIR ${PROJECT_BINARY_DIR}/CMakeTmp
191 OUTPUT_VARIABLE MY_OUTPUT
197 # I expect that MPGLIB, VOC, WAV, AIFF, and SHN are compiled in statically.
198 # I think Timidity is also compiled in statically.
199 # I've never had to explcitly link against Quicktime, so I'll skip that for now.
201 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY})
204 if("${MY_OUTPUT}" MATCHES "MikMod_")
205 find_library(MIKMOD_LIBRARY
206 NAMES libmikmod-coreaudio mikmod
216 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MIKMOD_LIBRARY})
217 endif(MIKMOD_LIBRARY)
218 endif("${MY_OUTPUT}" MATCHES "MikMod_")
221 if("${MY_OUTPUT}" MATCHES "MODPLUG_")
222 find_library(MODPLUG_LIBRARY
233 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MODPLUG_LIBRARY})
238 # Find Ogg and Vorbis
239 if("${MY_OUTPUT}" MATCHES "ov_")
240 find_library(VORBIS_LIBRARY
241 NAMES vorbis Vorbis VORBIS
252 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBIS_LIBRARY})
255 find_library(OGG_LIBRARY
267 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
273 if("${MY_OUTPUT}" MATCHES "SMPEG_")
274 find_library(SMPEG_LIBRARY
275 NAMES smpeg SMPEG Smpeg SMpeg
285 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SMPEG_LIBRARY})
291 if("${MY_OUTPUT}" MATCHES "FLAC_")
292 find_library(FLAC_LIBRARY
303 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${FLAC_LIBRARY})
308 # Hmmm...Speex seems to depend on Ogg. This might be a problem if
309 # the TRY_COMPILE attempt gets blocked at SPEEX before it can pull
310 # in the Ogg symbols. I'm not sure if I should duplicate the ogg stuff
311 # above for here or if two ogg entries will screw up things.
312 if("${MY_OUTPUT}" MATCHES "speex_")
313 find_library(SPEEX_LIBRARY
324 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SPEEX_LIBRARY})
327 # Find OGG (needed for Speex)
328 # We might have already found Ogg for Vorbis, so skip it if so.
330 find_library(OGG_LIBRARY
342 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
347 set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARIES_TMP})
349 set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARY})
353 if(SDL_SOUND_INCLUDE_DIR AND EXISTS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h")
354 file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SOUND_VER_MAJOR[ \t]+[0-9]+$")
355 file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MINOR_LINE REGEX "^#define[ \t]+SOUND_VER_MINOR[ \t]+[0-9]+$")
356 file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_PATCH_LINE REGEX "^#define[ \t]+SOUND_VER_PATCH[ \t]+[0-9]+$")
357 string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MAJOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MAJOR "${SDL_SOUND_VERSION_MAJOR_LINE}")
358 string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MINOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MINOR "${SDL_SOUND_VERSION_MINOR_LINE}")
359 string(REGEX REPLACE "^#define[ \t]+SOUND_VER_PATCH[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_PATCH "${SDL_SOUND_VERSION_PATCH_LINE}")
360 set(SDL_SOUND_VERSION_STRING ${SDL_SOUND_VERSION_MAJOR}.${SDL_SOUND_VERSION_MINOR}.${SDL_SOUND_VERSION_PATCH})
361 unset(SDL_SOUND_VERSION_MAJOR_LINE)
362 unset(SDL_SOUND_VERSION_MINOR_LINE)
363 unset(SDL_SOUND_VERSION_PATCH_LINE)
364 unset(SDL_SOUND_VERSION_MAJOR)
365 unset(SDL_SOUND_VERSION_MINOR)
366 unset(SDL_SOUND_VERSION_PATCH)
369 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)