CMakeLists.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
  2. project(sherpa-ncnn)
  3. set(SHERPA_NCNN_VERSION "2.0.5")
  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_GENERATE_INT8_SCALE_TABLE "Whether to generate-int8-scale-table" ON)
  36. option(SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES "Whether to enable ffmpeg-examples" OFF)
  37. if(DEFINED ANDROID_ABI)
  38. message(STATUS "Set SHERPA_NCNN_ENABLE_JNI to ON for Android")
  39. set(SHERPA_NCNN_ENABLE_JNI ON CACHE BOOL "" FORCE)
  40. endif()
  41. # See
  42. # https://stackoverflow.com/questions/33062728/cmake-link-shared-library-on-windows
  43. if(BUILD_SHARED_LIBS AND MSVC)
  44. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  45. endif()
  46. if(NOT BUILD_SHARED_LIBS AND MSVC)
  47. # see https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html
  48. # https://stackoverflow.com/questions/14172856/compile-with-mt-instead-of-md-using-cmake
  49. if(MSVC)
  50. add_compile_options(
  51. $<$<CONFIG:>:/MT> #---------|
  52. $<$<CONFIG:Debug>:/MTd> #---|-- Statically link the runtime libraries
  53. $<$<CONFIG:Release>:/MT> #--|
  54. )
  55. endif()
  56. endif()
  57. message(STATUS "CMAKE_EXPORT_COMPILE_COMMANDS: ${CMAKE_EXPORT_COMPILE_COMMANDS}")
  58. message(STATUS "BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}")
  59. message(STATUS "SHERPA_NCNN_ENABLE_PYTHON ${SHERPA_NCNN_ENABLE_PYTHON}")
  60. message(STATUS "SHERPA_NCNN_ENABLE_PORTAUDIO ${SHERPA_NCNN_ENABLE_PORTAUDIO}")
  61. message(STATUS "SHERPA_NCNN_ENABLE_JNI ${SHERPA_NCNN_ENABLE_JNI}")
  62. message(STATUS "SHERPA_NCNN_ENABLE_BINARY ${SHERPA_NCNN_ENABLE_BINARY}")
  63. message(STATUS "SHERPA_NCNN_ENABLE_TEST ${SHERPA_NCNN_ENABLE_TEST}")
  64. message(STATUS "SHERPA_NCNN_ENABLE_C_API ${SHERPA_NCNN_ENABLE_C_API}")
  65. message(STATUS "SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE ${SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE}")
  66. message(STATUS "SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES ${SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES}")
  67. if(NOT CMAKE_BUILD_TYPE)
  68. message(STATUS "No CMAKE_BUILD_TYPE given, default to Release")
  69. set(CMAKE_BUILD_TYPE Release)
  70. endif()
  71. message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
  72. # Set the release compiler flags to support debugging, see https://github.com/k2-fsa/sherpa-ncnn/issues/147
  73. if(SHERPA_NCNN_ENABLE_DEBUG_FOR_RELEASE)
  74. message(STATUS "Enable debugging for Release")
  75. string(APPEND CMAKE_CXX_FLAGS_RELEASE " -g -O0")
  76. endif()
  77. set(CMAKE_CXX_STANDARD 14 CACHE STRING "The C++ version to be used.")
  78. set(CMAKE_CXX_EXTENSIONS OFF)
  79. include(CheckIncludeFileCXX)
  80. check_include_file_cxx(alsa/asoundlib.h SHERPA_NCNN_HAS_ALSA)
  81. if(SHERPA_NCNN_HAS_ALSA)
  82. add_definitions(-DSHERPA_NCNN_ENABLE_ALSA=1)
  83. elseif(UNIX AND NOT APPLE)
  84. message(WARNING "\
  85. Could not find alsa/asoundlib.h !
  86. We won't build sherpa-ncnn-alsa
  87. To fix that, please do:
  88. (1) sudo apt-get install alsa-utils libasound2-dev
  89. (2) rm -rf build
  90. (3) re-try
  91. ")
  92. endif()
  93. list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
  94. if(WIN32)
  95. add_definitions(-DNOMINMAX) # Otherwise, std::max() and std::min() won't work
  96. endif()
  97. if(WIN32 AND MSVC)
  98. # disable various warnings for MSVC
  99. # 4244: 'return': conversion from 'unsigned __int64' to 'int', possible loss of data
  100. # 4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
  101. # 4305: 'argument': truncation from 'double' to 'const float'
  102. # 4334: '<<': result of 32-bit shift implicitly converted to 64 bits
  103. # 4800: 'int': forcing value to bool 'true' or 'false'
  104. # 4996: 'fopen': This function or variable may be unsafe
  105. set(disabled_warnings
  106. /wd4244
  107. /wd4267
  108. /wd4305
  109. /wd4334
  110. /wd4800
  111. /wd4996
  112. )
  113. message(STATUS "Disabled warnings: ${disabled_warnings}")
  114. foreach(w IN LISTS disabled_warnings)
  115. string(APPEND CMAKE_CXX_FLAGS " ${w} ")
  116. endforeach()
  117. endif()
  118. include(kaldi-native-fbank)
  119. include(ncnn)
  120. if(SHERPA_NCNN_ENABLE_PORTAUDIO)
  121. include(portaudio)
  122. endif()
  123. if(SHERPA_NCNN_ENABLE_PYTHON)
  124. include(pybind11)
  125. endif()
  126. add_subdirectory(sherpa-ncnn)
  127. if(SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES)
  128. add_subdirectory(ffmpeg-examples)
  129. endif()
  130. if(SHERPA_NCNN_ENABLE_C_API AND SHERPA_NCNN_ENABLE_BINARY)
  131. add_subdirectory(c-api-examples)
  132. endif()
  133. set(SHERPA_NCNN_PKG_CONFIG_EXTRA_LIBS)
  134. if(NOT BUILD_SHARED_LIBS)
  135. if(APPLE)
  136. set(SHERPA_NCNN_PKG_CONFIG_EXTRA_LIBS "-lomp -lc++")
  137. endif()
  138. if(UNIX AND NOT APPLE)
  139. set(SHERPA_NCNN_PKG_CONFIG_EXTRA_LIBS "-lm -lstdc++ -fopenmp")
  140. endif()
  141. endif()
  142. # See https://people.freedesktop.org/~dbn/pkg-config-guide.html
  143. configure_file(cmake/sherpa-ncnn.pc.in ${PROJECT_BINARY_DIR}/sherpa-ncnn.pc @ONLY)
  144. install(
  145. FILES
  146. ${PROJECT_BINARY_DIR}/sherpa-ncnn.pc
  147. DESTINATION
  148. .
  149. )