apollo.sh 21.5 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
storypku 已提交
23 24 25 26 27 28 29
APOLLO_ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source ${APOLLO_ROOT_DIR}/scripts/apollo_base.sh

MACHINE_ARCH=$(uname -m)
BUILD_CMD="bazel build --distdir=${APOLLO_CACHE_DIR}"
TEST_CMD="bazel test --distdir=${APOLLO_CACHE_DIR}"

F
Fla3inH0tCheet0s 已提交
30 31
DISABLED_CYBER_MODULES="except //cyber/record:record_file_integration_test"

32
function apollo_check_system_config() {
33
  # check docker environment
34
  if [ ${MACHINE_ARCH} == "x86_64" ] && [ ${APOLLO_IN_DOCKER} != "true" ]; then
S
storypku 已提交
35
    echo -e "${RED}Must run $0 in cyber or dev docker${NO_COLOR}"
36 37 38
    exit 0
  fi

39
  # check operating system
40
  OP_SYSTEM=$(uname -s)
41
  case $OP_SYSTEM in
42 43
    "Linux")
      echo "System check passed. Build continue ..."
44 45
      # check system configuration
      DEFAULT_MEM_SIZE="2.0"
S
storypku 已提交
46
      MEM_SIZE=$(free -m | grep Mem | awk '{printf("%0.2f", $2 / 1024.0)}')
47 48 49
      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
      error "Unsupported system: ${OP_SYSTEM}."
S
storypku 已提交
56
      error "Please use Linux, we recommend Ubuntu 18.04."
57 58
      exit 1
      ;;
59 60 61
  esac
}

S
siyangy 已提交
62
function check_machine_arch() {
63
  if [ "${MACHINE_ARCH}" != "x86_64" ] && [ "${MACHINE_ARCH}" != "aarch64" ]; then
S
storypku 已提交
64
    fail "Machine architecture $MACHINE_ARCH currently not supported yet."
S
siyangy 已提交
65 66
    exit 1
  fi
67

S
storypku 已提交
68
  #TODO(ALL): checks whether still in use
69
  #setup vtk folder name for different systems.
70
  VTK_VERSION=$(find /usr/include/ -type d  -name "vtk-*" | tail -n1 | cut -d '-' -f 2)
71
  sed "s/VTK_VERSION/${VTK_VERSION}/g" WORKSPACE.in > WORKSPACE
S
siyangy 已提交
72
}
D
Dong Li 已提交
73 74

function check_esd_files() {
S
siyangy 已提交
75 76
  CAN_CARD="fake_can"

D
Dong Li 已提交
77 78 79 80
  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 已提交
81
      CAN_CARD="esd_can"
D
Dong Li 已提交
82
  else
83
      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 已提交
84 85 86 87 88
      USE_ESD_CAN=false
  fi
}

function generate_build_targets() {
F
Fla3inH0tCheet0s 已提交
89
  COMMON_TARGETS="//cyber/... union //modules/common/kv_db/... union //modules/dreamview/... $DISABLED_CYBER_MODULES"
90
  case $BUILD_FILTER in
91
  cyber)
N
Ning Qu 已提交
92
    BUILD_TARGETS=`bazel query //cyber/... union //modules/tools/visualizer/...`
93 94
    ;;
  drivers)
N
Ning Qu 已提交
95
    BUILD_TARGETS=`bazel query //cyber/... union //modules/tools/visualizer/... union //modules/drivers/... except //modules/drivers/tools/... except //modules/drivers/canbus/... except //modules/drivers/video/...`
96
    ;;
97 98 99 100 101 102 103 104 105 106 107 108 109
  control)
    BUILD_TARGETS=`bazel query $COMMON_TARGETS union //modules/control/... `
    ;;
  planning)
    BUILD_TARGETS=`bazel query $COMMON_TARGETS union //modules/routing/... union //modules/planning/...`
    ;;
  prediction)
    BUILD_TARGETS=`bazel query $COMMON_TARGETS union //modules/routing/... union //modules/prediction/...`
    ;;
  pnc)
    BUILD_TARGETS=`bazel query $COMMON_TARGETS union //modules/routing/... union //modules/prediction/... union //modules/planning/... union //modules/control/...`
    ;;
  no_perception)
110
    BUILD_TARGETS=`bazel query //modules/... except //modules/perception/... union //cyber/...`
