build.sh 14.3 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
BUILD_CV=OFF
23
SHUTDOWN_LOG=ON
24 25 26 27
BUILD_NPU=OFF
NPU_DDK_ROOT="$(pwd)/ai_ddk_lib/" # Download HiAI DDK from https://developer.huawei.com/consumer/cn/hiai/
BUILD_XPU=OFF
XPU_SDK_ROOT="$(pwd)/xpu_sdk_lib/"
H
HappyAngel 已提交
28
LITE_WITH_ARM_LANG=OFF
29

30 31 32 33
readonly THIRDPARTY_TAR=https://paddle-inference-dist.bj.bcebos.com/PaddleLite/third-party-05b862.tar.gz

readonly workspace=$PWD

34 35 36 37 38 39
# if operating in mac env, we should expand the maximum file num
os_nmae=`uname -s`
if [ ${os_nmae} == "Darwin" ]; then
   ulimit -n 1024
fi

Y
Yan Chunwei 已提交
40 41 42
# 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 {
43 44
    local root_dir=$1
    local build_dir=$2
H
HappyAngel 已提交
45 46 47 48 49 50 51 52
    # ARM LANG
    if [ ${ARM_LANG} == "clang" ]; then
        LITE_WITH_ARM_LANG=ON
    else
        LITE_WITH_ARM_LANG=OFF
    fi
    echo "ARM_LANG is  ${ARM_LANG}"
    echo "LITE_WITH_ARM_LANG is ${LITE_WITH_ARM_LANG}"
Y
Yan Chunwei 已提交
53 54
    # in build directory
    # 1. Prepare gen_code file
55 56 57
    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 已提交
58
    # 2.Prepare debug tool
59 60 61
    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 已提交
62 63
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77

function prepare_opencl_source_code {
    local root_dir=$1
    local build_dir=$2
    # in build directory
    # Prepare opencl_kernels_source.cc file
    GEN_CODE_PATH_OPENCL=$root_dir/lite/backends/opencl
    rm -f GEN_CODE_PATH_OPENCL/opencl_kernels_source.cc
    OPENCL_KERNELS_PATH=$root_dir/lite/backends/opencl/cl_kernel
    mkdir -p ${GEN_CODE_PATH_OPENCL}
    touch $GEN_CODE_PATH_OPENCL/opencl_kernels_source.cc
    python $root_dir/lite/tools/cmake_tools/gen_opencl_code.py $OPENCL_KERNELS_PATH $GEN_CODE_PATH_OPENCL/opencl_kernels_source.cc 
}

78 79 80 81 82 83 84 85 86 87 88 89 90
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
}

91
function build_opt {
92
    cd $workspace
93
    prepare_thirdparty
94 95
    mkdir -p build.opt
    cd build.opt
96 97 98 99 100
    cmake .. -DWITH_LITE=ON \
      -DLITE_ON_MODEL_OPTIMIZE_TOOL=ON \
      -DWITH_TESTING=OFF \
      -DLITE_BUILD_EXTRA=ON \
      -DWITH_MKL=OFF
101
    make opt -j$NUM_PROC
102 103
}

Y
Yan Chunwei 已提交
104 105 106 107 108 109 110 111
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 已提交
112 113 114 115
  if [ -d $build_dir ]
  then
    rm -rf $build_dir
  fi
Y
Yan Chunwei 已提交
116 117 118
  mkdir -p $build_dir
  cd $build_dir

119 120 121
  if [ ${os} == "armlinux" ]; then
    BUILD_JAVA=OFF
  fi
H
HappyAngel 已提交
122
  
Y
Yan Chunwei 已提交
123
  cmake .. \
124
      ${PYTHON_FLAGS} \
Y
Yan Chunwei 已提交
125 126
      ${CMAKE_COMMON_OPTIONS} \
      -DWITH_TESTING=OFF \
127
      -DLITE_WITH_JAVA=$BUILD_JAVA \
128
      -DLITE_WITH_PYTHON=$BUILD_PYTHON \
129
      -DLITE_SHUTDOWN_LOG=$SHUTDOWN_LOG \
Y
Yan Chunwei 已提交
130 131
      -DLITE_ON_TINY_PUBLISH=ON \
      -DANDROID_STL_TYPE=$android_stl \
132
      -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
133
      -DLITE_WITH_CV=$BUILD_CV \
H
HappyAngel 已提交
134
      -DLITE_WITH_ARM_LANG=$LITE_WITH_ARM_LANG \
