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

朔-望's avatar
朔-望 已提交
3 4 5 6
. /etc/profile
. ~/.bashrc
#. ~/.zshrc

朔-望's avatar
朔-望 已提交
7
build_for_linux() {
朔-望's avatar
朔-望 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	if [ ! `which brew` ]; then
        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"
    CXX_FLAGS="-std=c++11 -O3 -s"
    BUILD_DIR=build/release/"${PLATFORM}"
    mkdir -p ${BUILD_DIR}/build

    mkdir -p ${BUILD_DIR}/test
    cp -r test/models ${BUILD_DIR}/test/models

    cmake . \
        -B"${BUILD_DIR}" \
    	-DCMAKE_BUILD_TYPE="${MODE}" \
    	-DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \
    	-DIS_MAC=true

    cd ${BUILD_DIR}
    make -j 8
朔-望's avatar
朔-望 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
}

build_for_mac() {
	if [ ! `which brew` ]; then
        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"
    CXX_FLAGS="-std=c++11 -O3 -s"
    BUILD_DIR=build/release/"${PLATFORM}"
    mkdir -p ${BUILD_DIR}/build

    mkdir -p ${BUILD_DIR}/test
    cp -r test/models ${BUILD_DIR}/test/models

    cmake . \
        -B"${BUILD_DIR}" \
    	-DCMAKE_BUILD_TYPE="${MODE}" \
    	-DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \
    	-DIS_MAC=true

    cd ${BUILD_DIR}
    make -j 8
}

build_for_android() {
朔-望's avatar
朔-望 已提交
72 73
    if [ -z "${ANDROID_NDK}" ]; then
        echo "ANDROID_NDK not found!"
朔-望's avatar
朔-望 已提交
74 75 76
        exit -1
    fi

W
wangliu 已提交
77 78
    PLATFORM="arm-v7a"
#    PLATFORM="arm-v8a"
朔-望's avatar
朔-望 已提交
79 80 81 82

    if [ "${PLATFORM}" = "arm-v7a" ]; then
        ABI="armeabi-v7a with NEON"
        ARM_PLATFORM="V7"
W
wangliu 已提交
83
        CXX_FLAGS="-O3 -std=c++11 -s -march=armv7-a -mfpu=neon -mfloat-abi=softfp -pie -fPIE -w -Wno-error=format-security -llog"
朔-望's avatar
朔-望 已提交
84 85 86
    elif [ "${PLATFORM}" = "arm-v8a" ]; then
        ABI="arm64-v8a"
        ARM_PLATFORM="V8"
W
wangliu 已提交
87
        CXX_FLAGS="-O3 -std=c++11 -s -march=armv8-a  -pie -fPIE -w -Wno-error=format-security -llog"
朔-望's avatar
朔-望 已提交
88 89 90 91 92 93 94
    else
        echo "unknown platform!"
        exit -1
    fi

    MODE="Release"
    ANDROID_PLATFORM_VERSION="android-15"
Z
zhaojiaying01 已提交
95
    TOOLCHAIN_FILE="./tools/android-cmake/android.toolchain.cmake"
朔-望's avatar
朔-望 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    ANDROID_ARM_MODE="arm"

    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

    cd "./build/release/${PLATFORM}"
    make -j 8
}

build_for_ios() {
    PLATFORM="ios"
    MODE="Release"
    BUILD_DIR=build/release/"${PLATFORM}"
Z
zhaojiaying01 已提交
117
    TOOLCHAIN_FILE="./tools/ios-cmake/ios.toolchain.cmake"
朔-望's avatar
朔-望 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    C_FLAGS="-fobjc-abi-version=2 -fobjc-arc -isysroot ${CMAKE_OSX_SYSROOT}"
    CXX_FLAGS="-fobjc-abi-version=2 -fobjc-arc -std=gnu++11 -stdlib=libc++ -isysroot ${CMAKE_OSX_SYSROOT}"
    mkdir -p "${BUILD_DIR}"

    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" \

    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
	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
朔-望 已提交
155
fi