build.sh 8.0 KB
Newer Older
Y
Yan Chunwei 已提交
1
#!/bin/bash
2
set -ex
Y
Yan Chunwei 已提交
3 4 5 6 7 8 9 10 11

readonly CMAKE_COMMON_OPTIONS="-DWITH_GPU=OFF \
                               -DWITH_MKL=OFF \
                               -DWITH_LITE=ON \
                               -DLITE_WITH_CUDA=OFF \
                               -DLITE_WITH_X86=OFF \
                               -DLITE_WITH_ARM=ON \
                               -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON"

12 13
readonly NUM_PROC=${LITE_BUILD_THREADS:-4}

14 15 16

# global variables
BUILD_EXTRA=OFF
17
BUILD_JAVA=ON
18
BUILD_DIR=$(pwd)
19

20 21 22 23
readonly THIRDPARTY_TAR=https://paddle-inference-dist.bj.bcebos.com/PaddleLite/third-party-05b862.tar.gz

readonly workspace=$PWD

Y
Yan Chunwei 已提交
24 25 26
# for code gen, a source file is generated after a test, but is dependended by some targets in cmake.
# here we fake an empty file to make cmake works.
function prepare_workspace {
27 28
    local root_dir=$1
    local build_dir=$2
Y
Yan Chunwei 已提交
29 30
    # in build directory
    # 1. Prepare gen_code file
31 32 33
    GEN_CODE_PATH_PREFIX=$build_dir/lite/gen_code
    mkdir -p ${GEN_CODE_PATH_PREFIX}
    touch ${GEN_CODE_PATH_PREFIX}/__generated_code__.cc
Y
Yan Chunwei 已提交
34 35

    # 2.Prepare debug tool
36 37 38
    DEBUG_TOOL_PATH_PREFIX=$build_dir/lite/tools/debug
    mkdir -p ${DEBUG_TOOL_PATH_PREFIX}
    cp $root_dir/lite/tools/debug/analysis_tool.py ${DEBUG_TOOL_PATH_PREFIX}/
Y
Yan Chunwei 已提交
39 40
}

41 42 43 44 45 46 47 48 49 50 51 52 53
function prepare_thirdparty {
    if [ ! -d $workspace/third-party -o -f $workspace/third-party-05b862.tar.gz ]; then
        rm -rf $workspace/third-party

        if [ ! -f $workspace/third-party-05b862.tar.gz ]; then
            wget $THIRDPARTY_TAR
        fi
        tar xzf third-party-05b862.tar.gz
    else
        git submodule update --init --recursive
    fi
}

54 55
function build_model_optimize_tool {
    cd $workspace
56
    prepare_thirdparty
57 58 59 60 61 62
    mkdir -p build.model_optimize_tool
    cd build.model_optimize_tool
    cmake .. -DWITH_LITE=ON -DLITE_ON_MODEL_OPTIMIZE_TOOL=ON -DWITH_TESTING=OFF -DLITE_BUILD_EXTRA=ON
    make model_optimize_tool -j$NUM_CORES_FOR_COMPILE
}

Y
Yan Chunwei 已提交
63 64 65 66 67 68 69 70
function make_tiny_publish_so {
  local os=$1
  local abi=$2
  local lang=$3
  local android_stl=$4

  cur_dir=$(pwd)
  build_dir=$cur_dir/build.lite.${os}.${abi}.${lang}
S
sangoly 已提交
71 72 73 74
  if [ -d $build_dir ]
  then
    rm -rf $build_dir
  fi
Y
Yan Chunwei 已提交
75 76 77
  mkdir -p $build_dir
  cd $build_dir

78 79 80 81
  if [ ${os} == "armlinux" ]; then
    BUILD_JAVA=OFF
  fi

Y
Yan Chunwei 已提交
82 83 84
  cmake .. \
      ${CMAKE_COMMON_OPTIONS} \
      -DWITH_TESTING=OFF \
85
      -DLITE_WITH_JAVA=$BUILD_JAVA \
Y
Yan Chunwei 已提交
86 87 88
      -DLITE_SHUTDOWN_LOG=ON \
      -DLITE_ON_TINY_PUBLISH=ON \
      -DANDROID_STL_TYPE=$android_stl \
89
      -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
Y
Yan Chunwei 已提交
90 91
      -DARM_TARGET_OS=${os} -DARM_TARGET_ARCH_ABI=${abi} -DARM_TARGET_LANG=${lang}

92
  make publish_inference -j$NUM_PROC
Y
Yan Chunwei 已提交
93 94 95 96 97 98 99 100 101
  cd - > /dev/null
}

