apollo.sh 25.9 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)
D
Dong Li 已提交
66

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

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

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

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

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

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

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

S
siyangy 已提交
142
function build() {
143
  if [ "${USE_GPU}" = "1" ] ; then
144 145 146 147
    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
148
  info "Start building, please wait ..."
149

D
Dong Li 已提交
150
  generate_build_targets
151 152
  info "Building on $MACHINE_ARCH..."

153
  MACHINE_ARCH=$(uname -m)
154
  JOB_ARG="--jobs=$(nproc) --ram_utilization_factor 80"
155 156 157
  if [ "$MACHINE_ARCH" == 'aarch64' ]; then
    JOB_ARG="--jobs=3"
  fi
158
  info "Building with $JOB_ARG for $MACHINE_ARCH"
159

X
 
Xiangquan Xiao 已提交
160
  bazel build $JOB_ARG $DEFINES -c $@ $BUILD_TARGETS
A
Aaron Xiao 已提交
161
  if [ ${PIPESTATUS[0]} -ne 0 ]; then
D
Dong Li 已提交
162 163
    fail 'Build failed!'
  fi
S
siyangy 已提交
164 165

  # Build python proto
L
Lei Wang 已提交
166
  build_py_proto
167

168
  # Clear KV DB and update commit_id after compiling.
169 170 171 172 173 174 175 176 177 178 179 180
  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
181

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

195
function cibuild_extended() {
196 197 198 199
  info "Building framework ..."

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

201
  JOB_ARG="--jobs=12"
202 203 204 205
  if [ "$MACHINE_ARCH" == 'aarch64' ]; then
    JOB_ARG="--jobs=3"
  fi

A
Aaron Xiao 已提交
206
  info "Building with $JOB_ARG for $MACHINE_ARCH"
J
Jiangtao Hu 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

  # 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/...
    `
225 226 227 228 229 230 231 232 233 234 235 236

  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 已提交
237

238 239
  info "Building modules ..."

240
  JOB_ARG="--jobs=12"
241 242 243 244 245 246 247 248 249
  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/...
250 251
    //modules/planning/...
    //modules/prediction/...
252
    //modules/routing/...
253
    //modules/transform/..."
254

255 256 257 258 259
  # 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

260
  bazel build $JOB_ARG $DEFINES $@ $BUILD_TARGETS
261

262 263 264 265 266 267 268
  if [ $? -eq 0 ]; then
    success 'Build passed!'
  else
    fail 'Build failed!'
  fi
}

269
function apollo_build_dbg() {
270
  build "dbg" $@
S
siyangy 已提交
271 272
}

273
function apollo_build_opt() {
274
  build "opt" $@
S
siyangy 已提交
275 276
}

L
Lei Wang 已提交
277 278 279 280 281
function build_py_proto() {
  if [ -d "./py_proto" ];then
    rm -rf py_proto
  fi
  mkdir py_proto
282
  find modules/ cyber/ -name "*.proto" \
S
siyangy 已提交
283
      | grep -v node_modules \
284
      | xargs protoc --python_out=py_proto
V
vlin17 已提交
285 286 287
  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 已提交
288 289 290
  find py_proto/* -type d -exec touch "{}/__init__.py" \;
}

D
Dong Li 已提交
291
function check() {
D
Dong Li 已提交
292
  bash $0 build && bash $0 "test" && bash $0 lint
D
Dong Li 已提交
293 294 295 296 297 298 299 300 301 302 303

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

function warn_proprietary_sw() {
304 305 306 307 308 309
  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 已提交
310 311 312
}

function release() {
313 314
  RELEASE_DIR="${HOME}/.cache/apollo_release"
  if [ -d "${RELEASE_DIR}" ]; then
315
    rm -rf "${RELEASE_DIR}"
316
  fi
D
Dong Li 已提交
317 318
  APOLLO_RELEASE_DIR="${RELEASE_DIR}/apollo"
  mkdir -p "${APOLLO_RELEASE_DIR}"
319

L
Lei Wang 已提交
320 321 322 323 324 325
  # 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
326
  mv ${APOLLO_RELEASE_DIR}/bazel-out/local-opt/bin/* ${APOLLO_RELEASE_DIR}/bazel-bin/
L
Lei Wang 已提交
327 328 329 330 331 332
  rm -rf ${APOLLO_RELEASE_DIR}/bazel-out

  # reset softlinks
  cd ${APOLLO_RELEASE_DIR}/bazel-bin
  LIST=("_solib_k8")
  for DIR in "${LIST[@]}"; do
333
    LINKS=$(find ${DIR}/* -name "*.so" -type l | sed '/.*@.*/d')
L
Lei Wang 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    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 已提交
349
  CYBERBIN=$(bazel query "kind(cc_binary, //cyber/... //modules/tools/visualizer/...)" | sed 's/^\/\///' | sed 's/:/\//' | sed '/.*.so$/d')
L
Lei Wang 已提交
350 351 352 353 354 355
  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}"
356
  cp --parent -a "cyber/python" "${APOLLO_RELEASE_DIR}"
L
Lei Wang 已提交
357 358 359 360 361 362 363 364 365 366


  # 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"
367
  done
D
Dong Li 已提交
368

L
Lei Wang 已提交
369
  # modules data, conf and dag
370 371
  CONFS=$(find modules/ cyber/ -name "conf")
  DATAS=$(find modules/ -name "data" | grep -v "testdata")
L
Lei Wang 已提交
372 373
  DAGS=$(find modules/ -name "dag")
  LAUNCHS=$(find modules/ -name "launch")
374
  PARAMS=$(find modules/ -name "params" | grep -v "testdata")
L
Lei Wang 已提交
375

376
  rm -rf test/*
L
Lei Wang 已提交
377 378
  for CONF in $CONFS; do
    cp -P --parent -a "${CONF}" "${APOLLO_RELEASE_DIR}"
379
  done
L
Lei Wang 已提交
380
  for DATA in $DATAS; do
381
    if [[ $DATA != *"map"* ]]; then
L
Lei Wang 已提交
382
        cp -P --parent -a "${DATA}" "${APOLLO_RELEASE_DIR}"
383 384
    fi
  done
L
Lei Wang 已提交
385 386 387 388 389
  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}"
390
  done
391 392 393
  for PARAM in $PARAMS; do
    cp -P --parent -a "${PARAM}" "${APOLLO_RELEASE_DIR}"
  done
394
  # perception model
L
Lei Wang 已提交
395 396
  MODEL="modules/perception/model"
  cp -P --parent -a "${MODEL}" "${APOLLO_RELEASE_DIR}"
397 398 399

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

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

S
siyangy 已提交
404
  # scripts
D
Dong Li 已提交
405
  cp -r scripts ${APOLLO_RELEASE_DIR}
S
siyangy 已提交
406

407 408 409 410
  # manual traffic light tool
  mkdir -p ${APOLLO_RELEASE_DIR}/modules/tools
  cp -r modules/tools/manual_traffic_light ${APOLLO_RELEASE_DIR}/modules/tools

411 412 413
  # remove mounted models
  rm -rf ${APOLLO_RELEASE_DIR}/modules/perception/model/yolo_camera_detector/

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

D
Dong Li 已提交
426
  # doc
D
Dong Li 已提交
427 428
  cp LICENSE "${APOLLO_RELEASE_DIR}"
  cp third_party/ACKNOWLEDGEMENT.txt "${APOLLO_RELEASE_DIR}"
L
Lei Wang 已提交
429

S
siyangy 已提交
430
  # release info
D
Dong Li 已提交
431
  META="${APOLLO_RELEASE_DIR}/meta.ini"
432
  echo "git_commit: $(get_revision)" >> $META
433
  echo "git_branch: $(get_branch)" >> $META
434 435
  echo "car_type: LINCOLN.MKZ" >> $META
  echo "arch: ${MACHINE_ARCH}" >> $META
D
Dong Li 已提交
436 437 438 439
}

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

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

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

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

S
siyangy 已提交
491
  if [ $? -eq 0 ]; then
D
Dong Li 已提交
492 493 494 495 496
    success 'Test passed!'
    return 0
  fi
}

L
Liangliang Zhang 已提交
497
function citest_basic() {
L
Liangliang Zhang 已提交
498 499
  set -e

500 501
  info "Building framework ..."
  cd /apollo
G
GoLancer 已提交
502
  source cyber/setup.bash
503

J
Jiangtao Hu 已提交
504 505 506 507
  # FIXME(all): temporarily disable modules doesn't compile in 18.04
#   BUILD_TARGETS="
#    `bazel query //modules/... union //cyber/...`
#  "
508
  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/... `
509

510
  JOB_ARG="--jobs=12 --ram_utilization_factor 80"
L
Liangliang Zhang 已提交
511

512
  BUILD_TARGETS="`echo "$BUILD_TARGETS" | grep "modules\/" | grep "test" \
513 514 515 516
          | grep -v "modules\/planning" \
          | grep -v "modules\/prediction" \
          | grep -v "modules\/control" \
          | grep -v "modules\/common" \
517
          | grep -v "can_client" \
518
          | grep -v "blob_test" \
519
          | grep -v "pyramid_map" \
520
          | grep -v "ndt_lidar_locator_test" \
521 522 523
          | grep -v "syncedmem_test" | grep -v "blob_test" \
          | grep -v "perception_inference_operators_test" \
          | grep -v "cuda_util_test" \
524 525
          | grep -v "modules\/perception"`"

526
  bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@ $BUILD_TARGETS
527

528 529 530 531 532 533 534 535 536
  if [ $? -eq 0 ]; then
    success 'Test passed!'
    return 0
  else
    fail 'Test failed!'
    return 1
  fi
}

537 538 539 540 541
function citest_extended() {
  set -e

  info "Building framework ..."
  cd /apollo
G
GoLancer 已提交
542
  source cyber/setup.bash
543 544

  BUILD_TARGETS="
F
Fla3inH0tCheet0s 已提交
545
    `bazel query //modules/planning/... union //modules/common/... union //cyber/... $DISABLED_CYBER_MODULES`
546
    `bazel query //modules/prediction/... union //modules/control/...`
547 548
  "

549
  JOB_ARG="--jobs=12 --ram_utilization_factor 80"
550

551
  BUILD_TARGETS="`echo "$BUILD_TARGETS" | grep "test"`"
552

553
  bazel test $DEFINES $JOB_ARG --config=unit_test -c dbg --test_verbose_timeout_warnings $@ $BUILD_TARGETS
554 555 556 557 558 559 560 561 562 563

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

L
Liangliang Zhang 已提交
564
function citest() {
565 566
  info "Building framework ..."
  cd /apollo
G
GoLancer 已提交
567
  source cyber/setup.bash
568

L
Liangliang Zhang 已提交
569
  citest_basic
570
  citest_extended
L
Liangliang Zhang 已提交
571 572 573 574 575 576 577 578 579
  if [ $? -eq 0 ]; then
    success 'Test passed!'
    return 0
  else
    fail 'Test failed!'
    return 1
  fi
}

D
Dong Li 已提交
580
function run_cpp_lint() {
581
  BUILD_TARGETS="`bazel query //modules/... except //modules/tools/visualizer/... union //cyber/...`"
582
  bazel test --config=cpplint -c dbg $BUILD_TARGETS
D
Dong Li 已提交
583 584 585
}

function run_bash_lint() {
S
siyangy 已提交
586
  FILES=$(find "${APOLLO_ROOT_DIR}" -type f -name "*.sh" | grep -v ros)
D
Dong Li 已提交
587 588 589 590
  echo "${FILES}" | xargs shellcheck
}

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

D
Dong Li 已提交
602 603 604 605 606 607 608 609
  if [ $? -eq 0 ]; then
    success 'Lint passed!'
  else
    fail 'Lint failed!'
  fi
}

function clean() {
A
Aaron Xiao 已提交
610
  # Remove bazel cache.
611
  bazel clean --async
612 613 614 615 616 617

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

A
Aaron Xiao 已提交
618 619
  # Remove cmake cache.
  rm -fr framework/build
D
Dong Li 已提交
620 621 622
}

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

S
siyangy 已提交
635 636 637 638 639
function build_fe() {
  cd modules/dreamview/frontend
  yarn build
}

D
Dong Li 已提交
640 641 642 643 644 645
function gen_doc() {
  rm -rf docs/doxygen
  doxygen apollo.doxygen
}

function version() {
646 647 648 649 650
  rev=$(get_revision)
  if [ "$rev" = "unknown" ];then
    echo "Version: $rev"
    return
  fi
D
Dong Li 已提交
651 652 653 654 655 656
  commit=$(git log -1 --pretty=%H)
  date=$(git log -1 --pretty=%cd)
  echo "Commit: ${commit}"
  echo "Date: ${date}"
}

657 658 659 660 661 662 663 664 665 666 667
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"
}

