build.sh 6.6 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
# merge cl to so
6
merge_cl_to_so=1
7
opencl_kernels="opencl_kernels.cpp"
Y
Yanzhan Yang 已提交
8
cd ../src/operators/kernel/cl
9 10 11 12
if [[ -f "${opencl_kernels}" ]]; then
    rm "${opencl_kernels}"
fi
python gen_code.py "${merge_cl_to_so}" > "${opencl_kernels}"
Y
Yanzhan Yang 已提交
13
cd -
14

15 16
# get cl headers
opencl_header_dir="../third_party/opencl/OpenCL-Headers"
17
commit_id="320d7189b3e0e7b6a8fc5c10334c79ef364b5ef6"
18 19 20 21 22
if [[ -d "$opencl_header_dir" && -d "$opencl_header_dir/.git" ]]; then
    echo "pulling opencl headers"
    cd $opencl_header_dir
    git stash
    git pull
23
    git checkout $commit_id
24 25 26 27 28
    cd -
else
    echo "cloning opencl headers"
    rm -rf $opencl_header_dir
    git clone https://github.com/KhronosGroup/OpenCL-Headers $opencl_header_dir
29
    git checkout $commit_id
30 31
fi

朔-望's avatar
朔-望 已提交
32
build_for_mac() {
朔-望's avatar
朔-望 已提交
33
    if [ ! `which brew` ]; then
朔-望's avatar
朔-望 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
        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 已提交
47
    BUILD_DIR=../build/release/"${PLATFORM}"
朔-望's avatar
朔-望 已提交
48 49 50
    mkdir -p ${BUILD_DIR}/build

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

W
wangliu 已提交
53
    cmake .. \
朔-望's avatar
朔-望 已提交
54 55 56 57 58 59 60 61 62
        -B"${BUILD_DIR}" \
    	-DCMAKE_BUILD_TYPE="${MODE}" \
    	-DIS_MAC=true

    cd ${BUILD_DIR}
    make -j 8
}