function make_full_publish_so {
  local os=$1
  local abi=$2
  local lang=$3
  local android_stl=$4

102 103
  #git submodule update --init --recursive
  prepare_thirdparty
104

105 106 107 108
  root_dir=$(pwd)
  build_directory=$BUILD_DIR/build.lite.${os}.${abi}.${lang}

  if [ -d $build_directory ]
S
sangoly 已提交
109
  then
110
    rm -rf $build_directory
S
sangoly 已提交
111
  fi
112 113
  mkdir -p $build_directory
  cd $build_directory
114 115 116 117
  
  if [ ${os} == "armlinux" ]; then
    BUILD_JAVA=OFF
  fi
Y
Yan Chunwei 已提交
118

119 120
  prepare_workspace $root_dir $build_directory
  cmake $root_dir \
Y
Yan Chunwei 已提交
121 122
      ${CMAKE_COMMON_OPTIONS} \
      -DWITH_TESTING=OFF \
123
      -DLITE_WITH_JAVA=$BUILD_JAVA \
Y
Yan Chunwei 已提交
124 125
      -DLITE_SHUTDOWN_LOG=ON \
      -DANDROID_STL_TYPE=$android_stl \
126
      -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
Y
Yan Chunwei 已提交
127 128 129 130 131 132 133 134 135 136 137
      -DARM_TARGET_OS=${os} -DARM_TARGET_ARCH_ABI=${abi} -DARM_TARGET_LANG=${lang}

  make publish_inference -j4
  cd - > /dev/null
}

function make_all_tests {
  local os=$1
  local abi=$2
  local lang=$3

138 139
  #git submodule update --init --recursive
  prepare_thirdparty
140 141
  root_dir=$(pwd)
  build_directory=$BUILD_DIR/build.lite.${os}.${abi}.${lang}
S
sangoly 已提交
142 143 144 145
  if [ -d $build_dir ]
  then
    rm -rf $build_dir
  fi
146 147
  mkdir -p $build_directory
  cd $build_directory
Y
Yan Chunwei 已提交
148

149 150
  prepare_workspace $root_dir $build_directory
  cmake $root_dir \
Y
Yan Chunwei 已提交
151 152
      ${CMAKE_COMMON_OPTIONS} \
      -DWITH_TESTING=ON \
153
      -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
Y
Yan Chunwei 已提交
154 155
      -DARM_TARGET_OS=${os} -DARM_TARGET_ARCH_ABI=${abi} -DARM_TARGET_LANG=${lang}

156
  make lite_compile_deps -j$NUM_PROC
Y
Yan Chunwei 已提交
157 158 159
  cd - > /dev/null
}

X
Xiaoyang LI 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
function make_ios {
    local os=$1
    local abi=$2
    build_dir=build.ios.${os}.${abi}
    echo "building ios target into $build_dir"
    echo "target os: $os"
    echo "target abi: $abi"
    mkdir -p ${build_dir}
    cd ${build_dir}
    GEN_CODE_PATH_PREFIX=lite/gen_code
    mkdir -p ./${GEN_CODE_PATH_PREFIX}
    touch ./${GEN_CODE_PATH_PREFIX}/__generated_code__.cc

    cmake .. \
            -DWITH_GPU=OFF \
            -DWITH_MKL=OFF \
            -DWITH_LITE=ON \
            -DLITE_WITH_CUDA=OFF \
            -DLITE_WITH_X86=OFF \
            -DLITE_WITH_ARM=ON \
            -DWITH_TESTING=OFF \
            -DLITE_WITH_JAVA=OFF \
            -DLITE_SHUTDOWN_LOG=ON \
            -DLITE_ON_TINY_PUBLISH=ON \
            -DLITE_WITH_OPENMP=OFF \
            -DWITH_ARM_DOTPROD=OFF \
            -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON \
            -DARM_TARGET_ARCH_ABI=$abi \
            -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
            -DARM_TARGET_OS=$os

    make -j4 publish_inference
    cd -
}