668 669 670 671 672 673 674 675 676 677 678
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"
}

679
function config() {
680
  ${APOLLO_ROOT_DIR}/scripts/configurator.sh
681 682
}

S
siyangy 已提交
683
function set_use_gpu() {
S
siyangy 已提交
684 685 686 687 688
  if [ "${USE_GPU}" = "1" ] ; then
    DEFINES="${DEFINES} --define USE_GPU=true"
  else
    DEFINES="${DEFINES} --cxxopt=-DCPU_ONLY"
  fi
S
siyangy 已提交
689 690
}

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

S
siyangy 已提交
729 730
function main() {
  source_apollo_base
731

S
siyangy 已提交
732
  check_machine_arch
733
  apollo_check_system_config
S
siyangy 已提交
734 735
  check_esd_files

736
  DEFINES="--define ARCH=${MACHINE_ARCH} --define CAN_CARD=${CAN_CARD} --cxxopt=-DUSE_ESD_CAN=${USE_ESD_CAN}"
F
Fla3inH0tCheet0s 已提交
737
  # Enable bazel's feature to compute md5 checksums in parallel
738
  DEFINES="${DEFINES} --experimental_multi_threaded_digest"
739 740 741 742

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

744 745 746
  local cmd=$1
  shift

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

910
main $@