• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.1)
2list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
3
4include(OpusPackageVersion)
5get_package_version(PACKAGE_VERSION PROJECT_VERSION)
6
7project(Opus LANGUAGES C VERSION ${PROJECT_VERSION})
8
9include(OpusFunctions)
10include(OpusBuildtype)
11include(OpusConfig)
12include(OpusSources)
13include(GNUInstallDirs)
14include(CMakeDependentOption)
15include(FeatureSummary)
16
17set(OPUS_BUILD_SHARED_LIBRARY_HELP_STR "build shared library.")
18option(OPUS_BUILD_SHARED_LIBRARY ${OPUS_BUILD_SHARED_LIBRARY_HELP_STR} OFF)
19if(OPUS_BUILD_SHARED_LIBRARY OR BUILD_SHARED_LIBS OR OPUS_BUILD_FRAMEWORK)
20  # Global flag to cause add_library() to create shared libraries if on.
21  set(BUILD_SHARED_LIBS ON)
22  set(OPUS_BUILD_SHARED_LIBRARY ON)
23endif()
24add_feature_info(OPUS_BUILD_SHARED_LIBRARY OPUS_BUILD_SHARED_LIBRARY ${OPUS_BUILD_SHARED_LIBRARY_HELP_STR})
25
26set(OPUS_BUILD_TESTING_HELP_STR "build tests.")
27option(OPUS_BUILD_TESTING ${OPUS_BUILD_TESTING_HELP_STR} OFF)
28if(OPUS_BUILD_TESTING OR BUILD_TESTING)
29  set(OPUS_BUILD_TESTING ON)
30  set(BUILD_TESTING ON)
31endif()
32add_feature_info(OPUS_BUILD_TESTING OPUS_BUILD_TESTING ${OPUS_BUILD_TESTING_HELP_STR})
33
34set(OPUS_CUSTOM_MODES_HELP_STR "enable non-Opus modes, e.g. 44.1 kHz & 2^n frames.")
35option(OPUS_CUSTOM_MODES ${OPUS_CUSTOM_MODES_HELP_STR} OFF)
36add_feature_info(OPUS_CUSTOM_MODES OPUS_CUSTOM_MODES ${OPUS_CUSTOM_MODES_HELP_STR})
37
38set(OPUS_BUILD_PROGRAMS_HELP_STR "build programs.")
39option(OPUS_BUILD_PROGRAMS ${OPUS_BUILD_PROGRAMS_HELP_STR} OFF)
40add_feature_info(OPUS_BUILD_PROGRAMS OPUS_BUILD_PROGRAMS ${OPUS_BUILD_PROGRAMS_HELP_STR})
41
42set(OPUS_DISABLE_INTRINSICS_HELP_STR "disable all intrinsics optimizations.")
43option(OPUS_DISABLE_INTRINSICS ${OPUS_DISABLE_INTRINSICS_HELP_STR} OFF)
44add_feature_info(OPUS_DISABLE_INTRINSICS OPUS_DISABLE_INTRINSICS ${OPUS_DISABLE_INTRINSICS_HELP_STR})
45
46set(OPUS_FIXED_POINT_HELP_STR "compile as fixed-point (for machines without a fast enough FPU).")
47option(OPUS_FIXED_POINT ${OPUS_FIXED_POINT_HELP_STR} OFF)
48add_feature_info(OPUS_FIXED_POINT OPUS_FIXED_POINT ${OPUS_FIXED_POINT_HELP_STR})
49
50set(OPUS_ENABLE_FLOAT_API_HELP_STR "compile with the floating point API (for machines with float library).")
51option(OPUS_ENABLE_FLOAT_API ${OPUS_ENABLE_FLOAT_API_HELP_STR} ON)
52add_feature_info(OPUS_ENABLE_FLOAT_API OPUS_ENABLE_FLOAT_API ${OPUS_ENABLE_FLOAT_API_HELP_STR})
53
54set(OPUS_FLOAT_APPROX_HELP_STR "enable floating point approximations (Ensure your platform supports IEEE 754 before enabling).")
55option(OPUS_FLOAT_APPROX ${OPUS_FLOAT_APPROX_HELP_STR} OFF)
56add_feature_info(OPUS_FLOAT_APPROX OPUS_FLOAT_APPROX ${OPUS_FLOAT_APPROX_HELP_STR})
57
58set(OPUS_ASSERTIONS_HELP_STR "additional software error checking.")
59option(OPUS_ASSERTIONS ${OPUS_ASSERTIONS_HELP_STR} OFF)
60add_feature_info(OPUS_ASSERTIONS OPUS_ASSERTIONS ${OPUS_ASSERTIONS_HELP_STR})
61
62set(OPUS_HARDENING_HELP_STR "run-time checks that are cheap and safe for use in production.")
63option(OPUS_HARDENING ${OPUS_HARDENING_HELP_STR} ON)
64add_feature_info(OPUS_HARDENING OPUS_HARDENING ${OPUS_HARDENING_HELP_STR})
65
66set(OPUS_FUZZING_HELP_STR "causes the encoder to make random decisions (do not use in production).")
67option(OPUS_FUZZING ${OPUS_FUZZING_HELP_STR} OFF)
68add_feature_info(OPUS_FUZZING OPUS_FUZZING ${OPUS_FUZZING_HELP_STR})
69
70set(OPUS_CHECK_ASM_HELP_STR "enable bit-exactness checks between optimized and c implementations.")
71option(OPUS_CHECK_ASM ${OPUS_CHECK_ASM_HELP_STR} OFF)
72add_feature_info(OPUS_CHECK_ASM OPUS_CHECK_ASM ${OPUS_CHECK_ASM_HELP_STR})
73
74set(OPUS_INSTALL_PKG_CONFIG_MODULE_HELP_STR "install pkg-config module.")
75option(OPUS_INSTALL_PKG_CONFIG_MODULE ${OPUS_INSTALL_PKG_CONFIG_MODULE_HELP_STR} ON)
76add_feature_info(OPUS_INSTALL_PKG_CONFIG_MODULE OPUS_INSTALL_PKG_CONFIG_MODULE ${OPUS_INSTALL_PKG_CONFIG_MODULE_HELP_STR})
77
78set(OPUS_INSTALL_CMAKE_CONFIG_MODULE_HELP_STR "install CMake package config module.")
79option(OPUS_INSTALL_CMAKE_CONFIG_MODULE ${OPUS_INSTALL_CMAKE_CONFIG_MODULE_HELP_STR} ON)
80add_feature_info(OPUS_INSTALL_CMAKE_CONFIG_MODULE OPUS_INSTALL_CMAKE_CONFIG_MODULE ${OPUS_INSTALL_CMAKE_CONFIG_MODULE_HELP_STR})
81
82if(APPLE)
83  set(OPUS_BUILD_FRAMEWORK_HELP_STR "build Framework bundle for Apple systems.")
84  option(OPUS_BUILD_FRAMEWORK ${OPUS_BUILD_FRAMEWORK_HELP_STR} OFF)
85  add_feature_info(OPUS_BUILD_FRAMEWORK OPUS_BUILD_FRAMEWORK ${OPUS_BUILD_FRAMEWORK_HELP_STR})
86endif()
87
88set(OPUS_FIXED_POINT_DEBUG_HELP_STR "debug fixed-point implementation.")
89cmake_dependent_option(OPUS_FIXED_POINT_DEBUG
90                      ${OPUS_FIXED_POINT_DEBUG_HELP_STR}
91                      ON
92                      "OPUS_FIXED_POINT; OPUS_FIXED_POINT_DEBUG"
93                      OFF)
94add_feature_info(OPUS_FIXED_POINT_DEBUG OPUS_FIXED_POINT_DEBUG ${OPUS_FIXED_POINT_DEBUG_HELP_STR})
95
96set(OPUS_VAR_ARRAYS_HELP_STR "use variable length arrays for stack arrays.")
97cmake_dependent_option(OPUS_VAR_ARRAYS
98                      ${OPUS_VAR_ARRAYS_HELP_STR}
99                      ON
100                      "VLA_SUPPORTED; NOT OPUS_USE_ALLOCA; NOT OPUS_NONTHREADSAFE_PSEUDOSTACK"
101                      OFF)
102add_feature_info(OPUS_VAR_ARRAYS OPUS_VAR_ARRAYS ${OPUS_VAR_ARRAYS_HELP_STR})
103
104set(OPUS_USE_ALLOCA_HELP_STR "use alloca for stack arrays (on non-C99 compilers).")
105cmake_dependent_option(OPUS_USE_ALLOCA
106                       ${OPUS_USE_ALLOCA_HELP_STR}
107                       ON
108                       "USE_ALLOCA_SUPPORTED; NOT OPUS_VAR_ARRAYS; NOT OPUS_NONTHREADSAFE_PSEUDOSTACK"
109                       OFF)
110add_feature_info(OPUS_USE_ALLOCA OPUS_USE_ALLOCA ${OPUS_USE_ALLOCA_HELP_STR})
111
112set(OPUS_NONTHREADSAFE_PSEUDOSTACK_HELP_STR "use a non threadsafe pseudostack when neither variable length arrays or alloca is supported.")
113cmake_dependent_option(OPUS_NONTHREADSAFE_PSEUDOSTACK
114                       ${OPUS_NONTHREADSAFE_PSEUDOSTACK_HELP_STR}
115                       ON
116                       "NOT OPUS_VAR_ARRAYS; NOT OPUS_USE_ALLOCA"
117                       OFF)
118add_feature_info(OPUS_NONTHREADSAFE_PSEUDOSTACK OPUS_NONTHREADSAFE_PSEUDOSTACK ${OPUS_NONTHREADSAFE_PSEUDOSTACK_HELP_STR})
119
120set(OPUS_FAST_MATH_HELP_STR "enable fast math (unsupported and discouraged use, as code is not well tested with this build option).")
121cmake_dependent_option(OPUS_FAST_MATH
122                       ${OPUS_FAST_MATH_HELP_STR}
123                       ON
124                       "OPUS_FLOAT_APPROX; OPUS_FAST_MATH; FAST_MATH_SUPPORTED"
125                       OFF)
126add_feature_info(OPUS_FAST_MATH OPUS_FAST_MATH ${OPUS_FAST_MATH_HELP_STR})
127
128set(OPUS_STACK_PROTECTOR_HELP_STR "use stack protection.")
129cmake_dependent_option(OPUS_STACK_PROTECTOR
130                       ${OPUS_STACK_PROTECTOR_HELP_STR}
131                       ON
132                       "STACK_PROTECTOR_SUPPORTED"
133                       OFF)
134add_feature_info(OPUS_STACK_PROTECTOR OPUS_STACK_PROTECTOR ${OPUS_STACK_PROTECTOR_HELP_STR})
135
136if(NOT MSVC)
137  set(OPUS_FORTIFY_SOURCE_HELP_STR "add protection against buffer overflows.")
138  cmake_dependent_option(OPUS_FORTIFY_SOURCE
139                         ${OPUS_FORTIFY_SOURCE_HELP_STR}
140                         ON
141                         "FORTIFY_SOURCE_SUPPORTED"
142                         OFF)
143  add_feature_info(OPUS_FORTIFY_SOURCE OPUS_FORTIFY_SOURCE ${OPUS_FORTIFY_SOURCE_HELP_STR})
144endif()
145
146if(MINGW AND (OPUS_FORTIFY_SOURCE OR OPUS_STACK_PROTECTOR))
147  # ssp lib is needed for security features for MINGW
148  list(APPEND OPUS_REQUIRED_LIBRARIES ssp)
149endif()
150
151if(OPUS_CPU_X86 OR OPUS_CPU_X64)
152  set(OPUS_X86_MAY_HAVE_SSE_HELP_STR "does runtime check for SSE1 support.")
153  cmake_dependent_option(OPUS_X86_MAY_HAVE_SSE
154                         ${OPUS_X86_MAY_HAVE_SSE_HELP_STR}
155                         ON
156                         "SSE1_SUPPORTED; NOT OPUS_DISABLE_INTRINSICS"
157                         OFF)
158  add_feature_info(OPUS_X86_MAY_HAVE_SSE OPUS_X86_MAY_HAVE_SSE ${OPUS_X86_MAY_HAVE_SSE_HELP_STR})
159
160  set(OPUS_X86_MAY_HAVE_SSE2_HELP_STR "does runtime check for SSE2 support.")
161  cmake_dependent_option(OPUS_X86_MAY_HAVE_SSE2
162                         ${OPUS_X86_MAY_HAVE_SSE2_HELP_STR}
163                         ON
164                         "SSE2_SUPPORTED; NOT OPUS_DISABLE_INTRINSICS"
165                         OFF)
166  add_feature_info(OPUS_X86_MAY_HAVE_SSE2 OPUS_X86_MAY_HAVE_SSE2 ${OPUS_X86_MAY_HAVE_SSE2_HELP_STR})
167
168  set(OPUS_X86_MAY_HAVE_SSE4_1_HELP_STR "does runtime check for SSE4.1 support.")
169  cmake_dependent_option(OPUS_X86_MAY_HAVE_SSE4_1
170                         ${OPUS_X86_MAY_HAVE_SSE4_1_HELP_STR}
171                         ON
172                         "SSE4_1_SUPPORTED; NOT OPUS_DISABLE_INTRINSICS"
173                         OFF)
174  add_feature_info(OPUS_X86_MAY_HAVE_SSE4_1 OPUS_X86_MAY_HAVE_SSE4_1 ${OPUS_X86_MAY_HAVE_SSE4_1_HELP_STR})
175
176  set(OPUS_X86_MAY_HAVE_AVX_HELP_STR "does runtime check for AVX support.")
177  cmake_dependent_option(OPUS_X86_MAY_HAVE_AVX
178                         ${OPUS_X86_MAY_HAVE_AVX_HELP_STR}
179                         ON
180                         "AVX_SUPPORTED; NOT OPUS_DISABLE_INTRINSICS"
181                         OFF)
182  add_feature_info(OPUS_X86_MAY_HAVE_AVX OPUS_X86_MAY_HAVE_AVX ${OPUS_X86_MAY_HAVE_AVX_HELP_STR})
183
184  # PRESUME depends on MAY HAVE, but PRESUME will override runtime detection
185  set(OPUS_X86_PRESUME_SSE_HELP_STR "assume target CPU has SSE1 support (override runtime check).")
186  set(OPUS_X86_PRESUME_SSE2_HELP_STR "assume target CPU has SSE2 support (override runtime check).")
187  if(OPUS_CPU_X64) # Assume x86_64 has up to SSE2 support
188    cmake_dependent_option(OPUS_X86_PRESUME_SSE
189                           ${OPUS_X86_PRESUME_SSE_HELP_STR}
190                           ON
191                           "OPUS_X86_MAY_HAVE_SSE; NOT OPUS_DISABLE_INTRINSICS"
192                           OFF)
193
194    cmake_dependent_option(OPUS_X86_PRESUME_SSE2
195                           ${OPUS_X86_PRESUME_SSE2_HELP_STR}
196                           ON
197                           "OPUS_X86_MAY_HAVE_SSE2; NOT OPUS_DISABLE_INTRINSICS"
198                           OFF)
199  else()
200    cmake_dependent_option(OPUS_X86_PRESUME_SSE
201                           ${OPUS_X86_PRESUME_SSE_HELP_STR}
202                           OFF
203                           "OPUS_X86_MAY_HAVE_SSE; NOT OPUS_DISABLE_INTRINSICS"
204                           OFF)
205
206    cmake_dependent_option(OPUS_X86_PRESUME_SSE2
207                           ${OPUS_X86_PRESUME_SSE2_HELP_STR}
208                           OFF
209                           "OPUS_X86_MAY_HAVE_SSE2; NOT OPUS_DISABLE_INTRINSICS"
210                           OFF)
211  endif()
212  add_feature_info(OPUS_X86_PRESUME_SSE OPUS_X86_PRESUME_SSE ${OPUS_X86_PRESUME_SSE_HELP_STR})
213  add_feature_info(OPUS_X86_PRESUME_SSE2 OPUS_X86_PRESUME_SSE2 ${OPUS_X86_PRESUME_SSE2_HELP_STR})
214
215  set(OPUS_X86_PRESUME_SSE4_1_HELP_STR "assume target CPU has SSE4.1 support (override runtime check).")
216  cmake_dependent_option(OPUS_X86_PRESUME_SSE4_1
217                         ${OPUS_X86_PRESUME_SSE4_1_HELP_STR}
218                         OFF
219                         "OPUS_X86_MAY_HAVE_SSE4_1; NOT OPUS_DISABLE_INTRINSICS"
220                         OFF)
221  add_feature_info(OPUS_X86_PRESUME_SSE4_1 OPUS_X86_PRESUME_SSE4_1 ${OPUS_X86_PRESUME_SSE4_1_HELP_STR})
222
223  set(OPUS_X86_PRESUME_AVX_HELP_STR "assume target CPU has AVX support (override runtime check).")
224  cmake_dependent_option(OPUS_X86_PRESUME_AVX
225                         ${OPUS_X86_PRESUME_AVX_HELP_STR}
226                         OFF
227                         "OPUS_X86_MAY_HAVE_AVX; NOT OPUS_DISABLE_INTRINSICS"
228                         OFF)
229  add_feature_info(OPUS_X86_PRESUME_AVX OPUS_X86_PRESUME_AVX ${OPUS_X86_PRESUME_AVX_HELP_STR})
230endif()
231
232feature_summary(WHAT ALL)
233
234set_package_properties(Git
235                       PROPERTIES
236                       TYPE
237                       REQUIRED
238                       DESCRIPTION
239                       "fast, scalable, distributed revision control system"
240                       URL
241                       "https://git-scm.com/"
242                       PURPOSE
243                       "required to set up package version")
244
245set(Opus_PUBLIC_HEADER
246    ${CMAKE_CURRENT_SOURCE_DIR}/include/opus.h
247    ${CMAKE_CURRENT_SOURCE_DIR}/include/opus_defines.h
248    ${CMAKE_CURRENT_SOURCE_DIR}/include/opus_multistream.h
249    ${CMAKE_CURRENT_SOURCE_DIR}/include/opus_projection.h
250    ${CMAKE_CURRENT_SOURCE_DIR}/include/opus_types.h)
251
252if(OPUS_CUSTOM_MODES)
253  list(APPEND Opus_PUBLIC_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/include/opus_custom.h)
254endif()
255
256add_library(opus ${opus_headers} ${opus_sources} ${opus_sources_float} ${Opus_PUBLIC_HEADER})
257add_library(Opus::opus ALIAS opus)
258
259get_library_version(OPUS_LIBRARY_VERSION OPUS_LIBRARY_VERSION_MAJOR)
260message(DEBUG "Opus library version: ${OPUS_LIBRARY_VERSION}")
261
262set_target_properties(opus
263                      PROPERTIES SOVERSION
264                                 ${OPUS_LIBRARY_VERSION_MAJOR}
265                                 VERSION
266                                 ${OPUS_LIBRARY_VERSION}
267                                 PUBLIC_HEADER
268                                 "${Opus_PUBLIC_HEADER}")
269
270target_include_directories(
271  opus
272  PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
273         $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
274         $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/opus>
275  PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
276          ${CMAKE_CURRENT_SOURCE_DIR}
277          celt
278          silk)
279
280target_link_libraries(opus PRIVATE ${OPUS_REQUIRED_LIBRARIES})
281target_compile_definitions(opus PRIVATE OPUS_BUILD)
282
283if(OPUS_FIXED_POINT_DEBUG)
284  target_compile_definitions(opus PRIVATE FIXED_DEBUG)
285endif()
286
287if(OPUS_FORTIFY_SOURCE AND NOT MSVC)
288  target_compile_definitions(opus PRIVATE
289                             $<$<NOT:$<CONFIG:debug>>:_FORTIFY_SOURCE=2>)
290endif()
291
292if(OPUS_FLOAT_APPROX)
293  target_compile_definitions(opus PRIVATE FLOAT_APPROX)
294endif()
295
296if(OPUS_ASSERTIONS)
297  target_compile_definitions(opus PRIVATE ENABLE_ASSERTIONS)
298endif()
299
300if(OPUS_HARDENING)
301  target_compile_definitions(opus PRIVATE ENABLE_HARDENING)
302endif()
303
304if(OPUS_FUZZING)
305  target_compile_definitions(opus PRIVATE FUZZING)
306endif()
307
308if(OPUS_CHECK_ASM)
309  target_compile_definitions(opus PRIVATE OPUS_CHECK_ASM)
310endif()
311
312if(OPUS_VAR_ARRAYS)
313  target_compile_definitions(opus PRIVATE VAR_ARRAYS)
314elseif(OPUS_USE_ALLOCA)
315  target_compile_definitions(opus PRIVATE USE_ALLOCA)
316elseif(OPUS_NONTHREADSAFE_PSEUDOSTACK)
317  target_compile_definitions(opus PRIVATE NONTHREADSAFE_PSEUDOSTACK)
318else()
319  message(ERROR "Need to set a define for stack allocation")
320endif()
321
322if(OPUS_CUSTOM_MODES)
323  target_compile_definitions(opus PRIVATE CUSTOM_MODES)
324endif()
325
326if(OPUS_FAST_MATH)
327  if(MSVC)
328    target_compile_options(opus PRIVATE /fp:fast)
329  else()
330    target_compile_options(opus PRIVATE -ffast-math)
331  endif()
332endif()
333
334if(OPUS_STACK_PROTECTOR)
335  if(MSVC)
336    target_compile_options(opus PRIVATE /GS)
337  else()
338    target_compile_options(opus PRIVATE -fstack-protector-strong)
339  endif()
340elseif(STACK_PROTECTOR_DISABLED_SUPPORTED)
341  target_compile_options(opus PRIVATE /GS-)
342endif()
343
344if(BUILD_SHARED_LIBS)
345  if(WIN32)
346    target_compile_definitions(opus PRIVATE DLL_EXPORT)
347  elseif(HIDDEN_VISIBILITY_SUPPORTED)
348    set_target_properties(opus PROPERTIES C_VISIBILITY_PRESET hidden)
349  endif()
350endif()
351
352add_sources_group(opus silk ${silk_headers} ${silk_sources})
353add_sources_group(opus celt ${celt_headers} ${celt_sources})
354
355if(OPUS_FIXED_POINT)
356  add_sources_group(opus silk ${silk_sources_fixed})
357  target_include_directories(opus PRIVATE silk/fixed)
358  target_compile_definitions(opus PRIVATE FIXED_POINT=1)
359else()
360  add_sources_group(opus silk ${silk_sources_float})
361  target_include_directories(opus PRIVATE silk/float)
362endif()
363
364if(NOT OPUS_ENABLE_FLOAT_API)
365  target_compile_definitions(opus PRIVATE DISABLE_FLOAT_API)
366endif()
367
368if(NOT OPUS_DISABLE_INTRINSICS)
369  if((OPUS_X86_MAY_HAVE_SSE AND NOT OPUS_X86_PRESUME_SSE) OR
370     (OPUS_X86_MAY_HAVE_SSE2 AND NOT OPUS_X86_PRESUME_SSE2) OR
371     (OPUS_X86_MAY_HAVE_SSE4_1 AND NOT OPUS_X86_PRESUME_SSE4_1) OR
372     (OPUS_X86_MAY_HAVE_AVX AND NOT OPUS_X86_PRESUME_AVX))
373    target_compile_definitions(opus PRIVATE OPUS_HAVE_RTCD)
374  endif()
375
376  if(SSE1_SUPPORTED)
377    if(OPUS_X86_MAY_HAVE_SSE)
378      add_sources_group(opus celt ${celt_sources_sse})
379      target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_SSE)
380      if(NOT MSVC)
381        set_source_files_properties(${celt_sources_sse} PROPERTIES COMPILE_FLAGS -msse)
382      endif()
383    endif()
384    if(OPUS_X86_PRESUME_SSE)
385      target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_SSE)
386      if(NOT MSVC)
387        target_compile_options(opus PRIVATE -msse)
388      endif()
389    endif()
390  endif()
391
392  if(SSE2_SUPPORTED)
393    if(OPUS_X86_MAY_HAVE_SSE2)
394      add_sources_group(opus celt ${celt_sources_sse2})
395      target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_SSE2)
396      if(NOT MSVC)
397        set_source_files_properties(${celt_sources_sse2} PROPERTIES COMPILE_FLAGS -msse2)
398      endif()
399    endif()
400    if(OPUS_X86_PRESUME_SSE2)
401      target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_SSE2)
402      if(NOT MSVC)
403        target_compile_options(opus PRIVATE -msse2)
404      endif()
405    endif()
406  endif()
407
408  if(SSE4_1_SUPPORTED)
409    if(OPUS_X86_MAY_HAVE_SSE4_1)
410      add_sources_group(opus celt ${celt_sources_sse4_1})
411      add_sources_group(opus silk ${silk_sources_sse4_1})
412      target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_SSE4_1)
413      if(NOT MSVC)
414        set_source_files_properties(${celt_sources_sse4_1} ${silk_sources_sse4_1} PROPERTIES COMPILE_FLAGS -msse4.1)
415      endif()
416
417      if(OPUS_FIXED_POINT)
418        add_sources_group(opus silk ${silk_sources_fixed_sse4_1})
419        if(NOT MSVC)
420          set_source_files_properties(${silk_sources_fixed_sse4_1} PROPERTIES COMPILE_FLAGS -msse4.1)
421        endif()
422      endif()
423    endif()
424    if(OPUS_X86_PRESUME_SSE4_1)
425      target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_SSE4_1)
426      if(NOT MSVC)
427        target_compile_options(opus PRIVATE -msse4.1)
428      endif()
429    endif()
430  endif()
431
432  if(AVX_SUPPORTED)
433    # mostly placeholder in case of avx intrinsics is added
434    if(OPUS_X86_MAY_HAVE_AVX)
435      target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_AVX)
436    endif()
437    if(OPUS_X86_PRESUME_AVX)
438      target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_AVX)
439      if(NOT MSVC)
440        target_compile_options(opus PRIVATE -mavx)
441      endif()
442    endif()
443  endif()
444
445  if(MSVC)
446    if(AVX_SUPPORTED AND OPUS_X86_PRESUME_AVX) # on 64 bit and 32 bits
447      add_definitions(/arch:AVX)
448    elseif(OPUS_CPU_X86) # if AVX not supported then set SSE flag
449      if((SSE4_1_SUPPORTED AND OPUS_X86_PRESUME_SSE4_1)
450         OR (SSE2_SUPPORTED AND OPUS_X86_PRESUME_SSE2))
451        target_compile_definitions(opus PRIVATE /arch:SSE2)
452      elseif(SSE1_SUPPORTED AND OPUS_X86_PRESUME_SSE)
453        target_compile_definitions(opus PRIVATE /arch:SSE)
454      endif()
455    endif()
456  endif()
457
458  if(CMAKE_SYSTEM_PROCESSOR MATCHES "(arm|aarch64)")
459    add_sources_group(opus celt ${celt_sources_arm})
460  endif()
461
462  if(COMPILER_SUPPORT_NEON)
463    if(OPUS_MAY_HAVE_NEON)
464      if(RUNTIME_CPU_CAPABILITY_DETECTION)
465        message(STATUS "OPUS_MAY_HAVE_NEON enabling runtime detection")
466        target_compile_definitions(opus PRIVATE OPUS_HAVE_RTCD)
467      else()
468        message(ERROR "Runtime cpu capability detection needed for MAY_HAVE_NEON")
469      endif()
470      # Do runtime check for NEON
471      target_compile_definitions(opus
472                                 PRIVATE
473                                 OPUS_ARM_MAY_HAVE_NEON
474                                 OPUS_ARM_MAY_HAVE_NEON_INTR)
475    endif()
476
477    add_sources_group(opus celt ${celt_sources_arm_neon_intr})
478    add_sources_group(opus silk ${silk_sources_arm_neon_intr})
479
480    # silk arm neon depends on main_Fix.h
481    target_include_directories(opus PRIVATE silk/fixed)
482
483    if(OPUS_FIXED_POINT)
484      add_sources_group(opus silk ${silk_sources_fixed_arm_neon_intr})
485    endif()
486
487    if(OPUS_PRESUME_NEON)
488      target_compile_definitions(opus
489                                 PRIVATE
490                                 OPUS_ARM_PRESUME_NEON
491                                 OPUS_ARM_PRESUME_NEON_INTR)
492    endif()
493  endif()
494endif()
495
496target_compile_definitions(opus
497                           PRIVATE
498                           $<$<BOOL:${HAVE_LRINT}>:HAVE_LRINT>
499                           $<$<BOOL:${HAVE_LRINTF}>:HAVE_LRINTF>)
500
501if(OPUS_BUILD_FRAMEWORK)
502  set_target_properties(opus PROPERTIES
503                        FRAMEWORK TRUE
504                        FRAMEWORK_VERSION ${PROJECT_VERSION}
505                        MACOSX_FRAMEWORK_IDENTIFIER org.xiph.opus
506                        MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${PROJECT_VERSION}
507                        MACOSX_FRAMEWORK_BUNDLE_VERSION ${PROJECT_VERSION}
508                        XCODE_ATTRIBUTE_INSTALL_PATH "@rpath"
509                        OUTPUT_NAME Opus)
510endif()
511
512install(TARGETS opus
513        EXPORT OpusTargets
514        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
515        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
516        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
517        FRAMEWORK DESTINATION ${CMAKE_INSTALL_PREFIX}
518        PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/opus)
519
520if(OPUS_INSTALL_PKG_CONFIG_MODULE)
521  set(prefix ${CMAKE_INSTALL_PREFIX})
522  set(exec_prefix ${CMAKE_INSTALL_PREFIX})
523  set(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
524  set(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
525  set(VERSION ${PACKAGE_VERSION})
526  if(HAVE_LIBM)
527    set(LIBM "-lm")
528  endif()
529  configure_file(opus.pc.in opus.pc)
530  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/opus.pc
531          DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
532endif()
533
534if(OPUS_INSTALL_CMAKE_CONFIG_MODULE)
535  set(CPACK_GENERATOR TGZ)
536  include(CPack)
537  set(CMAKE_INSTALL_PACKAGEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
538  install(EXPORT OpusTargets
539          NAMESPACE Opus::
540          DESTINATION ${CMAKE_INSTALL_PACKAGEDIR})
541
542  include(CMakePackageConfigHelpers)
543
544  set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR})
545  configure_package_config_file(${PROJECT_SOURCE_DIR}/cmake/OpusConfig.cmake.in
546                                OpusConfig.cmake
547                                INSTALL_DESTINATION
548                                ${CMAKE_INSTALL_PACKAGEDIR}
549                                PATH_VARS
550                                INCLUDE_INSTALL_DIR
551                                INSTALL_PREFIX
552                                ${CMAKE_INSTALL_PREFIX})
553  write_basic_package_version_file(OpusConfigVersion.cmake
554                                   VERSION ${PROJECT_VERSION}
555                                   COMPATIBILITY SameMajorVersion)
556  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/OpusConfig.cmake
557                ${CMAKE_CURRENT_BINARY_DIR}/OpusConfigVersion.cmake
558          DESTINATION ${CMAKE_INSTALL_PACKAGEDIR})
559endif()
560
561if(OPUS_BUILD_PROGRAMS)
562  # demo
563  if(OPUS_CUSTOM_MODES)
564    add_executable(opus_custom_demo ${opus_custom_demo_sources})
565    target_include_directories(opus_custom_demo
566                               PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
567    target_link_libraries(opus_custom_demo PRIVATE opus)
568  endif()
569
570  add_executable(opus_demo ${opus_demo_sources})
571  target_include_directories(opus_demo PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
572  target_include_directories(opus_demo PRIVATE silk) # debug.h
573  target_include_directories(opus_demo PRIVATE celt) # arch.h
574  target_link_libraries(opus_demo PRIVATE opus ${OPUS_REQUIRED_LIBRARIES})
575
576  # compare
577  add_executable(opus_compare ${opus_compare_sources})
578  target_include_directories(opus_compare PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
579  target_link_libraries(opus_compare PRIVATE opus ${OPUS_REQUIRED_LIBRARIES})
580endif()
581
582if(BUILD_TESTING)
583  enable_testing()
584
585  # tests
586  add_executable(test_opus_decode ${test_opus_decode_sources})
587  target_include_directories(test_opus_decode
588                             PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
589  target_link_libraries(test_opus_decode PRIVATE opus)
590  if(OPUS_FIXED_POINT)
591    target_compile_definitions(test_opus_decode PRIVATE DISABLE_FLOAT_API)
592  endif()
593  add_test(NAME test_opus_decode COMMAND $<TARGET_FILE:test_opus_decode> WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
594
595  add_executable(test_opus_padding ${test_opus_padding_sources})
596  target_include_directories(test_opus_padding
597                             PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
598  target_link_libraries(test_opus_padding PRIVATE opus)
599  add_test(NAME test_opus_padding COMMAND $<TARGET_FILE:test_opus_padding> WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
600
601  if(NOT BUILD_SHARED_LIBS)
602    # disable tests that depends on private API when building shared lib
603    add_executable(test_opus_api ${test_opus_api_sources})
604    target_include_directories(test_opus_api
605                               PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt)
606    target_link_libraries(test_opus_api PRIVATE opus)
607    if(OPUS_FIXED_POINT)
608      target_compile_definitions(test_opus_api PRIVATE DISABLE_FLOAT_API)
609    endif()
610    add_test(NAME test_opus_api COMMAND $<TARGET_FILE:test_opus_api> WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
611
612    add_executable(test_opus_encode ${test_opus_encode_sources})
613    target_include_directories(test_opus_encode
614                               PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt)
615    target_link_libraries(test_opus_encode PRIVATE opus)
616    add_test(NAME test_opus_encode COMMAND $<TARGET_FILE:test_opus_encode> WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
617  endif()
618endif()
619