host_build.sh 9.5 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/env bash
set -e

function usage() {
    echo "$0 args1 args2 .."
    echo "available args detail:"
7
    echo "-d : Build with Debug mode, default Release mode"
8 9
    echo "-c : Build with CUDA, default without CUDA"
    echo "-t : Build with training mode, default inference only"
10
    echo "-m : Build with m32 mode(only for windows build), default m64"
11
    echo "-r : remove old build dir before make, default off"
12 13 14
    echo "-v : ninja with verbose and explain, default off"
    echo "-s : Do not build develop even build with training mode, default on when build with training, always for wheel"
    echo "-n : ninja with -n dry run (don't run commands but act like they succeeded)"
15
    echo "-e : build a specified target (always for debug, NOTICE: do not do strip/install target when use -e)"
16
    echo "-h : show usage"
17
    echo "append other cmake config by export EXTRA_CMAKE_ARGS=..."
18 19 20 21
    echo "example: $0 -d"
    exit -1
}

22 23
READLINK=readlink
OS=$(uname -s)
24 25 26
BUILD_TYPE=Release
MGE_WITH_CUDA=OFF
MGE_INFERENCE_ONLY=ON
27 28 29
MGE_WINDOWS_BUILD_ARCH=x64
MGE_WINDOWS_BUILD_MARCH=m64
MGE_ARCH=x86_64
30
REMOVE_OLD_BUILD=false
31 32 33
NINJA_VERBOSE=OFF
BUILD_DEVELOP=ON
NINJA_DRY_RUN=OFF
34 35 36 37 38
SPECIFIED_TARGET="install/strip"
if [[ $OS =~ "NT" ]]; then
    echo "Windows do not support strip/install by defaut"
    SPECIFIED_TARGET=""
fi
39

40
echo "EXTRA_CMAKE_ARGS: ${EXTRA_CMAKE_ARGS}"
41

42
while getopts "nsrhdctmve:" arg
43 44 45 46 47 48 49 50 51 52 53 54 55 56
do
    case $arg in
        d)
            echo "Build with Debug mode"
            BUILD_TYPE=Debug
            ;;
        c)
            echo "Build with CUDA"
            MGE_WITH_CUDA=ON
            ;;
        t)
            echo "Build with training mode"
            MGE_INFERENCE_ONLY=OFF
            ;;
57 58 59 60
        h)
            echo "show usage"
            usage
            ;;
61 62 63 64
        r)
            echo "config REMOVE_OLD_BUILD=true"
            REMOVE_OLD_BUILD=true
            ;;
65 66 67 68 69 70 71 72 73 74 75 76
        s)
            echo "config  BUILD_DEVELOP=OFF"
            BUILD_DEVELOP=OFF
            ;;
        v)
            echo "config NINJA_VERBOSE=ON"
            NINJA_VERBOSE=ON
            ;;
        n)
            echo "config NINJA_DRY_RUN=ON"
            NINJA_DRY_RUN=ON
            ;;
77
        m)
78
            echo "build for m32(only valid use for windows)"
79 80 81 82
            MGE_WINDOWS_BUILD_ARCH=x86
            MGE_WINDOWS_BUILD_MARCH=m32
            MGE_ARCH=i386
            ;;
83 84 85
        e)
            SPECIFIED_TARGET=$OPTARG
            ;;
86 87 88 89 90 91 92 93 94 95 96
        ?)
            echo "unkonw argument"
            usage
            ;;
    esac
done
echo "------------------------------------"
echo "build config summary:"
echo "BUILD_TYPE: $BUILD_TYPE"
echo "MGE_WITH_CUDA: $MGE_WITH_CUDA"
echo "MGE_INFERENCE_ONLY: $MGE_INFERENCE_ONLY"
97
echo "SPECIFIED_TARGET: ${SPECIFIED_TARGET}"
98 99 100 101 102 103 104 105
echo "------------------------------------"

if [ $OS = "Darwin" ];then
    READLINK=greadlink
    if [ $MGE_WITH_CUDA = "ON" ];then
        echo "MACOS DO NOT SUPPORT TensorRT, ABORT NOW!!"
        exit -1
    fi
106 107
elif [[ $OS =~ "NT" ]]; then
    echo "BUILD in NT ..."
108 109 110
fi

SRC_DIR=$($READLINK -f "`dirname $0`/../../")
111
source $SRC_DIR/scripts/cmake-build/utils/utils.sh
112

113 114 115 116 117
if [ ${MGE_INFERENCE_ONLY} = "ON" ]; then
    echo "config BUILD_DEVELOP=OFF when MGE_INFERENCE_ONLY=ON"
    BUILD_DEVELOP=OFF
fi

