build.sh 8.6 KB
Newer Older
Y
Yan Chunwei 已提交
1
#!/bin/bash
2
set -ex
Y
Yan Chunwei 已提交
3

4
TESTS_FILE="./lite_tests.txt"
Y
Yan Chunwei 已提交
5
LIBS_FILE="./lite_libs.txt"
Y
Yan Chunwei 已提交
6 7

readonly common_flags="-DWITH_LITE=ON -DLITE_WITH_LIGHT_WEIGHT_FRAMEWORK=OFF -DWITH_PYTHON=OFF -DWITH_TESTING=ON -DLITE_WITH_ARM=OFF"
8

C
Chunwei 已提交
9 10
NUM_CORES_FOR_COMPILE=8

11 12 13 14 15 16 17
# 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_for_codegen {
    # in build directory
    mkdir -p ./paddle/fluid/lite/gen_code
    touch ./paddle/fluid/lite/gen_code/__generated_code__.cc
}
S
up  
superjomn 已提交
18 19 20 21 22

function check_need_ci {
    git log -1 --oneline | grep "test=develop" || exit -1
}

Y
Yan Chunwei 已提交
23
function cmake_x86 {
24
    prepare_for_codegen
25
    cmake ..  -DWITH_GPU=OFF -DWITH_MKLDNN=OFF -DLITE_WITH_X86=ON ${common_flags}
Y
Yan Chunwei 已提交
26 27
}

C
Chunwei 已提交
28
# This method is only called in CI.
Y
Yan Chunwei 已提交
29
function cmake_x86_for_CI {
C
Chunwei 已提交
30
    prepare_for_codegen # fake an empty __generated_code__.cc to pass cmake.
Y
Yan Chunwei 已提交
31
    cmake ..  -DWITH_GPU=OFF -DWITH_MKLDNN=OFF -DLITE_WITH_X86=ON ${common_flags} -DLITE_WITH_PROFILE=ON
C
Chunwei 已提交
32 33 34 35 36 37 38

    # Compile and execute the gen_code related test, so it will generate some code, and make the compilation reasonable.
    make test_gen_code_lite -j$NUM_CORES_FOR_COMPILE
    make test_cxx_api_lite -j$NUM_CORES_FOR_COMPILE
    ctest -R test_cxx_api_lite
    ctest -R test_gen_code_lite
    make test_generated_code -j$NUM_CORES_FOR_COMPILE
Y
Yan Chunwei 已提交
39 40
}

Y
Yan Chunwei 已提交
41
function cmake_gpu {
42
    prepare_for_codegen
Y
Yan Chunwei 已提交
43 44 45
    cmake .. " -DWITH_GPU=ON {common_flags} -DLITE_WITH_GPU=ON"
}

S
up  
superjomn 已提交
46 47
function check_style {
    export PATH=/usr/bin:$PATH
S
update  
superjomn 已提交
48
    #pre-commit install
S
up  
superjomn 已提交
49 50 51 52 53 54 55 56
    clang-format --version

    if ! pre-commit run -a ; then
        git diff
        exit 1
    fi
}

Y
Yan Chunwei 已提交
57
function cmake_arm {
T
tensor-tang 已提交
58
    # $1: ARM_TARGET_OS in "android" , "armlinux"
T
tensor-tang 已提交
59
    # $2: ARM_TARGET_ARCH_ABI in "armv8", "armv7" ,"armv7hf"
T
tensor-tang 已提交
60 61 62 63 64 65 66 67 68 69
    cmake .. \
        -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 \
        -DWITH_TESTING=ON \
        -DARM_TARGET_OS=$1 -DARM_TARGET_ARCH_ABI=$2
Y
Yan Chunwei 已提交
70 71
}

T
tensor-tang 已提交
72
function build_single {
73
    #make $1 -j$(expr $(nproc) - 2)
C
Chunwei 已提交
74
    make $1 -j$NUM_CORES_FOR_COMPILE
T
tensor-tang 已提交
75 76
}

Y
Yan Chunwei 已提交
77
function build {
C
Chunwei 已提交
78
    make lite_compile_deps -j $NUM_CORES_FOR_COMPILE
Y
Yan Chunwei 已提交
79
}
Y
Yan Chunwei 已提交
80 81 82 83 84

