CMakeLists.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
  2. project(sherpa-ncnn)
  3. set(SHERPA_NCNN_VERSION "1.8.1")
  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. option(BUILD_SHARED_LIBS "Whether to build shared libraries" OFF)
  26. option(SHERPA_NCNN_ENABLE_PYTHON "Whether to build Python" OFF)
  27. option(SHERPA_NCNN_ENABLE_PORTAUDIO "Whether to build with portaudio" ON)
  28. option(SHERPA_NCNN_ENABLE_JNI "Whether to build JNI internface" OFF)
  29. option(SHERPA_NCNN_ENABLE_BINARY "Whether to build the binary sherpa-ncnn" ON)
  30. option(SHERPA_NCNN_ENABLE_TEST "Whether to build tests" OFF)
  31. option(SHERPA_NCNN_ENABLE_C_API "Whether to build C API" ON)
  32. option(SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE "Whether to generate-int8-scale-table" ON)
  33. option(SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES "Whether to enable ffmpeg-examples" OFF)
  34. if(DEFINED ANDROID_ABI)
  35. message(STATUS "Set SHERPA_NCNN_ENABLE_JNI to ON for Android")
  36. set(SHERPA_NCNN_ENABLE_JNI ON CACHE BOOL "" FORCE)
  37. endif()
  38. # See
  39. # https://stackoverflow.com/questions/33062728/cmake-link-shared-library-on-windows
  40. if(BUILD_SHARED_LIBS AND MSVC)
  41. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  42. endif()
  43. message(STATUS "BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}")
  44. message(STATUS "SHERPA_NCNN_ENABLE_PYTHON ${SHERPA_NCNN_ENABLE_PYTHON}")
  45. message(STATUS "SHERPA_NCNN_ENABLE_PORTAUDIO ${SHERPA_NCNN_ENABLE_PORTAUDIO}")
  46. message(STATUS "SHERPA_NCNN_ENABLE_JNI ${SHERPA_NCNN_ENABLE_JNI}")
  47. message(STATUS "SHERPA_NCNN_ENABLE_BINARY ${SHERPA_NCNN_ENABLE_BINARY}")
  48. message(STATUS "SHERPA_NCNN_ENABLE_TEST ${SHERPA_NCNN_ENABLE_TEST}")
  49. message(STATUS "SHERPA_NCNN_ENABLE_C_API ${SHERPA_NCNN_ENABLE_C_API}")
  50. message(STATUS "SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE ${SHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE}")
  51. message(STATUS "SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES ${SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES}")
  52. if(NOT CMAKE_BUILD_TYPE)
  53. message(STATUS "No CMAKE_BUILD_TYPE given, default to Release")
  54. set(CMAKE_BUILD_TYPE Release)
  55. endif()
  56. message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
  57. # Set the release compiler flags to support debugging, see https://github.com/k2-fsa/sherpa-ncnn/issues/147
  58. if(SHERPA_NCNN_ENABLE_DEBUG_FOR_RELEASE)
  59. message(STATUS "Enable debugging for Release")
  60. string(APPEND CMAKE_CXX_FLAGS_RELEASE " -g -O0")
  61. endif()
  62. set(CMAKE_CXX_STANDARD 14 CACHE STRING "The C++ version to be used.")
  63. set(CMAKE_CXX_EXTENSIONS OFF)
  64. include(CheckIncludeFileCXX)
  65. check_include_file_cxx(alsa/asoundlib.h SHERPA_NCNN_HAS_ALSA)
  66. if(SHERPA_NCNN_HAS_ALSA)
  67. add_definitions(-DSHERPA_NCNN_ENABLE_ALSA=1)
  68. endif()
  69. list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
  70. if(WIN32)
  71. add_definitions(-DNOMINMAX) # Otherwise, std::max() and std::min() won't work
  72. endif()
  73. if(WIN32 AND MSVC)
  74. # disable various warnings for MSVC
  75. # 4244: 'return': conversion from 'unsigned __int64' to 'int', possible loss of data
  76. # 4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
  77. # 4305: 'argument': truncation from 'double' to 'const float'
  78. # 4334: '<<': result of 32-bit shift implicitly converted to 64 bits
  79. # 4800: 'int': forcing value to bool 'true' or 'false'
  80. # 4996: 'fopen': This function or variable may be unsafe
  81. set(disabled_warnings
  82. /wd4244
  83. /wd4267
  84. /wd4305
  85. /wd4334
  86. /wd4800
  87. /wd4996
  88. )
  89. message(STATUS "Disabled warnings: ${disabled_warnings}")
  90. foreach(w IN LISTS disabled_warnings)
  91. string(APPEND CMAKE_CXX_FLAGS " ${w} ")
  92. endforeach()
  93. endif()
  94. include(kaldi-native-fbank)
  95. include(ncnn)
  96. if(SHERPA_NCNN_ENABLE_PORTAUDIO)
  97. include(portaudio)
  98. endif()
  99. if(SHERPA_NCNN_ENABLE_PYTHON)
  100. include(pybind11)
  101. endif()
  102. add_subdirectory(sherpa-ncnn)
  103. if(SHERPA_NCNN_ENABLE_FFMPEG_EXAMPLES)
  104. add_subdirectory(ffmpeg-examples)
  105. endif()
  106. if(SHERPA_NCNN_ENABLE_C_API AND SHERPA_NCNN_ENABLE_BINARY)
  107. add_subdirectory(c-api-examples)
  108. endif()