Android NDK compiles openblas and vector retrieval library faiss

Set the android SDK and NDK path

For example:

export SDK_ROOT=/root/codes/my_sdk/sdk/
export NDK_ROOT=/root/codes/my_sdk/sdk/ndk/24.0.8215888/
sdk and ndk download method reference:

Tflite builds and calls locally_Luchang-Li's blog-CSDN blog_How to call tensflow tflite model

After setting the environment variables, put the following build.sh in the code directory and execute it directly, and the so file will be generated in the corresponding position in the build directory.

compile openblas

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

You can set the required version in -b.

build.sh

#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 
mkdir ${SCRIPT_DIR}/build
cd ${SCRIPT_DIR}/build
 
# please set SDK_ROOT, NDK_ROOT, 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_ROOT=/root/codes/android_sdk
# export NDK_ROOT=/root/codes/android_sdk/ndk/24.0.8215888/

if [ ! -d ${SDK_ROOT} ] ; then
    echo "ERROR: please set valid sdk path by env SDK_ROOT"
    exit 1
fi
if [ ! -d ${SDK_ROOT}/cmake/3.18.1/ ] ; then
    echo "ERROR: please download cmake 3.18.1 for sdk"
    exit 1
fi
if [ ! -d ${NDK_ROOT} ] ; then
    echo "ERROR: please set valid ndk path by env NDK_ROOT"
    exit 1
fi
if [ -z ${ANDROID_ABI} ] ; then
    ANDROID_ABI=arm64-v8a
fi
if [ -z ${MINSDKVERSION} ] ; then
    MINSDKVERSION=29
fi
 
echo "SDK_ROOT:" $SDK_ROOT
echo "NDK_ROOT:" $NDK_ROOT
echo "ANDROID_ABI:" $ANDROID_ABI
echo "MINSDKVERSION:" $MINSDKVERSION
 
    # -DBUILD_SHARED_LIBS=ON \
 
${SDK_ROOT}/cmake/3.18.1/bin/cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_TOOLCHAIN_FILE=${NDK_ROOT}/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=${ANDROID_ABI} \
    -DANDROID_NDK=${NDK_ROOT} \
    -DANDROID_PLATFORM=android-${MINSDKVERSION} \
    -DCMAKE_ANDROID_ARCH_ABI=${ANDROID_ABI} \
    -DCMAKE_ANDROID_NDK=${NDK_ROOT} \
    -DCMAKE_MAKE_PROGRAM=${SDK_ROOT}/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_ROOT}/cmake/3.18.1/bin/ninja
if [ $? -ne 0 ]; then
    echo "ERROR: build failed"
    exit 1
fi

Compile dynamic library settings -DBUILD_SHARED_LIBS=ON, cancel -DBUILD_STATIC_LIBS=ON
 

compile 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_ROOT, NDK_ROOT, 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_ROOT=/root/codes/my_sdk/sdk/
# export NDK_ROOT=/root/codes/my_sdk/sdk/ndk/24.0.8215888/
 
if [ ! -d ${SDK_ROOT} ] ; then
    echo "ERROR: please set valid sdk path by env SDK_ROOT"
    exit 1
fi
if [ ! -d ${SDK_ROOT}/cmake/3.18.1/ ] ; then
    echo "ERROR: please download cmake 3.18.1 for sdk"
    exit 1
fi
if [ ! -d ${NDK_ROOT} ] ; then
    echo "ERROR: please set valid ndk path by env NDK_ROOT"
    exit 1
fi
if [ -z ${ANDROID_ABI} ] ; then
    ANDROID_ABI=arm64-v8a
fi
if [ -z ${MINSDKVERSION} ] ; then
    MINSDKVERSION=29
fi

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

Note that DMKL_LIBRARIES sets the so path of openblas and other cmake options.

The method of static linking is adopted above, so that the final compiled libfaiss.so does not depend on the additional libopenblas.so and the size of so is relatively small. The way libopenblas.so is dynamically linked requires an additional 30M space.

Guess you like

Origin blog.csdn.net/u013701860/article/details/127538917