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

ARCHS=("arm64" "armv7")
BUILD_TYPE=Release
MGE_ARMV8_2_FEATURE_FP16=OFF
MGE_DISABLE_FLOAT16=OFF
ARCH=arm64
9
REMOVE_OLD_BUILD=false
10 11
NINJA_VERBOSE=OFF
NINJA_DRY_RUN=OFF
12
SPECIFIED_TARGET="install"
13 14 15 16 17 18 19 20 21 22
READLINK=readlink
OS=$(uname -s)

if [ $OS = "Darwin" ];then
    READLINK=greadlink
fi

SRC_DIR=$($READLINK -f "`dirname $0`/../../")
source $SRC_DIR/scripts/cmake-build/utils/utils.sh
config_ninja_default_max_jobs
23

24
echo "EXTRA_CMAKE_ARGS: ${EXTRA_CMAKE_ARGS}"
25 26 27 28

function usage() {
    echo "$0 args1 args2 .."
    echo "available args detail:"
29
    echo "-d : Build with Debug mode, default Release mode"
30 31 32
    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[@]}"
33
    echo "-r : remove old build dir before make, default off"
34 35
    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)"
36
    echo "-j : run N jobs in parallel for ninja, defaut is cpu_number + 2"
37
    echo "-e : build a specified target (always for debug, NOTICE: do not do strip/install target when use -e)"
38
    echo "-l : list CMakeLists.txt all options, can be use to config EXTRA_CMAKE_ARGS"
39
    echo "-h : show usage"
40 41
    echo "append other cmake config by config EXTRA_CMAKE_ARGS, for example, enable MGE_WITH_TEST and build with Debug mode:"
    echo "EXTRA_CMAKE_ARGS=\"-DMGE_WITH_TEST=ON\" $0 -d"
42 43 44
    exit -1
}

45
while getopts "lnvrkhdfa:e:j:" arg
46 47
do
    case $arg in
48 49 50 51 52 53 54 55 56
        j)
            NINJA_MAX_JOBS=$OPTARG
            echo "config NINJA_MAX_JOBS to ${NINJA_MAX_JOBS}"
            ;;
        l)
            echo "list CMakeLists.txt all options, can be used to config EXTRA_CMAKE_ARGS"
            show_cmakelist_options
            exit 0
            ;;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        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
            ;;
89 90 91 92
        r)
            echo "config REMOVE_OLD_BUILD=true"
            REMOVE_OLD_BUILD=true
            ;;
93 94 95 96 97 98 99 100
        v)
            echo "config NINJA_VERBOSE=ON"
            NINJA_VERBOSE=ON
            ;;
        n)
            echo "config NINJA_DRY_RUN=ON"
            NINJA_DRY_RUN=ON
            ;;
101 102 103
        e)
            SPECIFIED_TARGET=$OPTARG
            ;;
104 105 106 107 108 109 110 111 112 113 114
        ?)
            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"
115
echo "SPECIFIED_TARGET: ${SPECIFIED_TARGET}"
116
echo "NINJA_MAX_JOBS: ${NINJA_MAX_JOBS}"
117 118 119 120
echo "ARCH: $ARCH"
echo "----------------------------------------------------"

if [ $OS = "Darwin" ];then
121
    echo "cross build ios from MacOS"
122
else
123
    echo "cross build ios only support on MacOS, abort now!!"
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    exit -1
fi

function cmake_build() {
    BUILD_DIR=$SRC_DIR/build_dir/apple/$3/$4/$1/$BUILD_TYPE/build
    INSTALL_DIR=$BUILD_DIR/../install
    TOOLCHAIN=$SRC_DIR/toolchains/$2
    OS_PLATFORM=$3
    XCODE_IOS_PLATFORM=$4
    IOS_ARCH=$1
    echo "build dir: $BUILD_DIR"
    echo "install dir: $INSTALL_DIR"
    echo "build type: $BUILD_TYPE"
    echo "build toolchain: $TOOLCHAIN"
    echo "build OS_PLATFORM: $OS_PLATFORM"
    echo "build XCODE_IOS_PLATFORM: $XCODE_IOS_PLATFORM"
    echo "build IOS_ARCH: $IOS_ARCH"
141
    try_remove_old_build $REMOVE_OLD_BUILD $BUILD_DIR $INSTALL_DIR
142 143 144 145

    echo "create build dir"
    mkdir -p $BUILD_DIR
    mkdir -p $INSTALL_DIR
146
    cd_real_build_dir $BUILD_DIR
147 148
    bash -c "cmake -G Ninja \
        -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN \
149 150 151 152 153 154 155 156
        -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
        -DIOS_TOOLCHAIN_ROOT=$TOOLCHAIN \
        -DOS_PLATFORM=$OS_PLATFORM \
        -DXCODE_IOS_PLATFORM=$XCODE_IOS_PLATFORM \
        -DIOS_ARCH=$IOS_ARCH \
        -DMGE_INFERENCE_ONLY=ON \
        -DPYTHON_EXECUTABLE=/usr/local/bin/python3 \
        -DMGE_WITH_CUDA=OFF \
M
Megvii Engine Team 已提交
157
        -DMGE_ARMV8_2_FEATURE_FP16=$MGE_ARMV8_2_FEATURE_FP16 \
158 159
        -DMGE_DISABLE_FLOAT16=$MGE_DISABLE_FLOAT16 \
        -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
160
        -DCMAKE_MAKE_PROGRAM=ninja \
161
        ${EXTRA_CMAKE_ARGS} \
162
        $SRC_DIR "
163

164
    config_ninja_target_cmd ${NINJA_VERBOSE} "OFF" "${SPECIFIED_TARGET}" ${NINJA_DRY_RUN} ${NINJA_MAX_JOBS}
165
    bash -c "${NINJA_CMD}"
166 167
}

168
build_flatc $SRC_DIR $REMOVE_OLD_BUILD
169 170 171 172 173 174 175 176 177 178 179 180

# refs for ../../toolchains/ios.toolchain.cmake
# to config this, if u want to build other,
# like simulator or for iwatch, please do manually modify
# OS_PLATFORM=("OS" "OS64" "SIMULATOR" "SIMULATOR64" "TVOS" "WATCHOS" "SIMULATOR_TVOS")
# XCODE_IOS_PLATFORM=("iphoneos" "iphonesimulator" "appletvos" "appletvsimulator" "watchos", "watchsimulator")
# IOS_ARCHS=("arm64" "armv7" "armv7k" "arm64e" "armv7s")

#by defaut we only triger build arm64/armv7 for iphoneos
OS_PLATFORM=OS
XCODE_IOS_PLATFORM=iphoneos
cmake_build $ARCH ios.toolchain.cmake $OS_PLATFORM $XCODE_IOS_PLATFORM