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

build_for_mac() {
朔-望's avatar
朔-望 已提交
6
    if [ ! `which brew` ]; then
朔-望's avatar
朔-望 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19
        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 已提交
20
    BUILD_DIR=../build/release/"${PLATFORM}"
朔-望's avatar
朔-望 已提交
21 22 23
    mkdir -p ${BUILD_DIR}/build

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

W
wangliu 已提交
26
    cmake .. \
朔-望's avatar
朔-望 已提交
27 28 29 30 31 32 33 34 35
        -B"${BUILD_DIR}" \
    	-DCMAKE_BUILD_TYPE="${MODE}" \
    	-DIS_MAC=true

    cd ${BUILD_DIR}
    make -j 8
}

build_for_android() {
E
eclipsess 已提交
36
    #rm -rf "../build"
I
itminner 已提交
37 38
    if [ -z "${NDK_ROOT}" ]; then
        echo "NDK_ROOT not found!"
朔-望's avatar
朔-望 已提交
39 40 41
        exit -1
    fi

Y
Yi Wang 已提交
42
    if [ -z "$PLATFORM" ]; then
L
liuruilong 已提交
43 44
        PLATFORM="arm-v7a"  # Users could choose "arm-v8a" platform.
#        PLATFORM="arm-v8a"
Y
Yi Wang 已提交
45
    fi
朔-望's avatar
朔-望 已提交
46 47 48 49

    if [ "${PLATFORM}" = "arm-v7a" ]; then
        ABI="armeabi-v7a with NEON"
        ARM_PLATFORM="V7"
Z
zhaojiaying01 已提交
50
        CXX_FLAGS="-march=armv7-a -mfpu=neon -mfloat-abi=softfp -pie -fPIE -w -Wno-error=format-security"
朔-望's avatar
朔-望 已提交
51 52 53
    elif [ "${PLATFORM}" = "arm-v8a" ]; then
        ABI="arm64-v8a"
        ARM_PLATFORM="V8"
Z
zhaojiaying01 已提交
54
        CXX_FLAGS="-march=armv8-a  -pie -fPIE -w -Wno-error=format-security -llog"
朔-望's avatar
朔-望 已提交
55 56 57 58 59
    else
        echo "unknown platform!"
        exit -1
    fi

L
liuruilong 已提交
60

朔-望's avatar
朔-望 已提交
61
    MODE="Release"
I
itminner 已提交
62 63
    ANDROID_PLATFORM_VERSION="android-22"
    TOOLCHAIN_FILE="./tools/android-cmake/android.toolchain.cmake"
朔-望's avatar
朔-望 已提交
64
    ANDROID_ARM_MODE="arm"
H
hanbuhe 已提交
65

H
hanbuhe 已提交
66
    if [ "${#NETS}" -gt 1 ]; then
W
wangliu 已提交
67 68 69 70 71 72 73 74 75
    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 已提交
76
        -DNET="${NETS}" \
W
wangliu 已提交
77
        -D"${ARM_PLATFORM}"=true
L
liuruilong 已提交
78
    else
W
wangliu 已提交
79 80 81 82 83 84 85 86 87 88 89

    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 已提交
90
    fi
91
    cd "../build/release/${PLATFORM}"
朔-望's avatar
朔-望 已提交
92 93 94
    make -j 8
}

xiebaiyuan's avatar
xiebaiyuan 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

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}" \
#            -DCMAKE_TOOLCHAIN_FILE="./tools/toolchains/arm-linux-gnueabihf.cmake" \
            -DCMAKE_CXX_FLAGS="-std=c++14 -mcpu=cortex-a53 -mtune=cortex-a53 -mfpu=neon-vfpv4 -mfloat-abi=hard -ftree-vectorize -funsafe-math-optimizations  -pipe -mlittle-endian -munaligned-access" \
            -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" \
            -DCMAKE_CXX_FLAGS="-std=c++14 -mcpu=cortex-a53 -mtune=cortex-a53 -mfpu=neon-vfpv4 -mfloat-abi=hard -ftree-vectorize -funsafe-math-optimizations  -pipe -mlittle-endian -munaligned-access" \
            -D"V7"=true
    fi

    cd "../build/release/arm-linux"
    make -j 8
}

朔-望's avatar
朔-望 已提交
121
build_for_ios() {
I
itminner 已提交
122
#    rm -rf "../build"
朔-望's avatar
朔-望 已提交
123 124
    PLATFORM="ios"
    MODE="Release"
L
liuruilong 已提交
125
    BUILD_DIR=../build/release/"${PLATFORM}"/
Z
zhaojiaying01 已提交
126
    TOOLCHAIN_FILE="./tools/ios-cmake/ios.toolchain.cmake"
朔-望's avatar
朔-望 已提交
127
    mkdir -p "${BUILD_DIR}"
H
hanbuhe 已提交
128
    if [ "${#NETS}" -gt 1 ]; then
129 130 131 132
        cmake .. \
            -B"${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DIOS_PLATFORM=OS \
133 134
            -DIOS_ARCH="${IOS_ARCH}" \
            -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
H
hanbuhe 已提交
135
            -DNET="${NETS}" \
136 137 138 139 140 141
            -DIS_IOS="true"
    else
        cmake .. \
            -B"${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DIOS_PLATFORM=OS \
142 143
            -DIOS_ARCH="${IOS_ARCH}" \
            -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
144 145
            -DIS_IOS="true"
    fi
朔-望's avatar
朔-望 已提交
146 147
    cd "${BUILD_DIR}"
    make -j 8
L
liuruilong 已提交
148
    cp ../../../src/ios_io/PaddleMobile.h ./build/PaddleMobile.h
I
itminner 已提交
149 150 151
    cd ./build
    # 生成符号表
    ranlib *.a
朔-望's avatar
朔-望 已提交
152 153 154
}

build_error() {
H
hanbuhe 已提交
155
    echo "unknown target : $1"
朔-望's avatar
朔-望 已提交
156 157 158 159
}

if [ $# -lt 1 ]; then
	echo "error: target missing!"
I
itminner 已提交
160 161
    echo "available targets: ios|android"
    echo "sample usage: ./build.sh android"
朔-望's avatar
朔-望 已提交
162
else
H
hanbuhe 已提交
163
    params=($@)
xiebaiyuan's avatar
xiebaiyuan 已提交
164
    for(( i=1; i<$#; i++ )); do
H
hanbuhe 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        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 已提交
186
        fi
H
hanbuhe 已提交
187 188 189 190
    done

    if [ $1 = "android" ]; then
        build_for_android
xiebaiyuan's avatar
xiebaiyuan 已提交
191 192
    elif [ $1 = "arm_linux" ]; then
        build_for_arm_linux
H
hanbuhe 已提交
193 194
    elif [ $1 = "ios" ]; then
        build_for_ios
L
liuruilong 已提交
195
    else
H
hanbuhe 已提交
196 197
        build_error "$1"
    fi
I
itminner 已提交
198
fi