blob: 5c9df24804244b6d4fb910eecc65bf0416e12be2 [file] [log] [blame]
Marcus Asteborgf9d3d432018-11-15 19:48:38 -08001cmake_minimum_required(VERSION 3.1)
2
3include(opus_functions.cmake)
4
5get_library_version(OPUS_LIBRARY_VERSION OPUS_LIBRARY_VERSION_MAJOR)
6message(STATUS "Opus library version: ${OPUS_LIBRARY_VERSION}")
7
8get_package_version(PACKAGE_VERSION)
9message(STATUS "Opus package version: ${PACKAGE_VERSION}")
10
11string(REGEX
12 REPLACE "^([0-9]+.[0-9]+\\.?([0-9]+)?).*"
13 "\\1"
14 PROJECT_VERSION
15 ${PACKAGE_VERSION})
16message(STATUS "Opus project version: ${PROJECT_VERSION}")
17
18project(Opus LANGUAGES C VERSION ${PROJECT_VERSION})
Marcus Asteborg1f926132019-04-11 09:50:14 -070019include(opus_buildtype.cmake)
Marcus Asteborgf9d3d432018-11-15 19:48:38 -080020
Davide Beatricic487f532019-10-01 06:08:04 +020021option(OPUS_BUILD_SHARED_LIBRARY "Build shared library" OFF)
Marcus Asteborg268780f2019-04-10 16:34:01 -070022option(OPUS_STACK_PROTECTOR "Use stack protection" ON)
23option(OPUS_USE_ALLOCA "Use alloca for stack arrays (on non-C99 compilers)" OFF)
24option(OPUS_CUSTOM_MODES "Enable non-Opus modes, e.g. 44.1 kHz & 2^n frames"
25 OFF)
26option(OPUS_BUILD_PROGRAMS "Build programs" OFF)
Marcus Asteborgf9d3d432018-11-15 19:48:38 -080027option(OPUS_FIXED_POINT
28 "Compile as fixed-point (for machines without a fast enough FPU)" OFF)
29option(OPUS_ENABLE_FLOAT_API
30 "Compile with the floating point API (for machines with float library"
31 ON)
32option(OPUS_INSTALL_PKG_CONFIG_MODULE "Install PkgConfig module" ON)
33option(OPUS_INSTALL_CMAKE_CONFIG_MODULE "Install CMake package config module"
34 ON)
35
36include(opus_config.cmake)
37include(opus_sources.cmake)
38include(GNUInstallDirs)
39include(CMakeDependentOption)
40include(FeatureSummary)
41
Marcus Asteborg965a72e2019-12-17 10:21:21 -080042if(OPUS_BUILD_SHARED_LIBRARY OR BUILD_SHARED_LIBS)
Davide Beatricic487f532019-10-01 06:08:04 +020043 # Global flag to cause add_library() to create shared libraries if on.
44 set(BUILD_SHARED_LIBS ON)
Marcus Asteborg965a72e2019-12-17 10:21:21 -080045 set(OPUS_BUILD_SHARED_LIBRARY ON)
Davide Beatricic487f532019-10-01 06:08:04 +020046endif()
47
Marcus Asteborg268780f2019-04-10 16:34:01 -070048if(OPUS_STACK_PROTECTOR)
49 if(NOT MSVC) # GC on by default on MSVC
50 check_and_set_flag(STACK_PROTECTION_STRONG -fstack-protector-strong)
51 endif()
52else()
53 if(MSVC)
54 check_and_set_flag(BUFFER_SECURITY_CHECK /GS-)
55 endif()
56endif()
57
Marcus Asteborgf9d3d432018-11-15 19:48:38 -080058if(OPUS_CPU_X86 OR OPUS_CPU_X64)
59 cmake_dependent_option(OPUS_X86_MAY_HAVE_SSE
60 "Does runtime check for SSE1 support"
61 ON
62 "SSE1_SUPPORTED"
63 OFF)
64 cmake_dependent_option(OPUS_X86_MAY_HAVE_SSE2
65 "Does runtime check for SSE2 support"
66 ON
67 "SSE2_SUPPORTED"
68 OFF)
69 cmake_dependent_option(OPUS_X86_MAY_HAVE_SSE4_1
70 "Does runtime check for SSE4.1 support"
71 ON
72 "SSE4_1_SUPPORTED"
73 OFF)
74 cmake_dependent_option(OPUS_X86_MAY_HAVE_AVX
75 "Does runtime check for AVX support"
76 ON
77 "AVX_SUPPORTED"
78 OFF)
79
80 if(OPUS_CPU_X64) # Assume 64 bit has SSE2 support
81 cmake_dependent_option(OPUS_X86_PRESUME_SSE
82 "Assume target CPU has SSE1 support"
83 ON
84 "OPUS_X86_MAY_HAVE_SSE"
85 OFF)
86 cmake_dependent_option(OPUS_X86_PRESUME_SSE2
87 "Assume target CPU has SSE2 support"
88 ON
89 "OPUS_X86_MAY_HAVE_SSE2"
90 OFF)
91 else()
92 cmake_dependent_option(OPUS_X86_PRESUME_SSE
93 "Assume target CPU has SSE1 support"
94 OFF
95 "OPUS_X86_MAY_HAVE_SSE"
96 OFF)
97 cmake_dependent_option(OPUS_X86_PRESUME_SSE2
98 "Assume target CPU has SSE2 support"
99 OFF
100 "OPUS_X86_MAY_HAVE_SSE2"
101 OFF)
102 endif()
103 cmake_dependent_option(OPUS_X86_PRESUME_SSE4_1
104 "Assume target CPU has SSE4.1 support"
105 OFF
106 "OPUS_X86_MAY_HAVE_SSE4_1"
107 OFF)
108 cmake_dependent_option(OPUS_X86_PRESUME_AVX
109 "Assume target CPU has AVX support"
110 OFF
111 "OPUS_X86_MAY_HAVE_AVX"
112 OFF)
113endif()
114
115set_package_properties(Git
116 PROPERTIES
117 TYPE
118 REQUIRED
119 DESCRIPTION
120 "fast, scalable, distributed revision control system"
121 URL
122 "https://git-scm.com/"
123 PURPOSE
124 "required to set up package version")
125
Marcus Asteborg965a72e2019-12-17 10:21:21 -0800126add_feature_info(BUILD_SHARED_LIBRARY OPUS_BUILD_SHARED_LIBRARY "Build shared library")
Marcus Asteborg268780f2019-04-10 16:34:01 -0700127add_feature_info(STACK_PROTECTOR OPUS_STACK_PROTECTOR "Use stack protection")
128add_feature_info(USE_ALLOCA OPUS_USE_ALLOCA
129 "Use alloca for stack arrays (on non-C99 compilers)")
130add_feature_info(CUSTOM_MODES OPUS_CUSTOM_MODES
131 "Enable non-Opus modes, e.g. 44.1 kHz & 2^n frames")
132add_feature_info(BUILD_PROGRAMS OPUS_BUILD_PROGRAMS "Build programs")
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800133add_feature_info(
134 FIXED_POINT OPUS_FIXED_POINT
135 "compile as fixed-point (for machines without a fast enough FPU)")
136add_feature_info(
137 FLOAT_API OPUS_ENABLE_FLOAT_API
138 "compile with the floating point API (for machines with float library)")
139
140add_feature_info(INSTALL_PKG_CONFIG_MODULE OPUS_INSTALL_PKG_CONFIG_MODULE
141 "install PkgConfig module")
142add_feature_info(INSTALL_CMAKE_CONFIG_MODULE OPUS_INSTALL_CMAKE_CONFIG_MODULE
143 "install CMake package config module")
144
145if(OPUS_CPU_X86 OR OPUS_CPU_X64)
146 add_feature_info(X86_MAY_HAVE_SSE OPUS_X86_MAY_HAVE_SSE
147 "does runtime check for SSE1 support")
148 add_feature_info(X86_MAY_HAVE_SSE2 OPUS_X86_MAY_HAVE_SSE2
149 "does runtime check for SSE2 support")
150 add_feature_info(X86_MAY_HAVE_SSE4_1 OPUS_X86_MAY_HAVE_SSE4_1
151 "does runtime check for SSE4_1 support")
152 add_feature_info(X86_MAY_HAVE_AVX OPUS_X86_MAY_HAVE_AVX
153 "does runtime check for AVX support")
154 add_feature_info(X86_PRESUME_SSE OPUS_X86_PRESUME_SSE
155 "assume target CPU has SSE1 support")
156 add_feature_info(X86_PRESUME_SSE2 OPUS_X86_PRESUME_SSE2
157 "assume target CPU has SSE2 support")
158 add_feature_info(X86_PRESUME_SSE4_1 OPUS_X86_PRESUME_SSE4_1
159 "assume target CPU has SSE4_1 support")
160 add_feature_info(X86_PRESUME_AVX OPUS_X86_PRESUME_AVX
161 "assume target CPU has AVX support")
162endif()
163
164feature_summary(WHAT ALL)
165
166add_library(opus ${opus_sources} ${opus_sources_float})
167
Nathaniel R. Lewis60472f22019-05-08 21:51:22 -0700168add_library(Opus::opus ALIAS opus)
169
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800170set(Opus_PUBLIC_HEADER
171 ${CMAKE_CURRENT_SOURCE_DIR}/include/opus.h
172 ${CMAKE_CURRENT_SOURCE_DIR}/include/opus_custom.h
173 ${CMAKE_CURRENT_SOURCE_DIR}/include/opus_defines.h
174 ${CMAKE_CURRENT_SOURCE_DIR}/include/opus_multistream.h
175 ${CMAKE_CURRENT_SOURCE_DIR}/include/opus_projection.h
176 ${CMAKE_CURRENT_SOURCE_DIR}/include/opus_types.h)
177
178set_target_properties(opus
179 PROPERTIES SOVERSION
180 ${OPUS_LIBRARY_VERSION_MAJOR}
181 VERSION
182 ${OPUS_LIBRARY_VERSION}
183 PUBLIC_HEADER
184 "${Opus_PUBLIC_HEADER}")
185
186target_include_directories(
187 opus
188 PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
189 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
190 PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
191 ${CMAKE_CURRENT_SOURCE_DIR}
192 celt
193 silk)
194
195target_link_libraries(opus PRIVATE ${OPUS_REQUIRED_LIBRARIES})
Marcus Asteborg268780f2019-04-10 16:34:01 -0700196target_compile_definitions(opus PRIVATE OPUS_BUILD ENABLE_HARDENING)
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800197
Marcus Asteborg268780f2019-04-10 16:34:01 -0700198if(NOT MSVC)
Marcus Asteborgad8fe902019-04-23 13:41:14 -0700199 target_compile_definitions(opus PRIVATE _FORTIFY_SOURCE=2)
Marcus Asteborg268780f2019-04-10 16:34:01 -0700200endif()
201
202# It is strongly recommended to uncomment one of these VAR_ARRAYS: Use C99
203# variable-length arrays for stack allocation USE_ALLOCA: Use alloca() for stack
204# allocation If none is defined, then the fallback is a non-threadsafe global
205# array
206if(OPUS_USE_ALLOCA OR MSVC)
207 target_compile_definitions(opus PRIVATE USE_ALLOCA)
208else()
209 target_compile_definitions(opus PRIVATE VAR_ARRAYS)
210endif()
211
212if(OPUS_CUSTOM_MODES)
213 target_compile_definitions(opus PRIVATE CUSTOM_MODES)
214endif()
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800215
216if(BUILD_SHARED_LIBS)
217 if(WIN32)
218 target_compile_definitions(opus PRIVATE DLL_EXPORT)
219 else()
220 include(CheckCCompilerFlag)
221 check_c_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
222 if(COMPILER_HAS_HIDDEN_VISIBILITY)
223 set_target_properties(opus PROPERTIES C_VISIBILITY_PRESET hidden)
224 endif()
225 endif()
226endif()
227
228add_sources_group(opus silk ${silk_sources})
229add_sources_group(opus celt ${celt_sources})
230
231if(OPUS_FIXED_POINT)
232 add_sources_group(opus silk ${silk_sources_fixed})
233 target_include_directories(opus PRIVATE silk/fixed)
234 target_compile_definitions(opus PRIVATE FIXED_POINT=1)
235else()
236 add_sources_group(opus silk ${silk_sources_float})
237 target_include_directories(opus PRIVATE silk/float)
238endif()
239
240if(NOT OPUS_ENABLE_FLOAT_API)
241 target_compile_definitions(opus PRIVATE DISABLE_FLOAT_API)
242endif()
243
244if(OPUS_X86_MAY_HAVE_SSE
245 OR OPUS_X86_MAY_HAVE_SSE2
246 OR OPUS_X86_MAY_HAVE_SSE4_1
247 OR OPUS_X86_MAY_HAVE_AVX)
248 target_compile_definitions(opus PRIVATE OPUS_HAVE_RTCD)
249endif()
250
251if(OPUS_X86_MAY_HAVE_SSE)
252 add_sources_group(opus celt ${celt_sources_sse})
253 target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_SSE)
254endif()
255if(OPUS_X86_PRESUME_SSE)
256 target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_SSE)
257endif()
258
259if(OPUS_X86_MAY_HAVE_SSE2)
260 add_sources_group(opus celt ${celt_sources_sse2})
261 target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_SSE2)
262endif()
263if(OPUS_X86_PRESUME_SSE2)
264 target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_SSE2)
265endif()
266
267if(OPUS_X86_MAY_HAVE_SSE)
268 add_sources_group(opus celt ${celt_sources_sse4_1})
269 add_sources_group(opus silk ${silk_sources_sse4_1})
270 if(OPUS_FIXED_POINT)
271 add_sources_group(opus silk ${silk_sources_fixed_sse4_1})
272 endif()
273 target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_SSE4_1)
274endif()
275if(OPUS_X86_PRESUME_SSE4_1)
276 target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_SSE4_1)
277endif()
278
279if(CMAKE_SYSTEM_PROCESSOR MATCHES "(armv7-a)")
280 add_sources_group(opus celt ${celt_sources_arm})
281endif()
282
283if(COMPILER_SUPPORT_NEON AND OPUS_USE_NEON)
284
285 if(OPUS_MAY_HAVE_NEON)
286 if(RUNTIME_CPU_CAPABILITY_DETECTION)
287 message(STATUS "OPUS_MAY_HAVE_NEON enabling runtime detection")
288 target_compile_definitions(opus PRIVATE OPUS_HAVE_RTCD)
289 else()
290 message(ERROR "Runtime cpu capability detection needed for MAY_HAVE_NEON")
291 endif()
292 # Do runtime check for NEON
293 target_compile_definitions(opus
294 PRIVATE
295 OPUS_ARM_MAY_HAVE_NEON
296 OPUS_ARM_MAY_HAVE_NEON_INTR)
297 endif()
298
299 add_sources_group(opus celt ${celt_sources_arm_neon_intr})
300 add_sources_group(opus silk ${silk_sources_arm_neon_intr})
301
302 # silk arm neon depends on main_Fix.h
303 target_include_directories(opus PRIVATE silk/fixed)
304
305 if(OPUS_FIXED_POINT)
306 add_sources_group(opus silk ${silk_sources_fixed_arm_neon_intr})
307 endif()
308
309 if(OPUS_PRESUME_NEON)
310 target_compile_definitions(opus
311 PRIVATE
312 OPUS_ARM_PRESUME_NEON
313 OPUS_ARM_PRESUME_NEON_INTR)
314 endif()
315endif()
316
317install(TARGETS opus
318 EXPORT OpusTargets
319 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
320 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
321 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
322 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/opus)
323
324if(OPUS_INSTALL_PKG_CONFIG_MODULE)
325 set(prefix ${CMAKE_INSTALL_PREFIX})
326 set(exec_prefix ${CMAKE_INSTALL_PREFIX})
327 set(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
328 set(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
329 set(VERSION ${OPUS_LIBRARY_VERSION})
330 set(VERSION ${OPUS_LIBRARY_VERSION})
331 if(HAVE_LIBM)
332 set(LIBM "-lm")
333 endif()
334 configure_file(opus.pc.in opus.pc)
335 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/opus.pc
336 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
337endif()
338
339if(OPUS_INSTALL_CMAKE_CONFIG_MODULE)
340 set(CMAKE_INSTALL_PACKAGEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
341 install(EXPORT OpusTargets
342 NAMESPACE Opus::
343 DESTINATION ${CMAKE_INSTALL_PACKAGEDIR})
344
345 include(CMakePackageConfigHelpers)
346
347 set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR})
348 configure_package_config_file(OpusConfig.cmake.in
349 OpusConfig.cmake
350 INSTALL_DESTINATION
351 ${CMAKE_INSTALL_PACKAGEDIR}
352 PATH_VARS
353 INCLUDE_INSTALL_DIR
354 INSTALL_PREFIX
355 ${CMAKE_INSTALL_PREFIX})
356 write_basic_package_version_file(OpusConfigVersion.cmake
357 VERSION ${PROJECT_VERSION}
358 COMPATIBILITY SameMajorVersion)
359 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/OpusConfig.cmake
360 ${CMAKE_CURRENT_BINARY_DIR}/OpusConfigVersion.cmake
361 DESTINATION ${CMAKE_INSTALL_PACKAGEDIR})
362endif()
363
Marcus Asteborg268780f2019-04-10 16:34:01 -0700364if(OPUS_BUILD_PROGRAMS)
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800365 # demo
Marcus Asteborg268780f2019-04-10 16:34:01 -0700366 if(OPUS_CUSTOM_MODES)
367 add_executable(opus_custom_demo ${opus_custom_demo_sources})
368 target_include_directories(opus_custom_demo
369 PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
370 target_link_libraries(opus_custom_demo PRIVATE opus)
371 endif()
372
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800373 add_executable(opus_demo ${opus_demo_sources})
374 target_include_directories(opus_demo PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
Marcus Asteborg268780f2019-04-10 16:34:01 -0700375 target_include_directories(opus_demo PRIVATE silk) # debug.h
376 target_include_directories(opus_demo PRIVATE celt) # arch.h
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800377 target_link_libraries(opus_demo PRIVATE opus)
378
379 # compare
380 add_executable(opus_compare ${opus_compare_sources})
381 target_include_directories(opus_compare PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
382 target_link_libraries(opus_compare PRIVATE opus)
383endif()
384
385if(BUILD_TESTING)
386 enable_testing()
387
388 # tests
389 add_executable(test_opus_decode ${test_opus_decode_sources})
390 target_include_directories(test_opus_decode
391 PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
392 target_link_libraries(test_opus_decode PRIVATE opus)
393 if(OPUS_FIXED_POINT)
394 target_compile_definitions(test_opus_decode PRIVATE DISABLE_FLOAT_API)
395 endif()
396 add_test(test_opus_decode test_opus_decode)
397
398 add_executable(test_opus_padding ${test_opus_padding_sources})
399 target_include_directories(test_opus_padding
400 PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
401 target_link_libraries(test_opus_padding PRIVATE opus)
402 add_test(test_opus_padding test_opus_padding)
403
404 if(NOT BUILD_SHARED_LIBS)
405 # disable tests that depends on private API when building shared lib
406 add_executable(test_opus_api ${test_opus_api_sources})
407 target_include_directories(test_opus_api
408 PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt)
409 target_link_libraries(test_opus_api PRIVATE opus)
410 if(OPUS_FIXED_POINT)
411 target_compile_definitions(test_opus_api PRIVATE DISABLE_FLOAT_API)
412 endif()
413 add_test(test_opus_api test_opus_api)
414
415 add_executable(test_opus_encode ${test_opus_encode_sources})
416 target_include_directories(test_opus_encode
417 PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt)
418 target_link_libraries(test_opus_encode PRIVATE opus)
419 add_test(test_opus_encode test_opus_encode)
420 endif()
421endif()