build.sh 4.3 KB
Newer Older
朔-望's avatar
朔-望 已提交
1
#!/usr/bin/env bash
朔-望's avatar
朔-望 已提交
2 3

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

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

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

    cd ${BUILD_DIR}
    make -j 8
}

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

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

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

L
liuruilong 已提交
58

朔-望's avatar
朔-望 已提交
59
    MODE="Release"
I
itminner 已提交
60 61
    ANDROID_PLATFORM_VERSION="android-22"
    TOOLCHAIN_FILE="./tools/android-cmake/android.toolchain.cmake"
朔-望's avatar
朔-望 已提交
62
    ANDROID_ARM_MODE="arm"
L
liuruilong 已提交
63
    if [ $# -eq 1 ]; then
W
wangliu 已提交
64 65 66 67 68 69 70 71 72
    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 \
I
itminner 已提交
73
        -DNET=$1 \
W
wangliu 已提交
74
        -D"${ARM_PLATFORM}"=true
L
liuruilong 已提交
75
    else
W
wangliu 已提交
76 77 78 79 80 81 82 83 84 85 86

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

build_for_ios() {
I
itminner 已提交
93
#    rm -rf "../build"
朔-望's avatar
朔-望 已提交
94 95
    PLATFORM="ios"
    MODE="Release"
96 97 98 99
#    IOS_ARCH="armv7"
#    IOS_ARCH="armv7s"
    IOS_ARCH="arm64"    # Users could choose "armv7" or "armv7s" platforms.
    BUILD_DIR=../build/release/"${PLATFORM}"/"${IOS_ARCH}"
Z
zhaojiaying01 已提交
100
    TOOLCHAIN_FILE="./tools/ios-cmake/ios.toolchain.cmake"
朔-望's avatar
朔-望 已提交
101
    mkdir -p "${BUILD_DIR}"
102 103 104 105 106
    if [ $# -eq 1 ]; then
        cmake .. \
            -B"${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DIOS_PLATFORM=OS \
107 108
            -DIOS_ARCH="${IOS_ARCH}" \
            -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
I
itminner 已提交
109
            -DNET=$1 \
110 111 112 113 114 115
            -DIS_IOS="true"
    else
        cmake .. \
            -B"${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DIOS_PLATFORM=OS \
116 117
            -DIOS_ARCH="${IOS_ARCH}" \
            -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
118 119
            -DIS_IOS="true"
    fi
朔-望's avatar
朔-望 已提交
120 121
    cd "${BUILD_DIR}"
    make -j 8
L
liuruilong 已提交
122
    cp ../../../src/ios_io/PaddleMobile.h ./build/PaddleMobile.h
I
itminner 已提交
123 124 125
    cd ./build
    # 生成符号表
    ranlib *.a
朔-望's avatar
朔-望 已提交
126 127 128 129 130 131 132 133
}

build_error() {
    echo "unknown argument"
}

if [ $# -lt 1 ]; then
	echo "error: target missing!"
I
itminner 已提交
134 135
    echo "available targets: ios|android"
    echo "sample usage: ./build.sh android"
朔-望's avatar
朔-望 已提交
136
else
L
liuruilong 已提交
137
    if [ $# -eq 2 ]; then
138
        if [ $2 != "googlenet" -a $2 != "mobilenet" -a $2 != "yolo" -a $2 != "squeezenet" -a $2 != "resnet" ]; then
I
itminner 已提交
139
	        if [ $1 = "android" ]; then
L
liuruilong 已提交
140 141 142 143 144 145 146
		        build_for_android
	        elif [ $1 = "ios" ]; then
		        build_for_ios
	        else
		        build_error
	        fi
        else
I
itminner 已提交
147
	        if [ $1 = "android" ]; then
L
liuruilong 已提交
148 149 150 151 152 153 154 155
		        build_for_android $2
	        elif [ $1 = "ios" ]; then
		        build_for_ios $2
	        else
		        build_error
	        fi
        fi
    else
I
itminner 已提交
156
	    if [ $1 = "android" ]; then
L
liuruilong 已提交
157 158 159 160 161 162
		    build_for_android
	    elif [ $1 = "ios" ]; then
		    build_for_ios
	    else
		    build_error
	    fi
朔-望's avatar
朔-望 已提交
163
	fi
I
itminner 已提交
164
fi