# It will eagerly test all lite related unittests.
function test_lite {
    local file=$1
    echo "file: ${file}"
Y
Yan Chunwei 已提交
85

Y
Yan Chunwei 已提交
86 87 88 89 90
    for _test in $(cat $file); do
        ctest -R $_test -V
    done
}

91 92 93 94 95
# Build the code and run lite server tests. This is executed in the CI system.
function build_test_server {
    mkdir -p ./build
    cd ./build
    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/paddle/build/third_party/install/mklml/lib"
Y
Yan Chunwei 已提交
96
    cmake_x86_for_CI
C
Chunwei 已提交
97
    build
98 99 100
    test_lite $TESTS_FILE
}

T
tensor-tang 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
# test_arm_android <some_test_name> <adb_port_number>
function test_arm_android {
    test_name=$1
    port=$2
    if [[ "${test_name}x" == "x" ]]; then
        echo "test_name can not be empty"
        exit 1
    fi
    if [[ "${port}x" == "x" ]]; then
        echo "Port can not be empty"
        exit 1
    fi

    echo "test name: ${test_name}"
    adb_work_dir="/data/local/tmp"
    skip_list="test_model_parser_lite" # add more with space
    [[ $skip_list =~ (^|[[:space:]])$test_name($|[[:space:]]) ]] && continue || echo 'skip $test_name'
    testpath=$(find ./paddle/fluid -name ${test_name})
    adb -s emulator-${port} push ${testpath} ${adb_work_dir}
    adb -s emulator-${port} shell chmod +x "${adb_work_dir}/${test_name}"
    adb -s emulator-${port} shell "./${adb_work_dir}/${test_name}"
}

# Build the code and run lite arm tests. This is executed in the CI system.
T
tensor-tang 已提交
125
function build_test_arm {
T
tensor-tang 已提交
126
    # 1. Build goes first
C
Chunwei 已提交
127
    cur_dir=$(pwd)
T
tensor-tang 已提交
128
    for os in "android" "armlinux" ; do
T
tensor-tang 已提交
129 130 131 132 133 134 135 136 137 138
        for abi in "armv8" "armv7" "armv7hf"; do 
            # TODO(hongming): enable compile armv7 and armv7hf on armlinux
            if [[ ${abi} == "armv7hf" ]]; then
                echo "armv7hf is not supported on both android and armlinux yet"
                continue
            fi
            
            # TODO(hongming): enable armv7 on armlinux
            if [[ ${os} == "armlinux" && ${abi} == "armv7" ]]; then
                echo "armv7 is not supported on armlinux yet"
T
tensor-tang 已提交
139 140 141
                continue
            fi

T
tensor-tang 已提交
142 143
            if [[ ${os} == "android" && ${abi} == "armv7hf" ]]; then
                echo "android do not need armv7hf"
T
tensor-tang 已提交
144 145 146
                continue
            fi

C
Chunwei 已提交
147
            build_dir=$cur_dir/build.lite.${os}.${abi}
T
tensor-tang 已提交
148 149
            mkdir -p $build_dir
            cd $build_dir
C
Chunwei 已提交
150

T
tensor-tang 已提交
151 152
            cmake_arm ${os} ${abi}
            build $TESTS_FILE
T
tensor-tang 已提交
153 154
        done
    done
T
tensor-tang 已提交
155

T
tensor-tang 已提交
156 157 158 159 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
    # 2. Then test
    port_armv8=5554
    port_armv7=5556

    adb kill-server
    adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
    # start android armv8 and armv7 emulators first
    echo n | avdmanager create avd -f -n paddle-armv8 -k "system-images;android-24;google_apis;arm64-v8a"
    echo -ne '\n' | ${ANDROID_HOME}/emulator/emulator -avd paddle-armv8 -noaudio -no-window -gpu off -verbose -port ${port_armv8} &
    sleep 1m
    echo n | avdmanager create avd -f -n paddle-armv7 -k "system-images;android-24;google_apis;armeabi-v7a"
    echo -ne '\n' | ${ANDROID_HOME}/emulator/emulator -avd paddle-armv7 -noaudio -no-window -gpu off -verbose -port ${port_armv7} &
    sleep 1m

    # now can only test android.
    for abi in "armv8" "armv7" ; do
        # TODO(yuanshuai): enable armv7 on android
        if [[ ${abi} == "armv7" ]]; then
            continue
        fi

        build_dir=$cur_dir/build.lite.android.${abi}
        cd $build_dir

        local port=
        if [[ ${abi} == "armv7" ]]; then
            port=${port_armv7}
        fi

        if [[ ${abi} == "armv8" ]]; then
            port=${port_armv8}
        fi
        echo "test file: ${TESTS_FILE}"
        for _test in $(cat $TESTS_FILE); do
            test_arm_android $_test $port
T
tensor-tang 已提交
191 192
        done
    done
T
tensor-tang 已提交
193 194 195 196

    # armlinux need in another docker
    # TODO(hongming): enable test armlinux on armv8, armv7 and armv7hf

T
tensor-tang 已提交
197 198 199 200
    adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
    echo "Done"
}

