build.sh 6.2 KB
Newer Older
朔-望's avatar
朔-望 已提交
1
#!/usr/bin/env bash
H
hanbuhe 已提交
2
NETS=""
3
declare -a supportedNets=("googlenet" "mobilenet" "yolo" "squeezenet" "resnet" "mobilenetssd" "nlp" "mobilenetfssd" "genet" "super" "op")
朔-望's avatar
朔-望 已提交
4

5 6 7 8 9 10 11 12 13
# merge cl to so
merge_cl_to_so=1
rm ../src/operators/kernel/cl/opencl_kernels.cpp
if [ $merge_cl_to_so == 1 ]; then
    cd ../src/operators/kernel/cl
    python gen_code.py > opencl_kernels.cpp
    cd -
fi

朔-望's avatar
朔-望 已提交
14
build_for_mac() {
朔-望's avatar
朔-望 已提交
15
    if [ ! `which brew` ]; then
朔-望's avatar
朔-望 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28
        echo "building failed! homebrew not found, please install homebrew."
        return
    fi
    if [ ! `which cmake` ]; then
        echo "installing cmake."
        brew install cmake
        if [ ! $? ]; then
            echo "cmake install failed."
            return
        fi
    fi
    PLATFORM="x86"
    MODE="Release"
W
wangliu 已提交
29
    BUILD_DIR=../build/release/"${PLATFORM}"
朔-望's avatar
朔-望 已提交
30 31 32
    mkdir -p ${BUILD_DIR}/build

    mkdir -p ${BUILD_DIR}/test
W
wangliu 已提交
33
    cp -r ../test/models ${BUILD_DIR}/test/models
朔-望's avatar
朔-望 已提交
34

W
wangliu 已提交
35
    cmake .. \
朔-望's avatar
朔-望 已提交
36 37 38 39 40 41 42 43 44
        -B"${BUILD_DIR}" \
    	-DCMAKE_BUILD_TYPE="${MODE}" \
    	-DIS_MAC=true

    cd ${BUILD_DIR}
    make -j 8
}