135 136
      -DLITE_BUILD_TAILOR=$BUILD_TAILOR \
      -DLITE_OPTMODEL_DIR=$OPTMODEL_DIR \
137 138 139 140
      -DLITE_WITH_NPU=$BUILD_NPU \
      -DNPU_DDK_ROOT=$NPU_DDK_ROOT \
      -DLITE_WITH_XPU=$BUILD_XPU \
      -DXPU_SDK_ROOT=$XPU_SDK_ROOT \
Y
Yan Chunwei 已提交
141 142
      -DARM_TARGET_OS=${os} -DARM_TARGET_ARCH_ABI=${abi} -DARM_TARGET_LANG=${lang}

143
  make publish_inference -j$NUM_PROC
Y
Yan Chunwei 已提交
144 145 146
  cd - > /dev/null
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
function make_opencl {
  local os=$1
  local abi=$2
  local lang=$3
  #git submodule update --init --recursive
  prepare_thirdparty

  root_dir=$(pwd)
  build_dir=$root_dir/build.lite.${os}.${abi}.${lang}.opencl
  if [ -d $build_directory ]
  then
  rm -rf $build_directory
  fi
  mkdir -p $build_dir
  cd $build_dir
  prepare_workspace $root_dir $build_dir
  prepare_opencl_source_code $root_dir $build_dir
  # $1: ARM_TARGET_OS in "android" , "armlinux"
  # $2: ARM_TARGET_ARCH_ABI in "armv8", "armv7" ,"armv7hf"
  # $3: ARM_TARGET_LANG in "gcc" "clang"
  cmake .. \
      -DLITE_WITH_OPENCL=ON \
      -DWITH_GPU=OFF \
      -DWITH_MKL=OFF \
      -DWITH_LITE=ON \
      -DLITE_WITH_CUDA=OFF \
      -DLITE_WITH_X86=OFF \
      -DLITE_WITH_ARM=ON \
      -DWITH_ARM_DOTPROD=ON   \
      -DLITE_ON_TINY_PUBLISH=ON \
      -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=ON \
      -DWITH_TESTING=OFF \
179
      -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
180
      -DLITE_SHUTDOWN_LOG=$SHUTDOWN_LOG \
181
      -DLITE_WITH_CV=$BUILD_CV \
182 183
      -DARM_TARGET_OS=$1 -DARM_TARGET_ARCH_ABI=$2 -DARM_TARGET_LANG=$3

184 185
    make opencl_clhpp -j$NUM_PROC
    make publish_inference -j$NUM_PROC
186 187
}

Y
Yan Chunwei 已提交
188 189 190 191 192 193
function make_full_publish_so {
  local os=$1
  local abi=$2
  local lang=$3
  local android_stl=$4

194 195
  #git submodule update --init --recursive
  prepare_thirdparty
196

197 198 199 200
  root_dir=$(pwd)
  build_directory=$BUILD_DIR/build.lite.${os}.${abi}.${lang}

  if [ -d $build_directory ]
S
sangoly 已提交
201
  then
202
    rm -rf $build_directory
S
sangoly 已提交
203
  fi
204 205
  mkdir -p $build_directory
  cd $build_directory
206

207 208 209
  if [ ${os} == "armlinux" ]; then
    BUILD_JAVA=OFF
  fi
Y
Yan Chunwei 已提交
210

211 212
  prepare_workspace $root_dir $build_directory
  cmake $root_dir \
213
      ${PYTHON_FLAGS} \
Y
Yan Chunwei 已提交
214 215
      ${CMAKE_COMMON_OPTIONS} \
      -DWITH_TESTING=OFF \
216
      -DLITE_WITH_JAVA=$BUILD_JAVA \
217
      -DLITE_WITH_PYTHON=$BUILD_PYTHON \
218
      -DLITE_SHUTDOWN_LOG=$SHUTDOWN_LOG \
Y
Yan Chunwei 已提交
219
      -DANDROID_STL_TYPE=$android_stl \
220
      -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
221
      -DLITE_WITH_CV=$BUILD_CV \
H
HappyAngel 已提交
222
      -DLITE_WITH_ARM_LANG=$LITE_WITH_ARM_LANG \
223 224
      -DLITE_BUILD_TAILOR=$BUILD_TAILOR \
      -DLITE_OPTMODEL_DIR=$OPTMODEL_DIR \
225 226 227 228
      -DLITE_WITH_NPU=$BUILD_NPU \
      -DNPU_DDK_ROOT=$NPU_DDK_ROOT \
      -DLITE_WITH_XPU=$BUILD_XPU \
      -DXPU_SDK_ROOT=$XPU_SDK_ROOT \
Y
Yan Chunwei 已提交
229 230
      -DARM_TARGET_OS=${os} -DARM_TARGET_ARCH_ABI=${abi} -DARM_TARGET_LANG=${lang}

231
  make publish_inference -j$NUM_PROC
Y
Yan Chunwei 已提交
232 233 234 235 236 237 238 239
  cd - > /dev/null
}

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

