build.sh 5.0 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"
S
sharper 已提交
18
    CXX_FLAGS="-std=c++11 -O3 -s"
W
wangliu 已提交
19
    BUILD_DIR=../build/release/"${PLATFORM}"
朔-望's avatar
朔-望 已提交
20 21 22
    mkdir -p ${BUILD_DIR}/build

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

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

    cd ${BUILD_DIR}
    make -j 8
}

build_for_android() {
S
sharper 已提交
36 37
    export ANDROID_NDK=/home/halsay/android-ndk-r16b
    export NDK_ROOT=/home/halsay/android-ndk-r16b
38
    rm -rf "../build"
朔-望's avatar
朔-望 已提交
39 40
    if [ -z "${ANDROID_NDK}" ]; then
        echo "ANDROID_NDK not found!"
朔-望's avatar
朔-望 已提交
41 42 43
        exit -1
    fi

Y
Yi Wang 已提交
44 45 46
    if [ -z "$PLATFORM" ]; then
        PLATFORM="arm-v7a"  # Users could choose "arm-v8a" or other platforms from the command line.
    fi
朔-望's avatar
朔-望 已提交
47 48 49 50

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

L
liuruilong 已提交
61

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

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

build_for_ios() {
97
    rm -rf "../build"
朔-望's avatar
朔-望 已提交
98 99
    PLATFORM="ios"
    MODE="Release"
W
wangliu 已提交
100
    BUILD_DIR=../build/release/"${PLATFORM}"
Z
zhaojiaying01 已提交
101
    TOOLCHAIN_FILE="./tools/ios-cmake/ios.toolchain.cmake"
朔-望's avatar
朔-望 已提交
102
    C_FLAGS="-fobjc-abi-version=2 -fobjc-arc -isysroot ${CMAKE_OSX_SYSROOT}"
L
liuruilong 已提交
103
    CXX_FLAGS="-fobjc-abi-version=2 -fobjc-arc -std=gnu++14 -stdlib=libc++ -isysroot ${CMAKE_OSX_SYSROOT}"
朔-望's avatar
朔-望 已提交
104
    mkdir -p "${BUILD_DIR}"
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    if [ $# -eq 1 ]; then
        NET=$1
        cmake .. \
            -B"${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
            -DIOS_PLATFORM=OS \
            -DCMAKE_C_FLAGS="${C_FLAGS}" \
            -DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \
            -D"${NET}"=true \
            -DIS_IOS="true"
    else
        cmake .. \
            -B"${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${MODE}" \
            -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
            -DIOS_PLATFORM=OS \
            -DCMAKE_C_FLAGS="${C_FLAGS}" \
            -DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \
            -DIS_IOS="true"
    fi
朔-望's avatar
朔-望 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138
    cd "${BUILD_DIR}"
    make -j 8
}

build_error() {
    echo "unknown argument"
}

if [ $# -lt 1 ]; then
	echo "error: target missing!"
    echo "available targets: mac|linux|ios|android"
    echo "sample usage: ./build.sh mac"
else
L
liuruilong 已提交
139
    if [ $# -eq 2 ]; then
140
        if [ $2 != "googlenet" -a $2 != "mobilenet" -a $2 != "yolo" -a $2 != "squeezenet" -a $2 != "resnet" ]; then
L
liuruilong 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            if [ $1 = "mac" ]; then
		        build_for_mac
	        elif [ $1 = "linux" ]; then
		        build_for_linux
	        elif [ $1 = "android" ]; then
		        build_for_android
	        elif [ $1 = "ios" ]; then
		        build_for_ios
	        else
		        build_error
	        fi
        else
            if [ $1 = "mac" ]; then
		        build_for_mac $2
	        elif [ $1 = "linux" ]; then
		        build_for_linux $2
	        elif [ $1 = "android" ]; then
		        build_for_android $2
	        elif [ $1 = "ios" ]; then
		        build_for_ios $2
	        else
		        build_error
	        fi
        fi
    else
        if [ $1 = "mac" ]; then
		    build_for_mac
	    elif [ $1 = "linux" ]; then
		    build_for_linux
	    elif [ $1 = "android" ]; then
		    build_for_android
	    elif [ $1 = "ios" ]; then
		    build_for_ios
	    else
		    build_error
	    fi
朔-望's avatar
朔-望 已提交
177
	fi
S
sharper 已提交
178
fi