opus_functions.cmake 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #[[Cmake helper function to parse source files from make files
  2. this is to avoid breaking existing make and auto make support
  3. but still have the option to use CMake with only lists at one place]]
  4. cmake_minimum_required(VERSION 3.1)
  5. function(get_library_version OPUS_LIBRARY_VERSION OPUS_LIBRARY_VERSION_MAJOR)
  6. file(STRINGS configure.ac opus_lt_current_string
  7. LIMIT_COUNT 1
  8. REGEX "OPUS_LT_CURRENT=")
  9. string(REGEX MATCH
  10. "OPUS_LT_CURRENT=([0-9]*)"
  11. _
  12. ${opus_lt_current_string})
  13. set(OPUS_LT_CURRENT ${CMAKE_MATCH_1})
  14. file(STRINGS configure.ac opus_lt_revision_string
  15. LIMIT_COUNT 1
  16. REGEX "OPUS_LT_REVISION=")
  17. string(REGEX MATCH
  18. "OPUS_LT_REVISION=([0-9]*)"
  19. _
  20. ${opus_lt_revision_string})
  21. set(OPUS_LT_REVISION ${CMAKE_MATCH_1})
  22. file(STRINGS configure.ac opus_lt_age_string
  23. LIMIT_COUNT 1
  24. REGEX "OPUS_LT_AGE=")
  25. string(REGEX MATCH
  26. "OPUS_LT_AGE=([0-9]*)"
  27. _
  28. ${opus_lt_age_string})
  29. set(OPUS_LT_AGE ${CMAKE_MATCH_1})
  30. math(EXPR OPUS_LIBRARY_VERSION_MAJOR "${OPUS_LT_CURRENT} - ${OPUS_LT_AGE}")
  31. set(OPUS_LIBRARY_VERSION_MINOR ${OPUS_LT_AGE})
  32. set(OPUS_LIBRARY_VERSION_PATCH ${OPUS_LT_REVISION})
  33. set(
  34. OPUS_LIBRARY_VERSION
  35. "${OPUS_LIBRARY_VERSION_MAJOR}.${OPUS_LIBRARY_VERSION_MINOR}.${OPUS_LIBRARY_VERSION_PATCH}"
  36. PARENT_SCOPE)
  37. set(OPUS_LIBRARY_VERSION_MAJOR ${OPUS_LIBRARY_VERSION_MAJOR} PARENT_SCOPE)
  38. endfunction()
  39. function(get_package_version PACKAGE_VERSION)
  40. find_package(Git)
  41. if(Git_FOUND AND EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git")
  42. execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --match "v*"
  43. OUTPUT_VARIABLE OPUS_PACKAGE_VERSION)
  44. if(OPUS_PACKAGE_VERSION)
  45. string(STRIP ${OPUS_PACKAGE_VERSION}, OPUS_PACKAGE_VERSION)
  46. string(REPLACE \n
  47. ""
  48. OPUS_PACKAGE_VERSION
  49. ${OPUS_PACKAGE_VERSION})
  50. string(REPLACE ,
  51. ""
  52. OPUS_PACKAGE_VERSION
  53. ${OPUS_PACKAGE_VERSION})
  54. string(SUBSTRING ${OPUS_PACKAGE_VERSION}
  55. 1
  56. -1
  57. OPUS_PACKAGE_VERSION)
  58. set(PACKAGE_VERSION ${OPUS_PACKAGE_VERSION} PARENT_SCOPE)
  59. return()
  60. endif()
  61. endif()
  62. if(EXISTS "${CMAKE_SOURCE_DIR}/package_version")
  63. # Not a git repo, lets' try to parse it from package_version file if exists
  64. file(STRINGS package_version opus_package_version_string
  65. LIMIT_COUNT 1
  66. REGEX "PACKAGE_VERSION=")
  67. string(REPLACE "PACKAGE_VERSION="
  68. ""
  69. opus_package_version_string
  70. ${opus_package_version_string})
  71. string(REPLACE "\""
  72. ""
  73. opus_package_version_string
  74. ${opus_package_version_string})
  75. # In case we have a unknown dist here we just replace it with 0
  76. string(REPLACE "unknown"
  77. "0"
  78. opus_package_version_string
  79. ${opus_package_version_string})
  80. set(PACKAGE_VERSION ${opus_package_version_string} PARENT_SCOPE)
  81. return()
  82. endif()
  83. # if all else fails set to 0
  84. set(PACKAGE_VERSION 0 PARENT_SCOPE)
  85. endfunction()
  86. function(check_and_set_flag NAME FLAG)
  87. include(CheckCCompilerFlag)
  88. check_c_compiler_flag(${FLAG} ${NAME}_SUPPORTED)
  89. if(${NAME}_SUPPORTED)
  90. add_definitions(${FLAG})
  91. endif()
  92. endfunction()
  93. function(check_flag NAME FLAG)
  94. include(CheckCCompilerFlag)
  95. check_c_compiler_flag(${FLAG} ${NAME}_SUPPORTED)
  96. endfunction()
  97. include(CheckIncludeFile)
  98. # function to check if compiler supports SSE, SSE2, SSE4.1 and AVX if target
  99. # systems may not have SSE support then use OPUS_MAY_HAVE_SSE option if target
  100. # system is guaranteed to have SSE support then OPUS_PRESUME_SSE can be used to
  101. # skip SSE runtime check
  102. function(opus_detect_sse COMPILER_SUPPORT_SIMD)
  103. message(STATUS "Check SIMD support by compiler")
  104. check_include_file(xmmintrin.h HAVE_XMMINTRIN_H) # SSE1
  105. if(HAVE_XMMINTRIN_H)
  106. if(MSVC)
  107. # different arch options for 32 and 64 bit target for MSVC
  108. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  109. check_flag(SSE1 /arch:SSE)
  110. else()
  111. set(SSE1_SUPPORTED
  112. 1
  113. PARENT_SCOPE)
  114. endif()
  115. else()
  116. check_flag(SSE1 -msse)
  117. endif()
  118. else()
  119. set(SSE1_SUPPORTED
  120. 0
  121. PARENT_SCOPE)
  122. endif()
  123. check_include_file(emmintrin.h HAVE_EMMINTRIN_H) # SSE2
  124. if(HAVE_EMMINTRIN_H)
  125. if(MSVC)
  126. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  127. check_flag(SSE2 /arch:SSE2)
  128. else()
  129. set(SSE2_SUPPORTED
  130. 1
  131. PARENT_SCOPE)
  132. endif()
  133. else()
  134. check_flag(SSE2 -msse2)
  135. endif()
  136. else()
  137. set(SSE2_SUPPORTED
  138. 0
  139. PARENT_SCOPE)
  140. endif()
  141. check_include_file(smmintrin.h HAVE_SMMINTRIN_H) # SSE4.1
  142. if(HAVE_SMMINTRIN_H)
  143. if(MSVC)
  144. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  145. check_flag(SSE4_1 /arch:SSE2) # SSE2 and above
  146. else()
  147. set(SSE4_1_SUPPORTED
  148. 1
  149. PARENT_SCOPE)
  150. endif()
  151. else()
  152. check_flag(SSE4_1 -msse4.1)
  153. endif()
  154. else()
  155. set(SSE4_1_SUPPORTED
  156. 0
  157. PARENT_SCOPE)
  158. endif()
  159. check_include_file(immintrin.h HAVE_IMMINTRIN_H) # AVX
  160. if(HAVE_IMMINTRIN_H)
  161. if(MSVC)
  162. check_flag(AVX /arch:AVX)
  163. else()
  164. check_flag(AVX -mavx)
  165. endif()
  166. else()
  167. set(AVX_SUPPORTED
  168. 0
  169. PARENT_SCOPE)
  170. endif()
  171. if(SSE1_SUPPORTED OR SSE2_SUPPORTED OR SSE4_1_SUPPORTED OR AVX_SUPPORTED)
  172. set(COMPILER_SUPPORT_SIMD 1 PARENT_SCOPE)
  173. else()
  174. message(STATUS "No SIMD support in compiler")
  175. endif()
  176. endfunction()
  177. function(opus_detect_neon COMPILER_SUPPORT_NEON)
  178. if(CMAKE_SYSTEM_PROCESSOR MATCHES "(arm|aarch64)")
  179. message(STATUS "Check NEON support by compiler")
  180. check_include_file(arm_neon.h HAVE_ARM_NEON_H)
  181. if(HAVE_ARM_NEON_H)
  182. set(COMPILER_SUPPORT_NEON ${HAVE_ARM_NEON_H} PARENT_SCOPE)
  183. endif()
  184. endif()
  185. endfunction()
  186. function(opus_supports_cpu_detection RUNTIME_CPU_CAPABILITY_DETECTION)
  187. if(MSVC)
  188. check_include_file(intrin.h HAVE_INTRIN_H)
  189. else()
  190. check_include_file(cpuid.h HAVE_CPUID_H)
  191. endif()
  192. if(HAVE_INTRIN_H OR HAVE_CPUID_H)
  193. set(RUNTIME_CPU_CAPABILITY_DETECTION 1 PARENT_SCOPE)
  194. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(arm|aarch64)")
  195. # ARM cpu detection is implemented for Windows and anything
  196. # using a Linux kernel (such as Android).
  197. if (CMAKE_SYSTEM_NAME MATCHES "(Windows|Linux|Android)")
  198. set(RUNTIME_CPU_CAPABILITY_DETECTION 1 PARENT_SCOPE)
  199. endif ()
  200. else()
  201. set(RUNTIME_CPU_CAPABILITY_DETECTION 0 PARENT_SCOPE)
  202. endif()
  203. endfunction()
  204. function(add_sources_group target group)
  205. target_sources(${target} PRIVATE ${ARGN})
  206. source_group(${group} FILES ${ARGN})
  207. endfunction()
  208. function(get_opus_sources SOURCE_GROUP MAKE_FILE SOURCES)
  209. # read file, each item in list is one group
  210. file(STRINGS ${MAKE_FILE} opus_sources)
  211. # add wildcard for regex match
  212. string(CONCAT SOURCE_GROUP ${SOURCE_GROUP} ".*$")
  213. # find group
  214. foreach(val IN LISTS opus_sources)
  215. if(val MATCHES ${SOURCE_GROUP})
  216. list(LENGTH val list_length)
  217. if(${list_length} EQUAL 1)
  218. # for tests split by '=' and clean up the rest into a list
  219. string(FIND ${val} "=" index)
  220. math(EXPR index "${index} + 1")
  221. string(SUBSTRING ${val}
  222. ${index}
  223. -1
  224. sources)
  225. string(REPLACE " "
  226. ";"
  227. sources
  228. ${sources})
  229. else()
  230. # discard the group
  231. list(REMOVE_AT val 0)
  232. set(sources ${val})
  233. endif()
  234. break()
  235. endif()
  236. endforeach()
  237. list(LENGTH sources list_length)
  238. if(${list_length} LESS 1)
  239. message(
  240. FATAL_ERROR
  241. "No files parsed succesfully from ${SOURCE_GROUP} in ${MAKE_FILE}")
  242. endif()
  243. # remove trailing whitespaces
  244. set(list_var "")
  245. foreach(source ${sources})
  246. string(STRIP "${source}" source)
  247. list(APPEND list_var "${source}")
  248. endforeach()
  249. set(${SOURCES} ${list_var} PARENT_SCOPE)
  250. endfunction()