android NDK编译openblas和向量检索库faiss

设置android SDK和NDK路径

例如:

export SDK_PATH=/root/codes/my_sdk/sdk/
export NDK_PATH=/root/codes/my_sdk/sdk/ndk/24.0.8215888/
sdk和ndk下载方法参考:

tflite本地构建和调用_Luchang-Li的博客-CSDN博客_tensflow tflite model如何调用

设置好环境变量,把下面的build.sh放在代码目录直接执行即可,在build目录内部相应位置会生成so文件。

编译openblas

git clone --recursive -b v0.3.21 https://github.com/xianyi/OpenBLAS.git

可以在-b设置需要的版本。

build.sh

#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

mkdir ${SCRIPT_DIR}/build
cd ${SCRIPT_DIR}/build

# please set SDK_PATH, NDK_PATH, etc. by env
# require download cmake, etc. by sdkmanager "cmake;3.18.1" "ndk;24.0.8215888" --channel=0 --sdk_root=./sdk_path
# require ndk >= 24.0.8215888
# for example:
# export SDK_PATH=/root/codes/my_sdk/sdk/
# export NDK_PATH=/root/codes/my_sdk/sdk/ndk/24.0.8215888/

if [ ! -d ${SDK_PATH} ] ; then
    echo "ERROR: please set valid sdk path by env SDK_PATH"
    exit 1
fi
if [ ! -d ${SDK_PATH}/cmake/3.18.1/ ] ; then
    echo "ERROR: please download cmake 3.18.1 for sdk"
    exit 1
fi
if [ ! -d ${NDK_PATH} ] ; then
    echo "ERROR: please set valid ndk path by env NDK_PATH"
    exit 1
fi
if [ -z ${ANDROID_ABI} ] ; then
    ANDROID_ABI=arm64-v8a
fi
if [ -z ${MINSDKVERSION} ] ; then
    MINSDKVERSION=29
fi

echo "SDK_PATH:" $SDK_PATH
echo "NDK_PATH:" $NDK_PATH
echo "ANDROID_ABI:" $ANDROID_ABI
echo "MINSDKVERSION:" $MINSDKVERSION

    # -DBUILD_SHARED_LIBS=ON \

${SDK_PATH}/cmake/3.18.1/bin/cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_TOOLCHAIN_FILE=${NDK_PATH}/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=${ANDROID_ABI} \
    -DANDROID_NDK=${NDK_PATH} \
    -DANDROID_PLATFORM=android-${MINSDKVERSION} \
    -DCMAKE_ANDROID_ARCH_ABI=${ANDROID_ABI} \
    -DCMAKE_ANDROID_NDK=${NDK_PATH} \
    -DCMAKE_MAKE_PROGRAM=${SDK_PATH}/cmake/3.18.1/bin/ninja \
    -DCMAKE_SYSTEM_NAME=Android \
    -DCMAKE_SYSTEM_VERSION=${MINSDKVERSION} \
    -DANDROID_STL=c++_static \
    -DCMAKE_CXX_FLAGS_RELEASE="-s"  \
    -DBUILD_STATIC_LIBS=ON \
    -GNinja \
    ..
if [ $? -ne 0 ]; then
    echo "ERROR: cmake failed"
    exit 1
fi

${SDK_PATH}/cmake/3.18.1/bin/ninja
if [ $? -ne 0 ]; then
    echo "ERROR: build failed"
    exit 1
fi

编译faiss

​git clone --recursive -b v1.7.2 https://github.com/facebookresearch/faiss.git
#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

mkdir ${SCRIPT_DIR}/build
cd ${SCRIPT_DIR}/build

# please set SDK_PATH, NDK_PATH, etc. by env
# require download cmake, etc. by sdkmanager "cmake;3.18.1" "ndk;24.0.8215888" --channel=0 --sdk_root=./sdk_path
# require ndk >= 24.0.8215888
# for example:
# export SDK_PATH=/root/codes/my_sdk/sdk/
# export NDK_PATH=/root/codes/my_sdk/sdk/ndk/24.0.8215888/

if [ ! -d ${SDK_PATH} ] ; then
    echo "ERROR: please set valid sdk path by env SDK_PATH"
    exit 1
fi
if [ ! -d ${SDK_PATH}/cmake/3.18.1/ ] ; then
    echo "ERROR: please download cmake 3.18.1 for sdk"
    exit 1
fi
if [ ! -d ${NDK_PATH} ] ; then
    echo "ERROR: please set valid ndk path by env NDK_PATH"
    exit 1
fi
if [ -z ${ANDROID_ABI} ] ; then
    ANDROID_ABI=arm64-v8a
fi
if [ -z ${MINSDKVERSION} ] ; then
    MINSDKVERSION=29
fi

echo "SDK_PATH:" $SDK_PATH
echo "NDK_PATH:" $NDK_PATH
echo "ANDROID_ABI:" $ANDROID_ABI
echo "MINSDKVERSION:" $MINSDKVERSION

${SDK_PATH}/cmake/3.18.1/bin/cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_TOOLCHAIN_FILE=${NDK_PATH}/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=${ANDROID_ABI} \
    -DANDROID_NDK=${NDK_PATH} \
    -DANDROID_PLATFORM=android-${MINSDKVERSION} \
    -DCMAKE_ANDROID_ARCH_ABI=${ANDROID_ABI} \
    -DCMAKE_ANDROID_NDK=${NDK_PATH} \
    -DCMAKE_MAKE_PROGRAM=${SDK_PATH}/cmake/3.18.1/bin/ninja \
    -DCMAKE_SYSTEM_NAME=Android \
    -DCMAKE_SYSTEM_VERSION=${MINSDKVERSION} \
    -DANDROID_STL=c++_static \
    -DFAISS_ENABLE_GPU=OFF \
    -DFAISS_ENABLE_PYTHON=OFF \
    -DBUILD_TESTING=OFF \
    -DBUILD_SHARED_LIBS=ON \
    -DBLA_VENDOR=OpenBLAS \
    -DMKL_LIBRARIES=/root/codes/engine/faiss1/OpenBLAS/build/lib/libopenblas.a \
    -DCMAKE_CXX_FLAGS_RELEASE="-s"  \
    -GNinja \
    ..
if [ $? -ne 0 ]; then
    echo "ERROR: cmake failed"
    exit 1
fi

${SDK_PATH}/cmake/3.18.1/bin/ninja
if [ $? -ne 0 ]; then
    echo "ERROR: build failed"
    exit 1
fi

注意这里面DMKL_LIBRARIES设置了openblas的so路径,以及其他cmake选项。

上面采用了静态链接的方式,使得最终编译的libfaiss.so不依赖额外的libopenblas.so而且so体积比较小。动态链接的方式libopenblas.so需要额外30M的空间。

你可能感兴趣的:(推理引擎,android,faiss,openblas,build,编译)