CMakeLists.txt 25 KB

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