apollo.sh 25.7 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
#=================================================

F
Fla3inH0tCheet0s 已提交
23 24
DISABLED_CYBER_MODULES="except //cyber/record:record_file_integration_test"

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

29
  source ${DIR}/scripts/apollo_base.sh $1
S
siyangy 已提交
30
}
31 32

function apollo_check_system_config() {
33
  # check docker environment
34
  if [ ${MACHINE_ARCH} == "x86_64" ] && [ ${APOLLO_IN_DOCKER} != "true" ]; then
35 36 37 38
    echo -e "${RED}Must run $0 in dev docker or release docker${NO_COLOR}"
    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 46 47 48 49 50

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

S
siyangy 已提交
63 64 65
function check_machine_arch() {
  # the machine type, currently support x86_64, aarch64
  MACHINE_ARCH=$(uname -m)
66
  if [ "${MACHINE_ARCH}" != "x86_64" ] && [ "${MACHINE_ARCH}" != "aarch64" ]; then
S
siyangy 已提交
67 68 69
    fail "Unknown machine architecture $MACHINE_ARCH"
    exit 1
  fi
70 71

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

function check_esd_files() {
S
siyangy 已提交
77 78
  CAN_CARD="fake_can"

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

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

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

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

S
siyangy 已提交
136
function build() {
137
  if [ "${USE_GPU}" = "1" ] ; then
138 139 140 141
    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
142
  info "Start building, please wait ..."
143

D
Dong Li 已提交
144
  generate_build_targets
145 146
  info "Building on $MACHINE_ARCH..."

147
  MACHINE_ARCH=$(uname -m)
148
  JOB_ARG="--jobs=$(nproc) --ram_utilization_factor 80"
149 150 151
  if [ "$MACHINE_ARCH" == 'aarch64' ]; then
    JOB_ARG="--jobs=3"
  fi
152
  info "Building with $JOB_ARG for $MACHINE_ARCH"
153

X
 
Xiangquan Xiao 已提交
154
  bazel build $JOB_ARG $DEFINES -c $@ $BUILD_TARGETS
A
Aaron Xiao 已提交
155
  if [ ${PIPESTATUS[0]} -ne 0 ]; then
D
Dong Li 已提交
156 157
    fail 'Build failed!'
  fi
S
siyangy 已提交
158 159

  # Build python proto
L
Lei Wang 已提交
160
  build_py_proto
161

162
  # Clear KV DB and update commit_id after compiling.
163 164 165 166 167 168 169 170 171 172 173 174
  if [ "$BUILD_FILTER" == 'cyber' ] || [ "$BUILD_FILTER" == 'drivers' ]; then
    info "Skipping revision recording"
  else
    bazel build $JOB_ARG $DEFINES -c $@ $BUILD_TARGETS
    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
175

S
siyangy 已提交
176 177 178 179 180 181
  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
182 183
  if [ $? -eq 0 ]; then
    success 'Build passed!'
184 185
  else
    fail 'Build failed'
186
  fi
D
Dong Li 已提交
187 188
}

189
function cibuild_extended() {
190 191 192 193
  info "Building framework ..."

  cd /apollo
  info "Building modules ..."
194

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

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

  # 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/...
    `
219 220 221 222 223 224 225 226 227 228 229 230

  bazel build $JOB_ARG $DEFINES $@ $BUILD_TARGETS

  if [ $? -eq 0 ]; then
    success 'Build passed!'
  else
    fail 'Build failed!'
  fi
}
function cibuild() {
  info "Building framework ..."
  cd /apollo
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 252 253
  # 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.
  bazel build $JOB_ARG $DEFINES $@ "//modules/data/..." || bazel clean

254
  bazel build $JOB_ARG $DEFINES $@ $BUILD_TARGETS
255

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

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

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

L
Lei Wang 已提交
271 272 273 274 275
function build_py_proto() {
  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

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

function warn_proprietary_sw() {
298 299 300 301 302 303
  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 已提交
304 305 306
}

function release() {
307 308
  RELEASE_DIR="${HOME}/.cache/apollo_release"
  if [ -d "${RELEASE_DIR}" ]; then
309
    rm -rf "${RELEASE_DIR}"
310
  fi
D
Dong Li 已提交
311 312
  APOLLO_RELEASE_DIR="${RELEASE_DIR}/apollo"
  mkdir -p "${APOLLO_RELEASE_DIR}"
313

L
Lei Wang 已提交
314 315 316 317 318 319
  # cp built dynamic libraries
  LIBS=$(find bazel-out/* -name "*.so" )
  for LIB in ${LIBS}; do
    cp -a --parent ${LIB} ${APOLLO_RELEASE_DIR}
  done
  mkdir ${APOLLO_RELEASE_DIR}/bazel-bin
320
  mv ${APOLLO_RELEASE_DIR}/bazel-out/local-opt/bin/* ${APOLLO_RELEASE_DIR}/bazel-bin/
L
Lei Wang 已提交
321 322 323 324 325 326
  rm -rf ${APOLLO_RELEASE_DIR}/bazel-out

  # reset softlinks
  cd ${APOLLO_RELEASE_DIR}/bazel-bin
  LIST=("_solib_k8")
  for DIR in "${LIST[@]}"; do
327
    LINKS=$(find ${DIR}/* -name "*.so" -type l | sed '/.*@.*/d')
L
Lei Wang 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    for LINK in $LINKS; do
      LIB=$(echo $LINK | sed 's/_S/\//g' | sed 's/_U/_/g')
      if [[ $LIB == *"_solib_k8/lib"* ]]; then
        LIB="/apollo/bazel-bin/$(echo $LIB | awk -F "_solib_k8/lib" '{print $NF}')"
      elif [[ $LIB == *"___"* ]]; then
        LIB="/apollo/bazel-bin/$(echo $LIB | awk -F "___" '{print $NF}')"
      else
        LIB="/apollo/bazel-bin/$(echo $LIB | awk -F "apollo/" '{print $NF}')"
      fi
      ln -sf $LIB $LINK
    done
  done
  cd -

  # setup cyber binaries and convert from //path:target to path/target
N
Ning Qu 已提交
343
  CYBERBIN=$(bazel query "kind(cc_binary, //cyber/... //modules/tools/visualizer/...)" | sed 's/^\/\///' | sed 's/:/\//' | sed '/.*.so$/d')
L
Lei Wang 已提交
344 345 346 347 348 349
  for BIN in ${CYBERBIN}; do
    cp -P --parent "bazel-bin/${BIN}" ${APOLLO_RELEASE_DIR}
  done
  cp --parent "cyber/setup.bash" "${APOLLO_RELEASE_DIR}"
  cp --parent -a "cyber/tools/cyber_launch" "${APOLLO_RELEASE_DIR}"
  cp --parent -a "cyber/tools/cyber_tools_auto_complete.bash" "${APOLLO_RELEASE_DIR}"
350
  cp --parent -a "cyber/python" "${APOLLO_RELEASE_DIR}"
L
Lei Wang 已提交
351 352 353 354 355 356 357 358 359 360


  # setup tools
  TOOLSBIN=$(bazel query "kind(cc_binary, //modules/tools/...)" | sed 's/^\/\///' | sed 's/:/\//' | sed '/.*.so$/d')
  for BIN in ${TOOLSBIN}; do
    cp -P --parent "bazel-bin/${BIN}" ${APOLLO_RELEASE_DIR}
  done
  TOOLSPY=$(find modules/tools/* -name "*.py")
  for PY in ${TOOLSPY}; do
    cp -P --parent "${PY}" "${APOLLO_RELEASE_DIR}/bazel-bin"
361
  done
D
Dong Li 已提交
362

L
Lei Wang 已提交
363
  # modules data, conf and dag
364 365
  CONFS=$(find modules/ cyber/ -name "conf")
  DATAS=$(find modules/ -name "data" | grep -v "testdata")
L
Lei Wang 已提交
366 367
  DAGS=$(find modules/ -name "dag")
  LAUNCHS=$(find modules/ -name "launch")
368
  PARAMS=$(find modules/ -name "params" | grep -v "testdata")
L
Lei Wang 已提交
369

370
  rm -rf test/*
L
Lei Wang 已提交
371 372
  for CONF in $CONFS; do
    cp -P --parent -a "${CONF}" "${APOLLO_RELEASE_DIR}"
373
  done
L
Lei Wang 已提交
374
  for DATA in $DATAS; do
375
    if [[ $DATA != *"map"* ]]; then
L
Lei Wang 已提交
376
        cp -P --parent -a "${DATA}" "${APOLLO_RELEASE_DIR}"
377 378
    fi
  done
L
Lei Wang 已提交
379 380 381 382 383
  for DAG in $DAGS; do
    cp -P --parent -a "${DAG}" "${APOLLO_RELEASE_DIR}"
  done
  for LAUNCH in $LAUNCHS; do
    cp -P --parent -a "${LAUNCH}" "${APOLLO_RELEASE_DIR}"
384
  done
385 386 387
  for PARAM in $PARAMS; do
    cp -P --parent -a "${PARAM}" "${APOLLO_RELEASE_DIR}"
  done
388
  # perception model
L
Lei Wang 已提交
389 390
  MODEL="modules/perception/model"
  cp -P --parent -a "${MODEL}" "${APOLLO_RELEASE_DIR}"
391 392 393

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

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

S
siyangy 已提交
398
  # scripts
D
Dong Li 已提交
399
  cp -r scripts ${APOLLO_RELEASE_DIR}
S
siyangy 已提交
400

401 402 403 404
  # manual traffic light tool
  mkdir -p ${APOLLO_RELEASE_DIR}/modules/tools
  cp -r modules/tools/manual_traffic_light ${APOLLO_RELEASE_DIR}/modules/tools

405 406 407
  # remove mounted models
  rm -rf ${APOLLO_RELEASE_DIR}/modules/perception/model/yolo_camera_detector/

D
Dong Li 已提交
408
  # lib
D
Dong Li 已提交
409
  LIB_DIR="${APOLLO_RELEASE_DIR}/lib"
410
  mkdir "${LIB_DIR}"
D
Dong Li 已提交
411
  if $USE_ESD_CAN; then
L
Lei Wang 已提交
412
    warn_proprietary_sw
D
Dong Li 已提交
413
  fi
414
  THIRDLIBS=$(find third_party/* -name "*.so*")
L
Lei Wang 已提交
415 416 417
  for LIB in ${THIRDLIBS}; do
    cp -a "${LIB}" $LIB_DIR
  done
L
Lei Wang 已提交
418
  cp -r py_proto/modules $LIB_DIR
419

D
Dong Li 已提交
420
  # doc
D
Dong Li 已提交
421 422
  cp LICENSE "${APOLLO_RELEASE_DIR}"
  cp third_party/ACKNOWLEDGEMENT.txt "${APOLLO_RELEASE_DIR}"
L
Lei Wang 已提交
423

S
siyangy 已提交
424
  # release info
D
Dong Li 已提交
425
  META="${APOLLO_RELEASE_DIR}/meta.ini"
426
  echo "git_commit: $(get_revision)" >> $META
427
  echo "git_branch: $(get_branch)" >> $META
428 429
  echo "car_type: LINCOLN.MKZ" >> $META
  echo "arch: ${MACHINE_ARCH}" >> $META
D
Dong Li 已提交
430 431 432 433
}

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

D
Dong Li 已提交
440 441
  COV_DIR=data/cov
  rm -rf $COV_DIR
442
  files=$(find bazel-out/local-dbg/bin/ -iname "*.gcda" -o -iname "*.gcno" | grep -v external | grep -v third_party)
D
Dong Li 已提交
443
  for f in $files; do
444 445 446 447 448
    if [ "$f" != "${f##*cyber}" ]; then
      target="$COV_DIR/objs/cyber${f##*cyber}"
    else
      target="$COV_DIR/objs/modules${f##*modules}"
    fi
449 450 451 452
    mkdir -p "$(dirname "$target")"
    cp "$f" "$target"
  done

453
  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 已提交
454 455 456
  if [ $? -ne 0 ]; then
    fail 'lcov failed!'
  fi
J
Jiangtao Hu 已提交
457
  lcov --rc lcov_branch_coverage=1 --remove "$COV_DIR/conv.info" \
D
Dong Li 已提交
458 459 460 461 462 463 464 465 466 467 468
      "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() {
469
  JOB_ARG="--jobs=$(nproc) --ram_utilization_factor 80"
470

471
  generate_build_targets
472
  if [ "$USE_GPU" == "1" ]; then
473
    echo -e "${YELLOW}Running tests under GPU mode. GPU is required to run the tests.${NO_COLOR}"
474
    bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@ $BUILD_TARGETS
475
  else
476
    echo -e "${YELLOW}Running tests under CPU mode. No GPU is required to run the tests.${NO_COLOR}"
477
    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"`"
478
    bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@ $BUILD_TARGETS
479
  fi
480 481 482 483 484
  if [ $? -ne 0 ]; then
    fail 'Test failed!'
    return 1
  fi

S
siyangy 已提交
485
  if [ $? -eq 0 ]; then
D
Dong Li 已提交
486 487 488 489 490
    success 'Test passed!'
    return 0
  fi
}

L
Liangliang Zhang 已提交
491
function citest_basic() {
L
Liangliang Zhang 已提交
492 493
  set -e

494 495
  info "Building framework ..."
  cd /apollo
G
GoLancer 已提交
496
  source cyber/setup.bash
497

J
Jiangtao Hu 已提交
498 499 500 501
  # FIXME(all): temporarily disable modules doesn't compile in 18.04
#   BUILD_TARGETS="
#    `bazel query //modules/... union //cyber/...`
#  "
502
  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/... `
503

504
  JOB_ARG="--jobs=12 --ram_utilization_factor 80"
L
Liangliang Zhang 已提交
505

506
  BUILD_TARGETS="`echo "$BUILD_TARGETS" | grep "modules\/" | grep "test" \
507 508 509 510
          | grep -v "modules\/planning" \
          | grep -v "modules\/prediction" \
          | grep -v "modules\/control" \
          | grep -v "modules\/common" \
511
          | grep -v "can_client" \
512
          | grep -v "blob_test" \
513
          | grep -v "pyramid_map" \
514
          | grep -v "ndt_lidar_locator_test" \
515 516 517
          | grep -v "syncedmem_test" | grep -v "blob_test" \
          | grep -v "perception_inference_operators_test" \
          | grep -v "cuda_util_test" \
518 519
          | grep -v "modules\/perception"`"

520
  bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@ $BUILD_TARGETS
521

522 523 524 525 526 527 528 529 530
  if [ $? -eq 0 ]; then
    success 'Test passed!'
    return 0
  else
    fail 'Test failed!'
    return 1
  fi
}

531 532 533 534 535
function citest_extended() {
  set -e

  info "Building framework ..."
  cd /apollo
G
GoLancer 已提交
536
  source cyber/setup.bash
537 538

  BUILD_TARGETS="
F
Fla3inH0tCheet0s 已提交
539
    `bazel query //modules/planning/... union //modules/common/... union //cyber/... $DISABLED_CYBER_MODULES`
540
    `bazel query //modules/prediction/... union //modules/control/...`
541 542
  "

543
  JOB_ARG="--jobs=12 --ram_utilization_factor 80"
544

545
  BUILD_TARGETS="`echo "$BUILD_TARGETS" | grep "test"`"
546

547
  bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@ $BUILD_TARGETS
548 549 550 551 552 553 554 555 556 557

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

L
Liangliang Zhang 已提交
558
function citest() {
559 560
  info "Building framework ..."
  cd /apollo
G
GoLancer 已提交
561
  source cyber/setup.bash
562

L
Liangliang Zhang 已提交
563
  citest_basic
564
  citest_extended
L
Liangliang Zhang 已提交
565 566 567 568 569 570 571 572 573
  if [ $? -eq 0 ]; then
    success 'Test passed!'
    return 0
  else
    fail 'Test failed!'
    return 1
  fi
}

D
Dong Li 已提交
574
function run_cpp_lint() {
575
  BUILD_TARGETS="`bazel query //modules/... except //modules/tools/visualizer/... union //cyber/...`"
576
  bazel test --config=cpplint -c dbg $BUILD_TARGETS
D
Dong Li 已提交
577 578 579
}

function run_bash_lint() {
S
siyangy 已提交
580
  FILES=$(find "${APOLLO_ROOT_DIR}" -type f -name "*.sh" | grep -v ros)
D
Dong Li 已提交
581 582 583 584
  echo "${FILES}" | xargs shellcheck
}

function run_lint() {
C
Calvin Miao 已提交
585
  # Add cpplint rule to BUILD files that do not contain it.
586
  for file in $(find cyber modules -name BUILD | \
H
hugomatic 已提交
587
    grep -v gnss/third_party | grep -v modules/teleop/encoder/nvenc_sdk6 | \
C
Calvin Miao 已提交
588 589 590 591 592 593
    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 已提交
594 595
  run_cpp_lint

D
Dong Li 已提交
596 597 598 599 600 601 602 603
  if [ $? -eq 0 ]; then
    success 'Lint passed!'
  else
    fail 'Lint failed!'
  fi
}

function clean() {
A
Aaron Xiao 已提交
604
  # Remove bazel cache.
605
  bazel clean --async
606 607 608 609 610 611

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

A
Aaron Xiao 已提交
612 613
  # Remove cmake cache.
  rm -fr framework/build
D
Dong Li 已提交
614 615 616
}

function buildify() {
617 618 619
  local buildifier_url=https://github.com/bazelbuild/buildtools/releases/download/0.4.5/buildifier
  wget $buildifier_url -O ~/.buildifier
  chmod +x ~/.buildifier
620
  find . -name '*BUILD' -type f -exec ~/.buildifier -showlog -mode=fix {} +
621 622 623 624 625 626
  if [ $? -eq 0 ]; then
    success 'Buildify worked!'
  else
    fail 'Buildify failed!'
  fi
  rm ~/.buildifier
D
Dong Li 已提交
627 628
}

S
siyangy 已提交
629 630 631 632 633
function build_fe() {
  cd modules/dreamview/frontend
  yarn build
}

D
Dong Li 已提交
634 635 636 637 638 639
function gen_doc() {
  rm -rf docs/doxygen
  doxygen apollo.doxygen
}

function version() {
640 641 642 643 644
  rev=$(get_revision)
  if [ "$rev" = "unknown" ];then
    echo "Version: $rev"
    return
  fi
D
Dong Li 已提交
645 646 647 648 649 650
  commit=$(git log -1 --pretty=%H)
  date=$(git log -1 --pretty=%cd)
  echo "Commit: ${commit}"
  echo "Date: ${date}"
}

651 652 653 654 655 656 657 658 659 660 661
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"
}

662 663 664 665 666 667 668 669 670 671 672
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"
}

673
function config() {
674
  ${APOLLO_ROOT_DIR}/scripts/configurator.sh
675 676
}

S
siyangy 已提交
677
function set_use_gpu() {
S
siyangy 已提交
678 679 680 681 682
  if [ "${USE_GPU}" = "1" ] ; then
    DEFINES="${DEFINES} --define USE_GPU=true"
  else
    DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
  fi
S
siyangy 已提交
683 684
}

685
function print_usage() {
C
Calvin Miao 已提交
686 687 688 689 690 691 692 693 694 695
  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
696
  ${BLUE}build_opt${NONE}: build optimized binary for the code
L
luoqi06 已提交
697
  ${BLUE}build_cpu${NONE}: dbg build with CPU
J
Jun Zhu 已提交
698 699
  ${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 已提交
700
  ${BLUE}build_fe${NONE}: compile frontend javascript code, this requires all the node_modules to be installed already
701 702
  ${BLUE}build_cyber [dbg|opt]${NONE}: build Cyber RT only
  ${BLUE}build_drivers [dbg|opt]${NONE}: build drivers only
703 704 705 706
  ${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 已提交
707
  ${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.
708
  ${BLUE}build_prof${NONE}: build for gprof support.
C
Calvin Miao 已提交
709 710 711 712 713 714 715 716 717 718 719 720
  ${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
  "
721 722
}

S
siyangy 已提交
723 724
function main() {
  source_apollo_base
725

S
siyangy 已提交
726
  check_machine_arch
727
  apollo_check_system_config
S
siyangy 已提交
728 729
  check_esd_files

730
  DEFINES="--define ARCH=${MACHINE_ARCH} --define CAN_CARD=${CAN_CARD} --cxxopt=-DUSE_ESD_CAN=${USE_ESD_CAN}"
F
Fla3inH0tCheet0s 已提交
731
  # Enable bazel's feature to compute md5 checksums in parallel
732
  DEFINES="${DEFINES} --experimental_multi_threaded_digest"
733 734 735 736

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

738 739 740
  local cmd=$1
  shift

S
siyangy 已提交
741
  START_TIME=$(get_now)
742
  case $cmd in
S
siyangy 已提交
743
    check)
744
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
745
      check $@
S
siyangy 已提交
746 747
      ;;
    build)
S
siyangy 已提交
748
      set_use_gpu
749
      apollo_build_dbg $@
750 751
      ;;
    build_cpu)
L
Lei Wang 已提交
752
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
753
      apollo_build_dbg $@
S
siyangy 已提交
754
      ;;
755
    build_prof)
D
Dong Li 已提交
756
      DEFINES="${DEFINES} --config=cpu_prof --cxxopt=-DCPU_ONLY"
757 758
      apollo_build_dbg $@
      ;;
759
    build_no_perception)
S
siyangy 已提交
760
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
761
      BUILD_FILTER="no_perception"
762 763 764
      if [ "$1" == "opt" ]; then
        shift
        apollo_build_opt $@
765
      else
766 767 768
        shift
        apollo_build_dbg $@
      fi
769
      ;;
770 771 772 773
    clean_cyber)
      export LD_PRELOAD=
      clean
      ;;
774
    build_cyber)
775
      export LD_PRELOAD=
776 777 778 779 780 781 782 783 784 785
      BUILD_FILTER="cyber"
      if [ "$1" == "opt" ]; then
        shift
        apollo_build_opt $@
      else
        shift
        apollo_build_dbg $@
      fi
      ;;
    build_drivers)