111 112
    ;;
  *)
113 114
#    BUILD_TARGETS=`bazel query //modules/... union //cyber/...`
    # FIXME(all): temporarily disable modules doesn't compile in 18.04
C
changsh726 已提交
115
    BUILD_TARGETS=`bazel query //modules/... union //cyber/... except //modules/v2x/... except //modules/map/tools/map_datachecker/... $DISABLE_CYBER_MODULES`
116
  esac
117

C
Calvin Miao 已提交
118 119 120
  if [ $? -ne 0 ]; then
    fail 'Build failed!'
  fi
D
Dong Li 已提交
121
  if ! $USE_ESD_CAN; then
122
     BUILD_TARGETS=$(echo $BUILD_TARGETS |tr ' ' '\n' | grep -v "esd")
D
Dong Li 已提交
123
  fi
124
  #skip msf for non x86_64 platforms
125
  if [ ${MACHINE_ARCH} != "x86_64" ]; then
126 127
     BUILD_TARGETS=$(echo $BUILD_TARGETS |tr ' ' '\n' | grep -v "msf")
  fi
D
Dong Li 已提交
128 129 130 131 132 133
}

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

S
siyangy 已提交
134
function build() {
135
  if [ "${USE_GPU}" = "1" ] ; then
136 137
    echo -e "${YELLOW}Running build under GPU mode. GPU is required to run the build.${NO_COLOR}"
  else
S
storypku 已提交
138
    echo -e "${YELLOW}Running build under CPU mode. GPU is not required to run the build.${NO_COLOR}"
139
  fi
140
  info "Start building, please wait ..."
141

D
Dong Li 已提交
142
  generate_build_targets
S
storypku 已提交
143

144 145
  info "Building on $MACHINE_ARCH..."

146
  JOB_ARG="--jobs=$(nproc) --local_ram_resources=HOST_RAM*0.8" # --ram_utilization_factor 80
147 148 149
  if [ "$MACHINE_ARCH" == 'aarch64' ]; then
    JOB_ARG="--jobs=3"
  fi
150
  info "Building with $JOB_ARG for $MACHINE_ARCH"
151

S
storypku 已提交
152
  ${BUILD_CMD} $JOB_ARG $DEFINES -c $@ $BUILD_TARGETS
A
Aaron Xiao 已提交
153
  if [ ${PIPESTATUS[0]} -ne 0 ]; then
D
Dong Li 已提交
154 155
    fail 'Build failed!'
  fi
S
siyangy 已提交
156 157

  # Build python proto
L
Lei Wang 已提交
158
  build_py_proto
159

160
  # Clear KV DB and update commit_id after compiling.
161 162 163
  if [ "$BUILD_FILTER" == 'cyber' ] || [ "$BUILD_FILTER" == 'drivers' ]; then
    info "Skipping revision recording"
  else
S
storypku 已提交
164
    ${BUILD_CMD} $JOB_ARG $DEFINES -c $@ $BUILD_TARGETS
165 166 167 168 169 170 171 172
    if [ ${PIPESTATUS[0]} -ne 0 ]; then
      fail 'Build failed!'
    fi
    rm -fr data/kv_db*
    REVISION=$(get_revision)
    ./bazel-bin/modules/common/kv_db/kv_db_tool --op=put \
        --key="apollo:data:commit_id" --value="$REVISION"
  fi
173

S
storypku 已提交
174
  # TODO(ALL): check whether still in public use.
S
siyangy 已提交
175 176 177 178 179 180
  if [ -d /apollo-simulator ] && [ -e /apollo-simulator/build.sh ]; then
    cd /apollo-simulator && bash build.sh build
    if [ $? -ne 0 ]; then
      fail 'Build failed!'
    fi
  fi
181 182
  if [ $? -eq 0 ]; then
    success 'Build passed!'
183 184
  else
    fail 'Build failed'
185
  fi
D
Dong Li 已提交
186 187
}