build_for_android() {
63
    # rm -rf "../build"
I
itminner 已提交
64 65
    if [ -z "${NDK_ROOT}" ]; then
        echo "NDK_ROOT not found!"
朔-望's avatar
朔-望 已提交
66 67 68
        exit -1
    fi

Y
Yi Wang 已提交
69
    if [ -z "$PLATFORM" ]; then
L
liuruilong 已提交
70
        PLATFORM="arm-v7a"  # Users could choose "arm-v8a" platform.
71
        # PLATFORM="arm-v8a"
Y
Yi Wang 已提交
72
    fi
朔-望's avatar
朔-望 已提交
73 74 75 76

    if [ "${PLATFORM}" = "arm-v7a" ]; then
        ABI="armeabi-v7a with NEON"
        ARM_PLATFORM="V7"
Z
zhaojiaying01 已提交
77
        CXX_FLAGS="-march=armv7-a -mfpu=neon -mfloat-abi=softfp -pie -fPIE -w -Wno-error=format-security"
朔-望's avatar
朔-望 已提交
78 79 80
    elif [ "${PLATFORM}" = "arm-v8a" ]; then
        ABI="arm64-v8a"
        ARM_PLATFORM="V8"
81
        CXX_FLAGS="-march=armv8-a  -pie -fPIE -w -Wno-error=format-security -llog -fuse-ld=gold"
朔-望's avatar
朔-望 已提交
82 83 84 85 86
    else
        echo "unknown platform!"
        exit -1
    fi

L
liuruilong 已提交
87

朔-望's avatar
朔-望 已提交
88
    MODE="Release"
89
    ANDROID_PLATFORM_VERSION="android-19"
I
itminner 已提交
90
    TOOLCHAIN_FILE="./tools/android-cmake/android.toolchain.cmake"
朔-望's avatar
朔-望 已提交
91
    ANDROID_ARM_MODE="arm"
H
hanbuhe 已提交
92

H
hanbuhe 已提交
93
    if [ "${#NETS}" -gt 1 ]; then
W
wangliu 已提交
94 95 96 97 98 99 100 101 102
    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 已提交
103
        -DNET="${NETS}" \
W
wangliu 已提交
104
        -D"${ARM_PLATFORM}"=true
L
liuruilong 已提交
105
    else
W
wangliu 已提交
106 107 108 109 110 111 112 113 114 115 116

    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 已提交
117
    fi
118
    cd "../build/release/${PLATFORM}"
朔-望's avatar
朔-望 已提交
119
    make -j 8
L
liuruilong 已提交
120 121
    mkdir ./build/cl_kernel
    cp ../../../src/operators/kernel/cl/cl_kernel/*  ./build/cl_kernel/
朔-望's avatar
朔-望 已提交
122 123
}

xiebaiyuan's avatar
xiebaiyuan 已提交
124 125 126 127 128 129 130 131
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 已提交
132
            -DCMAKE_TOOLCHAIN_FILE="./tools/toolchains/arm-linux-gnueabihf.cmake" \
133
            -DCMAKE_CXX_FLAGS=" " \
xiebaiyuan's avatar
xiebaiyuan 已提交
134 135 136 137 138 139 140
            -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" \
141
            -DCMAKE_CXX_FLAGS=" " \
xiebaiyuan's avatar
xiebaiyuan 已提交
142
            -DNET="${NETS}" \
xiebaiyuan's avatar
xiebaiyuan 已提交
143 144 145 146
            -D"V7"=true
    fi

    cd "../build/release/arm-linux"
147 148
    make -j 2

xiebaiyuan's avatar
xiebaiyuan 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    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 已提交
163 164
}

朔-望's avatar
朔-望 已提交
165
build_for_ios() {
I
itminner 已提交
166
#    rm -rf "../build"
朔-望's avatar
朔-望 已提交
167 168
    PLATFORM="ios"
    MODE="Release"
L
liuruilong 已提交
169
    BUILD_DIR=../build/release/"${PLATFORM}"/
Z
zhaojiaying01 已提交
170
    TOOLCHAIN_FILE="./tools/ios-cmake/ios.toolchain.cmake"
朔-望's avatar
朔-望 已提交
171
    mkdir -p "${BUILD_DIR}"
H
hanbuhe 已提交
172
    if [ "${#NETS}" -gt 1 ]; then
173 174 175 176
        cmake .. \
            -B"${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DIOS_PLATFORM=OS \
177 178
            -DIOS_ARCH="${IOS_ARCH}" \
            -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
H
hanbuhe 已提交
179
            -DNET="${NETS}" \
180 181 182 183 184 185
            -DIS_IOS="true"
    else
        cmake .. \
            -B"${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DIOS_PLATFORM=OS \
186 187
            -DIOS_ARCH="${IOS_ARCH}" \
            -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
188 189
            -DIS_IOS="true"
    fi
朔-望's avatar
朔-望 已提交
190 191
    cd "${BUILD_DIR}"
    make -j 8
192
    cp ../../../src/io/ios_io/PaddleMobileCPU.h ./build/PaddleMobileCPU.h
I
itminner 已提交
193 194 195
    cd ./build
    # 生成符号表
    ranlib *.a
朔-望's avatar
朔-望 已提交
196 197 198
}

build_error() {
H
hanbuhe 已提交
199
    echo "unknown target : $1"
朔-望's avatar
朔-望 已提交
200 201 202
}

if [ $# -lt 1 ]; then
203
    echo "error: target missing!"
I
itminner 已提交
204 205
    echo "available targets: ios|android"
    echo "sample usage: ./build.sh android"
朔-望's avatar
朔-望 已提交
206
else
H
hanbuhe 已提交
207
    params=($@)
xiebaiyuan's avatar
xiebaiyuan 已提交
208
    for(( i=1; i<$#; i++ )); do
H
hanbuhe 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        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 已提交
230
        fi
H
hanbuhe 已提交
231 232 233 234
    done

    if [ $1 = "android" ]; then
        build_for_android
xiebaiyuan's avatar
xiebaiyuan 已提交
235 236
    elif [ $1 = "arm_linux" ]; then
        build_for_arm_linux
H
hanbuhe 已提交
237 238
    elif [ $1 = "ios" ]; then
        build_for_ios
L
liuruilong 已提交
239
    else
H
hanbuhe 已提交
240 241
        build_error "$1"
    fi
242
fi