cross_build_android_arm_inference.sh 4.8 KB
Newer Older
1 2 3 4 5 6 7 8
#!/usr/bin/env bash
set -e

ARCHS=("arm64-v8a" "armeabi-v7a")
BUILD_TYPE=Release
MGE_ARMV8_2_FEATURE_FP16=OFF
MGE_DISABLE_FLOAT16=OFF
ARCH=arm64-v8a
9
REMOVE_OLD_BUILD=false
10 11
NINJA_VERBOSE=OFF
NINJA_DRY_RUN=OFF
12
SPECIFIED_TARGET="install/strip"
13

14
echo "EXTRA_CMAKE_ARGS: ${EXTRA_CMAKE_ARGS}"
15 16 17 18

function usage() {
    echo "$0 args1 args2 .."
    echo "available args detail:"
19
    echo "-d : Build with Debug mode, default Release mode"
20 21 22
    echo "-f : enable MGE_ARMV8_2_FEATURE_FP16 for ARM64, need toolchain and hardware support"
    echo "-k : open MGE_DISABLE_FLOAT16 for NEON "
    echo "-a : config build arch available: ${ARCHS[@]}"
23
    echo "-r : remove old build dir before make, default off"
24 25
    echo "-v : ninja with verbose and explain, default off"
    echo "-n : ninja with -n dry run (don't run commands but act like they succeeded)"
26
    echo "-e : build a specified target (always for debug, NOTICE: do not do strip/install target when use -k)"
27
    echo "-h : show usage"
28
    echo "append other cmake config by export EXTRA_CMAKE_ARGS=..."
29 30 31 32
    echo "example: $0 -d"
    exit -1
}

33
while getopts "nvrkhdfa:e:" arg
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
do
    case $arg in
        d)
            echo "Build with Debug mode"
            BUILD_TYPE=Debug
            ;;
        f)
            echo "enable MGE_ARMV8_2_FEATURE_FP16 for ARM64"
            MGE_ARMV8_2_FEATURE_FP16=ON
            ;;
        k)
            echo "open MGE_DISABLE_FLOAT16 for NEON"
            MGE_DISABLE_FLOAT16=ON
            ;;
        a)
            tmp_arch=null
            for arch in ${ARCHS[@]}; do
                if [ "$arch" = "$OPTARG" ]; then
                    echo "CONFIG BUILD ARCH to : $OPTARG"
                    tmp_arch=$OPTARG
                    ARCH=$OPTARG
                    break
                fi
            done
            if [ "$tmp_arch" = "null" ]; then
                echo "ERR args for arch (-a)"
                echo "available arch list: ${ARCHS[@]}"
                usage
            fi
            ;;
        h)
            echo "show usage"
            usage
            ;;
68 69 70 71
        r)
            echo "config REMOVE_OLD_BUILD=true"
            REMOVE_OLD_BUILD=true
            ;;
72 73 74 75 76 77 78 79
        v)
            echo "config NINJA_VERBOSE=ON"
            NINJA_VERBOSE=ON
            ;;
        n)
            echo "config NINJA_DRY_RUN=ON"
            NINJA_DRY_RUN=ON
            ;;
80 81 82
        e)
            SPECIFIED_TARGET=$OPTARG
            ;;
83 84 85 86 87 88 89 90 91 92 93
        ?)
            echo "unkonw argument"
            usage
            ;;
    esac
done
echo "----------------------------------------------------"
echo "build config summary:"
echo "BUILD_TYPE: $BUILD_TYPE"
echo "MGE_ARMV8_2_FEATURE_FP16: $MGE_ARMV8_2_FEATURE_FP16"
echo "MGE_DISABLE_FLOAT16: $MGE_DISABLE_FLOAT16"
94
echo "SPECIFIED_TARGET: ${SPECIFIED_TARGET}"
95 96 97 98 99 100 101 102
echo "ARCH: $ARCH"
echo "----------------------------------------------------"

READLINK=readlink
OS=$(uname -s)

if [ $OS = "Darwin" ];then
    READLINK=greadlink
103 104
elif [[ $OS =~ "NT" ]]; then
    echo "BUILD in NT ..."
105 106 107
fi

SRC_DIR=$($READLINK -f "`dirname $0`/../../")
108
source $SRC_DIR/scripts/cmake-build/utils/utils.sh
109 110 111 112 113 114 115

if [ -z $NDK_ROOT ];then
    echo "can not find NDK_ROOT env, pls export you NDK root dir to NDK_ROOT"
    exit -1
fi

function cmake_build() {
116 117 118 119 120
    if [ $1 = "armeabi-v7a with NEON" ] ;then
        BUILD_DIR=$SRC_DIR/build_dir/android/armeabi-v7a/$BUILD_TYPE/build
    else
        BUILD_DIR=$SRC_DIR/build_dir/android/$1/$BUILD_TYPE/build
    fi
121 122 123 124 125 126 127 128
    INSTALL_DIR=$BUILD_DIR/../install
    BUILD_ABI=$1
    BUILD_NATIVE_LEVEL=$2
    echo "build dir: $BUILD_DIR"
    echo "install dir: $INSTALL_DIR"
    echo "build type: $BUILD_TYPE"
    echo "build ABI: $BUILD_ABI"
    echo "build native level: $BUILD_NATIVE_LEVEL"
129
    try_remove_old_build $REMOVE_OLD_BUILD $BUILD_DIR $INSTALL_DIR
130 131 132 133

    echo "create build dir"
    mkdir -p $BUILD_DIR
    mkdir -p $INSTALL_DIR
134
    cd_real_build_dir $BUILD_DIR
135
    unset IFS
136
    bash -c "cmake -G Ninja \
137
        -DCMAKE_TOOLCHAIN_FILE="$NDK_ROOT/build/cmake/android.toolchain.cmake" \
138 139
        -DANDROID_NDK="$NDK_ROOT" \
        -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
140
        -DANDROID_ABI=\"$BUILD_ABI\" \
141 142 143 144 145 146
        -DANDROID_NATIVE_API_LEVEL=$BUILD_NATIVE_LEVEL \
        -DMGE_INFERENCE_ONLY=ON \
        -DMGE_WITH_CUDA=OFF \
        -DMGE_ARMV8_2_FEATURE_FP16= $MGE_ARMV8_2_FEATURE_FP16 \
        -DMGE_DISABLE_FLOAT16=$MGE_DISABLE_FLOAT16 \
        -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
147
        ${EXTRA_CMAKE_ARGS} \
148
        $SRC_DIR "
149

150
    config_ninja_target_cmd ${NINJA_VERBOSE} "OFF" "${SPECIFIED_TARGET}" ${NINJA_DRY_RUN}
151
    bash -c "${NINJA_CMD}"
152 153
}

154
build_flatc $SRC_DIR $REMOVE_OLD_BUILD
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

api_level=16
abi="armeabi-v7a with NEON"
IFS=""
if [ "$ARCH" = "arm64-v8a" ]; then
    api_level=21
    abi="arm64-v8a"
elif [ "$ARCH" = "armeabi-v7a" ]; then
    api_level=16
    abi="armeabi-v7a with NEON"
else
    echo "ERR CONFIG ABORT NOW!!"
    exit -1
fi
cmake_build $abi $api_level