240 241
  #git submodule update --init --recursive
  prepare_thirdparty
242 243
  root_dir=$(pwd)
  build_directory=$BUILD_DIR/build.lite.${os}.${abi}.${lang}
S
sangoly 已提交
244 245 246 247
  if [ -d $build_dir ]
  then
    rm -rf $build_dir
  fi
248 249
  mkdir -p $build_directory
  cd $build_directory
H
HappyAngel 已提交
250
 
251 252
  prepare_workspace $root_dir $build_directory
  cmake $root_dir \
Y
Yan Chunwei 已提交
253 254
      ${CMAKE_COMMON_OPTIONS} \
      -DWITH_TESTING=ON \
255
      -DLITE_BUILD_EXTRA=$BUILD_EXTRA \
256
      -DLITE_WITH_CV=$BUILD_CV \
H
HappyAngel 已提交
257
      -DLITE_WITH_ARM_LANG=$LITE_WITH_ARM_LANG \
258 259 260 261
      -DLITE_WITH_NPU=$BUILD_NPU \
      -DNPU_DDK_ROOT=$NPU_DDK_ROOT \
      -DLITE_WITH_XPU=$BUILD_XPU \
      -DXPU_SDK_ROOT=$XPU_SDK_ROOT \
Y
Yan Chunwei 已提交
262 263
      -DARM_TARGET_OS=${os} -DARM_TARGET_ARCH_ABI=${abi} -DARM_TARGET_LANG=${lang}

264
  make lite_compile_deps -j$NUM_PROC
Y
Yan Chunwei 已提交
265 266 267
  cd - > /dev/null
}

X
Xiaoyang LI 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
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 \
297
            -DLITE_WITH_CV=$BUILD_CV \
X
Xiaoyang LI 已提交
298 299
            -DARM_TARGET_OS=$os

300
    make publish_inference -j$NUM_PROC
X
Xiaoyang LI 已提交
301 302 303
    cd -
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
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 \
328
            -DLITE_WITH_PYTHON=${BUILD_PYTHON} \
H
huzhiqiang 已提交
329
            -DLITE_BUILD_EXTRA=ON
330
 
331
  make publish_inference -j$NUM_PROC
332 333
  cd -
}
Y
Yan Chunwei 已提交
334

H
huzhiqiang 已提交
335 336 337 338
function make_x86 {
  prepare_thirdparty

  root_dir=$(pwd)
339
  build_directory=$BUILD_DIR/build.lite.x86
H
huzhiqiang 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

  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=ON       \
            -DWITH_MKLDNN=OFF    \
            -DLITE_WITH_X86=ON  \
            -DLITE_WITH_PROFILE=OFF \
            -DWITH_LITE=ON \
            -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=OFF \
            -DLITE_WITH_ARM=OFF \
            -DWITH_GPU=OFF \
358 359 360
            -DLITE_BUILD_EXTRA=ON \
            -DLITE_WITH_XPU=$BUID_XPU \
            -DXPU_SDK_ROOT=$XPU_SDK_ROOT \
H
huzhiqiang 已提交
361

362
  make publish_inference -j$NUM_PROC
H
huzhiqiang 已提交
363 364 365
  cd -
}

