Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / FindSDL_sound.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindSDL_sound
6 -------------
7
8 Locates the SDL_sound library
9
10
11
12 This module depends on SDL being found and must be called AFTER
13 FindSDL.cmake is called.
14
15 This module defines
16
17 ::
18
19   SDL_SOUND_INCLUDE_DIR, where to find SDL_sound.h
20   SDL_SOUND_FOUND, if false, do not try to link to SDL_sound
21   SDL_SOUND_LIBRARIES, this contains the list of libraries that you need
22     to link against.
23   SDL_SOUND_EXTRAS, this is an optional variable for you to add your own
24     flags to SDL_SOUND_LIBRARIES. This is prepended to SDL_SOUND_LIBRARIES.
25     This is available mostly for cases this module failed to anticipate for
26     and you must add additional flags. This is marked as ADVANCED.
27   SDL_SOUND_VERSION_STRING, human-readable string containing the
28     version of SDL_sound
29
30
31
32 This module also defines (but you shouldn't need to use directly)
33
34 ::
35
36    SDL_SOUND_LIBRARY, the name of just the SDL_sound library you would link
37    against. Use SDL_SOUND_LIBRARIES for you link instructions and not this one.
38
39 And might define the following as needed
40
41 ::
42
43    MIKMOD_LIBRARY
44    MODPLUG_LIBRARY
45    OGG_LIBRARY
46    VORBIS_LIBRARY
47    SMPEG_LIBRARY
48    FLAC_LIBRARY
49    SPEEX_LIBRARY
50
51
52
53 Typically, you should not use these variables directly, and you should
54 use SDL_SOUND_LIBRARIES which contains SDL_SOUND_LIBRARY and the other
55 audio libraries (if needed) to successfully compile on your system.
56
57 Responds to the $SDLDIR and $SDLSOUNDDIR environmental variable that
58 would correspond to the ./configure --prefix=$SDLDIR used in building
59 SDL.
60
61 On OSX, this will prefer the Framework version (if found) over others.
62 People will have to manually change the cache values of SDL_LIBRARY to
63 override this selectionor set the CMake environment CMAKE_INCLUDE_PATH
64 to modify the search paths.
65 #]=======================================================================]
66
67
68 #[[
69 This module is a bit more complicated than the
70 other FindSDL* family modules.  The reason is that SDL_sound can be
71 compiled in a large variety of different ways which are independent of
72 platform.  SDL_sound may dynamically link against other 3rd party
73 libraries to get additional codec support, such as Ogg Vorbis, SMPEG,
74 ModPlug, MikMod, FLAC, Speex, and potentially others.  Under some
75 circumstances which I don't fully understand, there seems to be a
76 requirement that dependent libraries of libraries you use must also be
77 explicitly linked against in order to successfully compile.  SDL_sound
78 does not currently have any system in place to know how it was
79 compiled.  So this CMake module does the hard work in trying to
80 discover which 3rd party libraries are required for building (if any).
81 This module uses a brute force approach to create a test program that
82 uses SDL_sound, and then tries to build it.  If the build fails, it
83 parses the error output for known symbol names to figure out which
84 libraries are needed.
85 #]]
86
87 set(SDL_SOUND_EXTRAS "" CACHE STRING "SDL_sound extra flags")
88 mark_as_advanced(SDL_SOUND_EXTRAS)
89
90 # Find SDL_sound.h
91 find_path(SDL_SOUND_INCLUDE_DIR SDL_sound.h
92   HINTS
93     ENV SDLSOUNDDIR
94     ENV SDLDIR
95   PATH_SUFFIXES SDL
96                 # path suffixes to search inside ENV{SDLDIR}
97                 include/SDL include/SDL12 include/SDL11 include
98   )
99
100 find_library(SDL_SOUND_LIBRARY
101   NAMES SDL_sound
102   HINTS
103     ENV SDLSOUNDDIR
104     ENV SDLDIR
105   PATH_SUFFIXES lib VisualC/win32lib
106   )
107
108 if(SDL_FOUND AND SDL_SOUND_INCLUDE_DIR AND SDL_SOUND_LIBRARY)
109
110   # CMake is giving me problems using TRY_COMPILE with the CMAKE_FLAGS
111   # for the :STRING syntax if I have multiple values contained in a
112   # single variable. This is a problem for the SDL_LIBRARY variable
113   # because it does just that. When I feed this variable to the command,
114   # only the first value gets the appropriate modifier (e.g. -I) and
115   # the rest get dropped.
116   # To get multiple single variables to work, I must separate them with a "\;"
117   # I could go back and modify the FindSDL.cmake module, but that's kind of painful.
118   # The solution would be to try something like:
119   # string(APPEND SDL_TRY_COMPILE_LIBRARY_LIST "\;${CMAKE_THREAD_LIBS_INIT}")
120   # Instead, it was suggested on the mailing list to write a temporary CMakeLists.txt
121   # with a temporary test project and invoke that with TRY_COMPILE.
122   # See message thread "Figuring out dependencies for a library in order to build"
123   # 2005-07-16
124   #     try_compile(
125   #             MY_RESULT
126   #             ${CMAKE_BINARY_DIR}
127   #             ${PROJECT_SOURCE_DIR}/DetermineSoundLibs.c
128   #             CMAKE_FLAGS
129   #                     -DINCLUDE_DIRECTORIES:STRING=${SDL_INCLUDE_DIR}\;${SDL_SOUND_INCLUDE_DIR}
130   #                     -DLINK_LIBRARIES:STRING=${SDL_SOUND_LIBRARY}\;${SDL_LIBRARY}
131   #             OUTPUT_VARIABLE MY_OUTPUT
132   #     )
133
134   # To minimize external dependencies, create a sdlsound test program
135   # which will be used to figure out if additional link dependencies are
136   # required for the link phase.
137   file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/DetermineSoundLibs.c
138     "#include \"SDL_sound.h\"
139     #include \"SDL.h\"
140     int main(int argc, char* argv[])
141     {
142         Sound_AudioInfo desired;
143         Sound_Sample* sample;
144
145         SDL_Init(0);
146         Sound_Init();
147
148         /* This doesn't actually have to work, but Init() is a no-op
149          * for some of the decoders, so this should force more symbols
150          * to be pulled in.
151          */
152         sample = Sound_NewSampleFromFile(argv[1], &desired, 4096);
153
154         Sound_Quit();
155         SDL_Quit();
156         return 0;
157      }"
158   )
159
160   # Calling
161   # target_link_libraries(DetermineSoundLibs "${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
162   # causes problems when SDL_LIBRARY looks like
163   # /Library/Frameworks/SDL.framework;-framework Cocoa
164   # The ;-framework Cocoa seems to be confusing CMake once the OS X
165   # framework support was added. I was told that breaking up the list
166   # would fix the problem.
167   set(TMP_TRY_LIBS)
168   foreach(lib ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
169     string(APPEND TMP_TRY_LIBS " \"${lib}\"")
170   endforeach()
171
172
173   # Write the CMakeLists.txt and test project
174   # Weird, this is still sketchy. If I don't quote the variables
175   # in the TARGET_LINK_LIBRARIES, I seem to loose everything
176   # in the SDL_LIBRARY string after the "-framework".
177   # But if I quote the stuff in INCLUDE_DIRECTORIES, it doesn't work.
178   file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/CMakeLists.txt
179     "cmake_minimum_required(VERSION ${CMAKE_VERSION})
180      project(DetermineSoundLibs)
181      include_directories(${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
182      add_executable(DetermineSoundLibs DetermineSoundLibs.c)
183      target_link_libraries(DetermineSoundLibs ${TMP_TRY_LIBS})"
184     )
185
186   try_compile(
187     MY_RESULT
188     PROJECT DetermineSoundLibs
189     SOURCE_DIR ${PROJECT_BINARY_DIR}/CMakeTmp
190     BINARY_DIR ${PROJECT_BINARY_DIR}/CMakeTmp
191     OUTPUT_VARIABLE MY_OUTPUT
192     )
193
194
195   if(NOT MY_RESULT)
196
197     # I expect that MPGLIB, VOC, WAV, AIFF, and SHN are compiled in statically.
198     # I think Timidity is also compiled in statically.
199     # I've never had to explcitly link against Quicktime, so I'll skip that for now.
200
201     set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY})
202
203     # Find MikMod
204     if("${MY_OUTPUT}" MATCHES "MikMod_")
205       find_library(MIKMOD_LIBRARY
206         NAMES libmikmod-coreaudio mikmod
207         PATHS
208           ENV MIKMODDIR
209           ENV SDLSOUNDDIR
210           ENV SDLDIR
211           /opt
212         PATH_SUFFIXES
213           lib
214       )
215       if(MIKMOD_LIBRARY)
216         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MIKMOD_LIBRARY})
217       endif(MIKMOD_LIBRARY)
218     endif("${MY_OUTPUT}" MATCHES "MikMod_")
219
220     # Find ModPlug
221     if("${MY_OUTPUT}" MATCHES "MODPLUG_")
222       find_library(MODPLUG_LIBRARY
223         NAMES modplug
224         PATHS
225           ENV MODPLUGDIR
226           ENV SDLSOUNDDIR
227           ENV SDLDIR
228           /opt
229         PATH_SUFFIXES
230           lib
231       )
232       if(MODPLUG_LIBRARY)
233         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MODPLUG_LIBRARY})
234       endif()
235     endif()
236
237
238     # Find Ogg and Vorbis
239     if("${MY_OUTPUT}" MATCHES "ov_")
240       find_library(VORBIS_LIBRARY
241         NAMES vorbis Vorbis VORBIS
242         PATHS
243           ENV VORBISDIR
244           ENV OGGDIR
245           ENV SDLSOUNDDIR
246           ENV SDLDIR
247           /opt
248         PATH_SUFFIXES
249           lib
250         )
251       if(VORBIS_LIBRARY)
252         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBIS_LIBRARY})
253       endif()
254
255       find_library(OGG_LIBRARY
256         NAMES ogg Ogg OGG
257         PATHS
258           ENV OGGDIR
259           ENV VORBISDIR
260           ENV SDLSOUNDDIR
261           ENV SDLDIR
262           /opt
263         PATH_SUFFIXES
264           lib
265          )
266       if(OGG_LIBRARY)
267         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
268       endif()
269     endif()
270
271
272     # Find SMPEG
273     if("${MY_OUTPUT}" MATCHES "SMPEG_")
274       find_library(SMPEG_LIBRARY
275         NAMES smpeg SMPEG Smpeg SMpeg
276         PATHS
277           ENV SMPEGDIR
278           ENV SDLSOUNDDIR
279           ENV SDLDIR
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()
288
289
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           /opt
299         PATH_SUFFIXES
300           lib
301         )
302       if(FLAC_LIBRARY)
303         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${FLAC_LIBRARY})
304       endif()
305     endif()
306
307
308     # Hmmm...Speex seems to depend on Ogg. This might be a problem if
309     # the TRY_COMPILE attempt gets blocked at SPEEX before it can pull
310     # in the Ogg symbols. I'm not sure if I should duplicate the ogg stuff
311     # above for here or if two ogg entries will screw up things.
312     if("${MY_OUTPUT}" MATCHES "speex_")
313       find_library(SPEEX_LIBRARY
314         NAMES speex SPEEX
315         PATHS
316           ENV SPEEXDIR
317           ENV SDLSOUNDDIR
318           ENV SDLDIR
319           /opt
320         PATH_SUFFIXES
321           lib
322         )
323       if(SPEEX_LIBRARY)
324         set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SPEEX_LIBRARY})
325       endif()
326
327       # Find OGG (needed for Speex)
328       # We might have already found Ogg for Vorbis, so skip it if so.
329       if(NOT OGG_LIBRARY)
330         find_library(OGG_LIBRARY
331           NAMES ogg Ogg OGG
332           PATHS
333             ENV OGGDIR
334             ENV VORBISDIR
335             ENV SPEEXDIR
336             ENV SDLSOUNDDIR
337             ENV SDLDIR
338             /opt
339           PATH_SUFFIXES lib
340           )
341         if(OGG_LIBRARY)
342           set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
343         endif()
344       endif()
345     endif()
346
347     set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARIES_TMP})
348   else()
349     set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARY})
350   endif()
351  endif()
352
353 if(SDL_SOUND_INCLUDE_DIR AND EXISTS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h")
354   file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SOUND_VER_MAJOR[ \t]+[0-9]+$")
355   file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MINOR_LINE REGEX "^#define[ \t]+SOUND_VER_MINOR[ \t]+[0-9]+$")
356   file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_PATCH_LINE REGEX "^#define[ \t]+SOUND_VER_PATCH[ \t]+[0-9]+$")
357   string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MAJOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MAJOR "${SDL_SOUND_VERSION_MAJOR_LINE}")
358   string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MINOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MINOR "${SDL_SOUND_VERSION_MINOR_LINE}")
359   string(REGEX REPLACE "^#define[ \t]+SOUND_VER_PATCH[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_PATCH "${SDL_SOUND_VERSION_PATCH_LINE}")
360   set(SDL_SOUND_VERSION_STRING ${SDL_SOUND_VERSION_MAJOR}.${SDL_SOUND_VERSION_MINOR}.${SDL_SOUND_VERSION_PATCH})
361   unset(SDL_SOUND_VERSION_MAJOR_LINE)
362   unset(SDL_SOUND_VERSION_MINOR_LINE)
363   unset(SDL_SOUND_VERSION_PATCH_LINE)
364   unset(SDL_SOUND_VERSION_MAJOR)
365   unset(SDL_SOUND_VERSION_MINOR)
366   unset(SDL_SOUND_VERSION_PATCH)
367 endif()
368
369 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
370
371 FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL_sound
372                                   REQUIRED_VARS SDL_SOUND_LIBRARY SDL_SOUND_INCLUDE_DIR
373                                   VERSION_VAR SDL_SOUND_VERSION_STRING)