CMakeLists.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
  2. project(sherpa-ncnn)
  3. set(SHERPA_NCNN_VERSION "2.1.10")
  4. # Disable warning about
  5. #
  6. # "The DOWNLOAD_EXTRACT_TIMESTAMP option was not given and policy CMP0135 is
  7. # not set.
  8. if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
  9. cmake_policy(SET CMP0135 NEW)
  10. endif()
  11. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
  12. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
  13. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
  14. set(CMAKE_SKIP_BUILD_RPATH FALSE)
  15. set(BUILD_RPATH_USE_ORIGIN TRUE)
  16. set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
  17. if(NOT APPLE)
  18. set(SHERPA_NCNN_RPATH_ORIGIN "$ORIGIN")
  19. else()
  20. set(CMAKE_MACOSX_RPATH ON)
  21. set(SHERPA_NCNN_RPATH_ORIGIN "@loader_path")
  22. endif()
  23. set(CMAKE_INSTALL_RPATH ${SHERPA_NCNN_RPATH_ORIGIN})
  24. set(CMAKE_BUILD_RPATH ${SHERPA_NCNN_RPATH_ORIGIN})
  25. # You will find a file compile_commands.json in the build directory
  26. # if it is enabled
  27. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  28. option(BUILD_SHARED_LIBS "Whether to build shared libraries" OFF)
  29. option(SHERPA_NCNN_ENABLE_PYTHON "Whether to build Python" OFF)
  30. option(SHERPA_NCNN_ENABLE_PORTAUDIO "Whether to build with portaudio" ON)
  31. option(SHERPA_NCNN_ENABLE_JNI "Whether to build JNI internface" OFF)
  32. option(SHERPA_NCNN_ENABLE_BINARY "Whether to build the binary sherpa-ncnn" ON)
  33. option(SHERPA_NCNN_ENABLE_TEST "Whether to build tests" OFF)
  34. option(SHERPA_NCNN_ENABLE_C_API "Whether to build C API" ON)
  35. option(SHERPA_NCNN_ENABLE_WASM "Whether to enable WASM" OFF)
  36. option(SHERPA_NCNN_ENABLE_WASM_FOR_NODEJS "Whether to enable WASM for NodeJS" OFF)
  37. option(SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE "Whether to generate-int8-scale-table" ON)
  38. option(SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES "Whether to enable ffmpeg-examples" OFF)
  39. if(DEFINED ANDROID_ABI)
  40. message(STATUS "Set SHERPA_NCNN_ENABLE_JNI to ON for Android")
  41. set(SHERPA_NCNN_ENABLE_JNI ON CACHE BOOL "" FORCE)
  42. endif()
  43. # See
  44. # https://stackoverflow.com/questions/33062728/cmake-link-shared-library-on-windows
  45. if(BUILD_SHARED_LIBS AND MSVC)
  46. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  47. endif()
  48. if(NOT BUILD_SHARED_LIBS AND MSVC)
  49. # see https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html
  50. # https://stackoverflow.com/questions/14172856/compile-with-mt-instead-of-md-using-cmake
  51. if(MSVC)
  52. add_compile_options(
  53. $<$<CONFIG:>:/MT> #---------|
  54. $<$<CONFIG:Debug>:/MTd> #---|-- Statically link the runtime libraries
  55. $<$<CONFIG:Release>:/MT> #--|
  56. )
  57. endif()
  58. endif()
  59. message(STATUS "CMAKE_EXPORT_COMPILE_COMMANDS: ${CMAKE_EXPORT_COMPILE_COMMANDS}")
  60. message(STATUS "BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}")
  61. message(STATUS "SHERPA_NCNN_ENABLE_PYTHON ${SHERPA_NCNN_ENABLE_PYTHON}")
  62. message(STATUS "SHERPA_NCNN_ENABLE_PORTAUDIO ${SHERPA_NCNN_ENABLE_PORTAUDIO}")
  63. message(STATUS "SHERPA_NCNN_ENABLE_JNI ${SHERPA_NCNN_ENABLE_JNI}")
  64. message(STATUS "SHERPA_NCNN_ENABLE_BINARY ${SHERPA_NCNN_ENABLE_BINARY}")
  65. message(STATUS "SHERPA_NCNN_ENABLE_TEST ${SHERPA_NCNN_ENABLE_TEST}")
  66. message(STATUS "SHERPA_NCNN_ENABLE_C_API ${SHERPA_NCNN_ENABLE_C_API}")
  67. message(STATUS "SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE ${SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE}")
  68. message(STATUS "SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES ${SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES}")
  69. message(STATUS "SHERPA_NCNN_ENABLE_WASM ${SHERPA_NCNN_ENABLE_WASM}")
  70. message(STATUS "SHERPA_NCNN_ENABLE_WASM_FOR_NODEJS ${SHERPA_NCNN_ENABLE_WASM_FOR_NODEJS}")
  71. if(SHERPA_NCNN_ENABLE_WASM_FOR_NODEJS)
  72. if(NOT SHERPA_NCNN_ENABLE_WASM)
  73. message(FATAL_ERROR "Please set SHERPA_NCNN_ENABLE_WASM to ON if you enable WASM for NodeJS")
  74. endif()
  75. endif()
  76. if(NOT CMAKE_BUILD_TYPE)
  77. message(STATUS "No CMAKE_BUILD_TYPE given, default to Release")
  78. set(CMAKE_BUILD_TYPE Release)
  79. endif()
  80. message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
  81. # Set the release compiler flags to support debugging, see https://github.com/k2-fsa/sherpa-ncnn/issues/147
  82. if(SHERPA_NCNN_ENABLE_DEBUG_FOR_RELEASE)
  83. message(STATUS "Enable debugging for Release")
  84. string(APPEND CMAKE_CXX_FLAGS_RELEASE " -g -O0")
  85. endif()
  86. set(CMAKE_CXX_STANDARD 14 CACHE STRING "The C++ version to be used.")
  87. set(CMAKE_CXX_EXTENSIONS OFF)
  88. if(SHERPA_NCNN_ENABLE_BINARY AND UNIX AND NOT APPLE)
  89. include(CheckIncludeFileCXX)
  90. check_include_file_cxx(alsa/asoundlib.h SHERPA_NCNN_HAS_ALSA)
  91. if(SHERPA_NCNN_HAS_ALSA)
  92. message(STATUS "With Alsa")
  93. add_definitions(-DSHERPA_NCNN_ENABLE_ALSA=1)
  94. elseif(UNIX AND NOT APPLE)
  95. message(WARNING "\
  96. Could not find alsa/asoundlib.h !
  97. We won't build sherpa-ncnn-alsa
  98. To fix that, please do:
  99. (1) sudo apt-get install alsa-utils libasound2-dev
  100. (2) rm -rf build
  101. (3) re-try
  102. ")
  103. endif()
  104. endif()
  105. list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
  106. if(WIN32)
  107. add_definitions(-DNOMINMAX) # Otherwise, std::max() and std::min() won't work
  108. endif()
  109. if(WIN32 AND MSVC)
  110. # disable various warnings for MSVC
  111. # 4244: 'return': conversion from 'unsigned __int64' to 'int', possible loss of data
  112. # 4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
  113. # 4305: 'argument': truncation from 'double' to 'const float'
  114. # 4334: '<<': result of 32-bit shift implicitly converted to 64 bits
  115. # 4800: 'int': forcing value to bool 'true' or 'false'
  116. # 4996: 'fopen': This function or variable may be unsafe
  117. set(disabled_warnings
  118. /wd4244
  119. /wd4267
  120. /wd4305
  121. /wd4334
  122. /wd4800
  123. /wd4996
  124. )
  125. message(STATUS "Disabled warnings: ${disabled_warnings}")
  126. foreach(w IN LISTS disabled_warnings)
  127. string(APPEND CMAKE_CXX_FLAGS " ${w} ")
  128. endforeach()
  129. endif()
  130. include(kaldi-native-fbank)
  131. include(ncnn)
  132. if(SHERPA_NCNN_ENABLE_PORTAUDIO)
  133. include(portaudio)
  134. endif()
  135. if(SHERPA_NCNN_ENABLE_PYTHON)
  136. include(pybind11)
  137. endif()
  138. add_subdirectory(sherpa-ncnn)
  139. if(SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES)
  140. add_subdirectory(ffmpeg-examples)
  141. endif()
  142. if(SHERPA_NCNN_ENABLE_C_API AND SHERPA_NCNN_ENABLE_BINARY)
  143. add_subdirectory(c-api-examples)
  144. endif()
  145. if(SHERPA_NCNN_ENABLE_WASM)
  146. add_subdirectory(wasm)
  147. endif()
  148. set(SHERPA_NCNN_PKG_CONFIG_EXTRA_LIBS)
  149. if(NOT BUILD_SHARED_LIBS)
  150. if(APPLE)
  151. set(SHERPA_NCNN_PKG_CONFIG_EXTRA_LIBS "-lomp -lc++")
  152. endif()
  153. if(UNIX AND NOT APPLE)
  154. set(SHERPA_NCNN_PKG_CONFIG_EXTRA_LIBS "-lm -lstdc++ -fopenmp")
  155. endif()
  156. endif()
  157. # See https://people.freedesktop.org/~dbn/pkg-config-guide.html
  158. configure_file(cmake/sherpa-ncnn.pc.in ${PROJECT_BINARY_DIR}/sherpa-ncnn.pc @ONLY)
  159. install(
  160. FILES
  161. ${PROJECT_BINARY_DIR}/sherpa-ncnn.pc
  162. DESTINATION
  163. .
  164. )