118 119 120 121 122 123 124 125 126 127 128
function cmake_build() {
    BUILD_DIR=$SRC_DIR/build_dir/host/MGE_WITH_CUDA_$1/MGE_INFERENCE_ONLY_$2/$3/build
    INSTALL_DIR=$BUILD_DIR/../install
    MGE_WITH_CUDA=$1
    MGE_INFERENCE_ONLY=$2
    BUILD_TYPE=$3
    echo "build dir: $BUILD_DIR"
    echo "install dir: $INSTALL_DIR"
    echo "build type: $BUILD_TYPE"
    echo "MGE_WITH_CUDA: $MGE_WITH_CUDA"
    echo "MGE_INFERENCE_ONLY: $MGE_INFERENCE_ONLY"
129
    try_remove_old_build $REMOVE_OLD_BUILD $BUILD_DIR $INSTALL_DIR
130 131 132 133

    echo "create build dir"
    mkdir -p $BUILD_DIR
    mkdir -p $INSTALL_DIR
134
    cd_real_build_dir $BUILD_DIR
135 136
    # fork a new bash to handle EXTRA_CMAKE_ARGS env with space
    bash -c "cmake -G Ninja \
137 138 139 140
        -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
        -DMGE_INFERENCE_ONLY=$MGE_INFERENCE_ONLY \
        -DMGE_WITH_CUDA=$MGE_WITH_CUDA \
        -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
141
        ${EXTRA_CMAKE_ARGS} \
142
        ${SRC_DIR} "
143

144
    config_ninja_target_cmd ${NINJA_VERBOSE} ${BUILD_DEVELOP} "${SPECIFIED_TARGET}" ${NINJA_DRY_RUN}
145
    bash -c "${NINJA_CMD}"
146 147 148 149
}

function windows_env_err() {
    echo "check windows env failed!!"
150
    echo "please install env refs for: scripts/cmake-build/BUILD_README.md"
151 152 153 154 155 156 157 158 159 160 161 162
    exit -1
}

function prepare_env_for_windows_build() {
    echo "check Visual Studio install path env..."
    if [[ -z $VS_PATH ]];then
        echo "can not find visual_studio_path env, pls export you Visual Studio install dir to VS_PATH"
        echo "examle for export Visual Studio 2019 Enterprise default install dir"
        echo "export VS_PATH=/c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio/2019/Enterprise"
        exit -1
    fi
    echo $VS_PATH
163

164
    # only use cmake/clang-cl/Ninja install from Visual Studio, if not, may build failed
165
    # some user env may install cmake/clang-cl/Ninja at windows-git-bash env, so we put Visual Studio
166 167 168 169 170 171 172 173 174 175 176
    # path at the head of PATH, and check the valid
    echo "check cmake install..."
    export PATH=$VS_PATH/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/:$PATH
    which cmake
    cmake_loc=`which cmake`
    if [[ $cmake_loc =~ "Visual" ]]; then
        echo "cmake valid ..."
    else
        echo "cmake Invalid: ..."
        windows_env_err
    fi
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    echo "check clang-cl install..."
    export PATH=$VS_PATH/VC/Tools/Llvm/bin/:$PATH
    which clang-cl
    clang_loc=`which clang-cl`
    if [[ $clang_loc =~ "Visual" ]]; then
        echo "clang-cl valid ..."
    else
        echo "clang-cl Invalid: ..."
        windows_env_err
    fi

    echo "check Ninja install..."
    export PATH=$VS_PATH/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja/:$PATH
    which Ninja
    ninja_loc=`which Ninja`
    if [[ $ninja_loc =~ "Visual" ]]; then
        echo "Ninja valid ..."
    else
        echo "Ninja Invalid: ..."
197
        windows_env_err
198 199 200
    fi

    echo "put vcvarsall.bat path to PATH env.."
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    export PATH=$VS_PATH/VC/Auxiliary/Build:$PATH

    echo "config cuda/cudnn/TensorRT env..."
    export NIVIDA_INSTALL_PRE=/c/Program\ Files/NVIDIA\ GPU\ Computing\ Toolkit
    export CUDA_V=v10.1
    export CUDNN_V=cudnn-10.1-windows10-x64-v7.6.5.32
    export TRT_V=TensorRT-6.0.1.5
    export CUDA_PATH=$NIVIDA_INSTALL_PRE/CUDA/${CUDA_V}
    export PATH=$PATH:$CUDA_PATH/bin
    export CUDA_BIN_PATH=$CUDA_PATH
    export PC_CUDNN_INCLUDE_DIRS=$NIVIDA_INSTALL_PRE/${CUDNN_V}/cuda/include
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$NIVIDA_INSTALL_PRE/${TRT_V}/lib:$NIVIDA_INSTALL_PRE/CUDA/${CUDA_V}/lib/x64:$NIVIDA_INSTALL_PRE/${CUDNN_V}/cuda/lib/x64
    export CPATH=$CPATH:$NIVIDA_INSTALL_PRE/${TRT_V}/include:$NIVIDA_INSTALL_PRE/CUDA/${CUDA_V}/include:$NIVIDA_INSTALL_PRE/CUDA/${CUDA_V}/include/nvtx3:$PC_CUDNN_INCLUDE_DIRS
    export LIBRARY_PATH=$LIBRARY_PATH:$LD_LIBRARY_PATH
    export INCLUDE=$INCLUDE:$CPATH
216 217
    export CUDNN_ROOT_DIR=${NIVIDA_INSTALL_PRE}/${CUDNN_V}/cuda
    export TRT_ROOT_DIR=${NIVIDA_INSTALL_PRE}/${TRT_V}
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

    # python version will be config by whl build script or ci script, we need
    # a DFT version for build success when we just call host_build.sh
    if [[ -z ${ALREADY_CONFIG_PYTHON_VER} ]]
    then
        echo "config a default python3"
        DFT_PYTHON_BIN=/c/Users/${USER}/mge_whl_python_env/3.8.3
        if [ ! -f "${DFT_PYTHON_BIN}/python3.exe" ]; then
            echo "ERR: can not find ${DFT_PYTHON_BIN}/python3.exe , Invalid env"
            windows_env_err
        else
            echo "put python3 to env..."
            export PATH=${DFT_PYTHON_BIN}:$PATH
            which python3
        fi
    fi

    echo "export swig pwd to PATH"
    export PATH=/c/Users/${USER}/swigwin-4.0.2::$PATH
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
}