786
      export LD_PRELOAD=
787 788 789 790 791 792 793 794 795
      BUILD_FILTER="drivers"
      if [ "$1" == "opt" ]; then
        shift
        apollo_build_opt $@
      else
        shift
        apollo_build_dbg $@
      fi
      ;;
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
    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 $@
      ;;
812 813 814 815
    cibuild)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      cibuild $@
      ;;
816 817 818 819
    cibuild_extended)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      cibuild_extended $@
      ;;
820
    build_opt)
821
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY --copt=-fpic"
822
      apollo_build_opt $@
S
siyangy 已提交
823
      ;;
824
    build_gpu)
S
siyangy 已提交
825
      set_use_gpu
826
      apollo_build_dbg $@
J
Jun Zhu 已提交
827
      ;;
J
Jun Zhu 已提交
828
    build_opt_gpu)
S
siyangy 已提交
829 830
      set_use_gpu
      DEFINES="${DEFINES} --copt=-fpic"
831
      apollo_build_opt $@
Y
Yu Cao 已提交
832 833 834 835 836
      ;;
    build_teleop)
      set_use_gpu
      DEFINES="${DEFINES} --copt=-fpic --define WITH_TELEOP=1 --cxxopt=-DTELEOP"
      apollo_build_opt $@
S
siyangy 已提交
837
      ;;