Y
Yan Chunwei 已提交
195 196

function print_usage {
197
    set +x
Y
Yan Chunwei 已提交
198 199 200 201
    echo -e "\nUSAGE:"
    echo
    echo "----------------------------------------"
    echo -e "compile tiny publish so lib:"
X
Xiaoyang LI 已提交
202
    echo -e "for android:"
Y
Yan Chunwei 已提交
203
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> --arm_lang=<lang> --android_stl=<stl> tiny_publish"
X
Xiaoyang LI 已提交
204 205
    echo -e "for ios:"
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> ios"
Y
Yan Chunwei 已提交
206
    echo
X
Xiaoyang LI 已提交
207
    echo -e "compile full publish so lib (ios not support):"
Y
Yan Chunwei 已提交
208 209
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> --arm_lang=<lang> --android_stl=<stl> full_publish"
    echo
X
Xiaoyang LI 已提交
210
    echo -e "compile all arm tests (ios not support):"
Y
Yan Chunwei 已提交
211 212
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> --arm_lang=<lang> test"
    echo
213 214
    echo -e "optional argument:"
    echo -e "--build_extra: (OFF|ON); controls whether to publish extra operators and kernels for (sequence-related model such as OCR or NLP)"
215
    echo -e "--build_dir: directory for building"
Y
Yan Chunwei 已提交
216
    echo
217
    echo -e "argument choices:"
X
Xiaoyang LI 已提交
218
    echo -e "--arm_os:\t android|ios|ios64"
Y
Yan Chunwei 已提交
219
    echo -e "--arm_abi:\t armv8|armv7"
X
Xiaoyang LI 已提交
220 221
    echo -e "--arm_lang:\t gcc|clang (for android)"
    echo -e "--android_stl:\t c++_static|c++_shared (for android)"
Y
Yan Chunwei 已提交
222 223 224 225 226 227
    echo
    echo -e "tasks:"
    echo
    echo -e "tiny_publish: a small library for deployment."
    echo -e "full_publish: a full library for debug and test."
    echo -e "test: produce all the unittests."
Y
Yan Chunwei 已提交
228 229 230 231 232
    echo "----------------------------------------"
    echo
}

function main {
Y
Yan Chunwei 已提交
233 234 235 236 237
    if [ -z "$1" ]; then
        print_usage
        exit -1
    fi

Y
Yan Chunwei 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    # Parse command line.
    for i in "$@"; do
        case $i in
            --arm_os=*)
                ARM_OS="${i#*=}"
                shift
                ;;
            --arm_abi=*)
                ARM_ABI="${i#*=}"
                shift
                ;;
            --arm_lang=*)
                ARM_LANG="${i#*=}"
                shift
                ;;
            --android_stl=*)
                ANDROID_STL="${i#*=}"
                shift
                ;;
257 258 259 260
            --build_extra=*)
                BUILD_EXTRA="${i#*=}"
                shift
                ;;
261 262 263 264
            --build_dir=*)
                BUILD_DIR="${i#*=}"
                shift
		            ;;
Y
Yan Chunwei 已提交
265 266 267 268 269 270 271 272 273 274 275 276
            tiny_publish)
                make_tiny_publish_so $ARM_OS $ARM_ABI $ARM_LANG $ANDROID_STL
                shift
                ;;
            full_publish)
                make_full_publish_so $ARM_OS $ARM_ABI $ARM_LANG $ANDROID_STL
                shift
                ;;
            test)
                make_all_tests $ARM_OS $ARM_ABI $ARM_LANG
                shift
                ;;
X
Xiaoyang LI 已提交
277 278 279 280
            ios)
                make_ios $ARM_OS $ARM_ABI
                shift
                ;;
281 282 283 284
            build_optimize_tool)
                build_model_optimize_tool
                shift
                ;;
Y
Yan Chunwei 已提交
285 286 287 288 289 290 291 292 293 294
            *)
                # unknown option
                print_usage
                exit 1
                ;;
        esac
    done
}

main $@