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 Created by Eric Wing. This module is a bit more complicated than the
58 other FindSDL* family modules. The reason is that SDL_sound can be
59 compiled in a large variety of different ways which are independent of
60 platform. SDL_sound may dynamically link against other 3rd party
61 libraries to get additional codec support, such as Ogg Vorbis, SMPEG,
62 ModPlug, MikMod, FLAC, Speex, and potentially others. Under some
63 circumstances which I don't fully understand, there seems to be a
64 requirement that dependent libraries of libraries you use must also be
65 explicitly linked against in order to successfully compile. SDL_sound
66 does not currently have any system in place to know how it was
67 compiled. So this CMake module does the hard work in trying to
68 discover which 3rd party libraries are required for building (if any).
69 This module uses a brute force approach to create a test program that
70 uses SDL_sound, and then tries to build it. If the build fails, it
71 parses the error output for known symbol names to figure out which
74 Responds to the $SDLDIR and $SDLSOUNDDIR environmental variable that
75 would correspond to the ./configure --prefix=$SDLDIR used in building
78 On OSX, this will prefer the Framework version (if found) over others.
79 People will have to manually change the cache values of SDL_LIBRARY to
80 override this selectionor set the CMake environment CMAKE_INCLUDE_PATH
81 to modify the search paths.
82 #]=======================================================================]
84 set(SDL_SOUND_EXTRAS "" CACHE STRING "SDL_sound extra flags")
85 mark_as_advanced(SDL_SOUND_EXTRAS)
88 find_path(SDL_SOUND_INCLUDE_DIR SDL_sound.h
93 # path suffixes to search inside ENV{SDLDIR}
94 include/SDL include/SDL12 include/SDL11 include
97 find_library(SDL_SOUND_LIBRARY
102 PATH_SUFFIXES lib VisualC/win32lib
105 if(SDL_FOUND AND SDL_SOUND_INCLUDE_DIR AND SDL_SOUND_LIBRARY)
107 # CMake is giving me problems using TRY_COMPILE with the CMAKE_FLAGS
108 # for the :STRING syntax if I have multiple values contained in a
109 # single variable. This is a problem for the SDL_LIBRARY variable
110 # because it does just that. When I feed this variable to the command,
111 # only the first value gets the appropriate modifier (e.g. -I) and
112 # the rest get dropped.
113 # To get multiple single variables to work, I must separate them with a "\;"
114 # I could go back and modify the FindSDL.cmake module, but that's kind of painful.
115 # The solution would be to try something like:
116 # string(APPEND SDL_TRY_COMPILE_LIBRARY_LIST "\;${CMAKE_THREAD_LIBS_INIT}")
117 # Instead, it was suggested on the mailing list to write a temporary CMakeLists.txt
118 # with a temporary test project and invoke that with TRY_COMPILE.
119 # See message thread "Figuring out dependencies for a library in order to build"
123 # ${CMAKE_BINARY_DIR}
124 # ${PROJECT_SOURCE_DIR}/DetermineSoundLibs.c
126 # -DINCLUDE_DIRECTORIES:STRING=${SDL_INCLUDE_DIR}\;${SDL_SOUND_INCLUDE_DIR}
127 # -DLINK_LIBRARIES:STRING=${SDL_SOUND_LIBRARY}\;${SDL_LIBRARY}
128 # OUTPUT_VARIABLE MY_OUTPUT
131 # To minimize external dependencies, create a sdlsound test program
132 # which will be used to figure out if additional link dependencies are
133 # required for the link phase.
134 file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/DetermineSoundLibs.c
135 "#include \"SDL_sound.h\"
137 int main(int argc, char* argv[])
139 Sound_AudioInfo desired;
140 Sound_Sample* sample;
145 /* This doesn't actually have to work, but Init() is a no-op
146 * for some of the decoders, so this should force more symbols
149 sample = Sound_NewSampleFromFile(argv[1], &desired, 4096);
158 # target_link_libraries(DetermineSoundLibs "${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
159 # causes problems when SDL_LIBRARY looks like
160 # /Library/Frameworks/SDL.framework;-framework Cocoa
161 # The ;-framework Cocoa seems to be confusing CMake once the OS X
162 # framework support was added. I was told that breaking up the list
163 # would fix the problem.
165 foreach(lib ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
166 string(APPEND TMP_TRY_LIBS " \"${lib}\"")
170 # Write the CMakeLists.txt and test project
171 # Weird, this is still sketchy. If I don't quote the variables
172 # in the TARGET_LINK_LIBRARIES, I seem to loose everything
173 # in the SDL_LIBRARY string after the "-framework".
174 # But if I quote the stuff in INCLUDE_DIRECTORIES, it doesn't work.
175 file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/CMakeLists.txt
176 "cmake_minimum_required(VERSION ${CMAKE_VERSION})
177 project(DetermineSoundLibs)
178 include_directories(${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
179 add_executable(DetermineSoundLibs DetermineSoundLibs.c)
180 target_link_libraries(DetermineSoundLibs ${TMP_TRY_LIBS})"
185 ${PROJECT_BINARY_DIR}/CMakeTmp
186 ${PROJECT_BINARY_DIR}/CMakeTmp
188 OUTPUT_VARIABLE MY_OUTPUT
194 # I expect that MPGLIB, VOC, WAV, AIFF, and SHN are compiled in statically.
195 # I think Timidity is also compiled in statically.
196 # I've never had to explcitly link against Quicktime, so I'll skip that for now.
198 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY})
201 if("${MY_OUTPUT}" MATCHES "MikMod_")
202 find_library(MIKMOD_LIBRARY
203 NAMES libmikmod-coreaudio mikmod
213 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MIKMOD_LIBRARY})
214 endif(MIKMOD_LIBRARY)
215 endif("${MY_OUTPUT}" MATCHES "MikMod_")
218 if("${MY_OUTPUT}" MATCHES "MODPLUG_")
219 find_library(MODPLUG_LIBRARY
230 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MODPLUG_LIBRARY})
235 # Find Ogg and Vorbis
236 if("${MY_OUTPUT}" MATCHES "ov_")
237 find_library(VORBIS_LIBRARY
238 NAMES vorbis Vorbis VORBIS
249 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBIS_LIBRARY})
252 find_library(OGG_LIBRARY
264 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
270 if("${MY_OUTPUT}" MATCHES "SMPEG_")
271 find_library(SMPEG_LIBRARY
272 NAMES smpeg SMPEG Smpeg SMpeg
282 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SMPEG_LIBRARY})
288 if("${MY_OUTPUT}" MATCHES "FLAC_")
289 find_library(FLAC_LIBRARY
300 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${FLAC_LIBRARY})
305 # Hmmm...Speex seems to depend on Ogg. This might be a problem if
306 # the TRY_COMPILE attempt gets blocked at SPEEX before it can pull
307 # in the Ogg symbols. I'm not sure if I should duplicate the ogg stuff
308 # above for here or if two ogg entries will screw up things.
309 if("${MY_OUTPUT}" MATCHES "speex_")
310 find_library(SPEEX_LIBRARY
321 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SPEEX_LIBRARY})
324 # Find OGG (needed for Speex)
325 # We might have already found Ogg for Vorbis, so skip it if so.
327 find_library(OGG_LIBRARY
339 set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
344 set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARIES_TMP})
346 set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARY})
350 if(SDL_SOUND_INCLUDE_DIR AND EXISTS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h")
351 file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SOUND_VER_MAJOR[ \t]+[0-9]+$")
352 file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MINOR_LINE REGEX "^#define[ \t]+SOUND_VER_MINOR[ \t]+[0-9]+$")
353 file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_PATCH_LINE REGEX "^#define[ \t]+SOUND_VER_PATCH[ \t]+[0-9]+$")
354 string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MAJOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MAJOR "${SDL_SOUND_VERSION_MAJOR_LINE}")
355 string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MINOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MINOR "${SDL_SOUND_VERSION_MINOR_LINE}")
356 string(REGEX REPLACE "^#define[ \t]+SOUND_VER_PATCH[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_PATCH "${SDL_SOUND_VERSION_PATCH_LINE}")
357 set(SDL_SOUND_VERSION_STRING ${SDL_SOUND_VERSION_MAJOR}.${SDL_SOUND_VERSION_MINOR}.${SDL_SOUND_VERSION_PATCH})
358 unset(SDL_SOUND_VERSION_MAJOR_LINE)
359 unset(SDL_SOUND_VERSION_MINOR_LINE)
360 unset(SDL_SOUND_VERSION_PATCH_LINE)
361 unset(SDL_SOUND_VERSION_MAJOR)
362 unset(SDL_SOUND_VERSION_MINOR)
363 unset(SDL_SOUND_VERSION_PATCH)
366 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
368 FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL_sound
369 REQUIRED_VARS SDL_SOUND_LIBRARY SDL_SOUND_INCLUDE_DIR
370 VERSION_VAR SDL_SOUND_VERSION_STRING)