Skip to content

Commit

Permalink
use shared lib for android x86
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Dec 14, 2024
1 parent 4dd72c3 commit f52f9b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 54 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/android-static.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ jobs:
export ANDROID_NDK=$ANDROID_NDK_LATEST_HOME
./build-android-x86.sh
mkdir -p ./jniLibs/x86
cp -v ./build-android-x86-static/install/lib/*.so ./jniLibs/x86
rm -rf ./build-android-x86-static
cp -v ./build-android-x86/install/lib/*.so ./jniLibs/x86
rm -rf ./build-android-x86
- name: Copy files
shell: bash
Expand Down
69 changes: 17 additions & 52 deletions build-android-x86.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
#!/usr/bin/env bash
set -ex

# If BUILD_SHARED_LIBS is ON, we use libonnxruntime.so
# If BUILD_SHARED_LIBS is OFF, we use libonnxruntime.a
#
# In any case, we will have libsherpa-onnx-jni.so
#
# If BUILD_SHARED_LIBS is OFF, then libonnxruntime.a is linked into libsherpa-onnx-jni.so
# and you only need to copy libsherpa-onnx-jni.so to your Android projects.
#
# If BUILD_SHARED_LIBS is ON, then you need to copy both libsherpa-onnx-jni.so
# and libonnxruntime.so to your Android projects
#
if [ -z $BUILD_SHARED_LIBS ]; then
BUILD_SHARED_LIBS=ON
if [ x$BUILD_SHARED_LIBS == xOFF ]; then
echo "BUILD_SHARED_LIBS=OFF is ignored for Android x86."
echo "Always link with libonnxruntime.so"
sleep 2
fi

if [ $BUILD_SHARED_LIBS == ON ]; then
dir=$PWD/build-android-x86
else
dir=$PWD/build-android-x86-static
fi
dir=$PWD/build-android-x86

mkdir -p $dir
cd $dir
Expand All @@ -40,9 +27,6 @@ cd $dir

if [ -z $ANDROID_NDK ]; then
ANDROID_NDK=/star-fj/fangjun/software/android-sdk/ndk/22.1.7171670
if [ $BUILD_SHARED_LIBS == OFF ]; then
ANDROID_NDK=/star-fj/fangjun/software/android-sdk/ndk/27.0.11718014
fi
# or use
# ANDROID_NDK=/star-fj/fangjun/software/android-ndk
#
Expand All @@ -54,10 +38,6 @@ if [ -z $ANDROID_NDK ]; then
# Tools -> SDK manager -> Android SDK
# and set "Android SDK location" to /Users/fangjun/software/my-android
ANDROID_NDK=/Users/fangjun/software/my-android/ndk/22.1.7171670

if [ $BUILD_SHARED_LIBS == OFF ]; then
ANDROID_NDK=/Users/fangjun/software/my-android/ndk/27.0.11718014
fi
fi
fi

Expand All @@ -71,30 +51,18 @@ sleep 1

onnxruntime_version=1.17.1

if [ $BUILD_SHARED_LIBS == ON ]; then
if [ ! -f $onnxruntime_version/jni/x86/libonnxruntime.so ]; then
mkdir -p $onnxruntime_version
pushd $onnxruntime_version
wget -c -q https://github.com/csukuangfj/onnxruntime-libs/releases/download/v${onnxruntime_version}/onnxruntime-android-${onnxruntime_version}.zip
unzip onnxruntime-android-${onnxruntime_version}.zip
rm onnxruntime-android-${onnxruntime_version}.zip
popd
fi

export SHERPA_ONNXRUNTIME_LIB_DIR=$dir/$onnxruntime_version/jni/x86/
export SHERPA_ONNXRUNTIME_INCLUDE_DIR=$dir/$onnxruntime_version/headers/
else
if [ ! -f ${onnxruntime_version}-static/lib/libonnxruntime.a ]; then
wget -c -q https://github.com/csukuangfj/onnxruntime-libs/releases/download/v${onnxruntime_version}/onnxruntime-android-x86-static_lib-${onnxruntime_version}.zip
unzip onnxruntime-android-x86-static_lib-${onnxruntime_version}.zip
rm onnxruntime-android-x86-static_lib-${onnxruntime_version}.zip
mv onnxruntime-android-x86-static_lib-${onnxruntime_version} ${onnxruntime_version}-static
fi

export SHERPA_ONNXRUNTIME_LIB_DIR=$dir/$onnxruntime_version-static/lib/
export SHERPA_ONNXRUNTIME_INCLUDE_DIR=$dir/$onnxruntime_version-static/include/
if [ ! -f $onnxruntime_version/jni/x86/libonnxruntime.so ]; then
mkdir -p $onnxruntime_version
pushd $onnxruntime_version
wget -c -q https://github.com/csukuangfj/onnxruntime-libs/releases/download/v${onnxruntime_version}/onnxruntime-android-${onnxruntime_version}.zip
unzip onnxruntime-android-${onnxruntime_version}.zip
rm onnxruntime-android-${onnxruntime_version}.zip
popd
fi

export SHERPA_ONNXRUNTIME_LIB_DIR=$dir/$onnxruntime_version/jni/x86/
export SHERPA_ONNXRUNTIME_INCLUDE_DIR=$dir/$onnxruntime_version/headers/

echo "SHERPA_ONNXRUNTIME_LIB_DIR: $SHERPA_ONNXRUNTIME_LIB_DIR"
echo "SHERPA_ONNXRUNTIME_INCLUDE_DIR $SHERPA_ONNXRUNTIME_INCLUDE_DIR"

Expand Down Expand Up @@ -127,13 +95,12 @@ cmake -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake"
-DBUILD_ESPEAK_NG_EXE=OFF \
-DBUILD_ESPEAK_NG_TESTS=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=$BUILD_SHARED_LIBS \
-DBUILD_SHARED_LIBS=ON \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=$SHERPA_ONNX_ENABLE_JNI \
-DSHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY=OFF \
-DCMAKE_INSTALL_PREFIX=./install \
-DANDROID_ABI="x86" \
-DSHERPA_ONNX_ENABLE_C_API=$SHERPA_ONNX_ENABLE_C_API \
Expand All @@ -143,7 +110,5 @@ cmake -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake"
# make VERBOSE=1 -j4
make -j4
make install/strip
cp -fv $onnxruntime_version/jni/x86/libonnxruntime.so install/lib 2>/dev/null || true
rm -rf install/share
cp -fv $onnxruntime_version/jni/x86/libonnxruntime.so install/lib
rm -rf install/lib/pkgconfig
rm -rf install/lib/lib*.a

0 comments on commit f52f9b7

Please sign in to comment.