build.sh 10.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_PYTHON=OFF
19
BUILD_DIR=$(pwd)
20 21
OPTMODEL_DIR=""
BUILD_TAILOR=OFF
22

23 24 25 26
readonly THIRDPARTY_TAR=https://paddle-inference-dist.bj.bcebos.com/PaddleLite/third-party-05b862.tar.gz

readonly workspace=$PWD

Y
Yan Chunwei 已提交
27 28 29
# 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 {
30 31
    local root_dir=$1
    local build_dir=$2
Y
Yan Chunwei 已提交
32 33
    # in build directory
    # 1. Prepare gen_code file
34 35 36
    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 已提交
37 38

    # 2.Prepare debug tool
39 40 41
    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 已提交
42 43
}

44 45 46 47 48 49 50 51 52 53 54 55 56
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
}

57 58
function build_model_optimize_tool {
    cd $workspace
59
    prepare_thirdparty
60 61
    mkdir -p build.model_optimize_tool
    cd build.model_optimize_tool
62 63 64 65 66 67
    cmake .. -DWITH_LITE=ON \
      -DLITE_ON_MODEL_OPTIMIZE_TOOL=ON \
      -DWITH_TESTING=OFF \
      -DLITE_BUILD_EXTRA=ON \
      -DWITH_MKL=OFF
    make model_optimize_tool -j$NUM_PROC
68 69
}

Y
Yan Chunwei 已提交
70 71 72 73 74 75 76 77
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 已提交
78 79 80 81
  if [ -d $build_dir ]
  then
    rm -rf $build_dir
  fi
Y
Yan Chunwei 已提交
82 83 84
  mkdir -p $build_dir
  cd $build_dir

85 86 87 88
  if [ ${os} == "armlinux" ]; then
    BUILD_JAVA=OFF
  fi

Y
Yan Chunwei 已提交
89
  cmake .. \
90
      ${PYTHON_FLAGS} \
Y
Yan Chunwei 已提交
91 92
      ${CMAKE_COMMON_OPTIONS} \
      -DWITH_TESTING=OFF \
93
      -DLITE_WITH_JAVA=$BUILD_JAVA \
94
      -DLITE_WITH_PYTHON=$BUILD_PYTHON \
Y
Yan Chunwei 已提交
95 96 97
      -DLITE_SHUTDOWN_LOG=ON \
      -DLITE_ON_TINY_PUBLISH=ON \
      -DANDROID_STL_TYPE=$android_stl \
98
      -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
99 100
      -DLITE_BUILD_TAILOR=$BUILD_TAILOR \
      -DLITE_OPTMODEL_DIR=$OPTMODEL_DIR \
Y
Yan Chunwei 已提交
101 102
      -DARM_TARGET_OS=${os} -DARM_TARGET_ARCH_ABI=${abi} -DARM_TARGET_LANG=${lang}

103
  make publish_inference -j$NUM_PROC
Y
Yan Chunwei 已提交
104 105 106 107 108 109 110 111 112
  cd - > /dev/null
}

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

113 114
  #git submodule update --init --recursive
  prepare_thirdparty
115

116 117 118 119
  root_dir=$(pwd)
  build_directory=$BUILD_DIR/build.lite.${os}.${abi}.${lang}

  if [ -d $build_directory ]
S
sangoly 已提交
120
  then
121
    rm -rf $build_directory
S
sangoly 已提交
122
  fi
123 124
  mkdir -p $build_directory
  cd $build_directory
125 126 127 128
  
  if [ ${os} == "armlinux" ]; then
    BUILD_JAVA=OFF
  fi
Y
Yan Chunwei 已提交
129

130 131
  prepare_workspace $root_dir $build_directory
  cmake $root_dir \
132
      ${PYTHON_FLAGS} \
Y
Yan Chunwei 已提交
133 134
      ${CMAKE_COMMON_OPTIONS} \
      -DWITH_TESTING=OFF \
135
      -DLITE_WITH_JAVA=$BUILD_JAVA \
136
      -DLITE_WITH_PYTHON=$BUILD_PYTHON \
Y
Yan Chunwei 已提交
137 138
      -DLITE_SHUTDOWN_LOG=ON \
      -DANDROID_STL_TYPE=$android_stl \
139
      -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
140 141
      -DLITE_BUILD_TAILOR=$BUILD_TAILOR \
      -DLITE_OPTMODEL_DIR=$OPTMODEL_DIR \
Y
Yan Chunwei 已提交
142 143 144 145 146 147 148 149 150 151 152
      -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

153 154
  #git submodule update --init --recursive
  prepare_thirdparty
155 156
  root_dir=$(pwd)
  build_directory=$BUILD_DIR/build.lite.${os}.${abi}.${lang}
S
sangoly 已提交
157 158 159 160
  if [ -d $build_dir ]
  then
    rm -rf $build_dir
  fi
161 162
  mkdir -p $build_directory
  cd $build_directory
Y
Yan Chunwei 已提交
163

164 165
  prepare_workspace $root_dir $build_directory
  cmake $root_dir \
Y
Yan Chunwei 已提交
166 167
      ${CMAKE_COMMON_OPTIONS} \
      -DWITH_TESTING=ON \
168
      -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
