forked from FluidSynth/fluidsynth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindSndFile.cmake
180 lines (155 loc) · 5.43 KB
/
FindSndFile.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#[=======================================================================[.rst:
FindSndFile
-------
Finds the SndFile library.
Imported Targets
^^^^^^^^^^^^^^^^
This module provides the following imported targets, if found:
``SndFile::sndfile``
The SndFile library
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``SndFile_FOUND``
True if the system has the SndFile library.
``SndFile_VERSION``
The version of the SndFile library which was found.
``SndFile_WITH_EXTERNAL_LIBS``
True if the library was built with Xiph codecs.
For compatibility with upstream, the following variables are also set:
``SndFile_WITH_MPEG``
``SndFile_VERSION_MAJOR``
``SndFile_VERSION_MINOR``
``SndFile_VERSION_PATCH``
``SndFile_LIBRARY``
``SndFile_LIBRARIES``
``SNDFILE_LIBRARY``
``SNDFILE_LIBRARIES``
``SNDFILE_INCLUDE_DIR``
#]=======================================================================]
# Use pkg-config if available
find_package(PkgConfig QUIET)
pkg_check_modules(PC_SNDFILE QUIET sndfile)
# Find the headers and libraries
find_path(
SndFile_INCLUDE_DIR
NAMES "sndfile.h"
HINTS "${PC_SNDFILE_INCLUDEDIR}")
find_library(
_sndfile_library
NAMES "sndfile"
HINTS "${PC_SNDFILE_LIBDIR}")
# Get version from pkg-config or read the config header
if(PC_SNDFILE_VERSION)
set(SndFile_VERSION "${PC_SNDFILE_VERSION}")
string(REPLACE "." ";" _sndfile_version_list "${SndFile_VERSION}")
list(GET _sndfile_version_list 0 SndFile_VERSION_MAJOR)
list(GET _sndfile_version_list 1 SndFile_VERSION_MINOR)
list(GET _sndfile_version_list 2 SndFile_VERSION_PATCH)
elseif(SndFile_INCLUDE_DIR)
file(READ "${SndFile_INCLUDE_DIR}/sndfile.h" _sndfile_h)
if("#define SNDFILE_1" MATCHES _snfile_h)
set(SndFile_VERSION "1")
set(SndFile_VERSION_MAJOR "1")
endif()
endif()
# Check the features SndFile was built with
if(PC_SNDFILE_FOUND)
if("vorbis" IN_LIST PC_SNDFILE_STATIC_LIBRARIES)
set(SndFile_WITH_EXTERNAL_LIBS TRUE)
endif()
if("mpg123" IN_LIST PC_SNDFILE_STATIC_LIBRARIES)
set(SndFile_WITH_MPEG TRUE)
endif()
elseif(_sndfile_library)
# sndfile may need any of these libraries
find_package(Ogg 1.3 QUIET)
find_package(Vorbis QUIET)
find_package(FLAC QUIET)
find_package(Opus QUIET)
find_package(mp3lame QUIET)
find_package(mpg123 QUIET)
if(NOT CMAKE_CROSSCOMPILING)
include(CheckSourceRuns)
set(_backup_includes ${CMAKE_REQUIRED_INCLUDES})
set(_backup_libraries ${CMAKE_REQUIRED_LIBRARIES})
set(CMAKE_REQUIRED_INCLUDES "${SndFile_INCLUDE_DIR}")
set(CMAKE_REQUIRED_LIBRARIES "${_sndfile_library}")
set(_optional_libs "MPG123::libmpg123" "mp3lame::mp3lame" "FLAC::FLAC"
"Opus::opus" "Vorbis::vorbisenc" "Ogg::ogg")
foreach(_target ${_optional_libs})
if(TARGET "${_target}")
list(APPEND CMAKE_REQUIRED_LIBRARIES "${_target}")
endif()
endforeach()
check_source_runs(
C
"#include <stdlib.h>
#include <sndfile.h>
int main() {
SF_FORMAT_INFO info = {SF_FORMAT_VORBIS};
sf_command(NULL, SFC_GET_FORMAT_INFO, &info, sizeof info);
return info.name != NULL ? EXIT_SUCCESS : EXIT_FAILURE;
}"
SNDFILE_SUPPORTS_VORBIS)
check_source_runs(
C
"#include <stdlib.h>
#include <sndfile.h>
int main() {
SF_FORMAT_INFO info = {SF_FORMAT_MPEG_LAYER_III};
sf_command(NULL, SFC_GET_FORMAT_INFO, &info, sizeof info);
return info.name != NULL ? EXIT_SUCCESS : EXIT_FAILURE;
}"
SNDFILE_SUPPORTS_MPEG)
set(SndFile_WITH_EXTERNAL_LIBS ${SNDFILE_SUPPORTS_VORBIS})
set(SndFile_WITH_MPEG ${SNDFILE_SUPPORTS_MPEG})
set(CMAKE_REQUIRED_INCLUDES ${_backup_includes})
set(CMAKE_REQUIRED_LIBRARIES ${_backup_libraries})
else()
message(
STATUS
"Cross-compiling without pkg-config - cannot check for external libraries."
"If you have the upstream CMake config set CMAKE_FIND_PACKAGE_PREFER_CONFIG to true for accurate results."
)
set(SndFile_WITH_EXTERNAL_LIBS FALSE)
set(SndFile_WITH_MPEG FALSE)
endif()
endif()
# Handle transitive dependencies
if(PC_SNDFILE_FOUND)
get_target_properties_from_pkg_config("${_sndfile_library}" "PC_SNDFILE"
"_sndfile")
else()
if(SndFile_WITH_EXTERNAL_LIBS)
list(APPEND _sndfile_link_libraries "FLAC::FLAC" "Opus::opus"
"Vorbis::vorbisenc" "Ogg::ogg")
endif()
if(SndFile_WITH_MPEG)
list(APPEND _sndfile_link_libraries "MPG123::libmpg123" "mp3lame::mp3lame")
endif()
endif()
# Forward the result to CMake
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
SndFile
REQUIRED_VARS "_sndfile_library" "SndFile_INCLUDE_DIR"
VERSION_VAR "SndFile_VERSION")
if(SndFile_FOUND AND NOT TARGET SndFile::sndfile)
add_library(SndFile::sndfile UNKNOWN IMPORTED)
set_target_properties(
SndFile::sndfile
PROPERTIES IMPORTED_LOCATION "${_sndfile_library}"
INTERFACE_COMPILE_OPTIONS "${_sndfile_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${SndFile_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_sndfile_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_sndfile_link_directories}")
# Set additional variables for compatibility with upstream config
set(SNDFILE_FOUND TRUE)
set(SndFile_LIBRARY SndFile::sndfile)
set(SndFile_LIBRARIES SndFile::sndfile)
set(SNDFILE_LIBRARY SndFile::sndfile)
set(SNDFILE_LIBRARIES SndFile::sndfile)
set(SNDFILE_INCLUDE_DIR "${SndFile_INCLUDE_DIR}")
endif()
mark_as_advanced(_sndfile_library)