apollo.sh 19.2 KB
Newer Older
D
Dong Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/env bash

###############################################################################
# Copyright 2017 The Apollo Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################

#=================================================
#                   Utils
#=================================================

S
siyangy 已提交
23 24 25
function source_apollo_base() {
  DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  cd "${DIR}"
D
Dong Li 已提交
26

S
siyangy 已提交
27 28
  source "${DIR}/scripts/apollo_base.sh"
}
29 30

function apollo_check_system_config() {
31 32 33 34 35 36 37
  # check docker environment
  if [ ${MACHINE_ARCH} == "x86_64" ] && [ $(hostname) != "in_dev_docker" ] &&
       [ $(hostname) != "in_release_docker" ]; then
    echo -e "${RED}Must run $0 in dev docker or release docker${NO_COLOR}"
    exit 0
  fi

38
  # check operating system
39
  OP_SYSTEM=$(uname -s)
40
  case $OP_SYSTEM in
41 42
    "Linux")
      echo "System check passed. Build continue ..."
43 44 45 46 47 48 49

      # check system configuration
      DEFAULT_MEM_SIZE="2.0"
      MEM_SIZE=$(free | grep Mem | awk '{printf("%0.2f", $2 / 1024.0 / 1024.0)}')
      if (( $(echo "$MEM_SIZE < $DEFAULT_MEM_SIZE" | bc -l) )); then
         warning "System memory [${MEM_SIZE}G] is lower than minimum required memory size [2.0G]. Apollo build could fail."
      fi
50 51 52 53
      ;;
    "Darwin")
      warning "Mac OS is not officially supported in the current version. Build could fail. We recommend using Ubuntu 14.04."
      ;;
54
    *)
55 56 57 58
      error "Unsupported system: ${OP_SYSTEM}."
      error "Please use Linux, we recommend Ubuntu 14.04."
      exit 1
      ;;
59 60 61
  esac
}

S
siyangy 已提交
62 63 64
function check_machine_arch() {
  # the machine type, currently support x86_64, aarch64
  MACHINE_ARCH=$(uname -m)
D
Dong Li 已提交
65

S
siyangy 已提交
66 67 68 69 70 71 72 73 74
  # Generate WORKSPACE file based on marchine architecture
  if [ "$MACHINE_ARCH" == 'x86_64' ]; then
    sed "s/MACHINE_ARCH/x86_64/g" WORKSPACE.in > WORKSPACE
  elif [ "$MACHINE_ARCH" == 'aarch64' ]; then
    sed "s/MACHINE_ARCH/aarch64/g" WORKSPACE.in > WORKSPACE
  else
    fail "Unknown machine architecture $MACHINE_ARCH"
    exit 1
  fi
75 76

  #setup vtk folder name for different systems.
77
  VTK_VERSION=$(find /usr/include/ -type d  -name "vtk-*" | tail -n1 | cut -d '-' -f 2)
78
  sed -i "s/VTK_VERSION/${VTK_VERSION}/g" WORKSPACE
S
siyangy 已提交
79
}
D
Dong Li 已提交
80 81

function check_esd_files() {
S
siyangy 已提交
82 83
  CAN_CARD="fake_can"

D
Dong Li 已提交
84 85 86 87
  if [ -f ./third_party/can_card_library/esd_can/include/ntcan.h \
      -a -f ./third_party/can_card_library/esd_can/lib/libntcan.so.4 \
      -a -f ./third_party/can_card_library/esd_can/lib/libntcan.so.4.0.1 ]; then
      USE_ESD_CAN=true
L
Lei Wang 已提交
88
      CAN_CARD="esd_can"
D
Dong Li 已提交
89
  else
90
      warning "ESD CAN library supplied by ESD Electronics does not exist. If you need ESD CAN, please refer to third_party/can_card_library/esd_can/README.md."
D
Dong Li 已提交
91 92 93 94 95
      USE_ESD_CAN=false
  fi
}