Y
Yan Chunwei 已提交
169 170
      -DARM_TARGET_OS=${os} -DARM_TARGET_ARCH_ABI=${abi} -DARM_TARGET_LANG=${lang}

171
  make lite_compile_deps -j$NUM_PROC
Y
Yan Chunwei 已提交
172 173 174
  cd - > /dev/null
}

X
Xiaoyang LI 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
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 -
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
function make_cuda {
  prepare_thirdparty

  root_dir=$(pwd)
  build_directory=$BUILD_DIR/build_cuda

  if [ -d $build_directory ]
  then
    rm -rf $build_directory
  fi
  mkdir -p $build_directory
  cd $build_directory

  prepare_workspace $root_dir $build_directory

  cmake ..  -DWITH_MKL=OFF       \
            -DLITE_WITH_CUDA=ON  \
            -DWITH_MKLDNN=OFF    \
            -DLITE_WITH_X86=OFF  \
            -DLITE_WITH_PROFILE=OFF \
            -DWITH_LITE=ON \
            -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=OFF \
            -DWITH_TESTING=OFF \
            -DLITE_WITH_ARM=OFF \
            -DLITE_WITH_PYTHON=ON 

  make publish_inference_python_lib -j8
  cd -
}
Y
Yan Chunwei 已提交
239 240

function print_usage {
241
    set +x
Y
Yan Chunwei 已提交
242 243 244 245
    echo -e "\nUSAGE:"
    echo
    echo "----------------------------------------"
    echo -e "compile tiny publish so lib:"
X
Xiaoyang LI 已提交
246
    echo -e "for android:"
Y
Yan Chunwei 已提交
247
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> --arm_lang=<lang> --android_stl=<stl> tiny_publish"
X
Xiaoyang LI 已提交
248 249
    echo -e "for ios:"
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> ios"
Y
Yan Chunwei 已提交
250
    echo
X
Xiaoyang LI 已提交
251
    echo -e "compile full publish so lib (ios not support):"
Y
Yan Chunwei 已提交
252 253
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> --arm_lang=<lang> --android_stl=<stl> full_publish"
    echo
X
Xiaoyang LI 已提交
254
    echo -e "compile all arm tests (ios not support):"
Y
Yan Chunwei 已提交
255 256
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> --arm_lang=<lang> test"
    echo
257 258
    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)"
259 260
    echo -e "--build_python: (OFF|ON); controls whether to publish python api lib (ANDROID and IOS is not supported)"
    echo -e "--build_java: (OFF|ON); controls whether to publish java api lib (Only ANDROID is supported)"
261
    echo -e "--build_dir: directory for building"
Y
Yan Chunwei 已提交
262
    echo
263
    echo -e "argument choices:"
X
Xiaoyang LI 已提交
264
    echo -e "--arm_os:\t android|ios|ios64"
Y
Yan Chunwei 已提交
265
    echo -e "--arm_abi:\t armv8|armv7"
266
    echo -e "--arm_lang:\t only support gcc now, clang will be supported in future.(for android)"
X
Xiaoyang LI 已提交
267
    echo -e "--android_stl:\t c++_static|c++_shared (for android)"
Y
Yan Chunwei 已提交
268 269 270 271 272 273
    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 已提交
274 275 276 277 278
    echo "----------------------------------------"
    echo
}

function main {
Y
Yan Chunwei 已提交
279 280 281 282 283
    if [ -z "$1" ]; then
        print_usage
        exit -1
    fi

Y
Yan Chunwei 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296
    # 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#*=}"
297 298 299 300 301 302 303
                if [ ${ARM_LANG} == "clang" ]; then
                     set +x
                     echo
                     echo -e "error: only support gcc now, clang will be supported in future."
                     echo
                     exit 1
                fi
Y
Yan Chunwei 已提交
304 305 306 307 308 309
                shift
                ;;
            --android_stl=*)
                ANDROID_STL="${i#*=}"
                shift
                ;;
310 311 312 313
            --build_extra=*)
                BUILD_EXTRA="${i#*=}"
                shift
                ;;
314 315 316 317 318 319 320 321
            --build_python=*)
                BUILD_PYTHON="${i#*=}"
                shift
                ;;
            --build_java=*)
                BUILD_JAVA="${i#*=}"
                shift
                ;;
322 323 324 325
            --build_dir=*)
                BUILD_DIR="${i#*=}"
                shift
		            ;;
326 327 328 329 330 331 332 333
            --opt_model_dir=*)
                OPTMODEL_DIR="${i#*=}"
                shift
                ;;
            --build_tailor=*)
                BUILD_TAILOR="${i#*=}"
                shift
                ;;
Y
Yan Chunwei 已提交
334 335 336 337 338 339 340 341 342 343 344 345
            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 已提交
346 347 348 349
            ios)
                make_ios $ARM_OS $ARM_ABI
                shift
                ;;
350 351 352 353
            build_optimize_tool)
                build_model_optimize_tool
                shift
                ;;
354 355 356 357
            cuda)
                make_cuda 
                shift
                ;;
Y
Yan Chunwei 已提交
358 359 360 361 362 363 364 365 366 367
            *)
                # unknown option
                print_usage
                exit 1
                ;;
        esac
    done
}

main $@