188
function cibuild_extended() {
189 190
  info "Building framework ..."

S
storypku 已提交
191
  cd ${APOLLO_ROOT_DIR}
192
  info "Building modules ..."
193

194
  JOB_ARG="--jobs=12"
195 196 197 198
  if [ "$MACHINE_ARCH" == 'aarch64' ]; then
    JOB_ARG="--jobs=3"
  fi

A
Aaron Xiao 已提交
199
  info "Building with $JOB_ARG for $MACHINE_ARCH"
J
Jiangtao Hu 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

  # FIXME(all): temporarily disable modules doesn't compile in 18.04
  BUILD_TARGETS=`
    bazel query //cyber/... \
    union //modules/perception/... \
    union //modules/dreamview/... \
    union //modules/drivers/radar/conti_radar/... \
    union //modules/drivers/radar/racobit_radar/... \
    union //modules/drivers/radar/ultrasonic_radar/... \
    union //modules/drivers/gnss/... \
    union //modules/drivers/velodyne/... \
    union //modules/drivers/camera/... \
    union //modules/guardian/... \
    union //modules/localization/... \
    union //modules/map/... \
    union //modules/third_party_perception/... \
    except //modules/map/tools/map_datachecker/...
    `
218

S
storypku 已提交
219
  ${BUILD_CMD} $JOB_ARG $DEFINES $@ $BUILD_TARGETS
220 221 222 223 224 225 226

  if [ $? -eq 0 ]; then
    success 'Build passed!'
  else
    fail 'Build failed!'
  fi
}
S
storypku 已提交
227

228 229
function cibuild() {
  info "Building framework ..."
S
storypku 已提交
230
  cd "${APOLLO_ROOT_DIR}"
G
GoLancer 已提交
231

232 233
  info "Building modules ..."

234
  JOB_ARG="--jobs=12"
235 236 237 238 239 240 241 242 243
  if [ "$MACHINE_ARCH" == 'aarch64' ]; then
    JOB_ARG="--jobs=3"
  fi

  info "Building with $JOB_ARG for $MACHINE_ARCH"
  BUILD_TARGETS="
    //modules/canbus/...
    //modules/common/...
    //modules/control/...
244 245
    //modules/planning/...
    //modules/prediction/...
246
    //modules/routing/...
247
    //modules/transform/..."
248

249 250 251
  # The data module is lightweight and rarely changed. If it fails, it's
  # most-likely an environment mess. So we try `bazel clean` and then initial
  # the building process.
S
storypku 已提交
252 253
  ${BUILD_CMD} $JOB_ARG $DEFINES $@ "//modules/data/..." || bazel clean
  ${BUILD_CMD} $JOB_ARG $DEFINES $@ $BUILD_TARGETS
254

255 256 257 258 259 260 261
  if [ $? -eq 0 ]; then
    success 'Build passed!'
  else
    fail 'Build failed!'
  fi
}

262
function apollo_build_dbg() {
263
  build "dbg" $@
S
siyangy 已提交
264 265
}

266
function apollo_build_opt() {
267
  build "opt" $@
S
siyangy 已提交
268 269
}

