blob: f5325348bd3c70c96b480a3af277f30411a4762b [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}>
evpobrd637b902020-01-26 18:11:01 +0500190 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/opus>
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800191 PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
192 ${CMAKE_CURRENT_SOURCE_DIR}
193 celt
194 silk)
195
196target_link_libraries(opus PRIVATE ${OPUS_REQUIRED_LIBRARIES})
Marcus Asteborg268780f2019-04-10 16:34:01 -0700197target_compile_definitions(opus PRIVATE OPUS_BUILD ENABLE_HARDENING)
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800198
Marcus Asteborg268780f2019-04-10 16:34:01 -0700199if(NOT MSVC)
Marcus Asteborgad8fe902019-04-23 13:41:14 -0700200 target_compile_definitions(opus PRIVATE _FORTIFY_SOURCE=2)
Marcus Asteborg268780f2019-04-10 16:34:01 -0700201endif()
202
203# It is strongly recommended to uncomment one of these VAR_ARRAYS: Use C99
204# variable-length arrays for stack allocation USE_ALLOCA: Use alloca() for stack
205# allocation If none is defined, then the fallback is a non-threadsafe global
206# array
207if(OPUS_USE_ALLOCA OR MSVC)
208 target_compile_definitions(opus PRIVATE USE_ALLOCA)
209else()
210 target_compile_definitions(opus PRIVATE VAR_ARRAYS)
211endif()
212
213if(OPUS_CUSTOM_MODES)
214 target_compile_definitions(opus PRIVATE CUSTOM_MODES)
215endif()
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800216
217if(BUILD_SHARED_LIBS)
218 if(WIN32)
219 target_compile_definitions(opus PRIVATE DLL_EXPORT)
220 else()
221 include(CheckCCompilerFlag)
222 check_c_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
223 if(COMPILER_HAS_HIDDEN_VISIBILITY)
224 set_target_properties(opus PROPERTIES C_VISIBILITY_PRESET hidden)
225 endif()
226 endif()
227endif()
228
229add_sources_group(opus silk ${silk_sources})
230add_sources_group(opus celt ${celt_sources})
231
232if(OPUS_FIXED_POINT)
233 add_sources_group(opus silk ${silk_sources_fixed})
234 target_include_directories(opus PRIVATE silk/fixed)
235 target_compile_definitions(opus PRIVATE FIXED_POINT=1)
236else()
237 add_sources_group(opus silk ${silk_sources_float})
238 target_include_directories(opus PRIVATE silk/float)
239endif()
240
241if(NOT OPUS_ENABLE_FLOAT_API)
242 target_compile_definitions(opus PRIVATE DISABLE_FLOAT_API)
243endif()
244
245if(OPUS_X86_MAY_HAVE_SSE
246 OR OPUS_X86_MAY_HAVE_SSE2
247 OR OPUS_X86_MAY_HAVE_SSE4_1
248 OR OPUS_X86_MAY_HAVE_AVX)
249 target_compile_definitions(opus PRIVATE OPUS_HAVE_RTCD)
250endif()
251
252if(OPUS_X86_MAY_HAVE_SSE)
253 add_sources_group(opus celt ${celt_sources_sse})
254 target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_SSE)
255endif()
256if(OPUS_X86_PRESUME_SSE)
257 target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_SSE)
258endif()
259
260if(OPUS_X86_MAY_HAVE_SSE2)
261 add_sources_group(opus celt ${celt_sources_sse2})
262 target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_SSE2)
263endif()
264if(OPUS_X86_PRESUME_SSE2)
265 target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_SSE2)
266endif()
267
268if(OPUS_X86_MAY_HAVE_SSE)
269 add_sources_group(opus celt ${celt_sources_sse4_1})
270 add_sources_group(opus silk ${silk_sources_sse4_1})
271 if(OPUS_FIXED_POINT)
272 add_sources_group(opus silk ${silk_sources_fixed_sse4_1})
273 endif()
274 target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_SSE4_1)
275endif()
276if(OPUS_X86_PRESUME_SSE4_1)
277 target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_SSE4_1)
278endif()
279
280if(CMAKE_SYSTEM_PROCESSOR MATCHES "(armv7-a)")
281 add_sources_group(opus celt ${celt_sources_arm})
282endif()
283
284if(COMPILER_SUPPORT_NEON AND OPUS_USE_NEON)
285
286 if(OPUS_MAY_HAVE_NEON)
287 if(RUNTIME_CPU_CAPABILITY_DETECTION)
288 message(STATUS "OPUS_MAY_HAVE_NEON enabling runtime detection")
289 target_compile_definitions(opus PRIVATE OPUS_HAVE_RTCD)
290 else()
291 message(ERROR "Runtime cpu capability detection needed for MAY_HAVE_NEON")
292 endif()
293 # Do runtime check for NEON
294 target_compile_definitions(opus
295 PRIVATE
296 OPUS_ARM_MAY_HAVE_NEON
297 OPUS_ARM_MAY_HAVE_NEON_INTR)
298 endif()
299
300 add_sources_group(opus celt ${celt_sources_arm_neon_intr})
301 add_sources_group(opus silk ${silk_sources_arm_neon_intr})
302
303 # silk arm neon depends on main_Fix.h
304 target_include_directories(opus PRIVATE silk/fixed)
305
306 if(OPUS_FIXED_POINT)
307 add_sources_group(opus silk ${silk_sources_fixed_arm_neon_intr})
308 endif()
309
310 if(OPUS_PRESUME_NEON)
311 target_compile_definitions(opus
312 PRIVATE
313 OPUS_ARM_PRESUME_NEON
314 OPUS_ARM_PRESUME_NEON_INTR)
315 endif()
316endif()
317
Nathaniel R. Lewis94b68f32019-05-09 21:38:14 -0700318target_compile_definitions(opus
319 PRIVATE
320 $<$<BOOL:${HAVE_LRINT}>:HAVE_LRINT>
321 $<$<BOOL:${HAVE_LRINTF}>:HAVE_LRINTF>)
322
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800323install(TARGETS opus
324 EXPORT OpusTargets
325 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
326 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
327 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
328 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/opus)
329
330if(OPUS_INSTALL_PKG_CONFIG_MODULE)
331 set(prefix ${CMAKE_INSTALL_PREFIX})
332 set(exec_prefix ${CMAKE_INSTALL_PREFIX})
333 set(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
334 set(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
DeadSix276d29f512019-12-05 06:00:58 +0100335 set(VERSION ${PACKAGE_VERSION})
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800336 if(HAVE_LIBM)
337 set(LIBM "-lm")
338 endif()
339 configure_file(opus.pc.in opus.pc)
340 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/opus.pc
341 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
342endif()
343
344if(OPUS_INSTALL_CMAKE_CONFIG_MODULE)
Marcus Asteborge7806452020-03-26 23:13:04 -0700345 set(CPACK_GENERATOR TGZ)
346 include(CPack)
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800347 set(CMAKE_INSTALL_PACKAGEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
348 install(EXPORT OpusTargets
349 NAMESPACE Opus::
350 DESTINATION ${CMAKE_INSTALL_PACKAGEDIR})
351
352 include(CMakePackageConfigHelpers)
353
354 set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR})
355 configure_package_config_file(OpusConfig.cmake.in
356 OpusConfig.cmake
357 INSTALL_DESTINATION
358 ${CMAKE_INSTALL_PACKAGEDIR}
359 PATH_VARS
360 INCLUDE_INSTALL_DIR
361 INSTALL_PREFIX
362 ${CMAKE_INSTALL_PREFIX})
363 write_basic_package_version_file(OpusConfigVersion.cmake
364 VERSION ${PROJECT_VERSION}
365 COMPATIBILITY SameMajorVersion)
366 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/OpusConfig.cmake
367 ${CMAKE_CURRENT_BINARY_DIR}/OpusConfigVersion.cmake
368 DESTINATION ${CMAKE_INSTALL_PACKAGEDIR})
369endif()
370
Marcus Asteborg268780f2019-04-10 16:34:01 -0700371if(OPUS_BUILD_PROGRAMS)
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800372 # demo
Marcus Asteborg268780f2019-04-10 16:34:01 -0700373 if(OPUS_CUSTOM_MODES)
374 add_executable(opus_custom_demo ${opus_custom_demo_sources})
375 target_include_directories(opus_custom_demo
376 PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
377 target_link_libraries(opus_custom_demo PRIVATE opus)
378 endif()
379
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800380 add_executable(opus_demo ${opus_demo_sources})
381 target_include_directories(opus_demo PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
Marcus Asteborg268780f2019-04-10 16:34:01 -0700382 target_include_directories(opus_demo PRIVATE silk) # debug.h
383 target_include_directories(opus_demo PRIVATE celt) # arch.h
Marcus Asteborgf9d3d432018-11-15 19:48:38 -0800384 target_link_libraries(opus_demo PRIVATE opus)
385
386 # compare
387 add_executable(opus_compare ${opus_compare_sources})
388 target_include_directories(opus_compare PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
389 target_link_libraries(opus_compare PRIVATE opus)
390endif()
391
392if(BUILD_TESTING)
393 enable_testing()
394
395 # tests
396 add_executable(test_opus_decode ${test_opus_decode_sources})
397 target_include_directories(test_opus_decode
398 PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
399 target_link_libraries(test_opus_decode PRIVATE opus)
400 if(OPUS_FIXED_POINT)
401 target_compile_definitions(test_opus_decode PRIVATE DISABLE_FLOAT_API)
402 endif()
403 add_test(test_opus_decode test_opus_decode)
404
405 add_executable(test_opus_padding ${test_opus_padding_sources})
406 target_include_directories(test_opus_padding
407 PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
408 target_link_libraries(test_opus_padding PRIVATE opus)
409 add_test(test_opus_padding test_opus_padding)
410
411 if(NOT BUILD_SHARED_LIBS)
412 # disable tests that depends on private API when building shared lib
413 add_executable(test_opus_api ${test_opus_api_sources})
414 target_include_directories(test_opus_api
415 PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt)
416 target_link_libraries(test_opus_api PRIVATE opus)
417 if(OPUS_FIXED_POINT)
418 target_compile_definitions(test_opus_api PRIVATE DISABLE_FLOAT_API)
419 endif()
420 add_test(test_opus_api test_opus_api)
421
422 add_executable(test_opus_encode ${test_opus_encode_sources})
423 target_include_directories(test_opus_encode
424 PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt)
425 target_link_libraries(test_opus_encode PRIVATE opus)
426 add_test(test_opus_encode test_opus_encode)
427 endif()
428endif()