Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Modules / FindSDL_sound.cmake
1 # Locates the SDL_sound library
2
3 # This module depends on SDL being found and 
4 # must be called AFTER FindSDL.cmake is called.
5
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
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  
16 #
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
28 #
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 . 
32 #
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.
49 #
50 # Responds to the $SDLDIR and $SDLSOUNDDIR environmental variable that would
51 # correspond to the ./configure --prefix=$SDLDIR used in building SDL.
52 #
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.
57 #
58
59 #=============================================================================
60 # Copyright 2005-2009 Kitware, Inc.
61 #
62 # Distributed under the OSI-approved BSD License (the "License");
63 # see accompanying file Copyright.txt for details.
64 #
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.)
71
72 SET(SDL_SOUND_EXTRAS "" CACHE STRING "SDL_sound extra flags")
73 MARK_AS_ADVANCED(SDL_SOUND_EXTRAS)
74
75 # Find SDL_sound.h
76 FIND_PATH(SDL_SOUND_INCLUDE_DIR SDL_sound.h
77   HINTS
78   $ENV{SDLSOUNDDIR}/include
79   $ENV{SDLSOUNDDIR}
80   $ENV{SDLDIR}/include
81   $ENV{SDLDIR}
82   PATHS
83   /usr/local/include/SDL
84   /usr/include/SDL
85   /usr/local/include/SDL12
86   /usr/local/include/SDL11 # FreeBSD ports
87   /usr/include/SDL12
88   /usr/include/SDL11
89   /sw/include/SDL # Fink
90   /sw/include
91   /opt/local/include/SDL # DarwinPorts
92   /opt/local/include
93   /opt/csw/include/SDL # Blastwave
94   /opt/csw/include 
95   /opt/include/SDL
96   /opt/include
97   )
98
99 FIND_LIBRARY(SDL_SOUND_LIBRARY 
100   NAMES SDL_sound
101   HINTS
102   $ENV{SDLSOUNDDIR}/lib
103   $ENV{SDLSOUNDDIR}
104   $ENV{SDLDIR}/lib
105   $ENV{SDLDIR}
106   PATHS
107   /sw/lib
108   /opt/local/lib
109   /opt/csw/lib
110   /opt/lib
111   )
112
113 IF(SDL_FOUND AND SDL_SOUND_INCLUDE_DIR AND SDL_SOUND_LIBRARY)
114
115   # CMake is giving me problems using TRY_COMPILE with the CMAKE_FLAGS
116   # for the :STRING syntax if I have multiple values contained in a
117   # single variable. This is a problem for the SDL_LIBRARY variable
118   # because it does just that. When I feed this variable to the command,
119   # only the first value gets the appropriate modifier (e.g. -I) and 
120   # the rest get dropped.
121   # To get multiple single variables to work, I must separate them with a "\;"
122   # I could go back and modify the FindSDL.cmake module, but that's kind of painful.
123   # The solution would be to try something like:
124   # SET(SDL_TRY_COMPILE_LIBRARY_LIST "${SDL_TRY_COMPILE_LIBRARY_LIST}\;${CMAKE_THREAD_LIBS_INIT}")
125   # Instead, it was suggested on the mailing list to write a temporary CMakeLists.txt
126   # with a temporary test project and invoke that with TRY_COMPILE.
127   # See message thread "Figuring out dependencies for a library in order to build"
128   # 2005-07-16  
129   #     TRY_COMPILE( 
130   #             MY_RESULT
131   #             ${CMAKE_BINARY_DIR}
132   #             ${PROJECT_SOURCE_DIR}/DetermineSoundLibs.c
133   #             CMAKE_FLAGS 
134   #                     -DINCLUDE_DIRECTORIES:STRING=${SDL_INCLUDE_DIR}\;${SDL_SOUND_INCLUDE_DIR}
135   #                     -DLINK_LIBRARIES:STRING=${SDL_SOUND_LIBRARY}\;${SDL_LIBRARY}
136   #             OUTPUT_VARIABLE MY_OUTPUT
137   #     )
138
139   # To minimize external dependencies, create a sdlsound test program
140   # which will be used to figure out if additional link dependencies are
141   # required for the link phase.
142   FILE(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/DetermineSoundLibs.c
143     "#include \"SDL_sound.h\"
144     #include \"SDL.h\"
145     int main(int argc, char* argv[])
146     {
147         Sound_AudioInfo desired;
148         Sound_Sample* sample;
149
150         SDL_Init(0);
151         Sound_Init();
152         
153         /* This doesn't actually have to work, but Init() is a no-op
154          * for some of the decoders, so this should force more symbols
155          * to be pulled in.
156          */
157         sample = Sound_NewSampleFromFile(argv[1], &desired, 4096);
158         
159         Sound_Quit();
160         SDL_Quit();
161         return 0;
162      }"
163      )
164
165    # Calling 
166    # TARGET_LINK_LIBRARIES(DetermineSoundLibs "${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
167    # causes problems when SDL_LIBRARY looks like
168    # /Library/Frameworks/SDL.framework;-framework Cocoa
169    # The ;-framework Cocoa seems to be confusing CMake once the OS X
170    # framework support was added. I was told that breaking up the list 
171    # would fix the problem.
172    SET(TMP_TRY_LIBS)
173    FOREACH(lib ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
174      SET(TMP_TRY_LIBS "${TMP_TRY_LIBS} \"${lib}\"")
175    ENDFOREACH(lib)
176
177    # MESSAGE("TMP_TRY_LIBS ${TMP_TRY_LIBS}")
178    
179    # Write the CMakeLists.txt and test project
180    # Weird, this is still sketchy. If I don't quote the variables
181    # in the TARGET_LINK_LIBRARIES, I seem to loose everything 
182    # in the SDL_LIBRARY string after the "-framework".
183    # But if I quote the stuff in INCLUDE_DIRECTORIES, it doesn't work.
184    FILE(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/CMakeLists.txt
185      "PROJECT(DetermineSoundLibs)
186         INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
187         ADD_EXECUTABLE(DetermineSoundLibs DetermineSoundLibs.c)
188         TARGET_LINK_LIBRARIES(DetermineSoundLibs ${TMP_TRY_LIBS})"
189      )
190
191    TRY_COMPILE( 
192      MY_RESULT
193      ${PROJECT_BINARY_DIR}/CMakeTmp
194      ${PROJECT_BINARY_DIR}/CMakeTmp
195      DetermineSoundLibs
196      OUTPUT_VARIABLE MY_OUTPUT
197      )
198    
199    # MESSAGE("${MY_RESULT}")
200    # MESSAGE(${MY_OUTPUT})
201    
202    IF(NOT MY_RESULT)
203      
204      # I expect that MPGLIB, VOC, WAV, AIFF, and SHN are compiled in statically.
205      # I think Timidity is also compiled in statically.
206      # I've never had to explcitly link against Quicktime, so I'll skip that for now.
207      
208      SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY})
209      
210      # Find MikMod
211      IF("${MY_OUTPUT}" MATCHES "MikMod_")
212      FIND_LIBRARY(MIKMOD_LIBRARY
213          NAMES libmikmod-coreaudio mikmod
214          PATHS
215          $ENV{MIKMODDIR}/lib
216          $ENV{MIKMODDIR}
217          $ENV{SDLSOUNDDIR}/lib
218          $ENV{SDLSOUNDDIR}
219          $ENV{SDLDIR}/lib
220          $ENV{SDLDIR}
221          /sw/lib
222          /opt/local/lib
223          /opt/csw/lib
224        /opt/lib
225        ) 
226        IF(MIKMOD_LIBRARY)
227          SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MIKMOD_LIBRARY})
228        ENDIF(MIKMOD_LIBRARY)
229      ENDIF("${MY_OUTPUT}" MATCHES "MikMod_")
230      
231      # Find ModPlug
232      IF("${MY_OUTPUT}" MATCHES "MODPLUG_")
233        FIND_LIBRARY(MODPLUG_LIBRARY
234          NAMES modplug
235          PATHS
236          $ENV{MODPLUGDIR}/lib
237          $ENV{MODPLUGDIR}
238          $ENV{SDLSOUNDDIR}/lib
239          $ENV{SDLSOUNDDIR}
240          $ENV{SDLDIR}/lib
241          $ENV{SDLDIR}
242          /sw/lib
243          /opt/local/lib
244          /opt/csw/lib
245        /opt/lib
246        )
247        IF(MODPLUG_LIBRARY)
248          SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MODPLUG_LIBRARY})
249        ENDIF(MODPLUG_LIBRARY)
250      ENDIF("${MY_OUTPUT}" MATCHES "MODPLUG_")
251
252      
253      # Find Ogg and Vorbis
254      IF("${MY_OUTPUT}" MATCHES "ov_")
255        FIND_LIBRARY(VORBIS_LIBRARY
256          NAMES vorbis Vorbis VORBIS
257          PATHS
258          $ENV{VORBISDIR}/lib
259          $ENV{VORBISDIR}
260          $ENV{OGGDIR}/lib
261          $ENV{OGGDIR}
262          $ENV{SDLSOUNDDIR}/lib
263          $ENV{SDLSOUNDDIR}
264          $ENV{SDLDIR}/lib
265          $ENV{SDLDIR}
266          /sw/lib
267          /opt/local/lib
268          /opt/csw/lib
269        /opt/lib
270          )
271        IF(VORBIS_LIBRARY)
272          SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBIS_LIBRARY})
273        ENDIF(VORBIS_LIBRARY)
274        
275        FIND_LIBRARY(OGG_LIBRARY
276          NAMES ogg Ogg OGG
277          PATHS
278          $ENV{OGGDIR}/lib
279          $ENV{OGGDIR}
280          $ENV{VORBISDIR}/lib
281          $ENV{VORBISDIR}
282          $ENV{SDLSOUNDDIR}/lib
283          $ENV{SDLSOUNDDIR}
284          $ENV{SDLDIR}/lib
285          $ENV{SDLDIR}
286          /sw/lib
287          /opt/local/lib
288          /opt/csw/lib
289        /opt/lib
290          )
291        IF(OGG_LIBRARY)
292          SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
293        ENDIF(OGG_LIBRARY)
294      ENDIF("${MY_OUTPUT}" MATCHES "ov_")
295      
296      
297      # Find SMPEG
298      IF("${MY_OUTPUT}" MATCHES "SMPEG_")
299        FIND_LIBRARY(SMPEG_LIBRARY
300          NAMES smpeg SMPEG Smpeg SMpeg
301          PATHS
302          $ENV{SMPEGDIR}/lib
303          $ENV{SMPEGDIR}
304          $ENV{SDLSOUNDDIR}/lib
305          $ENV{SDLSOUNDDIR}
306          $ENV{SDLDIR}/lib
307          $ENV{SDLDIR}
308          /sw/lib
309          /opt/local/lib
310          /opt/csw/lib
311        /opt/lib
312          )
313        IF(SMPEG_LIBRARY)
314          SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SMPEG_LIBRARY})
315        ENDIF(SMPEG_LIBRARY)
316      ENDIF("${MY_OUTPUT}" MATCHES "SMPEG_")
317      
318      
319      # Find FLAC
320      IF("${MY_OUTPUT}" MATCHES "FLAC_")
321        FIND_LIBRARY(FLAC_LIBRARY
322          NAMES flac FLAC
323          PATHS
324          $ENV{FLACDIR}/lib
325          $ENV{FLACDIR}
326          $ENV{SDLSOUNDDIR}/lib
327          $ENV{SDLSOUNDDIR}
328          $ENV{SDLDIR}/lib
329          $ENV{SDLDIR}
330          /sw/lib
331          /opt/local/lib
332          /opt/csw/lib
333        /opt/lib
334          )
335        IF(FLAC_LIBRARY)
336          SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${FLAC_LIBRARY})
337        ENDIF(FLAC_LIBRARY)
338      ENDIF("${MY_OUTPUT}" MATCHES "FLAC_")
339      
340      
341      # Hmmm...Speex seems to depend on Ogg. This might be a problem if
342      # the TRY_COMPILE attempt gets blocked at SPEEX before it can pull
343      # in the Ogg symbols. I'm not sure if I should duplicate the ogg stuff
344      # above for here or if two ogg entries will screw up things.
345      IF("${MY_OUTPUT}" MATCHES "speex_")
346        FIND_LIBRARY(SPEEX_LIBRARY
347          NAMES speex SPEEX
348          PATHS
349          $ENV{SPEEXDIR}/lib
350          $ENV{SPEEXDIR}
351          $ENV{SDLSOUNDDIR}/lib
352          $ENV{SDLSOUNDDIR}
353          $ENV{SDLDIR}/lib
354          $ENV{SDLDIR}
355          /sw/lib
356          /opt/local/lib
357          /opt/csw/lib
358        /opt/lib
359          )
360        IF(SPEEX_LIBRARY)
361          SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SPEEX_LIBRARY})
362        ENDIF(SPEEX_LIBRARY)
363        
364        # Find OGG (needed for Speex)
365      # We might have already found Ogg for Vorbis, so skip it if so.
366        IF(NOT OGG_LIBRARY)
367          FIND_LIBRARY(OGG_LIBRARY
368            NAMES ogg Ogg OGG
369            PATHS
370            $ENV{OGGDIR}/lib
371            $ENV{OGGDIR}
372            $ENV{VORBISDIR}/lib
373            $ENV{VORBISDIR}
374            $ENV{SPEEXDIR}/lib
375            $ENV{SPEEXDIR}
376            $ENV{SDLSOUNDDIR}/lib
377            $ENV{SDLSOUNDDIR}
378            $ENV{SDLDIR}/lib
379            $ENV{SDLDIR}
380            /sw/lib
381            /opt/local/lib
382            /opt/csw/lib
383          /opt/lib
384            )
385          IF(OGG_LIBRARY)
386            SET(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
387          ENDIF(OGG_LIBRARY)
388        ENDIF(NOT OGG_LIBRARY)
389      ENDIF("${MY_OUTPUT}" MATCHES "speex_")
390      
391    ELSE(NOT MY_RESULT)
392      SET(SDL_SOUND_LIBRARIES "${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARY}" CACHE INTERNAL "SDL_sound and dependent libraries")
393    ENDIF(NOT MY_RESULT)
394
395    SET(SDL_SOUND_LIBRARIES "${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARIES_TMP}" CACHE INTERNAL "SDL_sound and dependent libraries")
396  ENDIF(SDL_FOUND AND SDL_SOUND_INCLUDE_DIR AND SDL_SOUND_LIBRARY)
397
398 INCLUDE(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
399
400 FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL_SOUND
401                                   REQUIRED_VARS SDL_SOUND_LIBRARIES SDL_SOUND_INCLUDE_DIR)