Y
Yan Chunwei 已提交
366
function print_usage {
367
    set +x
Y
Yan Chunwei 已提交
368 369 370 371
    echo -e "\nUSAGE:"
    echo
    echo "----------------------------------------"
    echo -e "compile tiny publish so lib:"
X
Xiaoyang LI 已提交
372
    echo -e "for android:"
Y
Yan Chunwei 已提交
373
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> --arm_lang=<lang> --android_stl=<stl> tiny_publish"
X
Xiaoyang LI 已提交
374 375
    echo -e "for ios:"
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> ios"
Y
Yan Chunwei 已提交
376
    echo
X
Xiaoyang LI 已提交
377
    echo -e "compile full publish so lib (ios not support):"
Y
Yan Chunwei 已提交
378 379
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> --arm_lang=<lang> --android_stl=<stl> full_publish"
    echo
X
Xiaoyang LI 已提交
380
    echo -e "compile all arm tests (ios not support):"
Y
Yan Chunwei 已提交
381 382
    echo -e "   ./build.sh --arm_os=<os> --arm_abi=<abi> --arm_lang=<lang> test"
    echo
383
    echo -e "optional argument:"
384
    echo -e "--shutdown_log: (OFF|ON); controls whether to shutdown log, default is ON"
385
    echo -e "--build_extra: (OFF|ON); controls whether to publish extra operators and kernels for (sequence-related model such as OCR or NLP)"
386 387
    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)"
388
    echo -e "--build_dir: directory for building"
Y
Yan Chunwei 已提交
389
    echo
390
    echo -e "argument choices:"
X
Xiaoyang LI 已提交
391
    echo -e "--arm_os:\t android|ios|ios64"
Y
Yan Chunwei 已提交
392
    echo -e "--arm_abi:\t armv8|armv7"
393
    echo -e "--arm_lang:\t only support gcc now, clang will be supported in future.(for android)"
X
Xiaoyang LI 已提交
394
    echo -e "--android_stl:\t c++_static|c++_shared (for android)"
Y
Yan Chunwei 已提交
395 396 397 398 399 400
    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 已提交
401 402 403 404 405
    echo "----------------------------------------"
    echo
}

function main {
Y
Yan Chunwei 已提交
406 407 408 409 410
    if [ -z "$1" ]; then
        print_usage
        exit -1
    fi

Y
Yan Chunwei 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    # 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
                ;;
430 431 432 433
            --build_extra=*)
                BUILD_EXTRA="${i#*=}"
                shift
                ;;
Y
Yuan Shuai 已提交
434 435 436 437
            --build_cv=*)
                BUILD_CV="${i#*=}"
                shift
                ;;
438 439 440 441 442 443 444 445
            --build_python=*)
                BUILD_PYTHON="${i#*=}"
                shift
                ;;
            --build_java=*)
                BUILD_JAVA="${i#*=}"
                shift
                ;;
446 447 448 449
            --build_dir=*)
                BUILD_DIR="${i#*=}"
                shift
		            ;;
450 451 452 453 454 455 456 457
            --opt_model_dir=*)
                OPTMODEL_DIR="${i#*=}"
                shift
                ;;
            --build_tailor=*)
                BUILD_TAILOR="${i#*=}"
                shift
                ;;
458 459 460 461
            --shutdown_log=*)
                SHUTDOWN_LOG="${i#*=}"
                shift
                ;;
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
            --build_npu=*)
                BUILD_NPU="${i#*=}"
                shift
                ;;
           --npu_ddk_root=*)
                NPU_DDK_ROOT="${i#*=}"
                shift
                ;;
            --build_xpu=*)
                BUILD_XPU="${i#*=}"
                shift
                ;;
            --xpu_sdk_root=*)
                XPU_SDK_ROOT="${i#*=}"
                shift
                ;;
Y
Yan Chunwei 已提交
478
            tiny_publish)
479
                make_tiny_publish_so $ARM_OS $ARM_ABI $ARM_LANG $ANDROID_STL 
Y
Yan Chunwei 已提交
480 481 482
                shift
                ;;
            full_publish)
483
                make_full_publish_so $ARM_OS $ARM_ABI $ARM_LANG $ANDROID_STL 
Y
Yan Chunwei 已提交
484 485 486 487 488 489
                shift
                ;;
            test)
                make_all_tests $ARM_OS $ARM_ABI $ARM_LANG
                shift
                ;;
X
Xiaoyang LI 已提交
490 491 492 493
            ios)
                make_ios $ARM_OS $ARM_ABI
                shift
                ;;
494
            build_optimize_tool)
495
                build_opt
496 497
                shift
                ;;
498 499 500 501
            opencl)
                make_opencl $ARM_OS $ARM_ABI $ARM_LANG
                shift
                ;;
502
            cuda)
503
                make_cuda
504 505
                shift
                ;;
H
huzhiqiang 已提交
506 507 508 509
            x86)
               make_x86
               shift
               ;;
Y
Yan Chunwei 已提交
510 511 512 513 514 515 516 517 518 519
            *)
                # unknown option
                print_usage
                exit 1
                ;;
        esac
    done
}

main $@