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

@@ -155,7 +155,18 @@ endif()
if(SHERPA_ONNX_ENABLE_CHECK)
list(APPEND sources log.cc)
endif()
add_library(sherpa-onnx-core ${sources})
# Always static build
add_library(sherpa-onnx-core STATIC ${sources})
set_target_properties(
sherpa-onnx-core
PROPERTIES
POSITION_INDEPENDENT_CODE ON
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
if(APPLE)
target_compile_options(sherpa-onnx-core PRIVATE
-Wno-deprecated-declarations
@@ -213,6 +224,7 @@ if(SHERPA_ONNX_ENABLE_CHECK)
endif()
if(NOT BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL Linux)
# This is for linux arm32 and arm64
target_link_libraries(sherpa-onnx-core -ldl)
endif()
@@ -264,16 +276,10 @@ if(SHERPA_ONNX_ENABLE_BINARY)
endif()
endif()
if(SHERPA_ONNX_ENABLE_PYTHON AND WIN32)
install(TARGETS sherpa-onnx-core DESTINATION ..)
else()
if(NOT BUILD_SHARED_LIBS)
install(TARGETS sherpa-onnx-core DESTINATION lib)
endif()
if(WIN32 AND BUILD_SHARED_LIBS)
install(TARGETS sherpa-onnx-core DESTINATION bin)
endif()
if(SHERPA_ONNX_ENABLE_BINARY)
install(
TARGETS
@@ -311,6 +317,9 @@ if(SHERPA_ONNX_HAS_ALSA AND SHERPA_ONNX_ENABLE_BINARY)
)
endif()
# # To fix the following error for Windows when building exe
# # mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_Dynamic Release'
foreach(exe IN LISTS exes)
target_link_libraries(${exe} sherpa-onnx-core)
endforeach()
@@ -386,12 +395,6 @@ if(SHERPA_ONNX_ENABLE_PORTAUDIO AND SHERPA_ONNX_ENABLE_BINARY)
microphone.cc
)
if(BUILD_SHARED_LIBS)
set(PA_LIB portaudio)
else()
set(PA_LIB portaudio_static)
endif()
set(exes
sherpa-onnx-microphone
sherpa-onnx-keyword-spotter-microphone
@@ -408,7 +411,7 @@ if(SHERPA_ONNX_ENABLE_PORTAUDIO AND SHERPA_ONNX_ENABLE_BINARY)
endif()
foreach(exe IN LISTS exes)
target_link_libraries(${exe} ${PA_LIB} sherpa-onnx-core)
target_link_libraries(${exe} portaudio_static sherpa-onnx-core)
endforeach()
if(NOT WIN32)
@@ -473,7 +476,7 @@ if(SHERPA_ONNX_ENABLE_WEBSOCKET AND SHERPA_ONNX_ENABLE_BINARY)
target_link_libraries(sherpa-onnx-offline-websocket-server "-Wl,-rpath,${SHERPA_ONNX_RPATH_ORIGIN}/../lib")
target_link_libraries(sherpa-onnx-offline-websocket-server "-Wl,-rpath,${SHERPA_ONNX_RPATH_ORIGIN}/../../../sherpa_onnx/lib")
if(SHERPA_ONNX_ENABLE_PYTHON)
if(SHERPA_ONNX_ENABLE_PYTHON AND NOT WIN32)
target_link_libraries(sherpa-onnx-online-websocket-server "-Wl,-rpath,${SHERPA_ONNX_RPATH_ORIGIN}/../lib/python${PYTHON_VERSION}/site-packages/sherpa_onnx/lib")
target_link_libraries(sherpa-onnx-online-websocket-client "-Wl,-rpath,${SHERPA_ONNX_RPATH_ORIGIN}/../lib/python${PYTHON_VERSION}/site-packages/sherpa_onnx/lib")
target_link_libraries(sherpa-onnx-offline-websocket-server "-Wl,-rpath,${SHERPA_ONNX_RPATH_ORIGIN}/../lib/python${PYTHON_VERSION}/site-packages/sherpa_onnx/lib")
@@ -550,4 +553,3 @@ add_custom_target(
DEPENDS ${sources})
add_custom_target(check DEPENDS clang-tidy-check)

View File

@@ -35,5 +35,8 @@ endif()
add_library(sherpa-onnx-jni ${sources})
target_compile_definitions(sherpa-onnx-jni PRIVATE SHERPA_ONNX_BUILD_SHARED_LIBS=1)
target_compile_definitions(sherpa-onnx-jni PRIVATE SHERPA_ONNX_BUILD_MAIN_LIB=1)
target_link_libraries(sherpa-onnx-jni sherpa-onnx-core)
install(TARGETS sherpa-onnx-jni DESTINATION lib)

View File

@@ -12,11 +12,31 @@
#include "android/asset_manager_jni.h"
#endif
#if defined(_WIN32)
#if defined(SHERPA_ONNX_BUILD_SHARED_LIBS)
#define SHERPA_ONNX_EXPORT __declspec(dllexport)
#define SHERPA_ONNX_IMPORT __declspec(dllimport)
#else
#define SHERPA_ONNX_EXPORT
#define SHERPA_ONNX_IMPORT
#endif
#else // WIN32
#define SHERPA_ONNX_EXPORT __attribute__((visibility("default")))
#define SHERPA_ONNX_IMPORT SHERPA_ONNX_EXPORT
#endif // WIN32
#if defined(SHERPA_ONNX_BUILD_MAIN_LIB)
#define SHERPA_ONNX_API SHERPA_ONNX_EXPORT
#else
#define SHERPA_ONNX_API SHERPA_ONNX_IMPORT
#endif
// If you use ndk, you can find "jni.h" inside
// android-ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
#include "jni.h" // NOLINT
#define SHERPA_ONNX_EXTERN_C extern "C"
#define SHERPA_ONNX_EXTERN_C extern "C" SHERPA_ONNX_API
// defined in jni.cc
jobject NewInteger(JNIEnv *env, int32_t value);