继续操作前请注册或者登录。
niansa/tuxifan
Committed by GitHub

Allow building repository as CMake subdirectory (#2059)

* Use PROJECT_SOURCE_DIR rather than CMAKE_SOURCE_DIR to allow building as subdirectory

* Also use PROJECT_SOURCE_DIR instead of CMAKE_SOURCE_DIR in c/cxx api examples

* Only build examples by default when not building as subdirectory

* Do not suggest building binaries either

---------

Co-authored-by: user <user@mail.tld>
@@ -20,6 +20,13 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0") @@ -20,6 +20,13 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
20 cmake_policy(SET CMP0135 NEW) 20 cmake_policy(SET CMP0135 NEW)
21 endif() 21 endif()
22 22
  23 +
  24 +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  25 + set(SUGGEST_BUILD_BINARIES ON)
  26 +else()
  27 + set(SUGGEST_BUILD_BINARIES OFF)
  28 +endif()
  29 +
23 option(SHERPA_ONNX_ENABLE_PYTHON "Whether to build Python" OFF) 30 option(SHERPA_ONNX_ENABLE_PYTHON "Whether to build Python" OFF)
24 option(SHERPA_ONNX_ENABLE_TESTS "Whether to build tests" OFF) 31 option(SHERPA_ONNX_ENABLE_TESTS "Whether to build tests" OFF)
25 option(SHERPA_ONNX_ENABLE_CHECK "Whether to build with assert" OFF) 32 option(SHERPA_ONNX_ENABLE_CHECK "Whether to build with assert" OFF)
@@ -39,13 +46,13 @@ option(SHERPA_ONNX_ENABLE_WASM_VAD "Whether to enable WASM for VAD" OFF) @@ -39,13 +46,13 @@ option(SHERPA_ONNX_ENABLE_WASM_VAD "Whether to enable WASM for VAD" OFF)
39 option(SHERPA_ONNX_ENABLE_WASM_VAD_ASR "Whether to enable WASM for VAD+ASR" OFF) 46 option(SHERPA_ONNX_ENABLE_WASM_VAD_ASR "Whether to enable WASM for VAD+ASR" OFF)
40 option(SHERPA_ONNX_ENABLE_WASM_NODEJS "Whether to enable WASM for NodeJS" OFF) 47 option(SHERPA_ONNX_ENABLE_WASM_NODEJS "Whether to enable WASM for NodeJS" OFF)
41 option(SHERPA_ONNX_ENABLE_WASM_SPEECH_ENHANCEMENT "Whether to enable WASM for speech enhancement" OFF) 48 option(SHERPA_ONNX_ENABLE_WASM_SPEECH_ENHANCEMENT "Whether to enable WASM for speech enhancement" OFF)
42 -option(SHERPA_ONNX_ENABLE_BINARY "Whether to build binaries" ON) 49 +option(SHERPA_ONNX_ENABLE_BINARY "Whether to build binaries" ${SUGGEST_BUILD_BINARIES})
43 option(SHERPA_ONNX_ENABLE_TTS "Whether to build TTS related code" ON) 50 option(SHERPA_ONNX_ENABLE_TTS "Whether to build TTS related code" ON)
44 option(SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION "Whether to build speaker diarization related code" ON) 51 option(SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION "Whether to build speaker diarization related code" ON)
45 option(SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY "True to link libstdc++ statically. Used only when BUILD_SHARED_LIBS is OFF on Linux" ON) 52 option(SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY "True to link libstdc++ statically. Used only when BUILD_SHARED_LIBS is OFF on Linux" ON)
46 option(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE "True to use pre-installed onnxruntime if available" ON) 53 option(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE "True to use pre-installed onnxruntime if available" ON)
47 option(SHERPA_ONNX_ENABLE_SANITIZER "Whether to enable ubsan and asan" OFF) 54 option(SHERPA_ONNX_ENABLE_SANITIZER "Whether to enable ubsan and asan" OFF)
48 -option(SHERPA_ONNX_BUILD_C_API_EXAMPLES "Whether to enable C API examples" ON) 55 +option(SHERPA_ONNX_BUILD_C_API_EXAMPLES "Whether to enable C API examples" ${SUGGEST_BUILD_BINARIES})
49 option(SHERPA_ONNX_ENABLE_RKNN "Whether to build for RKNN NPU " OFF) 56 option(SHERPA_ONNX_ENABLE_RKNN "Whether to build for RKNN NPU " OFF)
50 57
51 set(SHERPA_ONNX_LINUX_ARM64_GPU_ONNXRUNTIME_VERSION "1.11.0" CACHE STRING "Used only for Linux ARM64 GPU. Set to 1.11.0 if you use CUDA 10.2 and cudnn8. Set it to 1.16.0 if you use CUDA 11.4 and cudnn8. Set it to 1.18.0 if you use CUDA 12.2 and cudnn8. Set it to 1.18.1 if you use CUDA 12.6 and cudnn9") 58 set(SHERPA_ONNX_LINUX_ARM64_GPU_ONNXRUNTIME_VERSION "1.11.0" CACHE STRING "Used only for Linux ARM64 GPU. Set to 1.11.0 if you use CUDA 10.2 and cudnn8. Set it to 1.16.0 if you use CUDA 11.4 and cudnn8. Set it to 1.18.0 if you use CUDA 12.2 and cudnn8. Set it to 1.18.1 if you use CUDA 12.6 and cudnn9")
1 include(cargs) 1 include(cargs)
2 2
3 -include_directories(${CMAKE_SOURCE_DIR}) 3 +include_directories(${PROJECT_SOURCE_DIR})
4 add_executable(decode-file-c-api decode-file-c-api.c) 4 add_executable(decode-file-c-api decode-file-c-api.c)
5 target_link_libraries(decode-file-c-api sherpa-onnx-c-api cargs) 5 target_link_libraries(decode-file-c-api sherpa-onnx-c-api cargs)
6 6
1 -include_directories(${CMAKE_SOURCE_DIR}) 1 +include_directories(${PROJECT_SOURCE_DIR})
2 2
3 add_executable(streaming-zipformer-cxx-api ./streaming-zipformer-cxx-api.cc) 3 add_executable(streaming-zipformer-cxx-api ./streaming-zipformer-cxx-api.cc)
4 target_link_libraries(streaming-zipformer-cxx-api sherpa-onnx-cxx-api) 4 target_link_libraries(streaming-zipformer-cxx-api sherpa-onnx-cxx-api)
1 -include_directories(${CMAKE_SOURCE_DIR}) 1 +include_directories(${PROJECT_SOURCE_DIR})
2 add_library(sherpa-onnx-c-api c-api.cc) 2 add_library(sherpa-onnx-c-api c-api.cc)
3 target_link_libraries(sherpa-onnx-c-api sherpa-onnx-core) 3 target_link_libraries(sherpa-onnx-c-api sherpa-onnx-core)
  4 +target_include_directories(sherpa-onnx-c-api PUBLIC ${PROJECT_SOURCE_DIR})
4 5
5 if(BUILD_SHARED_LIBS) 6 if(BUILD_SHARED_LIBS)
6 target_compile_definitions(sherpa-onnx-c-api PUBLIC SHERPA_ONNX_BUILD_SHARED_LIBS=1) 7 target_compile_definitions(sherpa-onnx-c-api PUBLIC SHERPA_ONNX_BUILD_SHARED_LIBS=1)
@@ -9,6 +10,7 @@ endif() @@ -9,6 +10,7 @@ endif()
9 10
10 add_library(sherpa-onnx-cxx-api cxx-api.cc) 11 add_library(sherpa-onnx-cxx-api cxx-api.cc)
11 target_link_libraries(sherpa-onnx-cxx-api sherpa-onnx-c-api) 12 target_link_libraries(sherpa-onnx-cxx-api sherpa-onnx-c-api)
  13 +target_include_directories(sherpa-onnx-cxx-api PUBLIC ${PROJECT_SOURCE_DIR})
12 14
13 install( 15 install(
14 TARGETS 16 TARGETS
1 -include_directories(${CMAKE_SOURCE_DIR}) 1 +include_directories(${PROJECT_SOURCE_DIR})
2 2
3 if(SHERPA_ONNX_ENABLE_PYTHON) 3 if(SHERPA_ONNX_ENABLE_PYTHON)
4 message(STATUS "PYTHON_EXECUTABLE: ${PYTHON_EXECUTABLE}") 4 message(STATUS "PYTHON_EXECUTABLE: ${PYTHON_EXECUTABLE}")
@@ -648,7 +648,7 @@ endforeach() @@ -648,7 +648,7 @@ endforeach()
648 # For clang-tidy 648 # For clang-tidy
649 add_custom_target( 649 add_custom_target(
650 clang-tidy-check 650 clang-tidy-check
651 - clang-tidy -p ${CMAKE_BINARY_DIR}/compile_commands.json --config-file ${CMAKE_SOURCE_DIR}/.clang-tidy ${srcs_to_check} 651 + clang-tidy -p ${CMAKE_BINARY_DIR}/compile_commands.json --config-file ${PROJECT_SOURCE_DIR}/.clang-tidy ${srcs_to_check}
652 DEPENDS ${sources}) 652 DEPENDS ${sources})
653 653
654 add_custom_target(check DEPENDS clang-tidy-check) 654 add_custom_target(check DEPENDS clang-tidy-check)
1 -include_directories(${CMAKE_SOURCE_DIR}) 1 +include_directories(${PROJECT_SOURCE_DIR})
2 2
3 if(NOT DEFINED ANDROID_ABI) 3 if(NOT DEFINED ANDROID_ABI)
4 if(NOT DEFINED ENV{JAVA_HOME}) 4 if(NOT DEFINED ENV{JAVA_HOME})
1 -include_directories(${CMAKE_SOURCE_DIR}) 1 +include_directories(${PROJECT_SOURCE_DIR})
2 2
3 set(srcs 3 set(srcs
4 audio-tagging.cc 4 audio-tagging.cc
@@ -51,7 +51,7 @@ set(srcs @@ -51,7 +51,7 @@ set(srcs
51 wave-writer.cc 51 wave-writer.cc
52 ) 52 )
53 if(SHERPA_ONNX_HAS_ALSA) 53 if(SHERPA_ONNX_HAS_ALSA)
54 - list(APPEND srcs ${CMAKE_SOURCE_DIR}/sherpa-onnx/csrc/alsa.cc alsa.cc) 54 + list(APPEND srcs ${PROJECT_SOURCE_DIR}/sherpa-onnx/csrc/alsa.cc alsa.cc)
55 else() 55 else()
56 list(APPEND srcs faked-alsa.cc) 56 list(APPEND srcs faked-alsa.cc)
57 endif() 57 endif()