Don't try to use the non-standard alloca.h
[openal-soft.git] / cmake / FindSDL_sound.cmake
blob97d2a84c9ea772c1403db17fcc5a9c61d2c91e6c
1 # - Locates the SDL_sound library
3 # This module depends on SDL being found and
4 # must be called AFTER FindSDL.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 to override this selectionor set the CMake environment
56 # 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
81   )
83 find_library(SDL_SOUND_LIBRARY
84   NAMES SDL_sound
85   HINTS
86     ENV SDLSOUNDDIR
87     ENV SDLDIR
88   )
90 if(SDL_FOUND AND 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 SDL_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 FindSDL.cmake module, but that's kind of painful.
100   # The solution would be to try something like:
101   # set(SDL_TRY_COMPILE_LIBRARY_LIST "${SDL_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=${SDL_INCLUDE_DIR}\;${SDL_SOUND_INCLUDE_DIR}
112   #                     -DLINK_LIBRARIES:STRING=${SDL_SOUND_LIBRARY}\;${SDL_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} ${SDL_LIBRARY})
144    # causes problems when SDL_LIBRARY looks like
145    # /Library/Frameworks/SDL.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_TRY_LIBS)
150    foreach(lib ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
151      set(TMP_TRY_LIBS "${TMP_TRY_LIBS} \"${lib}\"")
152    endforeach()
154    # message("TMP_TRY_LIBS ${TMP_TRY_LIBS}")
156    # Write the CMakeLists.txt and test project
157    # Weird, this is still sketchy. If I don't quote the variables
158    # in the TARGET_LINK_LIBRARIES, I seem to loose everything
159    # in the SDL_LIBRARY string after the "-framework".
160    # But if I quote the stuff in INCLUDE_DIRECTORIES, it doesn't work.
161    file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/CMakeLists.txt
162      "cmake_minimum_required(VERSION 2.8)
163         project(DetermineSoundLibs C)
164         include_directories(${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
165         add_executable(DetermineSoundLibs DetermineSoundLibs.c)
166         target_link_libraries(DetermineSoundLibs ${TMP_TRY_LIBS})"
167      )
169    try_compile(
170      MY_RESULT
171      ${PROJECT_BINARY_DIR}/CMakeTmp
172      ${PROJECT_BINARY_DIR}/CMakeTmp
173      DetermineSoundLibs
174      OUTPUT_VARIABLE MY_OUTPUT
175      )
177    # message("${MY_RESULT}")
178    # message(${MY_OUTPUT})
180    if(NOT MY_RESULT)
182      # I expect that MPGLIB, VOC, WAV, AIFF, and SHN are compiled in statically.
183      # I think Timidity is also compiled in statically.
184      # I've never had to explcitly link against Quicktime, so I'll skip that for now.
186      set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY})
188      # Find MikMod
189      if("${MY_OUTPUT}" MATCHES "MikMod_")
190      find_library(MIKMOD_LIBRARY
191          NAMES libmikmod-coreaudio mikmod
192          PATHS
193            ENV MIKMODDIR
194            ENV SDLSOUNDDIR
195            ENV SDLDIR
196            /sw
197            /opt/local
198            /opt/csw
199            /opt
200          PATH_SUFFIXES
201            lib
202        )
203        if(MIKMOD_LIBRARY)
204          set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MIKMOD_LIBRARY})
205        endif(MIKMOD_LIBRARY)
206      endif("${MY_OUTPUT}" MATCHES "MikMod_")
208      # Find ModPlug
209      if("${MY_OUTPUT}" MATCHES "MODPLUG_")
210        find_library(MODPLUG_LIBRARY
211          NAMES modplug
212          PATHS
213            ENV MODPLUGDIR
214            ENV SDLSOUNDDIR
215            ENV SDLDIR
216            /sw
217            /opt/local
218            /opt/csw
219            /opt
220          PATH_SUFFIXES
221            lib
222        )
223        if(MODPLUG_LIBRARY)
224          set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MODPLUG_LIBRARY})
225        endif()
226      endif()
229      # Find Ogg and Vorbis
230      if("${MY_OUTPUT}" MATCHES "ov_")
231        find_library(VORBIS_LIBRARY
232          NAMES vorbis Vorbis VORBIS
233          PATHS
234            ENV VORBISDIR
235            ENV OGGDIR
236            ENV SDLSOUNDDIR
237            ENV SDLDIR
238            /sw
239            /opt/local
240            /opt/csw
241            /opt
242          PATH_SUFFIXES
243            lib
244          )
245        if(VORBIS_LIBRARY)
246          set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBIS_LIBRARY})
247        endif()
249        find_library(OGG_LIBRARY
250          NAMES ogg Ogg OGG
251          PATHS
252            ENV OGGDIR
253            ENV VORBISDIR
254            ENV SDLSOUNDDIR
255            ENV SDLDIR
256            /sw
257            /opt/local
258            /opt/csw
259            /opt
260          PATH_SUFFIXES
261            lib
262          )
263        if(OGG_LIBRARY)
264          set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
265        endif()
266      endif()
269      # Find SMPEG
270      if("${MY_OUTPUT}" MATCHES "SMPEG_")
271        find_library(SMPEG_LIBRARY
272          NAMES smpeg SMPEG Smpeg SMpeg
273          PATHS
274            ENV SMPEGDIR
275            ENV SDLSOUNDDIR
276            ENV SDLDIR
277            /sw
278            /opt/local
279            /opt/csw
280            /opt
281          PATH_SUFFIXES
282            lib
283          )
284        if(SMPEG_LIBRARY)
285          set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SMPEG_LIBRARY})
286        endif()
287      endif()
290      # Find FLAC
291      if("${MY_OUTPUT}" MATCHES "FLAC_")
292        find_library(FLAC_LIBRARY
293          NAMES flac FLAC
294          PATHS
295            ENV FLACDIR
296            ENV SDLSOUNDDIR
297            ENV SDLDIR
298            /sw
299            /opt/local
300            /opt/csw
301            /opt
302          PATH_SUFFIXES
303            lib
304          )
305        if(FLAC_LIBRARY)
306          set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${FLAC_LIBRARY})
307        endif()
308      endif()
311      # Hmmm...Speex seems to depend on Ogg. This might be a problem if
312      # the TRY_COMPILE attempt gets blocked at SPEEX before it can pull
313      # in the Ogg symbols. I'm not sure if I should duplicate the ogg stuff
314      # above for here or if two ogg entries will screw up things.
315      if("${MY_OUTPUT}" MATCHES "speex_")
316        find_library(SPEEX_LIBRARY
317          NAMES speex SPEEX
318          PATHS
319            ENV SPEEXDIR
320            ENV SDLSOUNDDIR
321            ENV SDLDIR
322            /sw
323            /opt/local
324            /opt/csw
325            /opt
326          PATH_SUFFIXES
327            lib
328          )
329        if(SPEEX_LIBRARY)
330          set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SPEEX_LIBRARY})
331        endif()
333        # Find OGG (needed for Speex)
334      # We might have already found Ogg for Vorbis, so skip it if so.
335        if(NOT OGG_LIBRARY)
336          find_library(OGG_LIBRARY
337            NAMES ogg Ogg OGG
338            PATHS
339              ENV OGGDIR
340              ENV VORBISDIR
341              ENV SPEEXDIR
342              ENV SDLSOUNDDIR
343              ENV SDLDIR
344              /sw
345              /opt/local
346              /opt/csw
347              /opt
348            PATH_SUFFIXES lib
349            )
350          if(OGG_LIBRARY)
351            set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
352          endif()
353        endif()
354      endif()
356      set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARIES_TMP} CACHE INTERNAL "SDL_sound and dependent libraries")
357    else()
358      set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARY} CACHE INTERNAL "SDL_sound and dependent libraries")
359    endif()
360 endif()
362 if(SDL_SOUND_INCLUDE_DIR AND EXISTS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h")
363   file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SOUND_VER_MAJOR[ \t]+[0-9]+$")
364   file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MINOR_LINE REGEX "^#define[ \t]+SOUND_VER_MINOR[ \t]+[0-9]+$")
365   file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_PATCH_LINE REGEX "^#define[ \t]+SOUND_VER_PATCH[ \t]+[0-9]+$")
366   string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MAJOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MAJOR "${SDL_SOUND_VERSION_MAJOR_LINE}")
367   string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MINOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MINOR "${SDL_SOUND_VERSION_MINOR_LINE}")
368   string(REGEX REPLACE "^#define[ \t]+SOUND_VER_PATCH[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_PATCH "${SDL_SOUND_VERSION_PATCH_LINE}")
369   set(SDL_SOUND_VERSION_STRING ${SDL_SOUND_VERSION_MAJOR}.${SDL_SOUND_VERSION_MINOR}.${SDL_SOUND_VERSION_PATCH})
370   unset(SDL_SOUND_VERSION_MAJOR_LINE)
371   unset(SDL_SOUND_VERSION_MINOR_LINE)
372   unset(SDL_SOUND_VERSION_PATCH_LINE)
373   unset(SDL_SOUND_VERSION_MAJOR)
374   unset(SDL_SOUND_VERSION_MINOR)
375   unset(SDL_SOUND_VERSION_PATCH)
376 endif()
378 include(FindPackageHandleStandardArgs)
380 FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL_sound
381                                   REQUIRED_VARS SDL_SOUND_LIBRARY SDL_SOUND_INCLUDE_DIR
382                                   VERSION_VAR SDL_SOUND_VERSION_STRING)