apollo.sh 25.0 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
  # check docker environment
32
  if [ ${MACHINE_ARCH} == "x86_64" ] && [ ${APOLLO_IN_DOCKER} != "true" ]; then
33 34 35 36
    echo -e "${RED}Must run $0 in dev docker or release docker${NO_COLOR}"
    exit 0
  fi

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

      # 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
49 50 51 52
      ;;
    "Darwin")
      warning "Mac OS is not officially supported in the current version. Build could fail. We recommend using Ubuntu 14.04."
      ;;
53
    *)
54 55 56 57
      error "Unsupported system: ${OP_SYSTEM}."
      error "Please use Linux, we recommend Ubuntu 14.04."
      exit 1
      ;;
58 59 60
  esac
}

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

S
siyangy 已提交
65 66 67 68 69 70 71 72 73
  # 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
74 75

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

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

D
Dong Li 已提交
83 84 85 86
  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 已提交
87
      CAN_CARD="esd_can"
D
Dong Li 已提交
88
  else
89
      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 已提交
90 91 92 93 94
      USE_ESD_CAN=false
  fi
}

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

L
Liangliang Zhang 已提交
103
    BUILD_TARGETS="
104
    //modules/canbus/...
105
    //modules/common/...
106
    //modules/control/...
107
    //modules/dreamview/...
L
Liangliang Zhang 已提交
108
    //modules/drivers/...
109
    //modules/guardian/...
110
    //modules/integration_test/...
111
    //modules/localization/...
112
    //modules/map/...
A
Aaron Xiao 已提交
113
    //modules/monitor/...
L
Liangliang Zhang 已提交
114
    //modules/perception/...
J
JasonZhou404 已提交
115
    //modules/planning/...
116 117
    //modules/prediction/...
    //modules/routing/...
118
    //modules/transform/...
D
Dong Li 已提交
119
    //modules/third_party_perception/...
120
    //modules/tools/manual_traffic_light/...
121
    //modules/tools/prediction/fake_prediction/...
122
    //modules/tools/rosbag_to_record/...
123 124
    //modules/tools/visualizer/...
    //modules/transform/..."
125
    # //modules/calibration/...
126
    # //modules/data/...
127

C
Calvin Miao 已提交
128 129 130
  if [ $? -ne 0 ]; then
    fail 'Build failed!'
  fi
D
Dong Li 已提交
131
  if ! $USE_ESD_CAN; then
132
     BUILD_TARGETS=$(echo $BUILD_TARGETS |tr ' ' '\n' | grep -v "esd")
D
Dong Li 已提交
133
  fi
134
  #skip msf for non x86_64 platforms
135
  if [ ${MACHINE_ARCH} != "x86_64" ]; then
136 137
     BUILD_TARGETS=$(echo $BUILD_TARGETS |tr ' ' '\n' | grep -v "msf")
  fi
D
Dong Li 已提交
138 139 140 141 142 143
}

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

144 145 146 147 148 149 150 151
function build_cybertron() {
  cd /apollo/framework
  bash cybertron.sh build_fast
  cd /apollo
  info "Building cybertron ..."
  source framework/install/setup.bash
}

S
siyangy 已提交
152
function build() {
153
  if [ "${USE_GPU}" = "1" ] ; then
154 155 156 157
    echo -e "${YELLOW}Running build under GPU mode. GPU is required to run the build.${NO_COLOR}"
  else
    echo -e "${YELLOW}Running build under CPU mode. No GPU is required to run the build.${NO_COLOR}"
  fi
158
  info "Start building, please wait ..."
159 160
  build_cybertron

D
Dong Li 已提交
161
  generate_build_targets
162 163
  info "Building on $MACHINE_ARCH..."

164
  MACHINE_ARCH=$(uname -m)
165
  JOB_ARG="--jobs=$(nproc) --ram_utilization_factor 80"
166 167 168
  if [ "$MACHINE_ARCH" == 'aarch64' ]; then
    JOB_ARG="--jobs=3"
  fi
169
  info "Building with $JOB_ARG for $MACHINE_ARCH"
170

J
jiayunhan 已提交
171
  echo "$BUILD_TARGETS" | xargs bazel build $JOB_ARG $DEFINES -c $@
172

173
  if [ $? -ne 0 ]; then
D
Dong Li 已提交
174 175
    fail 'Build failed!'
  fi
S
siyangy 已提交
176 177

  # Build python proto
L
Lei Wang 已提交
178
  build_py_proto
179

180 181
  # Clear KV DB and update commit_id after compiling.
  rm -fr data/kv_db
182
  REVISION=$(get_revision)
183
  python modules/tools/common/kv_db.py put \
184
      "apollo:data:commit_id" "$REVISION"
185

S
siyangy 已提交
186 187 188 189 190 191
  # 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
192 193
  if [ $? -eq 0 ]; then
    success 'Build passed!'
194 195
  else
    fail 'Build failed'
196
  fi
D
Dong Li 已提交
197 198
}

