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