Y
Yan Chunwei 已提交
201 202 203 204 205 206 207
############################# MAIN #################################
function print_usage {
    echo -e "\nUSAGE:"
    echo
    echo "----------------------------------------"
    echo -e "cmake_x86: run cmake with X86 mode"
    echo -e "cmake_cuda: run cmake with CUDA mode"
T
tensor-tang 已提交
208
    echo -e "--arm_os=<os> --arm_abi=<abi> cmake_arm: run cmake with ARM mode"
Y
Yan Chunwei 已提交
209 210
    echo
    echo -e "build: compile the tests"
T
tensor-tang 已提交
211
    echo -e "--test_name=<test_name> build_single: compile single test"
Y
Yan Chunwei 已提交
212 213
    echo
    echo -e "test_server: run server tests"
T
tensor-tang 已提交
214
    echo -e "--test_name=<test_name> --adb_port_number=<adb_port_number> test_arm_android: run arm test"
Y
Yan Chunwei 已提交
215 216 217 218 219 220 221 222 223 224 225 226
    echo "----------------------------------------"
    echo
}

function main {
    # Parse command line.
    for i in "$@"; do
        case $i in
            --tests=*)
                TESTS_FILE="${i#*=}"
                shift
                ;;
T
tensor-tang 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
            --test_name=*)
                TEST_NAME="${i#*=}"
                shift
                ;;
            --arm_os=*)
                ARM_OS="${i#*=}"
                shift
                ;;
            --arm_abi=*)
                ARM_ABI="${i#*=}"
                shift
                ;;
            --arm_port=*)
                ARM_PORT="${i#*=}"
                shift
                ;;
Y
Yan Chunwei 已提交
243 244 245 246 247
            build)
                build $TESTS_FILE
                build $LIBS_FILE
                shift
                ;;
T
tensor-tang 已提交
248 249 250 251
            build_single)
                build_single $TEST_NAME
                shift
                ;;
Y
Yan Chunwei 已提交
252 253 254 255 256 257 258 259 260
            cmake_x86)
                cmake_x86
                shift
                ;;
            cmake_cuda)
                cmake_cuda
                shift
                ;;
            cmake_arm)
T
tensor-tang 已提交
261
                cmake_arm $ARM_OS $ARM_ABI
Y
Yan Chunwei 已提交
262 263 264 265 266 267
                shift
                ;;
            test_server)
                test_lite $TESTS_FILE
                shift
                ;;
T
tensor-tang 已提交
268 269
            test_arm_android)
                test_arm_android $TEST_NAME $ARM_PORT
Y
Yan Chunwei 已提交
270 271
                shift
                ;;
272 273 274 275
            build_test_server)
                build_test_server
                shift
                ;;
T
tensor-tang 已提交
276 277 278 279
            build_test_arm)
                build_test_arm
                shift
                ;;
S
up  
superjomn 已提交
280 281 282 283
            check_style)
                check_style
                shift
                ;;
S
up  
superjomn 已提交
284 285 286 287
            check_need_ci)
                check_need_ci
                shift
                ;;
Y
Yan Chunwei 已提交
288 289 290 291 292 293 294 295 296 297
            *)
                # unknown option
                print_usage
                exit 1
                ;;
        esac
    done
}

main $@
T
tensor-tang 已提交
298