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