L
Lei Wang 已提交
270
function build_py_proto() {
271
  # TODO(xiaoxq): Retire this as we are using bazel to compile protos into bazel-genfiles.
L
Lei Wang 已提交
272 273 274 275
  if [ -d "./py_proto" ];then
    rm -rf py_proto
  fi
  mkdir py_proto
276
  find modules/ cyber/ -name "*.proto" \
S
siyangy 已提交
277
      | grep -v node_modules \
278
      | xargs protoc --python_out=py_proto
V
vlin17 已提交
279 280 281
  find modules/ cyber/ -name "*_service.proto" \
      | grep -v node_modules \
      | xargs python -m grpc_tools.protoc --proto_path=. --python_out=py_proto --grpc_python_out=py_proto
L
Lei Wang 已提交
282 283 284
  find py_proto/* -type d -exec touch "{}/__init__.py" \;
}

D
Dong Li 已提交
285
function check() {
D
Dong Li 已提交
286
  bash $0 build && bash $0 "test" && bash $0 lint
D
Dong Li 已提交
287 288 289 290 291 292 293 294 295 296 297 298

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

function gen_coverage() {
  bazel clean
299
  generate_build_targets
S
storypku 已提交
300
  echo "$BUILD_TARGETS" | grep -v "cnn_segmentation_test" | xargs ${TEST_CMD} $DEFINES -c dbg --config=coverage $@
D
Dong Li 已提交
301 302 303
  if [ $? -ne 0 ]; then
    fail 'run test failed!'
  fi
S
siyangy 已提交
304

D
Dong Li 已提交
305 306
  COV_DIR=data/cov
  rm -rf $COV_DIR
307
  files=$(find bazel-out/local-dbg/bin/ -iname "*.gcda" -o -iname "*.gcno" | grep -v external | grep -v third_party)
D
Dong Li 已提交
308
  for f in $files; do
309 310 311 312 313
    if [ "$f" != "${f##*cyber}" ]; then
      target="$COV_DIR/objs/cyber${f##*cyber}"
    else
      target="$COV_DIR/objs/modules${f##*modules}"
    fi
314 315 316 317
    mkdir -p "$(dirname "$target")"
    cp "$f" "$target"
  done

318
  lcov --rc lcov_branch_coverage=1 --base-directory "/apollo/bazel-apollo" --capture --directory "$COV_DIR/objs" --output-file "$COV_DIR/conv.info"
D
Dong Li 已提交
319 320 321
  if [ $? -ne 0 ]; then
    fail 'lcov failed!'
  fi
J
Jiangtao Hu 已提交
322
  lcov --rc lcov_branch_coverage=1 --remove "$COV_DIR/conv.info" \
D
Dong Li 已提交
323 324 325 326 327 328 329 330 331 332 333
      "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() {
334
  JOB_ARG="--jobs=$(nproc) --ram_utilization_factor 80"
335

336
  generate_build_targets
337
  if [ "$USE_GPU" == "1" ]; then
338
    echo -e "${YELLOW}Running tests under GPU mode. GPU is required to run the tests.${NO_COLOR}"
S
storypku 已提交
339
    ${TEST_CMD} $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@ $BUILD_TARGETS
340
  else
341
    echo -e "${YELLOW}Running tests under CPU mode. No GPU is required to run the tests.${NO_COLOR}"
342
    BUILD_TARGETS="`echo "$BUILD_TARGETS" | grep -v "cnn_segmentation_test\|yolo_camera_detector_test\|unity_recognize_test\|perception_traffic_light_rectify_test\|cuda_util_test"`"
S
storypku 已提交
343
    ${TEST_CMD} $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@ $BUILD_TARGETS
344
  fi
345 346 347 348 349
  if [ $? -ne 0 ]; then
    fail 'Test failed!'
    return 1
  fi

S
siyangy 已提交
350
  if [ $? -eq 0 ]; then
D
Dong Li 已提交
351 352 353 354 355
    success 'Test passed!'
    return 0
  fi
}

L
Liangliang Zhang 已提交
356
function citest_basic() {
L
Liangliang Zhang 已提交
357 358
  set -e

359 360
  info "Building framework ..."
  cd /apollo
G
GoLancer 已提交
361
  source cyber/setup.bash
362

J
Jiangtao Hu 已提交
363 364 365 366
  # FIXME(all): temporarily disable modules doesn't compile in 18.04
#   BUILD_TARGETS="
#    `bazel query //modules/... union //cyber/...`
#  "
367
  BUILD_TARGETS=`bazel query //modules/... union //cyber/... except //modules/tools/visualizer/... except //modules/v2x/... except //modules/drivers/video/tools/decode_video/... except //modules/map/tools/map_datachecker/... `
368

369
  JOB_ARG="--jobs=12 --ram_utilization_factor 80"
L
Liangliang Zhang 已提交
370

371
  BUILD_TARGETS="`echo "$BUILD_TARGETS" | grep "modules\/" | grep "test" \
372 373 374 375
          | grep -v "modules\/planning" \
          | grep -v "modules\/prediction" \
          | grep -v "modules\/control" \
          | grep -v "modules\/common" \
376
          | grep -v "can_client" \
377
          | grep -v "blob_test" \
378
          | grep -v "pyramid_map" \
379
          | grep -v "ndt_lidar_locator_test" \
380 381 382
          | grep -v "syncedmem_test" | grep -v "blob_test" \
          | grep -v "perception_inference_operators_test" \
          | grep -v "cuda_util_test" \
383 384
          | grep -v "modules\/perception"`"

S
storypku 已提交
385
  ${TEST_CMD} $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@ $BUILD_TARGETS
386

387 388 389 390 391 392 393 394 395
  if [ $? -eq 0 ]; then
    success 'Test passed!'
    return 0
  else
    fail 'Test failed!'
    return 1
  fi
}

396 397 398 399 400
function citest_extended() {
  set -e

  info "Building framework ..."
  cd /apollo
G
GoLancer 已提交
401
  source cyber/setup.bash
402 403

  BUILD_TARGETS="
F
Fla3inH0tCheet0s 已提交
404
    `bazel query //modules/planning/... union //modules/common/... union //cyber/... $DISABLED_CYBER_MODULES`
405
    `bazel query //modules/prediction/... union //modules/control/...`
406 407
  "

408
  JOB_ARG="--jobs=12 --ram_utilization_factor 80"
409

410
  BUILD_TARGETS="`echo "$BUILD_TARGETS" | grep "test"`"
411

S
storypku 已提交
412
  ${TEST_CMD} $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@ $BUILD_TARGETS
413 414 415 416 417 418 419 420 421 422

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

L
Liangliang Zhang 已提交
423
function citest() {
424 425
  info "Building framework ..."
  cd /apollo
G
GoLancer 已提交
426
  source cyber/setup.bash
427

L
Liangliang Zhang 已提交
428
  citest_basic
429
  citest_extended
L
Liangliang Zhang 已提交
430 431 432 433 434 435 436 437 438
  if [ $? -eq 0 ]; then
    success 'Test passed!'
    return 0
  else
    fail 'Test failed!'
    return 1
  fi
}

D
Dong Li 已提交
439
function run_cpp_lint() {
440
  BUILD_TARGETS="`bazel query //modules/... except //modules/tools/visualizer/... union //cyber/...`"
S
storypku 已提交
441
  ${TEST_CMD} --config=cpplint -c dbg $BUILD_TARGETS
D
Dong Li 已提交
442 443 444
}

function run_bash_lint() {
S
siyangy 已提交
445
  FILES=$(find "${APOLLO_ROOT_DIR}" -type f -name "*.sh" | grep -v ros)
D
Dong Li 已提交
446 447 448 449
  echo "${FILES}" | xargs shellcheck
}

function run_lint() {
C
Calvin Miao 已提交
450
  # Add cpplint rule to BUILD files that do not contain it.
451
  for file in $(find cyber modules -name BUILD | \
H
hugomatic 已提交
452
    grep -v gnss/third_party | grep -v modules/teleop/encoder/nvenc_sdk6 | \
C
Calvin Miao 已提交
453 454 455 456 457 458
    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 已提交
459 460
  run_cpp_lint

D
Dong Li 已提交
461 462 463 464 465 466 467 468
  if [ $? -eq 0 ]; then
    success 'Lint passed!'
  else
    fail 'Lint failed!'
  fi
}

function clean() {
A
Aaron Xiao 已提交
469
  # Remove bazel cache.
470
  bazel clean --async
471 472 473 474 475 476

  # Remove bazel cache in associated directories
  if [ -d /apollo-simulator ]; then
    cd /apollo-simulator && bazel clean --async
  fi

A
Aaron Xiao 已提交
477 478
  # Remove cmake cache.
  rm -fr framework/build
D
Dong Li 已提交
479 480 481
}

function buildify() {
S
storypku 已提交
482
  find . -name '*BUILD' -or -name '*.bzl' -type f -exec buildifier -showlog -mode=fix {} +
483 484 485 486 487
  if [ $? -eq 0 ]; then
    success 'Buildify worked!'
  else
    fail 'Buildify failed!'
  fi
D
Dong Li 已提交
488 489
}

S
siyangy 已提交
490 491 492 493 494
function build_fe() {
  cd modules/dreamview/frontend
  yarn build
}

D
Dong Li 已提交
495 496 497 498 499 500
function gen_doc() {
  rm -rf docs/doxygen
  doxygen apollo.doxygen
}

function version() {
501 502 503 504 505
  rev=$(get_revision)
  if [ "$rev" = "unknown" ];then
    echo "Version: $rev"
    return
  fi
D
Dong Li 已提交
506 507 508 509 510 511
  commit=$(git log -1 --pretty=%H)
  date=$(git log -1 --pretty=%cd)
  echo "Commit: ${commit}"
  echo "Date: ${date}"
}

512 513 514 515 516 517 518 519 520 521 522
function get_revision() {
  git rev-parse --is-inside-work-tree &> /dev/null
  if [ $? = 0 ];then
    REVISION=$(git rev-parse HEAD)
  else
    warning "Could not get the version number, maybe this is not a git work tree." >&2
    REVISION="unknown"
  fi
  echo "$REVISION"
}

523 524 525 526 527 528 529 530 531 532 533
function get_branch() {
  git branch &> /dev/null
  if [ $? = 0 ];then
    BRANCH=$(git rev-parse --abbrev-ref HEAD)
  else
    warning "Could not get the branch name, maybe this is not a git work tree." >&2
    BRANCH="unknown"
  fi
  echo "$BRANCH"
}

534
function config() {
535
  ${APOLLO_ROOT_DIR}/scripts/configurator.sh
536 537
}

S
siyangy 已提交
538
function set_use_gpu() {
S
siyangy 已提交
539 540 541 542 543
  if [ "${USE_GPU}" = "1" ] ; then
    DEFINES="${DEFINES} --define USE_GPU=true"
  else
    DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
  fi
S
siyangy 已提交
544 545
}

546
function print_usage() {
C
Calvin Miao 已提交
547 548 549 550 551 552 553 554 555 556
  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
557
  ${BLUE}build_opt${NONE}: build optimized binary for the code
L
luoqi06 已提交
558
  ${BLUE}build_cpu${NONE}: dbg build with CPU
J
Jun Zhu 已提交
559 560
  ${BLUE}build_gpu${NONE}: run build only with Caffe GPU mode support
  ${BLUE}build_opt_gpu${NONE}: build optimized binary with Caffe GPU mode support
C
Calvin Miao 已提交
561
  ${BLUE}build_fe${NONE}: compile frontend javascript code, this requires all the node_modules to be installed already
562 563
  ${BLUE}build_cyber [dbg|opt]${NONE}: build Cyber RT only
  ${BLUE}build_drivers [dbg|opt]${NONE}: build drivers only
564 565 566 567
  ${BLUE}build_planning${NONE}: compile planning and its dependencies.
  ${BLUE}build_control${NONE}: compile control and its dependencies.
  ${BLUE}build_prediction${NONE}: compile prediction and its dependencies.
  ${BLUE}build_pnc${NONE}: compile pnc and its dependencies.
X
Xiangquan Xiao 已提交
568
  ${BLUE}build_no_perception [dbg|opt]${NONE}: run build, skip building perception module, useful when some perception dependencies are not satisfied, e.g., CUDA, CUDNN, LIDAR, etc.
569
  ${BLUE}build_prof${NONE}: build for gprof support.
C
Calvin Miao 已提交
570 571 572 573 574 575 576 577 578 579 580 581
  ${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
  "
582 583
}

S
storypku 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597
function bootstrap() {
  if [ -z "$PYTHON_BIN_PATH" ]; then
    PYTHON_BIN_PATH=$(which python3 || true)
  fi
  if [[ -f "${APOLLO_ROOT_DIR}/.apollo.bazelrc" ]]; then
    return
  fi
  cp -f "${TOP_DIR}/tools/sample.bazelrc" "${APOLLO_ROOT_DIR}/.apollo.bazelrc"
  # Set all env variables
  # TODO(storypku): enable bootstrap.py inside docker
  # $PYTHON_BIN_PATH ${APOLLO_ROOT_DIR}/tools/bootstrap.py $@
  echo "bootstrap done"
}

S
siyangy 已提交
598
function main() {
599

S
siyangy 已提交
600
  check_machine_arch
601
  apollo_check_system_config
S
storypku 已提交
602 603 604

  bootstrap

S
siyangy 已提交
605 606
  check_esd_files

607
  DEFINES="--define ARCH=${MACHINE_ARCH} --define CAN_CARD=${CAN_CARD} --cxxopt=-DUSE_ESD_CAN=${USE_ESD_CAN}"
F
Fla3inH0tCheet0s 已提交
608
  # Enable bazel's feature to compute md5 checksums in parallel
609
  DEFINES="${DEFINES} --experimental_multi_threaded_digest"
610 611 612 613

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

615 616 617
  local cmd=$1
  shift

S
siyangy 已提交
618
  START_TIME=$(get_now)
619
  case $cmd in
S
siyangy 已提交
620
    check)
621
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
622
      check $@
S
siyangy 已提交
623 624
      ;;
    build)
S
siyangy 已提交
625
      set_use_gpu
626
      apollo_build_dbg $@
627 628
      ;;
    build_cpu)
L
Lei Wang 已提交
629
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
630
      apollo_build_dbg $@
S
siyangy 已提交
631
      ;;
632
    build_prof)
D
Dong Li 已提交
633
      DEFINES="${DEFINES} --config=cpu_prof --cxxopt=-DCPU_ONLY"
634 635
      apollo_build_dbg $@
      ;;
636
    build_no_perception)
S
siyangy 已提交
637
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
638
      BUILD_FILTER="no_perception"
639 640 641
      if [ "$1" == "opt" ]; then
        shift
        apollo_build_opt $@
642
      else
643 644 645
        shift
        apollo_build_dbg $@
      fi
646
      ;;
647 648 649 650
    clean_cyber)
      export LD_PRELOAD=
      clean
      ;;
651
    build_cyber)
652
      export LD_PRELOAD=
653 654 655 656 657 658 659 660 661 662
      BUILD_FILTER="cyber"
      if [ "$1" == "opt" ]; then
        shift
        apollo_build_opt $@
      else
        shift
        apollo_build_dbg $@
      fi
      ;;
    build_drivers)
663
      export LD_PRELOAD=
664 665 666 667 668 669 670 671 672
      BUILD_FILTER="drivers"
      if [ "$1" == "opt" ]; then
        shift
        apollo_build_opt $@
      else
        shift
        apollo_build_dbg $@
      fi
      ;;
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
    build_control)
      BUILD_FILTER="control"
      apollo_build_dbg $@
      ;;
    build_planning)
      BUILD_FILTER="planning"
      apollo_build_dbg $@
      ;;
    build_prediction)
      BUILD_FILTER="prediction"
      apollo_build_dbg $@
      ;;
    build_pnc)
      BUILD_FILTER="pnc"
      apollo_build_dbg $@
      ;;
689 690 691 692
    cibuild)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      cibuild $@
      ;;
693 694 695 696
    cibuild_extended)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      cibuild_extended $@
      ;;
697
    build_opt)
698
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY --copt=-fpic"
699
      apollo_build_opt $@
S
siyangy 已提交
700
      ;;
701
    build_gpu)
S
siyangy 已提交
702
      set_use_gpu
703
      apollo_build_dbg $@
J
Jun Zhu 已提交
704
      ;;
J
Jun Zhu 已提交
705
    build_opt_gpu)
S
siyangy 已提交
706 707
      set_use_gpu
      DEFINES="${DEFINES} --copt=-fpic"
708
      apollo_build_opt $@
Y
Yu Cao 已提交
709 710 711 712 713
      ;;
    build_teleop)
      set_use_gpu
      DEFINES="${DEFINES} --copt=-fpic --define WITH_TELEOP=1 --cxxopt=-DTELEOP"
      apollo_build_opt $@
S
siyangy 已提交
714
      ;;
S
siyangy 已提交
715 716 717
    build_fe)
      build_fe
      ;;
S
siyangy 已提交
718 719 720
    buildify)
      buildify
      ;;
L
Lei Wang 已提交
721 722 723
    build_py)
      build_py_proto
      ;;
724 725 726
    config)
      config
      ;;
S
siyangy 已提交
727 728 729 730 731 732 733
    doc)
      gen_doc
      ;;
    lint)
      run_lint
      ;;
    test)
S
siyangy 已提交
734
      set_use_gpu
735 736 737
      run_test $@
      ;;
    test_cpu)
738
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
L
Lei Wang 已提交
739
      run_test $@
S
siyangy 已提交
740
      ;;
L
Liangliang Zhang 已提交
741 742 743 744
    citest)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest $@
      ;;
745 746 747 748 749 750 751 752
    citest_basic)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest_basic $@
      ;;
    citest_extended)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest_extended $@
      ;;
753
    test_gpu)
S
siyangy 已提交
754
      set_use_gpu
L
Lei Wang 已提交
755
      run_test $@
756
      ;;
S
siyangy 已提交
757
    coverage)
L
Lei Wang 已提交
758
      gen_coverage $@
S
siyangy 已提交
759 760 761 762 763 764 765
      ;;
    clean)
      clean
      ;;
    version)
      version
      ;;
C
Calvin Miao 已提交
766 767 768
    usage)
      print_usage
      ;;
S
siyangy 已提交
769 770 771 772 773 774
    *)
      print_usage
      ;;
  esac
}

775
main $@