opus_functions.cmake 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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)
  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. set(PACKAGE_VERSION ${opus_package_version_string} PARENT_SCOPE)
  76. return()
  77. endif()
  78. # if all else fails set to 0
  79. set(PACKAGE_VERSION 0 PARENT_SCOPE)
  80. endfunction()
  81. function(check_and_set_flag NAME FLAG)
  82. include(CheckCCompilerFlag)
  83. check_c_compiler_flag(${FLAG} ${NAME}_SUPPORTED)
  84. if(${NAME}_SUPPORTED)
  85. add_definitions(${FLAG})
  86. endif()
  87. endfunction()
  88. function(check_flag NAME FLAG)
  89. include(CheckCCompilerFlag)
  90. check_c_compiler_flag(${FLAG} ${NAME}_SUPPORTED)
  91. endfunction()
  92. include(CheckIncludeFile)
  93. # function to check if compiler supports SSE, SSE2, SSE4.1 and AVX if target
  94. # systems may not have SSE support then use OPUS_MAY_HAVE_SSE option if target
  95. # system is guaranteed to have SSE support then OPUS_PRESUME_SSE can be used to
  96. # skip SSE runtime check
  97. function(opus_detect_sse COMPILER_SUPPORT_SIMD)
  98. message(STATUS "Check SIMD support by compiler")
  99. check_include_file(xmmintrin.h HAVE_XMMINTRIN_H) # SSE1
  100. if(HAVE_XMMINTRIN_H)
  101. if(MSVC)
  102. # different arch options for 32 and 64 bit target for MSVC
  103. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  104. check_flag(SSE1 /arch:SSE)
  105. else()
  106. set(SSE1_SUPPORTED 1 PARENT_SCOPE)
  107. endif()
  108. else()
  109. check_and_set_flag(SSE1 -msse)
  110. endif()
  111. else()
  112. set(SSE1_SUPPORTED 0 PARENT_SCOPE)
  113. endif()
  114. check_include_file(emmintrin.h HAVE_EMMINTRIN_H) # SSE2
  115. if(HAVE_EMMINTRIN_H)
  116. if(MSVC)
  117. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  118. check_flag(SSE2 /arch:SSE2)
  119. else()
  120. set(SSE2_SUPPORTED 1 PARENT_SCOPE)
  121. endif()
  122. else()
  123. check_and_set_flag(SSE2 -msse2)
  124. endif()
  125. else()
  126. set(SSE2_SUPPORTED 0 PARENT_SCOPE)
  127. endif()
  128. check_include_file(smmintrin.h HAVE_SMMINTRIN_H) # SSE4.1
  129. if(HAVE_SMMINTRIN_H)
  130. if(MSVC)
  131. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  132. check_flag(SSE4_1 /arch:SSE2) # SSE2 and above
  133. else()
  134. set(SSE4_1_SUPPORTED 1 PARENT_SCOPE)
  135. endif()
  136. else()
  137. check_and_set_flag(SSE4_1 -msse4.1)
  138. endif()
  139. else()
  140. set(SSE4_1_SUPPORTED 0 PARENT_SCOPE)
  141. endif()
  142. check_include_file(immintrin.h HAVE_IMMINTRIN_H) # AVX
  143. if(HAVE_IMMINTRIN_H)
  144. if(MSVC)
  145. check_flag(AVX /arch:AVX)
  146. else()
  147. check_and_set_flag(AVX -mavx)
  148. endif()
  149. else()
  150. set(AVX_SUPPORTED 0 PARENT_SCOPE)
  151. endif()
  152. if(MSVC) # To avoid warning D9025 of overriding compiler options
  153. if(AVX_SUPPORTED) # on 64 bit and 32 bits
  154. add_definitions(/arch:AVX)
  155. elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) # if AVX not supported then set SSE flag
  156. if(SSE4_1_SUPPORTED OR SSE2_SUPPORTED)
  157. add_definitions(/arch:SSE2)
  158. elseif(SSE1_SUPPORTED)
  159. add_definitions(/arch:SSE)
  160. endif()
  161. endif()
  162. endif()
  163. if(SSE1_SUPPORTED OR SSE2_SUPPORTED OR SSE4_1_SUPPORTED OR AVX_SUPPORTED)
  164. set(COMPILER_SUPPORT_SIMD 1 PARENT_SCOPE)
  165. else()
  166. message(STATUS "No SIMD support in compiler")
  167. endif()
  168. endfunction()
  169. function(opus_detect_neon COMPILER_SUPPORT_NEON)
  170. if(CMAKE_SYSTEM_PROCESSOR MATCHES "(armv7-a|aarch64)")
  171. message(STATUS "Check NEON support by compiler")
  172. check_include_file(arm_neon.h HAVE_ARM_NEON_H)
  173. if(HAVE_ARM_NEON_H)
  174. set(COMPILER_SUPPORT_NEON ${HAVE_ARM_NEON_H} PARENT_SCOPE)
  175. endif()
  176. endif()
  177. endfunction()
  178. function(opus_supports_cpu_detection RUNTIME_CPU_CAPABILITY_DETECTION)
  179. if(MSVC)
  180. check_include_file(intrin.h HAVE_INTRIN_H)
  181. else()
  182. check_include_file(cpuid.h HAVE_CPUID_H)
  183. endif()
  184. if(HAVE_INTRIN_H OR HAVE_CPUID_H)
  185. set(RUNTIME_CPU_CAPABILITY_DETECTION 1 PARENT_SCOPE)
  186. else()
  187. set(RUNTIME_CPU_CAPABILITY_DETECTION 0 PARENT_SCOPE)
  188. endif()
  189. endfunction()
  190. function(add_sources_group target group)
  191. target_sources(${target} PRIVATE ${ARGN})
  192. source_group(${group} FILES ${ARGN})
  193. endfunction()
  194. function(get_opus_sources SOURCE_GROUP MAKE_FILE SOURCES)
  195. # read file, each item in list is one group
  196. file(STRINGS ${MAKE_FILE} opus_sources)
  197. # add wildcard for regex match
  198. string(CONCAT SOURCE_GROUP ${SOURCE_GROUP} ".*$")
  199. # find group
  200. foreach(val IN LISTS opus_sources)
  201. if(val MATCHES ${SOURCE_GROUP})
  202. list(LENGTH val list_length)
  203. if(${list_length} EQUAL 1)
  204. # for tests split by '=' and clean up the rest into a list
  205. string(FIND ${val} "=" index)
  206. math(EXPR index "${index} + 1")
  207. string(SUBSTRING ${val}
  208. ${index}
  209. -1
  210. sources)
  211. string(REPLACE " "
  212. ";"
  213. sources
  214. ${sources})
  215. else()
  216. # discard the group
  217. list(REMOVE_AT val 0)
  218. set(sources ${val})
  219. endif()
  220. break()
  221. endif()
  222. endforeach()
  223. list(LENGTH sources list_length)
  224. if(${list_length} LESS 1)
  225. message(
  226. FATAL_ERROR
  227. "No files parsed succesfully from ${SOURCE_GROUP} in ${MAKE_FILE}")
  228. endif()
  229. # remove trailing whitespaces
  230. set(list_var "")
  231. foreach(source ${sources})
  232. string(STRIP "${source}" source)
  233. list(APPEND list_var "${source}")
  234. endforeach()
  235. set(${SOURCES} ${list_var} PARENT_SCOPE)
  236. endfunction()