build_for_android() {
E
eclipsess 已提交
45
    #rm -rf "../build"
I
itminner 已提交
46 47
    if [ -z "${NDK_ROOT}" ]; then
        echo "NDK_ROOT not found!"
朔-望's avatar
朔-望 已提交
48 49 50
        exit -1
    fi

Y
Yi Wang 已提交
51
    if [ -z "$PLATFORM" ]; then
L
liuruilong 已提交
52 53
        PLATFORM="arm-v7a"  # Users could choose "arm-v8a" platform.
#        PLATFORM="arm-v8a"
Y
Yi Wang 已提交
54
    fi
朔-望's avatar
朔-望 已提交
55 56 57 58

    if [ "${PLATFORM}" = "arm-v7a" ]; then
        ABI="armeabi-v7a with NEON"
        ARM_PLATFORM="V7"
Z
zhaojiaying01 已提交
59
        CXX_FLAGS="-march=armv7-a -mfpu=neon -mfloat-abi=softfp -pie -fPIE -w -Wno-error=format-security"
朔-望's avatar
朔-望 已提交
60 61 62
    elif [ "${PLATFORM}" = "arm-v8a" ]; then
        ABI="arm64-v8a"
        ARM_PLATFORM="V8"
Z
zhaojiaying01 已提交
63
        CXX_FLAGS="-march=armv8-a  -pie -fPIE -w -Wno-error=format-security -llog"
朔-望's avatar
朔-望 已提交
64 65 66 67 68
    else
        echo "unknown platform!"
        exit -1
    fi

L
liuruilong 已提交
69

朔-望's avatar
朔-望 已提交
70
    MODE="Release"
71
    ANDROID_PLATFORM_VERSION="android-19"
I
itminner 已提交
72
    TOOLCHAIN_FILE="./tools/android-cmake/android.toolchain.cmake"
朔-望's avatar
朔-望 已提交
73
    ANDROID_ARM_MODE="arm"
H
hanbuhe 已提交
74

H
hanbuhe 已提交
75
    if [ "${#NETS}" -gt 1 ]; then
W
wangliu 已提交
76 77 78 79 80 81 82 83 84
    cmake .. \
        -B"../build/release/${PLATFORM}" \
        -DANDROID_ABI="${ABI}" \
        -DCMAKE_BUILD_TYPE="${MODE}" \
        -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
        -DANDROID_PLATFORM="${ANDROID_PLATFORM_VERSION}" \
        -DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \
        -DANDROID_STL=c++_static \
        -DANDROID=true \
H
hanbuhe 已提交
85
        -DNET="${NETS}" \
W
wangliu 已提交
86
        -D"${ARM_PLATFORM}"=true
L
liuruilong 已提交
87
    else
W
wangliu 已提交
88 89 90 91 92 93 94 95 96 97 98

    cmake .. \
        -B"../build/release/${PLATFORM}" \
        -DANDROID_ABI="${ABI}" \
        -DCMAKE_BUILD_TYPE="${MODE}" \
        -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
        -DANDROID_PLATFORM="${ANDROID_PLATFORM_VERSION}" \
        -DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \
        -DANDROID_STL=c++_static \
        -DANDROID=true \
        -D"${ARM_PLATFORM}"=true
L
liuruilong 已提交
99
    fi
100
    cd "../build/release/${PLATFORM}"
朔-望's avatar
朔-望 已提交
101
    make -j 8
L
liuruilong 已提交
102 103
    mkdir ./build/cl_kernel
    cp ../../../src/operators/kernel/cl/cl_kernel/*  ./build/cl_kernel/
朔-望's avatar
朔-望 已提交
104 105
}

xiebaiyuan's avatar
xiebaiyuan 已提交
106 107 108 109 110 111 112 113
build_for_arm_linux() {
    MODE="Release"
    ARM_LINUX="arm-linux"

    if [ "${#NETS}" -gt 1 ]; then
        cmake .. \
            -B"../build/release/arm-linux" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
xiebaiyuan's avatar
xiebaiyuan 已提交
114
            -DCMAKE_TOOLCHAIN_FILE="./tools/toolchains/arm-linux-gnueabihf.cmake" \
115
            -DCMAKE_CXX_FLAGS="-std=c++14 -mcpu=cortex-a53 -mtune=cortex-a53 -ftree-vectorize -funsafe-math-optimizations  -pipe -mlittle-endian " \
xiebaiyuan's avatar
xiebaiyuan 已提交
116 117 118 119 120 121 122
            -DNET="${NETS}" \
            -D"V7"=true
    else
        cmake .. \
            -B"../build/release/arm-linux" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DCMAKE_TOOLCHAIN_FILE="./tools/toolchains/arm-linux-gnueabihf.cmake" \
123
            -DCMAKE_CXX_FLAGS="-std=c++14 -mcpu=cortex-a53 -mtune=cortex-a53 -ftree-vectorize -funsafe-math-optimizations -pipe -mlittle-endian " \
xiebaiyuan's avatar
xiebaiyuan 已提交
124
            -DNET="${NETS}" \
xiebaiyuan's avatar
xiebaiyuan 已提交
125 126 127 128
            -D"V7"=true
    fi

    cd "../build/release/arm-linux"
129 130
    make -j 2

xiebaiyuan's avatar
xiebaiyuan 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144
    cd "../../../test/"
    DIRECTORY="models"
    if [ "`ls -A $DIRECTORY`" = "" ]; then
        echo "$DIRECTORY is indeed empty pull images"
        wget http://mms-graph.bj.bcebos.com/paddle-mobile%2FmodelsAndImages.zip
        unzip paddle-mobile%2FmodelsAndImages.zip
        mv modelsAndImages/images/ images
        mv modelsAndImages/models/ models
        rm -rf paddle-mobile%2FmodelsAndImages.zip
        rm -rf __MACOS
    else
        echo "$DIRECTORY is indeed not empty, DONE!"
    fi

xiebaiyuan's avatar
xiebaiyuan 已提交
145 146
}

朔-望's avatar
朔-望 已提交
147
build_for_ios() {
I
itminner 已提交
148
#    rm -rf "../build"
朔-望's avatar
朔-望 已提交
149 150
    PLATFORM="ios"
    MODE="Release"
L
liuruilong 已提交
151
    BUILD_DIR=../build/release/"${PLATFORM}"/
Z
zhaojiaying01 已提交
152
    TOOLCHAIN_FILE="./tools/ios-cmake/ios.toolchain.cmake"
朔-望's avatar
朔-望 已提交
153
    mkdir -p "${BUILD_DIR}"
H
hanbuhe 已提交
154
    if [ "${#NETS}" -gt 1 ]; then
155 156 157 158
        cmake .. \
            -B"${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DIOS_PLATFORM=OS \
159 160
            -DIOS_ARCH="${IOS_ARCH}" \
            -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
H
hanbuhe 已提交
161
            -DNET="${NETS}" \
162 163 164 165 166 167
            -DIS_IOS="true"
    else
        cmake .. \
            -B"${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DIOS_PLATFORM=OS \
168 169
            -DIOS_ARCH="${IOS_ARCH}" \
            -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
170 171
            -DIS_IOS="true"
    fi
朔-望's avatar
朔-望 已提交
172 173
    cd "${BUILD_DIR}"
    make -j 8
174
    cp ../../../src/io/ios_io/PaddleMobileCPU.h ./build/PaddleMobileCPU.h
I
itminner 已提交
175 176 177
    cd ./build
    # 生成符号表
    ranlib *.a
朔-望's avatar
朔-望 已提交
178 179 180
}

build_error() {
H
hanbuhe 已提交
181
    echo "unknown target : $1"
朔-望's avatar
朔-望 已提交
182 183 184
}

if [ $# -lt 1 ]; then
185
    echo "error: target missing!"
I
itminner 已提交
186 187
    echo "available targets: ios|android"
    echo "sample usage: ./build.sh android"
朔-望's avatar
朔-望 已提交
188
else
H
hanbuhe 已提交
189
    params=($@)
xiebaiyuan's avatar
xiebaiyuan 已提交
190
    for(( i=1; i<$#; i++ )); do
H
hanbuhe 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        if [ ${i} != 1 ]; then
            NETS=$NETS$";"
        fi
        NETS=$NETS$"${params[i]}"
    done
    params=${@:2}

    supported=false
    for name in ${params[@]}; do
        for net in ${supportedNets[@]}; do
            match=false
            if [ "$name"x = "$net"x ];then
                supported=true
                match=true
                break 1
            fi
        done
        if [ "$match" = false ];then
            echo "${name} not supported!"
            echo "supported nets are: ${supportedNets[@]}"
            exit -1
L
liuruilong 已提交
212
        fi
H
hanbuhe 已提交
213 214 215 216
    done

    if [ $1 = "android" ]; then
        build_for_android
xiebaiyuan's avatar
xiebaiyuan 已提交
217 218
    elif [ $1 = "arm_linux" ]; then
        build_for_arm_linux
H
hanbuhe 已提交
219 220
    elif [ $1 = "ios" ]; then
        build_for_ios
L
liuruilong 已提交
221
    else
H
hanbuhe 已提交
222 223
        build_error "$1"
    fi
224
fi