function generate_build_targets() {
S
siyangy 已提交
96
  if [ -z $NOT_BUILD_PERCEPTION ] ; then
97 98 99
    BUILD_TARGETS=`bazel query //...`
  else
    info 'Skip building perception module!'
L
Lei Wang 已提交
100
    BUILD_TARGETS=`bazel query //... except //modules/perception/... except //modules/calibration/lidar_ex_checker/...`
101 102
  fi

C
Calvin Miao 已提交
103 104 105
  if [ $? -ne 0 ]; then
    fail 'Build failed!'
  fi
D
Dong Li 已提交
106
  if ! $USE_ESD_CAN; then
107
     BUILD_TARGETS=$(echo $BUILD_TARGETS |tr ' ' '\n' | grep -v "esd")
D
Dong Li 已提交
108
  fi
109
  #skip msf for non x86_64 platforms
110
  if [ ${MACHINE_ARCH} != "x86_64" ]; then
111 112
     BUILD_TARGETS=$(echo $BUILD_TARGETS |tr ' ' '\n' | grep -v "msf")
  fi
D
Dong Li 已提交
113 114 115 116 117 118
}

#=================================================
#              Build functions
#=================================================

S
siyangy 已提交
119
function build() {
S
siyangy 已提交
120
  START_TIME=$(get_now)
D
Dong Li 已提交
121

122

123
  info "Start building, please wait ..."
D
Dong Li 已提交
124
  generate_build_targets
125 126
  info "Building on $MACHINE_ARCH..."

127 128 129 130 131 132
  MACHINE_ARCH=$(uname -m)
  JOB_ARG=""
  if [ "$MACHINE_ARCH" == 'aarch64' ]; then
    JOB_ARG="--jobs=3"
  fi
  echo "$BUILD_TARGETS" | xargs bazel build $JOB_ARG $DEFINES -c $@
133
  if [ $? -ne 0 ]; then
D
Dong Li 已提交
134 135
    fail 'Build failed!'
  fi
S
siyangy 已提交
136 137

  # Build python proto
L
Lei Wang 已提交
138
  build_py_proto
139

140 141
  # Clear KV DB and update commit_id after compiling.
  rm -fr data/kv_db
142 143
  python modules/tools/common/kv_db.py put \
      "apollo:data:commit_id" "$(git rev-parse HEAD)"
144

145
  if [ -d /apollo-simulator ] && [ -e /apollo-simulator/build.sh ]; then
146 147 148 149
    cd /apollo-simulator && bash build.sh build
    if [ $? -ne 0 ]; then
      fail 'Build failed!'
    fi
150 151 152 153
  fi
  if [ $? -eq 0 ]; then
    success 'Build passed!'
  fi
D
Dong Li 已提交
154 155
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
function cibuild() {
  START_TIME=$(get_now)

  echo "Start building, please wait ..."
  generate_build_targets
  echo "Building on $MACHINE_ARCH..."
  BUILD_TARGETS="
  //modules/control
  //modules/dreamview
  //modules/localization
  //modules/perception
  //modules/planning
  //modules/prediction
  //modules/routing
  "
A
Aaron Xiao 已提交
171
  bazel build $DEFINES $@ $BUILD_TARGETS
172 173 174 175 176 177 178
  if [ $? -eq 0 ]; then
    success 'Build passed!'
  else
    fail 'Build failed!'
  fi
}

179
function apollo_build_dbg() {
180
  build "dbg" $@
S
siyangy 已提交
181 182
}

183
function apollo_build_opt() {
184
  build "opt" $@
S
siyangy 已提交
185 186
}

L
Lei Wang 已提交
187 188 189 190 191 192
function build_py_proto() {
  if [ -d "./py_proto" ];then
    rm -rf py_proto
  fi
  mkdir py_proto
  PROTOC='./bazel-out/host/bin/external/com_google_protobuf/protoc'
193
  find modules/ -name "*.proto" \
S
siyangy 已提交
194
      | grep -v node_modules \
195 196
      | grep -v modules/drivers/gnss \
      | xargs ${PROTOC} --python_out=py_proto
L
Lei Wang 已提交
197 198 199
  find py_proto/* -type d -exec touch "{}/__init__.py" \;
}

D
Dong Li 已提交
200
function check() {
S
siyangy 已提交
201
  local check_start_time=$(get_now)
D
Dong Li 已提交
202 203

  bash $0 build && bash $0 "test" && bash $0 lint
D
Dong Li 已提交
204 205 206 207 208 209 210 211 212 213 214 215

  START_TIME=$check_start_time
  if [ $? -eq 0 ]; then
    success 'Check passed!'
    return 0
  else
    fail 'Check failed!'
    return 1
  fi
}

function warn_proprietary_sw() {
216 217 218 219 220 221
  echo -e "${RED}The release built contains proprietary software provided by other parties.${NO_COLOR}"
  echo -e "${RED}Make sure you have obtained proper licensing agreement for redistribution${NO_COLOR}"
  echo -e "${RED}if you intend to publish the release package built.${NO_COLOR}"
  echo -e "${RED}Such licensing agreement is solely between you and the other parties,${NO_COLOR}"
  echo -e "${RED}and is not covered by the license terms of the apollo project${NO_COLOR}"
  echo -e "${RED}(see file license).${NO_COLOR}"
D
Dong Li 已提交
222 223 224
}

function release() {
225 226
  RELEASE_DIR="${HOME}/.cache/apollo_release"
  if [ -d "${RELEASE_DIR}" ]; then
227
    rm -rf "${RELEASE_DIR}"
228
  fi
D
Dong Li 已提交
229 230
  APOLLO_RELEASE_DIR="${RELEASE_DIR}/apollo"
  mkdir -p "${APOLLO_RELEASE_DIR}"
231 232 233 234 235 236

  # Find binaries and convert from //path:target to path/target
  BINARIES=$(bazel query "kind(cc_binary, //...)" | sed 's/^\/\///' | sed 's/:/\//')
  # Copy binaries to release dir.
  for BIN in ${BINARIES}; do
    SRC_PATH="bazel-bin/${BIN}"
D
Dong Li 已提交
237
    DST_PATH="${APOLLO_RELEASE_DIR}/${BIN}"
238 239 240 241 242
    if [ -e "${SRC_PATH}" ]; then
      mkdir -p "$(dirname "${DST_PATH}")"
      cp "${SRC_PATH}" "${DST_PATH}"
    fi
  done
D
Dong Li 已提交
243

244
  # modules data and conf
245 246
  CONFS=$(find modules/ -name "conf")
  DATAS=$(find modules/ -name "data")
247 248
  OTHER=("modules/tools"
         "modules/perception/model")
249 250 251 252 253 254 255 256 257 258 259
  rm -rf test/*
  for conf in $CONFS; do
    mkdir -p $APOLLO_RELEASE_DIR/$conf
    rsync -a $conf/* $APOLLO_RELEASE_DIR/$conf
  done
  for data in $DATAS; do
    mkdir -p $APOLLO_RELEASE_DIR/$data
    if [ $data != "modules/map/data" ]; then
      rsync -a $data/* $APOLLO_RELEASE_DIR/$data
    fi
  done
260
  # Other
261
  for path in "${OTHER[@]}"; do
262 263 264
    mkdir -p $APOLLO_RELEASE_DIR/$path
    rsync -a $path/* $APOLLO_RELEASE_DIR/$path
  done
265 266 267

  # dreamview frontend
  cp -a modules/dreamview/frontend $APOLLO_RELEASE_DIR/modules/dreamview
S
siyangy 已提交
268

269
  # remove all pyc file in modules/
D
Dong Li 已提交
270
  find modules/ -name "*.pyc" | xargs -I {} rm {}
271

S
siyangy 已提交
272
  # scripts
D
Dong Li 已提交
273
  cp -r scripts ${APOLLO_RELEASE_DIR}
S
siyangy 已提交
274

D
Dong Li 已提交
275
  # lib
D
Dong Li 已提交
276
  LIB_DIR="${APOLLO_RELEASE_DIR}/lib"
277
  mkdir "${LIB_DIR}"
D
Dong Li 已提交
278
  if $USE_ESD_CAN; then
L
Lei Wang 已提交
279
    warn_proprietary_sw
D
Dong Li 已提交
280 281
    for m in esd_can
    do
282
      cp third_party/can_card_library/$m/lib/* $LIB_DIR
D
Dong Li 已提交
283 284
    done
  fi
L
Lei Wang 已提交
285 286
  cp -r bazel-genfiles/external $LIB_DIR
  cp -r py_proto/modules $LIB_DIR
287
  cp /apollo/modules/perception/cuda_util/cmake_build/libcuda_util.so $LIB_DIR
288

D
Dong Li 已提交
289
  # doc
D
Dong Li 已提交
290 291 292
  cp -r docs "${APOLLO_RELEASE_DIR}"
  cp LICENSE "${APOLLO_RELEASE_DIR}"
  cp third_party/ACKNOWLEDGEMENT.txt "${APOLLO_RELEASE_DIR}"
L
Lei Wang 已提交
293

S
siyangy 已提交
294
  # release info
D
Dong Li 已提交
295
  META="${APOLLO_RELEASE_DIR}/meta.ini"
L
Lei Wang 已提交
296
  echo "git_commit: $(git rev-parse HEAD)" >> $META
297 298
  echo "car_type: LINCOLN.MKZ" >> $META
  echo "arch: ${MACHINE_ARCH}" >> $META
D
Dong Li 已提交
299 300 301 302
}

function gen_coverage() {
  bazel clean
303
  generate_build_targets
L
Lei Wang 已提交
304
  echo "$BUILD_TARGETS" | grep -v "cnn_segmentation_test" | xargs bazel test $DEFINES -c dbg --config=coverage $@
D
Dong Li 已提交
305 306 307
  if [ $? -ne 0 ]; then
    fail 'run test failed!'
  fi
S
siyangy 已提交
308

D
Dong Li 已提交
309 310 311 312 313 314 315 316
  COV_DIR=data/cov
  rm -rf $COV_DIR
  files=$(find bazel-out/local-dbg/bin/modules/ -iname "*.gcda" -o -iname "*.gcno" | grep -v external)
  for f in $files; do
    target="$COV_DIR/objs/modules/${f##*modules}"
    mkdir -p "$(dirname "$target")"
    cp "$f" "$target"
  done
317 318 319 320 321 322 323 324

  files=$(find bazel-out/local-opt/bin/modules/ -iname "*.gcda" -o -iname "*.gcno" | grep -v external)
  for f in $files; do
    target="$COV_DIR/objs/modules/${f##*modules}"
    mkdir -p "$(dirname "$target")"
    cp "$f" "$target"
  done

D
Dong Li 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  lcov --capture --directory "$COV_DIR/objs" --output-file "$COV_DIR/conv.info"
  if [ $? -ne 0 ]; then
    fail 'lcov failed!'
  fi
  lcov --remove "$COV_DIR/conv.info" \
      "external/*" \
      "/usr/*" \
      "bazel-out/*" \
      "*third_party/*" \
      "tools/*" \
      -o $COV_DIR/stripped_conv.info
  genhtml $COV_DIR/stripped_conv.info --output-directory $COV_DIR/report
  echo "Generated coverage report in $COV_DIR/report/index.html"
}

function run_test() {
S
siyangy 已提交
341
  START_TIME=$(get_now)
D
Dong Li 已提交
342

343
  generate_build_targets
344 345
  if [ "$USE_GPU" == "1" ]; then
    echo -e "${RED}Need GPU to run the tests.${NO_COLOR}"
L
Lei Wang 已提交
346
    echo "$BUILD_TARGETS" | xargs bazel test $DEFINES --config=unit_test -c dbg --test_verbose_timeout_warnings $@
347
  else
348
    echo "$BUILD_TARGETS" | grep -v "cnn_segmentation_test" | grep -v "yolo_camera_detector_test" | xargs bazel test $DEFINES --config=unit_test -c dbg --test_verbose_timeout_warnings $@
349
  fi
350 351 352 353 354
  if [ $? -ne 0 ]; then
    fail 'Test failed!'
    return 1
  fi

355
  if [ -d /apollo-simulator ] && [ -e /apollo-simulator/build.sh ]; then
356 357 358 359 360 361 362
      cd /apollo-simulator && bash build.sh test
      if [ $? -ne 0 ]; then
        fail 'Test failed!'
        return 1
      fi
  fi

S
siyangy 已提交
363
  if [ $? -eq 0 ]; then
D
Dong Li 已提交
364 365 366 367 368
    success 'Test passed!'
    return 0
  fi
}

369 370 371 372 373 374 375
function citest() {
  START_TIME=$(get_now)
  BUILD_TARGETS="
  //modules/planning/integration_tests:garage_test
  //modules/planning/integration_tests:sunnyvale_loop_test
  //modules/control/integration_tests:simple_control_test
  //modules/prediction/container/obstacles:obstacle_test
376
  //modules/dreamview/backend/simulation_world:simulation_world_service_test
377
  "
A
Aaron Xiao 已提交
378
  bazel test $DEFINES --config=unit_test --test_verbose_timeout_warnings $@ $BUILD_TARGETS
379 380 381 382 383 384 385 386 387
  if [ $? -eq 0 ]; then
    success 'Test passed!'
    return 0
  else
    fail 'Test failed!'
    return 1
  fi
}

D
Dong Li 已提交
388
function run_cpp_lint() {
389 390
  generate_build_targets
  echo "$BUILD_TARGETS" | xargs bazel test --config=cpplint -c dbg
D
Dong Li 已提交
391 392 393
}

function run_bash_lint() {
S
siyangy 已提交
394
  FILES=$(find "${APOLLO_ROOT_DIR}" -type f -name "*.sh" | grep -v ros)
D
Dong Li 已提交
395 396 397 398
  echo "${FILES}" | xargs shellcheck
}

function run_lint() {
S
siyangy 已提交
399
  START_TIME=$(get_now)
D
Dong Li 已提交
400

C
Calvin Miao 已提交
401 402 403 404 405 406 407 408
  # Add cpplint rule to BUILD files that do not contain it.
  for file in $(find modules -name BUILD | \
    xargs grep -l -E 'cc_library|cc_test|cc_binary' | xargs grep -L 'cpplint()')
  do
    sed -i '1i\load("//tools:cpplint.bzl", "cpplint")\n' $file
    sed -i -e '$a\\ncpplint()' $file
  done

S
siyangy 已提交
409 410
  run_cpp_lint

D
Dong Li 已提交
411 412 413 414 415 416 417 418
  if [ $? -eq 0 ]; then
    success 'Lint passed!'
  else
    fail 'Lint failed!'
  fi
}

function clean() {
419
  bazel clean --async
D
Dong Li 已提交
420 421 422
}

function buildify() {
423 424 425 426 427
  START_TIME=$(get_now)

  local buildifier_url=https://github.com/bazelbuild/buildtools/releases/download/0.4.5/buildifier
  wget $buildifier_url -O ~/.buildifier
  chmod +x ~/.buildifier
428
  find . -name '*BUILD' -type f -exec ~/.buildifier -showlog -mode=fix {} +
429 430 431 432 433 434
  if [ $? -eq 0 ]; then
    success 'Buildify worked!'
  else
    fail 'Buildify failed!'
  fi
  rm ~/.buildifier
D
Dong Li 已提交
435 436
}

S
siyangy 已提交
437 438 439 440 441
function build_fe() {
  cd modules/dreamview/frontend
  yarn build
}

D
Dong Li 已提交
442 443 444 445 446 447 448 449 450 451 452 453
function gen_doc() {
  rm -rf docs/doxygen
  doxygen apollo.doxygen
}

function version() {
  commit=$(git log -1 --pretty=%H)
  date=$(git log -1 --pretty=%cd)
  echo "Commit: ${commit}"
  echo "Date: ${date}"
}

S
siyangy 已提交
454
function build_gnss() {
455
  CURRENT_PATH=$(pwd)
F
fengkaiwen01 已提交
456 457
  if [ -d "${ROS_ROOT}" ]; then
    ROS_PATH="${ROS_ROOT}/../.."
L
Lei Wang 已提交
458 459 460 461
  else
    warning "ROS not found. Run apolllo.sh build first."
    exit 1
  fi
462

L
Lei Wang 已提交
463
  source "${ROS_PATH}/setup.bash"
464 465 466 467 468 469 470 471 472 473 474

  protoc modules/common/proto/error_code.proto --cpp_out=./
  protoc modules/common/proto/header.proto --cpp_out=./
  protoc modules/common/proto/geometry.proto --cpp_out=./

  protoc modules/localization/proto/imu.proto --cpp_out=./
  protoc modules/localization/proto/gps.proto --cpp_out=./
  protoc modules/localization/proto/pose.proto --cpp_out=./

  protoc modules/drivers/gnss/proto/gnss.proto --cpp_out=./
  protoc modules/drivers/gnss/proto/imu.proto --cpp_out=./
F
fengkaiwen01 已提交
475
  protoc modules/drivers/gnss/proto/ins.proto --cpp_out=./ --python_out=./
476
  protoc modules/drivers/gnss/proto/config.proto --cpp_out=./
F
fengkaiwen01 已提交
477
  protoc modules/drivers/gnss/proto/gnss_status.proto --cpp_out=./ --python_out=./
478
  protoc modules/drivers/gnss/proto/gpgga.proto --cpp_out=./
479 480
  protoc modules/drivers/gnss/proto/gnss_raw_observation.proto --cpp_out=./ --python_out=./
  protoc modules/drivers/gnss/proto/gnss_best_pose.proto --cpp_out=./ --python_out=./
481 482

  cd modules
F
fengkaiwen01 已提交
483
  catkin_make_isolated --install --source drivers/gnss \
L
Lei Wang 已提交
484
    --install-space "${ROS_PATH}" -DCMAKE_BUILD_TYPE=Release \
485
    --cmake-args --no-warn-unused-cli
L
Lei Wang 已提交
486
  find "${ROS_PATH}" -name "*.pyc" -print0 | xargs -0 rm -rf
487 488 489 490 491 492
  cd -

  rm -rf modules/common/proto/*.pb.cc
  rm -rf modules/common/proto/*.pb.h
  rm -rf modules/drivers/gnss/proto/*.pb.cc
  rm -rf modules/drivers/gnss/proto/*.pb.h
F
fengkaiwen01 已提交
493
  rm -rf modules/drivers/gnss/proto/*_pb2.py
494 495 496 497 498 499
  rm -rf modules/localization/proto/*.pb.cc
  rm -rf modules/localization/proto/*.pb.h

  rm -rf modules/.catkin_workspace
  rm -rf modules/build_isolated/
  rm -rf modules/devel_isolated/
D
Dong Li 已提交
500 501
}

F
fengkaiwen01 已提交
502 503
function build_velodyne() {
  CURRENT_PATH=$(pwd)
F
fengkaiwen01 已提交
504 505
  if [ -d "${ROS_ROOT}" ]; then
    ROS_PATH="${ROS_ROOT}/../.."
F
fengkaiwen01 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
  else
    warning "ROS not found. Run apolllo.sh build first."
    exit 1
  fi

  source "${ROS_PATH}/setup.bash"

  cd modules
  catkin_make_isolated --install --source drivers/velodyne \
    --install-space "${ROS_PATH}" -DCMAKE_BUILD_TYPE=Release \
    --cmake-args --no-warn-unused-cli
  find "${ROS_PATH}" -name "*.pyc" -print0 | xargs -0 rm -rf
  cd -

  rm -rf modules/.catkin_workspace
  rm -rf modules/build_isolated/
  rm -rf modules/devel_isolated/
}

Y
yibingl 已提交
525 526
function build_usbcam() {
  CURRENT_PATH=$(pwd)
F
fengkaiwen01 已提交
527 528
  if [ -d "${ROS_ROOT}" ]; then
    ROS_PATH="${ROS_ROOT}/../.."
Y
yibingl 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
  else
    warning "ROS not found. Run apolllo.sh build first."
    exit 1
  fi

  source "${ROS_PATH}/setup.bash"

  cd modules
  catkin_make_isolated --install --source drivers/usb_cam \
    --install-space "${ROS_PATH}" -DCMAKE_BUILD_TYPE=Release \
    --cmake-args --no-warn-unused-cli
  find "${ROS_PATH}" -name "*.pyc" -print0 | xargs -0 rm -rf
  cd -

  rm -rf modules/.catkin_workspace
  rm -rf modules/build_isolated/
  rm -rf modules/devel_isolated/
}

548
function config() {
549
  ${APOLLO_ROOT_DIR}/scripts/configurator.sh
550 551
}

552
function print_usage() {
C
Calvin Miao 已提交
553 554 555 556 557 558 559 560 561 562
  RED='\033[0;31m'
  BLUE='\033[0;34m'
  BOLD='\033[1m'
  NONE='\033[0m'

  echo -e "\n${RED}Usage${NONE}:
  .${BOLD}/apollo.sh${NONE} [OPTION]"

  echo -e "\n${RED}Options${NONE}:
  ${BLUE}build${NONE}: run build only
563
  ${BLUE}build_opt${NONE}: build optimized binary for the code
J
Jun Zhu 已提交
564
  ${BLUE}build_gpu${NONE}: run build only with Caffe GPU mode support
F
fengkaiwen01 已提交
565 566
  ${BLUE}build_gnss${NONE}: build gnss driver
  ${BLUE}build_velodyne${NONE}: build velodyne driver
gengqx's avatar
gengqx 已提交
567
  ${BLUE}build_usbcam${NONE}: build usb camera driver
J
Jun Zhu 已提交
568
  ${BLUE}build_opt_gpu${NONE}: build optimized binary with Caffe GPU mode support
C
Calvin Miao 已提交
569
  ${BLUE}build_fe${NONE}: compile frontend javascript code, this requires all the node_modules to be installed already
570
  ${BLUE}build_no_perception [dbg|opt]${NONE}: run build build skip building perception module, useful when some perception dependencies are not satisified, e.g., CUDA, CUDNN, LIDAR, etc.
571
  ${BLUE}build_prof${NONE}: build for gprof support.
C
Calvin Miao 已提交
572 573 574 575 576 577 578 579 580 581 582 583
  ${BLUE}buildify${NONE}: fix style of BUILD files
  ${BLUE}check${NONE}: run build/lint/test, please make sure it passes before checking in new code
  ${BLUE}clean${NONE}: run Bazel clean
  ${BLUE}config${NONE}: run configurator tool
  ${BLUE}coverage${NONE}: generate test coverage report
  ${BLUE}doc${NONE}: generate doxygen document
  ${BLUE}lint${NONE}: run code style check
  ${BLUE}usage${NONE}: print this menu
  ${BLUE}release${NONE}: build release version
  ${BLUE}test${NONE}: run all unit tests
  ${BLUE}version${NONE}: display current commit and date
  "
584 585
}

S
siyangy 已提交
586 587 588
function main() {
  source_apollo_base
  check_machine_arch
589
  apollo_check_system_config
S
siyangy 已提交
590 591
  check_esd_files

592 593 594 595 596
  DEFINES="--define ARCH=${MACHINE_ARCH} --define CAN_CARD=${CAN_CARD} --cxxopt=-DUSE_ESD_CAN=${USE_ESD_CAN}"

  if [ ${MACHINE_ARCH} == "x86_64" ]; then
    DEFINES="${DEFINES} --copt=-mavx2"
  fi
S
siyangy 已提交
597

598 599 600 601
  local cmd=$1
  shift

  case $cmd in
S
siyangy 已提交
602
    check)
603
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
604
      check $@
S
siyangy 已提交
605 606
      ;;
    build)
L
Lei Wang 已提交
607
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
608
      apollo_build_dbg $@
S
siyangy 已提交
609
      ;;
610
    build_prof)
D
Dong Li 已提交
611
      DEFINES="${DEFINES} --config=cpu_prof --cxxopt=-DCPU_ONLY"
612 613
      apollo_build_dbg $@
      ;;
614
    build_no_perception)
S
siyangy 已提交
615
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
616
      NOT_BUILD_PERCEPTION=true
617 618 619 620 621 622 623
      if [ "$1" == "opt" ]; then
        shift
        apollo_build_opt $@
      elif [ "$1" == "dbg" ]; then
        shift
        apollo_build_dbg $@
      fi
624
      ;;
625 626 627 628
    cibuild)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      cibuild $@
      ;;
629
    build_opt)
L
Lei Wang 已提交
630
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
631
      apollo_build_opt $@
S
siyangy 已提交
632
      ;;
633
    build_gpu)
634
      DEFINES="${DEFINES} --cxxopt=-DUSE_CAFFE_GPU"
635
      apollo_build_dbg $@
J
Jun Zhu 已提交
636
      ;;
J
Jun Zhu 已提交
637
    build_opt_gpu)
638
      DEFINES="${DEFINES} --cxxopt=-DUSE_CAFFE_GPU"
639
      apollo_build_opt $@
S
siyangy 已提交
640
      ;;
S
siyangy 已提交
641 642 643
    build_fe)
      build_fe
      ;;
S
siyangy 已提交
644 645 646
    buildify)
      buildify
      ;;
F
fengkaiwen01 已提交
647
    build_gnss)
S
siyangy 已提交
648 649
      build_gnss
      ;;
L
Lei Wang 已提交
650 651 652
    build_py)
      build_py_proto
      ;;
F
fengkaiwen01 已提交
653
    build_velodyne)
F
fengkaiwen01 已提交
654 655
      build_velodyne
      ;;
F
fengkaiwen01 已提交
656
    build_usbcam)
Y
yibingl 已提交
657 658
      build_usbcam
      ;;
659 660 661
    config)
      config
      ;;
S
siyangy 已提交
662 663 664 665 666 667 668
    doc)
      gen_doc
      ;;
    lint)
      run_lint
      ;;
    test)
669
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
L
Lei Wang 已提交
670
      run_test $@
S
siyangy 已提交
671
      ;;
672 673 674 675
    citest)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest $@
      ;;
676 677 678
    test_gpu)
      DEFINES="${DEFINES} --cxxopt=-DUSE_CAFFE_GPU"
      USE_GPU="1"
L
Lei Wang 已提交
679
      run_test $@
680
      ;;
S
siyangy 已提交
681 682 683 684 685 686 687
    release)
      release 1
      ;;
    release_noproprietary)
      release 0
      ;;
    coverage)
L
Lei Wang 已提交
688
      gen_coverage $@
S
siyangy 已提交
689 690 691 692 693 694 695
      ;;
    clean)
      clean
      ;;
    version)
      version
      ;;
C
Calvin Miao 已提交
696 697 698
    usage)
      print_usage
      ;;
S
siyangy 已提交
699 700 701 702 703 704 705
    *)
      print_usage
      ;;
  esac
}

main $@