build.sh 3.5 KB
Newer Older
1 2
#!/bin/bash

羽飞's avatar
羽飞 已提交
3 4 5
# readlink -f cannot work on mac
TOPDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

6 7
BUILD_SH=$TOPDIR/build.sh

羽飞's avatar
羽飞 已提交
8
CMAKE_COMMAND="cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 --log-level=STATUS"
9 10 11 12 13 14 15 16 17 18 19 20

ALL_ARGS=("$@")
BUILD_ARGS=()
MAKE_ARGS=(-j $CPU_CORES)
MAKE=make

echo "$0 ${ALL_ARGS[@]}"

function usage
{
  echo "Usage:"
  echo "./build.sh -h"
羽飞's avatar
羽飞 已提交
21
  echo "./build.sh init # install dependence"
22 23 24 25
  echo "./build.sh clean"
  echo "./build.sh [BuildType] [--make [MakeOptions]]"
  echo ""
  echo "OPTIONS:"
羽飞's avatar
羽飞 已提交
26
  echo "BuildType => debug(default), release"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  echo "MakeOptions => Options to make command, default: -j N"

  echo ""
  echo "Examples:"
  echo "# Init."
  echo "./build.sh init"
  echo ""
  echo "# Build by debug mode and make with -j24."
  echo "./build.sh debug --make -j24"
}

function parse_args
{
  make_start=false
  for arg in "${ALL_ARGS[@]}"; do
    if [[ "$arg" == "--make" ]]
    then
      make_start=true
    elif [[ $make_start == false ]]
    then
      BUILD_ARGS+=("$arg")
    else
      MAKE_ARGS+=("$arg")
    fi

  done
}

# try call command make, if use give --make in command line.
function try_make
{
  if [[ $MAKE != false ]]
  then
羽飞's avatar
羽飞 已提交
60 61
    # use single thread `make` if concurrent building failed
    $MAKE "${MAKE_ARGS[@]}" || $MAKE
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  fi
}

# create build directory and cd it.
function prepare_build_dir
{
  TYPE=$1
  mkdir -p $TOPDIR/build_$TYPE && cd $TOPDIR/build_$TYPE
}

function do_init
{
  git submodule update --init || return
  current_dir=$PWD

羽飞's avatar
羽飞 已提交
77 78
  MAKE_COMMAND="make --silent"

79 80 81
  # build libevent
  cd ${TOPDIR}/deps/3rd/libevent && \
    git checkout release-2.1.12-stable && \
羽飞's avatar
羽飞 已提交
82
    mkdir -p build && \
83
    cd build && \
羽飞's avatar
羽飞 已提交
84 85
    ${CMAKE_COMMAND} .. -DEVENT__DISABLE_OPENSSL=ON -DEVENT__LIBRARY_TYPE=BOTH && \
    ${MAKE_COMMAND} -j4 && \
86 87 88 89
    make install

  # build googletest
  cd ${TOPDIR}/deps/3rd/googletest && \
羽飞's avatar
羽飞 已提交
90
    mkdir -p build && \
91
    cd build && \
羽飞's avatar
羽飞 已提交
92 93 94
    ${CMAKE_COMMAND} .. && \
    ${MAKE_COMMAND} -j4 && \
    ${MAKE_COMMAND} install
95 96 97

  # build google benchmark
  cd ${TOPDIR}/deps/3rd/benchmark && \
羽飞's avatar
羽飞 已提交
98
    mkdir -p build && \
99
    cd build && \
羽飞's avatar
羽飞 已提交
100 101 102
    ${CMAKE_COMMAND} .. -DBENCHMARK_ENABLE_TESTING=OFF  -DBENCHMARK_INSTALL_DOCS=OFF -DBENCHMARK_ENABLE_GTEST_TESTS=OFF -DBENCHMARK_USE_BUNDLED_GTEST=OFF -DBENCHMARK_ENABLE_ASSEMBLY_TESTS=OFF && \
    ${MAKE_COMMAND} -j4 && \
    ${MAKE_COMMAND} install
103 104 105

  # build jsoncpp
  cd ${TOPDIR}/deps/3rd/jsoncpp && \
羽飞's avatar
羽飞 已提交
106
    mkdir -p build && \
107
    cd build && \
羽飞's avatar
羽飞 已提交
108 109 110
    ${CMAKE_COMMAND} -DJSONCPP_WITH_TESTS=OFF -DJSONCPP_WITH_POST_BUILD_UNITTEST=OFF .. && \
    ${MAKE_COMMAND} && \
    ${MAKE_COMMAND} install
111 112 113 114 115 116 117

  cd $current_dir
}

function prepare_build_dir
{
  TYPE=$1
X
xzhangxian1008 已提交
118 119 120 121 122
  mkdir -p ${TOPDIR}/build_${TYPE}
  rm -f build
  echo "create soft link for build_${TYPE}, linked by directory named build"
  ln -s build_${TYPE} build
  cd ${TOPDIR}/build_${TYPE}
123 124 125 126 127 128 129
}

function do_build
{
  TYPE=$1; shift
  prepare_build_dir $TYPE || return
  echo "${CMAKE_COMMAND} ${TOPDIR} $@"
羽飞's avatar
羽飞 已提交
130
  ${CMAKE_COMMAND} -S ${TOPDIR} $@
131 132 133 134 135 136 137 138 139 140 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
}

function do_clean
{
  echo "clean build_* dirs"
  find . -maxdepth 1 -type d -name 'build_*' | xargs rm -rf
}

function build
{
  set -- "${BUILD_ARGS[@]}"
  case "x$1" in
    xrelease)
      do_build "$@" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DDEBUG=OFF
      ;;
    xdebug)
      do_build "$@" -DCMAKE_BUILD_TYPE=Debug -DDEBUG=ON
      ;;
    *)
      BUILD_ARGS=(debug "${BUILD_ARGS[@]}")
      build
      ;;
  esac
}

function main
{
  case "$1" in
    -h)
      usage
      ;;
    init)
      do_init
      ;;
    clean)
      do_clean
      ;;
    *)
      parse_args
      build
      try_make
      ;;
  esac
}

main "$@"