S
siyangy 已提交
838 839 840
    build_fe)
      build_fe
      ;;
S
siyangy 已提交
841 842 843
    buildify)
      buildify
      ;;
L
Lei Wang 已提交
844 845 846
    build_py)
      build_py_proto
      ;;
847 848 849
    config)
      config
      ;;
S
siyangy 已提交
850 851 852 853 854 855 856
    doc)
      gen_doc
      ;;
    lint)
      run_lint
      ;;
    test)
S
siyangy 已提交
857
      set_use_gpu
858 859 860
      run_test $@
      ;;
    test_cpu)
861
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
L
Lei Wang 已提交
862
      run_test $@
S
siyangy 已提交
863
      ;;
L
Liangliang Zhang 已提交
864 865 866 867
    citest)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest $@
      ;;
868 869 870 871 872 873 874 875
    citest_basic)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest_basic $@
      ;;
    citest_extended)
      DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
      citest_extended $@
      ;;
876
    test_gpu)
S
siyangy 已提交
877
      set_use_gpu
L
Lei Wang 已提交
878
      run_test $@
879
      ;;
S
siyangy 已提交
880 881 882 883 884 885 886
    release)
      release 1
      ;;
    release_noproprietary)
      release 0
      ;;
    coverage)
L
Lei Wang 已提交
887
      gen_coverage $@
S
siyangy 已提交
888 889 890 891 892 893 894
      ;;
    clean)
      clean
      ;;
    version)
      version
      ;;
C
Calvin Miao 已提交
895 896 897
    usage)
      print_usage
      ;;
S
siyangy 已提交
898 899 900 901 902 903
    *)
      print_usage
      ;;
  esac
}

904
main $@