Build sherpa-onnx as a single shared library (#1078)

When `-D BUILD_SHARED_LIBS=ON` is passed to `cmake`, it builds a single shared library.

Specifically, 

- For C APIs, it builds `libsherpa-onnx-c-api.so`
- For Python APIs, it builds `_sherpa_onnx.cpython-xx-xx.so`
- For Kotlin and Java APIs, it builds `libsherpa-onnx-jni.so`

There is no `libsherpa-onnx-core.so` any longer.

Note it affects only shared libraries.
This commit is contained in:
Fangjun Kuang
2024-07-06 16:41:54 +08:00
committed by GitHub
parent 55decb7bee
commit a25075101c
69 changed files with 571 additions and 324 deletions

View File

@@ -76,19 +76,8 @@ def get_binaries():
if is_windows():
binaries += [
"espeak-ng.dll",
"kaldi-decoder-core.dll",
"kaldi-native-fbank-core.dll",
"onnxruntime.dll",
"ssentencepiece_core.dll",
"piper_phonemize.dll",
"sherpa-onnx-c-api.dll",
"sherpa-onnx-core.dll",
"sherpa-onnx-fstfar.dll",
"sherpa-onnx-fst.dll",
"sherpa-onnx-kaldifst-core.dll",
"sherpa-onnx-portaudio.dll",
"ucd.dll",
]
return binaries
@@ -145,6 +134,7 @@ class BuildExtension(build_ext):
extra_cmake_args += " -DBUILD_PIPER_PHONMIZE_TESTS=OFF "
extra_cmake_args += " -DBUILD_ESPEAK_NG_EXE=OFF "
extra_cmake_args += " -DBUILD_ESPEAK_NG_TESTS=OFF "
extra_cmake_args += " -DSHERPA_ONNX_ENABLE_C_API=ON "
extra_cmake_args += " -DSHERPA_ONNX_BUILD_C_API_EXAMPLES=OFF "
extra_cmake_args += " -DSHERPA_ONNX_ENABLE_CHECK=OFF "
@@ -161,7 +151,7 @@ class BuildExtension(build_ext):
if is_windows():
build_cmd = f"""
cmake {cmake_args} -B {self.build_temp} -S {sherpa_onnx_dir}
cmake --build {self.build_temp} --target install --config Release -- -m
cmake --build {self.build_temp} --target install --config Release -- -m:2
"""
print(f"build command is:\n{build_cmd}")
ret = os.system(
@@ -171,7 +161,7 @@ class BuildExtension(build_ext):
raise Exception("Failed to configure sherpa")
ret = os.system(
f"cmake --build {self.build_temp} --target install --config Release -- -m" # noqa
f"cmake --build {self.build_temp} --target install --config Release -- -m:2" # noqa
)
if ret != 0:
raise Exception("Failed to build and install sherpa")
@@ -226,5 +216,11 @@ class BuildExtension(build_ext):
shutil.rmtree(f"{install_dir}/share")
shutil.rmtree(f"{install_dir}/lib/pkgconfig")
if is_linux():
os.remove(f"{install_dir}/lib/libonnxruntime.so")
if is_macos():
os.remove(f"{install_dir}/lib/libonnxruntime.dylib")
if is_windows():
shutil.rmtree(f"{install_dir}/lib")

View File

@@ -1,9 +1,9 @@
function(download_espeak_ng_for_piper)
include(FetchContent)
set(espeak_ng_URL "https://github.com/csukuangfj/espeak-ng/archive/69bf6927964fb042aeb827cfdf6082a30f5802eb.zip")
set(espeak_ng_URL2 "https://hub.nuaa.cf/csukuangfj/espeak-ng/archive/69bf6927964fb042aeb827cfdf6082a30f5802eb.zip")
set(espeak_ng_HASH "SHA256=745e35b21ece6804b4a1839722f9e625ac909380c8f85873ad71bf145877075a")
set(espeak_ng_URL "https://github.com/csukuangfj/espeak-ng/archive/f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip")
set(espeak_ng_URL2 "https://hub.nuaa.cf/csukuangfj/espeak-ng/archive/f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip")
set(espeak_ng_HASH "SHA256=70cbf4050e7a014aae19140b05e57249da4720f56128459fbe3a93beaf971ae6")
set(BUILD_ESPEAK_NG_TESTS OFF CACHE BOOL "" FORCE)
set(USE_ASYNC OFF CACHE BOOL "" FORCE)
@@ -21,11 +21,11 @@ function(download_espeak_ng_for_piper)
# If you don't have access to the Internet,
# please pre-download kaldi-decoder
set(possible_file_locations
$ENV{HOME}/Downloads/espeak-ng-69bf6927964fb042aeb827cfdf6082a30f5802eb.zip
${CMAKE_SOURCE_DIR}/espeak-ng-69bf6927964fb042aeb827cfdf6082a30f5802eb.zip
${CMAKE_BINARY_DIR}/espeak-ng-69bf6927964fb042aeb827cfdf6082a30f5802eb.zip
/tmp/espeak-ng-69bf6927964fb042aeb827cfdf6082a30f5802eb.zip
/star-fj/fangjun/download/github/espeak-ng-69bf6927964fb042aeb827cfdf6082a30f5802eb.zip
$ENV{HOME}/Downloads/espeak-ng-f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip
${CMAKE_SOURCE_DIR}/espeak-ng-f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip
${CMAKE_BINARY_DIR}/espeak-ng-f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip
/tmp/espeak-ng-f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip
/star-fj/fangjun/download/github/espeak-ng-f6fed6c58b5e0998b8e68c6610125e2d07d595a7.zip
)
foreach(f IN LISTS possible_file_locations)
@@ -53,7 +53,23 @@ function(download_espeak_ng_for_piper)
message(STATUS "espeak-ng is downloaded to ${espeak_ng_SOURCE_DIR}")
message(STATUS "espeak-ng binary dir is ${espeak_ng_BINARY_DIR}")
if(BUILD_SHARED_LIBS)
set(_build_shared_libs_bak ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
endif()
add_subdirectory(${espeak_ng_SOURCE_DIR} ${espeak_ng_BINARY_DIR})
if(_build_shared_libs_bak)
set_target_properties(espeak-ng
PROPERTIES
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
set(BUILD_SHARED_LIBS ON)
endif()
set(espeak_ng_SOURCE_DIR ${espeak_ng_SOURCE_DIR} PARENT_SCOPE)
if(WIN32 AND MSVC)
@@ -107,28 +123,12 @@ function(download_espeak_ng_for_piper)
${espeak_ng_SOURCE_DIR}/src/ucd-tools/src/include
)
if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32)
install(TARGETS
espeak-ng
ucd
DESTINATION ..)
else()
if(NOT BUILD_SHARED_LIBS)
install(TARGETS
espeak-ng
ucd
DESTINATION lib)
endif()
if(NOT BUILD_SHARED_LIBS)
install(TARGETS ucd DESTINATION lib)
endif()
if(WIN32 AND BUILD_SHARED_LIBS)
install(TARGETS
espeak-ng
ucd
DESTINATION bin)
endif()
endfunction()
download_espeak_ng_for_piper()

View File

@@ -45,8 +45,25 @@ function(download_kaldi_decoder)
message(STATUS "kaldi-decoder's binary dir is ${kaldi_decoder_BINARY_DIR}")
include_directories(${kaldi_decoder_SOURCE_DIR})
if(BUILD_SHARED_LIBS)
set(_build_shared_libs_bak ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
endif()
add_subdirectory(${kaldi_decoder_SOURCE_DIR} ${kaldi_decoder_BINARY_DIR} EXCLUDE_FROM_ALL)
if(_build_shared_libs_bak)
set_target_properties(
kaldi-decoder-core
PROPERTIES
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
set(BUILD_SHARED_LIBS ON)
endif()
if(WIN32 AND MSVC)
target_compile_options(kaldi-decoder-core PUBLIC
/wd4018
@@ -58,14 +75,7 @@ function(download_kaldi_decoder)
INTERFACE
${kaldi-decoder_SOURCE_DIR}/
)
if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32)
install(TARGETS
kaldi-decoder-core
kaldifst_core
fst
fstfar
DESTINATION ..)
else()
if(NOT BUILD_SHARED_LIBS)
install(TARGETS
kaldi-decoder-core
kaldifst_core
@@ -73,15 +83,6 @@ function(download_kaldi_decoder)
fstfar
DESTINATION lib)
endif()
if(WIN32 AND BUILD_SHARED_LIBS)
install(TARGETS
kaldi-decoder-core
kaldifst_core
fst
fstfar
DESTINATION bin)
endif()
endfunction()
download_kaldi_decoder()

View File

@@ -44,20 +44,30 @@ function(download_kaldi_native_fbank)
message(STATUS "kaldi-native-fbank is downloaded to ${kaldi_native_fbank_SOURCE_DIR}")
message(STATUS "kaldi-native-fbank's binary dir is ${kaldi_native_fbank_BINARY_DIR}")
if(BUILD_SHARED_LIBS)
set(_build_shared_libs_bak ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
endif()
add_subdirectory(${kaldi_native_fbank_SOURCE_DIR} ${kaldi_native_fbank_BINARY_DIR} EXCLUDE_FROM_ALL)
if(_build_shared_libs_bak)
set_target_properties(kaldi-native-fbank-core
PROPERTIES
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
set(BUILD_SHARED_LIBS ON)
endif()
target_include_directories(kaldi-native-fbank-core
INTERFACE
${kaldi_native_fbank_SOURCE_DIR}/
)
if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32)
install(TARGETS kaldi-native-fbank-core DESTINATION ..)
else()
install(TARGETS kaldi-native-fbank-core DESTINATION lib)
endif()
if(WIN32 AND BUILD_SHARED_LIBS)
install(TARGETS kaldi-native-fbank-core DESTINATION bin)
if(NOT BUILD_SHARED_LIBS)
install(TARGETS kaldi-native-fbank-core DESTINATION lib)
endif()
endfunction()

View File

@@ -43,8 +43,23 @@ function(download_kaldifst)
list(APPEND CMAKE_MODULE_PATH ${kaldifst_SOURCE_DIR}/cmake)
if(BUILD_SHARED_LIBS)
set(_build_shared_libs_bak ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
endif()
add_subdirectory(${kaldifst_SOURCE_DIR} ${kaldifst_BINARY_DIR} EXCLUDE_FROM_ALL)
if(_build_shared_libs_bak)
set_target_properties(kaldifst_core
PROPERTIES
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
set(BUILD_SHARED_LIBS ON)
endif()
target_include_directories(kaldifst_core
PUBLIC
${kaldifst_SOURCE_DIR}/

View File

@@ -67,7 +67,24 @@ function(download_openfst)
FetchContent_Populate(openfst)
endif()
message(STATUS "openfst is downloaded to ${openfst_SOURCE_DIR}")
if(_build_shared_libs_bak)
set(_build_shared_libs_bak ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
endif()
add_subdirectory(${openfst_SOURCE_DIR} ${openfst_BINARY_DIR} EXCLUDE_FROM_ALL)
if(_build_shared_libs_bak)
set_target_properties(fst fstfar
PROPERTIES
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
set(BUILD_SHARED_LIBS ON)
endif()
set(openfst_SOURCE_DIR ${openfst_SOURCE_DIR} PARENT_SCOPE)
set_target_properties(fst PROPERTIES OUTPUT_NAME "sherpa-onnx-fst")

View File

@@ -40,8 +40,23 @@ function(download_piper_phonemize)
message(STATUS "piper-phonemize is downloaded to ${piper_phonemize_SOURCE_DIR}")
message(STATUS "piper-phonemize binary dir is ${piper_phonemize_BINARY_DIR}")
if(BUILD_SHARED_LIBS)
set(_build_shared_libs_bak ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
endif()
add_subdirectory(${piper_phonemize_SOURCE_DIR} ${piper_phonemize_BINARY_DIR} EXCLUDE_FROM_ALL)
if(_build_shared_libs_bak)
set_target_properties(piper_phonemize
PROPERTIES
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
set(BUILD_SHARED_LIBS ON)
endif()
if(WIN32 AND MSVC)
target_compile_options(piper_phonemize PUBLIC
/wd4309
@@ -53,21 +68,11 @@ function(download_piper_phonemize)
${piper_phonemize_SOURCE_DIR}/src/include
)
if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32)
install(TARGETS
piper_phonemize
DESTINATION ..)
else()
if(NOT BUILD_SHARED_LIBS)
install(TARGETS
piper_phonemize
DESTINATION lib)
endif()
if(WIN32 AND BUILD_SHARED_LIBS)
install(TARGETS
piper_phonemize
DESTINATION bin)
endif()
endfunction()
download_piper_phonemize()

View File

@@ -26,13 +26,9 @@ function(download_portaudio)
endif()
endforeach()
if(BUILD_SHARED_LIBS)
set(PA_BUILD_SHARED ON CACHE BOOL "" FORCE)
set(PA_BUILD_STATIC OFF CACHE BOOL "" FORCE)
else()
set(PA_BUILD_SHARED OFF CACHE BOOL "" FORCE)
set(PA_BUILD_STATIC ON CACHE BOOL "" FORCE)
endif()
# Always use static build
set(PA_BUILD_SHARED OFF CACHE BOOL "" FORCE)
set(PA_BUILD_STATIC ON CACHE BOOL "" FORCE)
FetchContent_Declare(portaudio
URL
@@ -55,28 +51,17 @@ function(download_portaudio)
add_subdirectory(${portaudio_SOURCE_DIR} ${portaudio_BINARY_DIR} EXCLUDE_FROM_ALL)
if(BUILD_SHARED_LIBS)
set_target_properties(portaudio PROPERTIES OUTPUT_NAME "sherpa-onnx-portaudio")
if(NOT WIN32)
target_compile_options(portaudio PRIVATE "-Wno-deprecated-declarations")
endif()
if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32)
install(TARGETS portaudio DESTINATION ..)
else()
install(TARGETS portaudio DESTINATION lib)
endif()
else()
set_target_properties(portaudio_static PROPERTIES OUTPUT_NAME "sherpa-onnx-portaudio_static")
if(NOT WIN32)
target_compile_options(portaudio_static PRIVATE "-Wno-deprecated-declarations")
endif()
if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32)
install(TARGETS portaudio_static DESTINATION ..)
else()
install(TARGETS portaudio_static DESTINATION lib)
endif()
set_target_properties(portaudio_static PROPERTIES OUTPUT_NAME "sherpa-onnx-portaudio_static")
if(NOT WIN32)
target_compile_options(portaudio_static PRIVATE "-Wno-deprecated-declarations")
endif()
if(NOT BUILD_SHARED_LIBS AND SHERPA_ONNX_ENABLE_BINARY)
install(TARGETS
portaudio_static
DESTINATION lib)
endif()
endfunction()
download_portaudio()

View File

@@ -0,0 +1,25 @@
# Note: If you use Python, then the prefix might not be correct.
#
# You need to either manually modify this file to change the prefix to the location
# where this sherpa-onnx.pc file actually resides
# or
# you can use
#
# pkg-config --define-variable=prefix=/path/to/the/dir/containing/this/file --cflags sherpa-onnx
prefix="@CMAKE_INSTALL_PREFIX@"
exec_prefix="${prefix}"
includedir="${prefix}/include"
libdir="${exec_prefix}/lib"
Name: sherpa-onnx
Description: pkg-config for sherpa-onnx
URL: https://github.com/k2-fsa/sherpa-onnx
Version: @SHERPA_ONNX_VERSION@
Cflags: -I"${includedir}"
# Note: -lcargs is required only for the following file
# https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/decode-file-c-api.c
# We add it here so that users don't need to specify -lcargs when compiling decode-file-c-api.c
Libs: -L"${libdir}" -lsherpa-onnx-c-api -lonnxruntime -Wl,-rpath,${libdir} @SHERPA_ONNX_PKG_WITH_CARGS@ @SHERPA_ONNX_PKG_CONFIG_EXTRA_LIBS@

View File

@@ -1,3 +1,12 @@
# Note: If you use Python, then the prefix might not be correct.
#
# You need to either manually modify this file to change the prefix to the location
# where this sherpa-onnx.pc file actually resides
# or
# you can use
#
# pkg-config --define-variable=prefix=/path/to/the/dir/containing/this/file --cflags sherpa-onnx
prefix="@CMAKE_INSTALL_PREFIX@"
exec_prefix="${prefix}"
includedir="${prefix}/include"

View File

@@ -1,3 +1,12 @@
# Note: If you use Python, then the prefix might not be correct.
#
# You need to either manually modify this file to change the prefix to the location
# where this sherpa-onnx.pc file actually resides
# or
# you can use
#
# pkg-config --define-variable=prefix=/path/to/the/dir/containing/this/file --cflags sherpa-onnx
prefix="@CMAKE_INSTALL_PREFIX@"
exec_prefix="${prefix}"
includedir="${prefix}/include"

View File

@@ -42,22 +42,32 @@ function(download_simple_sentencepiece)
FetchContent_Populate(simple-sentencepiece)
endif()
message(STATUS "simple-sentencepiece is downloaded to ${simple-sentencepiece_SOURCE_DIR}")
if(BUILD_SHARED_LIBS)
set(_build_shared_libs_bak ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
endif()
add_subdirectory(${simple-sentencepiece_SOURCE_DIR} ${simple-sentencepiece_BINARY_DIR} EXCLUDE_FROM_ALL)
if(_build_shared_libs_bak)
set_target_properties(ssentencepiece_core
PROPERTIES
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
set(BUILD_SHARED_LIBS ON)
endif()
target_include_directories(ssentencepiece_core
PUBLIC
${simple-sentencepiece_SOURCE_DIR}/
)
if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32)
install(TARGETS ssentencepiece_core DESTINATION ..)
else()
if(NOT BUILD_SHARED_LIBS)
install(TARGETS ssentencepiece_core DESTINATION lib)
endif()
if(WIN32 AND BUILD_SHARED_LIBS)
install(TARGETS ssentencepiece_core DESTINATION bin)
endif()
endfunction()
download_simple_sentencepiece()