function cmake_build_windows() {
    # windows do not support long path, so we cache the BUILD_DIR ASAP
    prepare_env_for_windows_build
    BUILD_DIR=$SRC_DIR/build_dir/host/build
    INSTALL_DIR=$BUILD_DIR/../install
    MGE_WITH_CUDA=$1
    MGE_INFERENCE_ONLY=$2
    BUILD_TYPE=$3
    echo "build dir: $BUILD_DIR"
    echo "install dir: $INSTALL_DIR"
    echo "build type: $BUILD_TYPE"
    echo "MGE_WITH_CUDA: $MGE_WITH_CUDA"
    echo "MGE_INFERENCE_ONLY: $MGE_INFERENCE_ONLY"
252
    try_remove_old_build $REMOVE_OLD_BUILD $BUILD_DIR $INSTALL_DIR
253 254 255 256

    echo "create build dir"
    mkdir -p $BUILD_DIR
    mkdir -p $INSTALL_DIR
257
    cd_real_build_dir $BUILD_DIR
258 259 260 261 262 263 264 265
    echo "now try build windows native with cmake/clang-ci/Ninja/Visual Studio ....."
    export CFLAGS=-$MGE_WINDOWS_BUILD_MARCH
    export CXXFLAGS=-$MGE_WINDOWS_BUILD_MARCH
    cmd.exe /c " \
        vcvarsall.bat $MGE_WINDOWS_BUILD_ARCH && cmake  -G "Ninja" \
        -DMGE_ARCH=$MGE_ARCH \
        -DMGE_INFERENCE_ONLY=$MGE_INFERENCE_ONLY \
        -DMGE_WITH_CUDA=$MGE_WITH_CUDA \
266
        -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
267 268 269 270
        -DCMAKE_INSTALL_PREFIX:PATH=$INSTALL_DIR  \
        -DCMAKE_C_COMPILER=clang-cl.exe \
        -DCMAKE_CXX_COMPILER=clang-cl.exe \
        -DCMAKE_MAKE_PROGRAM=ninja.exe \
271 272
        ${EXTRA_CMAKE_ARGS} ../../.. "

273
    config_ninja_target_cmd ${NINJA_VERBOSE} ${BUILD_DEVELOP} "${SPECIFIED_TARGET}" ${NINJA_DRY_RUN}
274
    cmd.exe /c " vcvarsall.bat $MGE_WINDOWS_BUILD_ARCH && ${NINJA_CMD} "
275 276 277
}

if [[ $OS =~ "NT" ]]; then
278 279 280 281 282
    if [ ${MGE_ARCH} = "i386" ] && [ ${MGE_INFERENCE_ONLY} = "OFF" ]; then
        echo "ERR: training mode(-t) only support 64 bit mode"
        echo "pls remove -t or remove -m"
        exit -1
    fi
283 284 285 286
    cmake_build_windows $MGE_WITH_CUDA $MGE_INFERENCE_ONLY $BUILD_TYPE
else
    cmake_build $MGE_WITH_CUDA $MGE_INFERENCE_ONLY $BUILD_TYPE
fi