199
function cibuild() {
200 201 202 203 204 205
  info "Building framework ..."
  cd /apollo/framework
  bash cybertron.sh build_fast

  cd /apollo
  info "Building modules ..."
206 207 208 209 210 211

  JOB_ARG="--jobs=$(nproc)"
  if [ "$MACHINE_ARCH" == 'aarch64' ]; then
    JOB_ARG="--jobs=3"
  fi

A
Aaron Xiao 已提交
212
  info "Building with $JOB_ARG for $MACHINE_ARCH"
213
  BUILD_TARGETS="
214
    //modules/canbus/...
215 216
    //modules/common/...
    //modules/control/...
217
    //modules/dreamview/...
L
Liangliang Zhang 已提交
218 219 220 221 222
    //modules/drivers/radar/conti_radar/...
    //modules/drivers/radar/racobit_radar/...
    //modules/drivers/radar/ultrasonic_radar/...
    //modules/drivers/gnss/...
    //modules/drivers/velodyne/...
fengqikai1414's avatar
fengqikai1414 已提交
223
    //modules/drivers/camera/...
224
    //modules/guardian/...
225
    //modules/localization/...
226
    //modules/map/...
L
Liangliang Zhang 已提交
227
    //modules/perception/...
228 229
    //modules/planning/...
    //modules/prediction/...
230
    //modules/routing/...
231
    //modules/transform/...
232 233
    //modules/third_party_perception/...
    //modules/transform/..."
234

235
  bazel build $JOB_ARG $DEFINES $@ $BUILD_TARGETS
236

237 238 239 240 241 242 243
  if [ $? -eq 0 ]; then
    success 'Build passed!'
  else
    fail 'Build failed!'
  fi
}

244
function apollo_build_dbg() {
245
  build "dbg" $@
S
siyangy 已提交
246 247
}

248
function apollo_build_opt() {
249
  build "opt" $@
S
siyangy 已提交
250 251
}

L
Lei Wang 已提交
252 253 254 255 256 257
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'
258
  find modules/ -name "*.proto" \
S
siyangy 已提交
259
      | grep -v node_modules \
260
      | xargs ${PROTOC} --python_out=py_proto
