Recognize Headset formfactors as headphones
[openal-soft.git] / cmake / FindSDL_sound.cmake
blob5557b55b1587a293afc2c41f57f7f3625b20c2b9
1 # - Locates the SDL_sound library
3 # This module depends on SDL being found and
4 # must be called AFTER FindSDL.cmake or FindSDL2.cmake is called.
6 # This module defines
7 #  SDL_SOUND_INCLUDE_DIR, where to find SDL_sound.h
8 #  SDL_SOUND_FOUND, if false, do not try to link to SDL_sound
9 #  SDL_SOUND_LIBRARIES, this contains the list of libraries that you need
10 #    to link against. This is a read-only variable and is marked INTERNAL.
11 #  SDL_SOUND_EXTRAS, this is an optional variable for you to add your own
12 #    flags to SDL_SOUND_LIBRARIES. This is prepended to SDL_SOUND_LIBRARIES.
13 #    This is available mostly for cases this module failed to anticipate for
14 #    and you must add additional flags. This is marked as ADVANCED.
15 #  SDL_SOUND_VERSION_STRING, human-readable string containing the version of SDL_sound
17 # This module also defines (but you shouldn't need to use directly)
18 #   SDL_SOUND_LIBRARY, the name of just the SDL_sound library you would link
19 #   against. Use SDL_SOUND_LIBRARIES for you link instructions and not this one.
20 # And might define the following as needed
21 #   MIKMOD_LIBRARY
22 #   MODPLUG_LIBRARY
23 #   OGG_LIBRARY
24 #   VORBIS_LIBRARY
25 #   SMPEG_LIBRARY
26 #   FLAC_LIBRARY
27 #   SPEEX_LIBRARY
29 # Typically, you should not use these variables directly, and you should use
30 # SDL_SOUND_LIBRARIES which contains SDL_SOUND_LIBRARY and the other audio libraries
31 # (if needed) to successfully compile on your system.
33 # Created by Eric Wing.
34 # This module is a bit more complicated than the other FindSDL* family modules.
35 # The reason is that SDL_sound can be compiled in a large variety of different ways
36 # which are independent of platform. SDL_sound may dynamically link against other 3rd
37 # party libraries to get additional codec support, such as Ogg Vorbis, SMPEG, ModPlug,
38 # MikMod, FLAC, Speex, and potentially others.
39 # Under some circumstances which I don't fully understand,
40 # there seems to be a requirement
41 # that dependent libraries of libraries you use must also be explicitly
42 # linked against in order to successfully compile. SDL_sound does not currently
43 # have any system in place to know how it was compiled.
44 # So this CMake module does the hard work in trying to discover which 3rd party
45 # libraries are required for building (if any).
46 # This module uses a brute force approach to create a test program that uses SDL_sound,
47 # and then tries to build it. If the build fails, it parses the error output for
48 # known symbol names to figure out which libraries are needed.
50 # Responds to the $SDLDIR and $SDLSOUNDDIR environmental variable that would
51 # correspond to the ./configure --prefix=$SDLDIR used in building SDL.
53 # On OSX, this will prefer the Framework version (if found) over others.
54 # People will have to manually change the cache values of
55 # SDL_LIBRARY or SDL2_LIBRARY to override this selection or set the CMake
56 # environment CMAKE_INCLUDE_PATH to modify the search paths.
58 #=============================================================================
59 # Copyright 2005-2009 Kitware, Inc.
60 # Copyright 2012 Benjamin Eikel
62 # Distributed under the OSI-approved BSD License (the "License");
63 # see accompanying file Copyright.txt for details.
65 # This software is distributed WITHOUT ANY WARRANTY; without even the
66 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
67 # See the License for more information.
68 #=============================================================================
69 # (To distribute this file outside of CMake, substitute the full
70 #  License text for the above reference.)
72 set(SDL_SOUND_EXTRAS "" CACHE STRING "SDL_sound extra flags")
73 mark_as_advanced(SDL_SOUND_EXTRAS)
75 # Find SDL_sound.h
76 find_path(SDL_SOUND_INCLUDE_DIR SDL_sound.h
77     HINTS
78         ENV SDLSOUNDDIR
79         ENV SDLDIR
80     PATH_SUFFIXES SDL SDL12 SDL11
83 find_library(SDL_SOUND_LIBRARY
84     NAMES SDL_sound
85     HINTS
86         ENV SDLSOUNDDIR
87         ENV SDLDIR
90 if(SDL2_FOUND OR SDL_FOUND)
91     if(SDL_SOUND_INCLUDE_DIR AND SDL_SOUND_LIBRARY)
92         # CMake is giving me problems using TRY_COMPILE with the CMAKE_FLAGS
93         # for the :STRING syntax if I have multiple values contained in a
94         # single variable. This is a problem for the SDL2_LIBRARY variable
95         # because it does just that. When I feed this variable to the command,
96         # only the first value gets the appropriate modifier (e.g. -I) and
97         # the rest get dropped.
98         # To get multiple single variables to work, I must separate them with a "\;"
99         # I could go back and modify the FindSDL2.cmake module, but that's kind of painful.
100         # The solution would be to try something like:
101         # set(SDL2_TRY_COMPILE_LIBRARY_LIST "${SDL2_TRY_COMPILE_LIBRARY_LIST}\;${CMAKE_THREAD_LIBS_INIT}")
102         # Instead, it was suggested on the mailing list to write a temporary CMakeLists.txt
103         # with a temporary test project and invoke that with TRY_COMPILE.
104         # See message thread "Figuring out dependencies for a library in order to build"
105         # 2005-07-16
106         #     try_compile(
107         #             MY_RESULT
108         #             ${CMAKE_BINARY_DIR}
109         #             ${PROJECT_SOURCE_DIR}/DetermineSoundLibs.c
110         #             CMAKE_FLAGS
111         #                     -DINCLUDE_DIRECTORIES:STRING=${SDL2_INCLUDE_DIR}\;${SDL_SOUND_INCLUDE_DIR}
112         #                     -DLINK_LIBRARIES:STRING=${SDL_SOUND_LIBRARY}\;${SDL2_LIBRARY}
113         #             OUTPUT_VARIABLE MY_OUTPUT
114         #     )
116         # To minimize external dependencies, create a sdlsound test program
117         # which will be used to figure out if additional link dependencies are
118         # required for the link phase.
119         file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/DetermineSoundLibs.c
120             "#include \"SDL_sound.h\"
121             #include \"SDL.h\"
122             int main(int argc, char* argv[])
123             {
124                 Sound_AudioInfo desired;
125                 Sound_Sample* sample;
127                 SDL_Init(0);
128                 Sound_Init();
130                 /* This doesn't actually have to work, but Init() is a no-op
131                  * for some of the decoders, so this should force more symbols
132                  * to be pulled in.
133                  */
134                 sample = Sound_NewSampleFromFile(argv[1], &desired, 4096);
136                 Sound_Quit();
137                 SDL_Quit();
138                 return 0;
139             }"
140         )
142         # Calling
143         # target_link_libraries(DetermineSoundLibs "${SDL_SOUND_LIBRARY} ${SDL2_LIBRARY})
144         # causes problems when SDL2_LIBRARY looks like
145         # /Library/Frameworks/SDL2.framework;-framework Cocoa
146         # The ;-framework Cocoa seems to be confusing CMake once the OS X
147         # framework support was added. I was told that breaking up the list
148         # would fix the problem.
149         set(TMP_LIBS "")
150         if(SDL2_FOUND)
151             set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY} ${SDL2_LIBRARY})
152             foreach(lib ${SDL_SOUND_LIBRARY} ${SDL2_LIBRARY})
153                 set(TMP_LIBS "${TMP_LIBS} \"${lib}\"")
154             endforeach()
155             set(TMP_INCLUDE_DIRS ${SDL2_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
156         else()
157             set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
158             foreach(lib ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
159                 set(TMP_LIBS "${TMP_LIBS} \"${lib}\"")
160             endforeach()
161             set(TMP_INCLUDE_DIRS ${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
162         endif()
164         # Keep trying to build a temp project until we find all missing libs.
165         set(TRY_AGAIN TRUE)
166         WHILE(TRY_AGAIN)
167             set(TRY_AGAIN FALSE)
168             # message("TMP_TRY_LIBS ${TMP_TRY_LIBS}")
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 SDL2_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 2.8)
177                 project(DetermineSoundLibs C)
178                 include_directories(${TMP_INCLUDE_DIRS})
179                 add_executable(DetermineSoundLibs DetermineSoundLibs.c)
180                 target_link_libraries(DetermineSoundLibs ${TMP_LIBS})"
181             )
183             try_compile(
184                 MY_RESULT
185                 ${PROJECT_BINARY_DIR}/CMakeTmp
186                 ${PROJECT_BINARY_DIR}/CMakeTmp
187                 DetermineSoundLibs
188                 OUTPUT_VARIABLE MY_OUTPUT
189             )
190             # message("${MY_RESULT}")
191             # message(${MY_OUTPUT})
193             if(NOT MY_RESULT)
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                 # Find libmath
199                 if("${MY_OUTPUT}" MATCHES "cos@@GLIBC")
200                     find_library(MATH_LIBRARY NAMES m)
201                     if(MATH_LIBRARY)
202                         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MATH_LIBRARY})
203                         set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${MATH_LIBRARY}\"")
204                         set(TRY_AGAIN TRUE)
205                     endif(MATH_LIBRARY)
206                 endif("${MY_OUTPUT}" MATCHES "cos@@GLIBC")
208                 # Find MikMod
209                 if("${MY_OUTPUT}" MATCHES "MikMod_")
210                     find_library(MIKMOD_LIBRARY
211                         NAMES libmikmod-coreaudio mikmod
212                         PATHS
213                             ENV MIKMODDIR
214                             ENV SDLSOUNDDIR
215                             ENV SDLDIR
216                             /sw
217                             /opt/local
218                             /opt/csw
219                             /opt
220                         PATH_SUFFIXES lib
221                     )
222                     if(MIKMOD_LIBRARY)
223                         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MIKMOD_LIBRARY})
224                         set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${MIKMOD_LIBRARY}\"")
225                         set(TRY_AGAIN TRUE)
226                     endif(MIKMOD_LIBRARY)
227                 endif("${MY_OUTPUT}" MATCHES "MikMod_")
229                 # Find ModPlug
230                 if("${MY_OUTPUT}" MATCHES "MODPLUG_")
231                     find_library(MODPLUG_LIBRARY
232                         NAMES modplug
233                         PATHS
234                             ENV MODPLUGDIR
235                             ENV SDLSOUNDDIR
236                             ENV SDLDIR
237                             /sw
238                             /opt/local
239                             /opt/csw
240                             /opt
241                         PATH_SUFFIXES lib
242                     )
243                     if(MODPLUG_LIBRARY)
244                         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MODPLUG_LIBRARY})
245                         set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${MODPLUG_LIBRARY}\"")
246                         set(TRY_AGAIN TRUE)
247                     endif()
248                 endif()
250                 # Find Ogg and Vorbis
251                 if("${MY_OUTPUT}" MATCHES "ov_")
252                     find_library(VORBISFILE_LIBRARY
253                         NAMES vorbisfile VorbisFile VORBISFILE
254                         PATHS
255                             ENV VORBISDIR
256                             ENV OGGDIR
257                             ENV SDLSOUNDDIR
258                             ENV SDLDIR
259                             /sw
260                             /opt/local
261                             /opt/csw
262                             /opt
263                         PATH_SUFFIXES lib
264                     )
265                     if(VORBISFILE_LIBRARY)
266                         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBISFILE_LIBRARY})
267                         set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${VORBISFILE_LIBRARY}\"")
268                         set(TRY_AGAIN TRUE)
269                     endif()
271                     find_library(VORBIS_LIBRARY
272                         NAMES vorbis Vorbis VORBIS
273                         PATHS
274                             ENV OGGDIR
275                             ENV VORBISDIR
276                             ENV SDLSOUNDDIR
277                             ENV SDLDIR
278                             /sw
279                             /opt/local
280                             /opt/csw
281                             /opt
282                         PATH_SUFFIXES lib
283                     )
284                     if(VORBIS_LIBRARY)
285                         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBIS_LIBRARY})
286                         set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${VORBIS_LIBRARY}\"")
287                         set(TRY_AGAIN TRUE)
288                     endif()
290                     find_library(OGG_LIBRARY
291                         NAMES ogg Ogg OGG
292                         PATHS
293                             ENV OGGDIR
294                             ENV VORBISDIR
295                             ENV SDLSOUNDDIR
296                             ENV SDLDIR
297                             /sw
298                             /opt/local
299                             /opt/csw
300                             /opt
301                         PATH_SUFFIXES lib
302                     )
303                     if(OGG_LIBRARY)
304                         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
305                         set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${OGG_LIBRARY}\"")
306                         set(TRY_AGAIN TRUE)
307                     endif()
308                 endif()
310                 # Find SMPEG
311                 if("${MY_OUTPUT}" MATCHES "SMPEG_")
312                     find_library(SMPEG_LIBRARY
313                         NAMES smpeg SMPEG Smpeg SMpeg
314                         PATHS
315                             ENV SMPEGDIR
316                             ENV SDLSOUNDDIR
317                             ENV SDLDIR
318                             /sw
319                             /opt/local
320                             /opt/csw
321                             /opt
322                         PATH_SUFFIXES lib
323                     )
324                     if(SMPEG_LIBRARY)
325                         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SMPEG_LIBRARY})
326                         set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${SMPEG_LIBRARY}\"")
327                         set(TRY_AGAIN TRUE)
328                     endif()
329                 endif()
332                 # Find FLAC
333                 if("${MY_OUTPUT}" MATCHES "FLAC_")
334                     find_library(FLAC_LIBRARY
335                         NAMES flac FLAC
336                         PATHS
337                             ENV FLACDIR
338                             ENV SDLSOUNDDIR
339                             ENV SDLDIR
340                             /sw
341                             /opt/local
342                             /opt/csw
343                             /opt
344                         PATH_SUFFIXES lib
345                     )
346                     if(FLAC_LIBRARY)
347                         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${FLAC_LIBRARY})
348                         set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${FLAC_LIBRARY}\"")
349                         set(TRY_AGAIN TRUE)
350                     endif()
351                 endif()
354                 # Hmmm...Speex seems to depend on Ogg. This might be a problem if
355                 # the TRY_COMPILE attempt gets blocked at SPEEX before it can pull
356                 # in the Ogg symbols. I'm not sure if I should duplicate the ogg stuff
357                 # above for here or if two ogg entries will screw up things.
358                 if("${MY_OUTPUT}" MATCHES "speex_")
359                     find_library(SPEEX_LIBRARY
360                         NAMES speex SPEEX
361                         PATHS
362                             ENV SPEEXDIR
363                             ENV SDLSOUNDDIR
364                             ENV SDLDIR
365                             /sw
366                             /opt/local
367                             /opt/csw
368                             /opt
369                         PATH_SUFFIXES lib
370                     )
371                     if(SPEEX_LIBRARY)
372                         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SPEEX_LIBRARY})
373                         set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${SPEEX_LIBRARY}\"")
374                         set(TRY_AGAIN TRUE)
375                     endif()
377                     # Find OGG (needed for Speex)
378                     # We might have already found Ogg for Vorbis, so skip it if so.
379                     if(NOT OGG_LIBRARY)
380                         find_library(OGG_LIBRARY
381                             NAMES ogg Ogg OGG
382                             PATHS
383                                 ENV OGGDIR
384                                 ENV VORBISDIR
385                                 ENV SPEEXDIR
386                                 ENV SDLSOUNDDIR
387                                 ENV SDLDIR
388                                 /sw
389                                 /opt/local
390                                 /opt/csw
391                                 /opt
392                             PATH_SUFFIXES lib
393                         )
394                         if(OGG_LIBRARY)
395                             set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
396                             set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${OGG_LIBRARY}\"")
397                             set(TRY_AGAIN TRUE)
398                         endif()
399                     endif()
400                 endif()
401             endif()
402         ENDWHILE()
403         unset(TMP_INCLUDE_DIRS)
404         unset(TMP_LIBS)
406         set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARIES_TMP} CACHE INTERNAL "SDL_sound and dependent libraries")
407     endif()
408 endif()
410 if(SDL_SOUND_INCLUDE_DIR AND EXISTS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h")
411   file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SOUND_VER_MAJOR[ \t]+[0-9]+$")
412   file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MINOR_LINE REGEX "^#define[ \t]+SOUND_VER_MINOR[ \t]+[0-9]+$")
413   file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_PATCH_LINE REGEX "^#define[ \t]+SOUND_VER_PATCH[ \t]+[0-9]+$")
414   string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MAJOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MAJOR "${SDL_SOUND_VERSION_MAJOR_LINE}")
415   string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MINOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MINOR "${SDL_SOUND_VERSION_MINOR_LINE}")
416   string(REGEX REPLACE "^#define[ \t]+SOUND_VER_PATCH[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_PATCH "${SDL_SOUND_VERSION_PATCH_LINE}")
417   set(SDL_SOUND_VERSION_STRING ${SDL_SOUND_VERSION_MAJOR}.${SDL_SOUND_VERSION_MINOR}.${SDL_SOUND_VERSION_PATCH})
418   unset(SDL_SOUND_VERSION_MAJOR_LINE)
419   unset(SDL_SOUND_VERSION_MINOR_LINE)
420   unset(SDL_SOUND_VERSION_PATCH_LINE)
421   unset(SDL_SOUND_VERSION_MAJOR)
422   unset(SDL_SOUND_VERSION_MINOR)
423   unset(SDL_SOUND_VERSION_PATCH)
424 endif()
426 include(FindPackageHandleStandardArgs)
427 FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL_sound
428                                   REQUIRED_VARS SDL_SOUND_LIBRARIES SDL_SOUND_INCLUDE_DIR
429                                   VERSION_VAR SDL_SOUND_VERSION_STRING)