L
Lei Wang 已提交
261 262 263
  find py_proto/* -type d -exec touch "{}/__init__.py" \;
}

D
Dong Li 已提交
264
function check() {
D
Dong Li 已提交
265
  bash $0 build && bash $0 "test" && bash $0 lint
D
Dong Li 已提交
266 267 268 269 270 271 272 273 274 275 276

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

function warn_proprietary_sw() {
277 278 279 280 281 282
  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 已提交
283 284 285
}

function release() {
286 287
  RELEASE_DIR="${HOME}/.cache/apollo_release"
  if [ -d "${RELEASE_DIR}" ]; then
288
    rm -rf "${RELEASE_DIR}"
289
  fi
D
Dong Li 已提交
290 291
  APOLLO_RELEASE_DIR="${RELEASE_DIR}/apollo"
  mkdir -p "${APOLLO_RELEASE_DIR}"
292 293 294 295 296 297

  # 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 已提交
298
    DST_PATH="${APOLLO_RELEASE_DIR}/${BIN}"
299 300 301 302 303
    if [ -e "${SRC_PATH}" ]; then
      mkdir -p "$(dirname "${DST_PATH}")"
      cp "${SRC_PATH}" "${DST_PATH}"
    fi
  done
D
Dong Li 已提交
304

305
  # modules data and conf
306 307
  CONFS=$(find modules/ -name "conf")
  DATAS=$(find modules/ -name "data")
308 309
  OTHER=("modules/tools"
         "modules/perception/model")
310 311 312 313 314 315 316 317 318 319 320
  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
321
  # Other
322
  for path in "${OTHER[@]}"; do
323 324 325
    mkdir -p $APOLLO_RELEASE_DIR/$path
    rsync -a $path/* $APOLLO_RELEASE_DIR/$path
  done
326 327 328

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

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

S
siyangy 已提交
333
  # scripts
D
Dong Li 已提交
334
  cp -r scripts ${APOLLO_RELEASE_DIR}
S
siyangy 已提交
335

336 337 338
  # remove mounted models
  rm -rf ${APOLLO_RELEASE_DIR}/modules/perception/model/yolo_camera_detector/

D
Dong Li 已提交
339
  # lib
D
Dong Li 已提交
340
  LIB_DIR="${APOLLO_RELEASE_DIR}/lib"
341
  mkdir "${LIB_DIR}"
D
Dong Li 已提交
342
  if $USE_ESD_CAN; then
L
Lei Wang 已提交
343
    warn_proprietary_sw
D
Dong Li 已提交
344
  fi
345
  cp -r third_party/can_card_library/*/lib/* $LIB_DIR
L
Lei Wang 已提交
346 347
  cp -r bazel-genfiles/external $LIB_DIR
  cp -r py_proto/modules $LIB_DIR
348
  cp /apollo/bazel-apollo/bazel-out/local-opt/bin/modules/perception/cuda_util/libintegrated_cuda_util.so $LIB_DIR
349

D
Dong Li 已提交
350
  # doc
D
Dong Li 已提交
351 352
  cp LICENSE "${APOLLO_RELEASE_DIR}"
  cp third_party/ACKNOWLEDGEMENT.txt "${APOLLO_RELEASE_DIR}"
L
Lei Wang 已提交
353

S
siyangy 已提交
354
  # release info
D
Dong Li 已提交
355
  META="${APOLLO_RELEASE_DIR}/meta.ini"
356
  echo "git_commit: $(get_revision)" >> $META
357
  echo "git_branch: $(get_branch)" >> $META
358 359
  echo "car_type: LINCOLN.MKZ" >> $META
  echo "arch: ${MACHINE_ARCH}" >> $META
D
Dong Li 已提交
360 361 362 363
}

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

D
Dong Li 已提交
370 371 372 373 374 375 376 377
  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
378 379 380 381 382 383 384 385

  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

J
Jiangtao Hu 已提交
386
  lcov --rc lcov_branch_coverage=1 --capture --directory "$COV_DIR/objs" --output-file "$COV_DIR/conv.info"
D
Dong Li 已提交
387 388 389
  if [ $? -ne 0 ]; then
    fail 'lcov failed!'
  fi
J
Jiangtao Hu 已提交
390
  lcov --rc lcov_branch_coverage=1 --remove "$COV_DIR/conv.info" \
D
Dong Li 已提交
391 392 393 394 395 396 397 398 399 400 401
      "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() {
402
  JOB_ARG="--jobs=$(nproc) --ram_utilization_factor 80"
403
  generate_build_targets
404
  if [ "$USE_GPU" == "1" ]; then
405
    echo -e "${YELLOW}Running tests under GPU mode. GPU is required to run the tests.${NO_COLOR}"
406
    echo "$BUILD_TARGETS" | xargs bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@
407
  else
408
    echo -e "${YELLOW}Running tests under CPU mode. No GPU is required to run the tests.${NO_COLOR}"
409
    echo "$BUILD_TARGETS" | grep -v "cnn_segmentation_test\|yolo_camera_detector_test\|unity_recognize_test\|perception_traffic_light_rectify_test\|cuda_util_test" | xargs bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@
410
  fi
411 412 413 414 415
  if [ $? -ne 0 ]; then
    fail 'Test failed!'
    return 1
  fi

S
siyangy 已提交
416
  if [ $? -eq 0 ]; then
D
Dong Li 已提交
417 418 419 420 421
    success 'Test passed!'
    return 0
  fi
}

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
function citest_perception() {
  generate_build_targets

  # common related test
  echo "$BUILD_TARGETS" | grep "perception\/" | grep -v "sunnyvale_big_loop\|cnn_segmentation_test\|yolo_camera_detector_test\|unity_recognize_test\|perception_traffic_light_rectify_test\|cuda_util_test" | xargs bazel test $DEFINES --config=unit_test -c dbg --test_verbose_timeout_warnings $@

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

L
Liangliang Zhang 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
function citest_dreamview() {
  generate_build_targets

  # common related test
  echo "$BUILD_TARGETS" | grep "dreamview\/" | xargs bazel test $DEFINES --config=unit_test -c dbg --test_verbose_timeout_warnings $@

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

function citest_map() {
  generate_build_targets

  # common related test
L
Liangliang Zhang 已提交
456
  echo "$BUILD_TARGETS" | grep "map\/" | grep -v "cuda_util_test" | xargs bazel test $DEFINES --config=unit_test -c dbg --test_verbose_timeout_warnings $@
L
Liangliang Zhang 已提交
457 458 459 460 461 462 463 464 465 466 467

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

function citest_basic() {
L
Liangliang Zhang 已提交
468 469
  set -e

470 471 472 473 474 475 476 477 478 479 480
  BUILD_TARGETS="
    //modules/common/...
    //modules/control/...
    //modules/localization/proto/...
    //modules/localization/rtk/...
    `bazel query //modules/localization/msf/... except //modules/localization/msf/local_tool/...`
    `bazel query //modules/localization/msf/local_tool/local_visualization/...`
    //modules/perception/proto/...
    //modules/planning/...
    //modules/prediction/...
    //modules/routing/..."
481

L
Liangliang Zhang 已提交
482 483
  JOB_ARG="--jobs=$(nproc) --ram_utilization_factor 80"

484
  # control related test
L
Liangliang Zhang 已提交
485
  echo "$BUILD_TARGETS" | grep "control\/" | xargs bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@
486 487

  # prediction related test
L
Liangliang Zhang 已提交
488
  echo "$BUILD_TARGETS" | grep "prediction\/" | xargs bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@
489 490

  # planning related test
L
Liangliang Zhang 已提交
491
  echo "$BUILD_TARGETS" | grep "planning\/" | xargs bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@
492

493 494 495 496 497 498 499 500 501
  if [ $? -eq 0 ]; then
    success 'Test passed!'
    return 0
  else
    fail 'Test failed!'
    return 1
  fi
}

L
Liangliang Zhang 已提交
502
function citest() {
503 504 505 506
  info "Building framework ..."
  cd /apollo/framework
  bash cybertron.sh build_fast
  cd /apollo
507
  source framework/install/setup.bash
508

L
Liangliang Zhang 已提交
509
  citest_basic
L
Liangliang Zhang 已提交
510 511 512 513 514 515 516 517 518
  if [ $? -eq 0 ]; then
    success 'Test passed!'
    return 0
  else
    fail 'Test failed!'
    return 1
  fi
}

D
Dong Li 已提交
519
function run_cpp_lint() {
520
  set -e
521
  # check /apollo/module
522
  generate_build_targets
523
  echo "$BUILD_TARGETS" | grep -v "tools/visualizer" | xargs bazel test --config=cpplint -c dbg
524 525 526 527

  # check /apollo/framework
  BUILD_TARGETS="//framework/cybertron/..."
  echo "$BUILD_TARGETS" | xargs bazel test --config=cpplint -c dbg
D
Dong Li 已提交
528 529 530
}

function run_bash_lint() {
S
siyangy 已提交
531
  FILES=$(find "${APOLLO_ROOT_DIR}" -type f -name "*.sh" | grep -v ros)
D
Dong Li 已提交
532 533 534 535
  echo "${FILES}" | xargs shellcheck
}

function run_lint() {
C
Calvin Miao 已提交
536
  # Add cpplint rule to BUILD files that do not contain it.
537
  for file in $(find framework modules -name BUILD |  grep -v gnss/third_party | \
C
Calvin Miao 已提交
538 539 540 541 542 543
    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 已提交
544 545
  run_cpp_lint

D
Dong Li 已提交
546 547 548 549 550 551 552 553
  if [ $? -eq 0 ]; then
    success 'Lint passed!'
  else
    fail 'Lint failed!'
  fi
}

function clean() {
A
Aaron Xiao 已提交
554
  # Remove bazel cache.
555
  bazel clean --async
A
Aaron Xiao 已提交
556 557
  # Remove cmake cache.
  rm -fr framework/build
D
Dong Li 已提交
558 559 560
}

function buildify() {
561 562 563
  local buildifier_url=https://github.com/bazelbuild/buildtools/releases/download/0.4.5/buildifier
  wget $buildifier_url -O ~/.buildifier
  chmod +x ~/.buildifier
564
  find . -name '*BUILD' -type f -exec ~/.buildifier -showlog -mode=fix {} +
565 566 567 568 569 570
  if [ $? -eq 0 ]; then
    success 'Buildify worked!'
  else
    fail 'Buildify failed!'
  fi
  rm ~/.buildifier
D
Dong Li 已提交
571 572
}

S
siyangy 已提交
573 574 575 576 577
function build_fe() {
  cd modules/dreamview/frontend
  yarn build
}

D
Dong Li 已提交
578 579 580 581 582 583
function gen_doc() {
  rm -rf docs/doxygen
  doxygen apollo.doxygen
}

function version() {
584 585 586 587 588
  rev=$(get_revision)
  if [ "$rev" = "unknown" ];then
    echo "Version: $rev"
    return
  fi
D
Dong Li 已提交
589 590 591 592 593 594
  commit=$(git log -1 --pretty=%H)
  date=$(git log -1 --pretty=%cd)
  echo "Commit: ${commit}"
  echo "Date: ${date}"
}

595 596 597 598 599 600 601 602 603 604 605
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"
}

606 607 608 609 610 611 612 613 614 615 616
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"
}

F
fengkaiwen01 已提交
617 618
function build_velodyne() {
  CURRENT_PATH=$(pwd)
F
fengkaiwen01 已提交
619 620
  if [ -d "${ROS_ROOT}" ]; then
    ROS_PATH="${ROS_ROOT}/../.."
F
fengkaiwen01 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
  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/
}

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
function build_velodyne_vls128() {
  CURRENT_PATH=$(pwd)
  if [ -d "${ROS_ROOT}" ]; then
    ROS_PATH="${ROS_ROOT}/../.."
  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_vls \
    --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/
}


664 665 666 667 668 669 670 671 672 673 674 675 676

function build_lslidar() {
  CURRENT_PATH=$(pwd)
  if [ -d "${ROS_ROOT}" ]; then
    ROS_PATH="${ROS_ROOT}/../.."
  else
    warning "ROS not found. Run apolllo.sh build first."
    exit 1
  fi

  source "${ROS_PATH}/setup.bash"

  cd modules
677
  catkin_make_isolated --install --source drivers/lslidar_apollo \
678 679 680 681 682 683 684 685 686 687
    --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/
}

T
Tony Zhang 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
function build_rslidar() {
  CURRENT_PATH=$(pwd)
  if [ -d "${ROS_ROOT}" ]; then
    ROS_PATH="${ROS_ROOT}/../.."
  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/rslidar \
    --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 已提交
712 713
function build_usbcam() {
  CURRENT_PATH=$(pwd)
F
fengkaiwen01 已提交
714 715
  if [ -d "${ROS_ROOT}" ]; then
    ROS_PATH="${ROS_ROOT}/../.."
Y
yibingl 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
  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/
}

735
function config() {
736
  ${APOLLO_ROOT_DIR}/scripts/configurator.sh
737 738
}

739
function print_usage() {
C
Calvin Miao 已提交
740 741 742 743 744 745 746 747 748 749
  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
750
  ${BLUE}build_opt${NONE}: build optimized binary for the code
J
Jun Zhu 已提交
751
  ${BLUE}build_gpu${NONE}: run build only with Caffe GPU mode support
752
  ${BLUE}build_cybertron${NONE}: run build cybertron framework only
F
fengkaiwen01 已提交
753
  ${BLUE}build_velodyne${NONE}: build velodyne driver
754
  ${BLUE}build_velodyne_vls128${NONE}: build velodyne vls-128 driver
755
  ${BLUE}build_lslidar${NONE}: build lslidar driver
T
Tony Zhang 已提交
756
  ${BLUE}build_rslidar${NONE}: build rslidar driver
gengqx's avatar
gengqx 已提交
757
  ${BLUE}build_usbcam${NONE}: build usb camera driver
J
Jun Zhu 已提交
758
  ${BLUE}build_opt_gpu${NONE}: build optimized binary with Caffe GPU mode support
C
Calvin Miao 已提交
759
  ${BLUE}build_fe${NONE}: compile frontend javascript code, this requires all the node_modules to be installed already
760
  ${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.
761
  ${BLUE}build_prof${NONE}: build for gprof support.
C
Calvin Miao 已提交
762 763 764 765 766 767 768 769 770 771 772 773
  ${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
  "
774 775
}

S
siyangy 已提交
776 777 778
function main() {
  source_apollo_base
  check_machine_arch
779
  apollo_check_system_config
S
siyangy 已提交
780 781
  check_esd_files

782 783 784 785 786
  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 已提交
787

788 789 790
  local cmd=$1
  shift

S
siyangy 已提交
791
  START_TIME=$(get_now)
792
  case $cmd in
S
siyangy 已提交
793
    check)
794
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
795
      check $@
S
siyangy 已提交
796 797
      ;;
    build)
798
      DEFINES="${DEFINES} --define USE_GPU=true --cxxopt=-DUSE_GPU"
799
      USE_GPU="1"
800 801 802
      apollo_build_dbg $@
      ;;
    build_cpu)
L
Lei Wang 已提交
803
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
804
      apollo_build_dbg $@
S
siyangy 已提交
805
      ;;
806
    build_prof)
D
Dong Li 已提交
807
      DEFINES="${DEFINES} --config=cpu_prof --cxxopt=-DCPU_ONLY"
808 809
      apollo_build_dbg $@
      ;;
810
    build_no_perception)
S
siyangy 已提交
811
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
812
      NOT_BUILD_PERCEPTION=true
813 814 815
      if [ "$1" == "opt" ]; then
        shift
        apollo_build_opt $@
816
      else
817 818 819
        shift
        apollo_build_dbg $@
      fi
820
      ;;
821 822 823 824
    cibuild)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      cibuild $@
      ;;
825
    build_opt)
826
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY --copt=-fpic"
827
      apollo_build_opt $@
S
siyangy 已提交
828
      ;;
829
    build_gpu)
830
      DEFINES="${DEFINES} --define USE_GPU=true --cxxopt=-DUSE_GPU"
831
      USE_GPU="1"
832
      apollo_build_dbg $@
J
Jun Zhu 已提交
833
      ;;
J
Jun Zhu 已提交
834
    build_opt_gpu)
835
      DEFINES="${DEFINES} --define USE_GPU=true --cxxopt=-DUSE_GPU --copt=-fpic"
836
      USE_GPU="1"
837
      apollo_build_opt $@
S
siyangy 已提交
838
      ;;
S
siyangy 已提交
839 840 841
    build_fe)
      build_fe
      ;;
S
siyangy 已提交
842 843 844
    buildify)
      buildify
      ;;
845 846 847
    build_cybertron)
     build_cybertron
      ;;
L
Lei Wang 已提交
848 849 850
    build_py)
      build_py_proto
      ;;
F
fengkaiwen01 已提交
851
    build_velodyne)
F
fengkaiwen01 已提交
852 853
      build_velodyne
      ;;
854 855 856
    build_velodyne_vls128)
      build_velodyne_vls128
      ;;
857 858 859
    build_lslidar)
      build_lslidar
      ;;
T
Tony Zhang 已提交
860 861 862
    build_rslidar)
      build_rslidar
      ;;
F
fengkaiwen01 已提交
863
    build_usbcam)
Y
yibingl 已提交
864 865
      build_usbcam
      ;;
866 867 868
    config)
      config
      ;;
S
siyangy 已提交
869 870 871 872 873 874 875
    doc)
      gen_doc
      ;;
    lint)
      run_lint
      ;;
    test)
876 877 878 879 880
      DEFINES="${DEFINES} --cxxopt=-DUSE_GPU"
      USE_GPU="1"
      run_test $@
      ;;
    test_cpu)
881
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
L
Lei Wang 已提交
882
      run_test $@
S
siyangy 已提交
883
      ;;
L
Liangliang Zhang 已提交
884 885 886 887 888 889 890 891 892 893 894 895
    citest)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest $@
      ;;
    citest_map)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest_map $@
      ;;
    citest_dreamview)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest_dreamview $@
      ;;
896 897 898 899
    citest_perception)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest_perception $@
      ;;
L
Liangliang Zhang 已提交
900
    citest_basic)
901
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
L
Liangliang Zhang 已提交
902
      citest_basic $@
903
      ;;
904
    test_gpu)
905
      DEFINES="${DEFINES} --cxxopt=-DUSE_GPU"
906
      USE_GPU="1"
L
Lei Wang 已提交
907
      run_test $@
908
      ;;
S
siyangy 已提交
909 910 911 912 913 914 915
    release)
      release 1
      ;;
    release_noproprietary)
      release 0
      ;;
    coverage)
L
Lei Wang 已提交
916
      gen_coverage $@
S
siyangy 已提交
917 918 919 920 921 922 923
      ;;
    clean)
      clean
      ;;
    version)
      version
      ;;
C
Calvin Miao 已提交
924 925 926
    usage)
      print_usage
      ;;
S
siyangy 已提交
927 928 929 930 931 932
    *)
      print_usage
      ;;
  esac
}

933
main $@