apollo.sh 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#! /usr/bin/env bash
set -e

TOP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
source "${TOP_DIR}/scripts/apollo.bashrc"

ARCH="$(uname -m)"
SUPPORTED_ARCHS=" x86_64 aarch64 "
APOLLO_VERSION="@non-git"
APOLLO_ENV=""

USE_ESD_CAN=false
: ${STAGE:=dev}

function check_architecture_support() {
    if [[ "${SUPPORTED_ARCHS}" != *" ${ARCH} "* ]]; then
        error "Unsupported CPU arch: ${ARCH}. Currently, Apollo only" \
              "supports running on the following CPU archs:"
        error "${TAB}${SUPPORTED_ARCHS}"
        exit 1
S
siyangy 已提交
21
    fi
D
Dong Li 已提交
22 23
}

24 25 26 27 28 29
function check_platform_support() {
    local platform="$(uname -s)"
    if [ "$platform" != "Linux" ]; then
        error "Unsupported platform: ${platform}."
        error "${TAB}Apollo is expected to run on Linux systems (E.g., Debian/Ubuntu)."
        exit 1
30
    fi
D
Dong Li 已提交
31 32
}

33 34 35 36 37 38 39
function check_minimal_memory_requirement() {
    local minimal_mem_gb="2.0"
    local actual_mem_gb="$(free -m | awk '/Mem:/ {printf("%0.2f", $2 / 1024.0)}')"
    if (( $(echo "$actual_mem_gb < $minimal_mem_gb" | bc -l) )); then
        warning "System memory [${actual_mem_gb}G] is lower than the minimum required" \
                "[${minimal_mem_gb}G]. Apollo build could fail."
    fi
D
Dong Li 已提交
40 41
}

42 43 44 45 46 47 48 49
function determine_esdcan_use() {
    local esdcan_dir="${APOLLO_ROOT_DIR}/third_party/can_card_library/esd_can"
    local use_esd=false
    if [ -f "${esdcan_dir}/include/ntcan.h" ] && \
       [ -f "${esdcan_dir}/lib/libntcan.so.4" ]; then
        use_esd=true
    fi
    USE_ESD_CAN="${use_esd}"
D
Dong Li 已提交
50 51
}

52 53 54 55 56 57 58 59
function check_apollo_version() {
    local branch="$(git_branch)"
    if [ "${branch}" == "${APOLLO_VERSION}" ]; then
        return
    fi
    local sha1="$(git_sha1)"
    local stamp="$(git_date)"
    APOLLO_VERSION="${branch}-${stamp}-${sha1}"
S
siyangy 已提交
60 61
}

62 63
function apollo_env_setup() {
    check_apollo_version
D
Dong Li 已提交
64

65 66 67 68 69
    check_architecture_support
    check_platform_support
    check_minimal_memory_requirement
    # determine_gpu_use # work done by scripts/apollo.bashrc
    determine_esdcan_use
D
Dong Li 已提交
70

71 72 73
    APOLLO_ENV="${APOLLO_ENV} STAGE=${STAGE}"
    APOLLO_ENV="${APOLLO_ENV} USE_ESD_CAN=${USE_ESD_CAN}"
    # Add more here ...
74

75 76 77 78 79
    info "Apollo Environment Settings:"
    info "${TAB}APOLLO_ROOT_DIR: ${APOLLO_ROOT_DIR}"
    info "${TAB}APOLLO_CACHE_DIR: ${APOLLO_CACHE_DIR}"
    info "${TAB}APOLLO_IN_DOCKER: ${APOLLO_IN_DOCKER}"
    info "${TAB}APOLLO_VERSION: ${APOLLO_VERSION}"
80 81 82
    if "${APOLLO_IN_DOCKER}" ; then
        info "${TAB}DOCKER_IMG: ${DOCKER_IMG##*:}"
    fi
83
    info "${TAB}APOLLO_ENV: ${APOLLO_ENV} USE_GPU=${USE_GPU}"
84 85

    if [ ! -f "${APOLLO_ROOT_DIR}/.apollo.bazelrc" ]; then
86
        env ${APOLLO_ENV} bash "${APOLLO_ROOT_DIR}/scripts/apollo_config.sh" --noninteractive
87
    fi
88 89
}

90 91 92 93 94
#TODO(all): Update node modules
function build_dreamview_frontend() {
    pushd "${APOLLO_ROOT_DIR}/modules/dreamview/frontend" >/dev/null
        yarn build
    popd >/dev/null
S
siyangy 已提交
95 96
}

97 98 99 100 101
function build_test_and_lint() {
    env ${APOLLO_ENV} bash "${build_sh}" --config=cpu
    env ${APOLLO_ENV} bash "${test_sh}" --config=unit_test --config=cpu
    env ${APOLLO_ENV} bash "${APOLLO_ROOT_DIR}/scripts/apollo_lint.sh" cpp
    success "Build and Test and Lint finished."
102 103
}

104
function _usage() {
L
liuxu 已提交
105 106 107 108
  echo -e "\n${RED}Usage${NO_COLOR}:
  .${BOLD}/apollo.sh${NO_COLOR} [OPTION]"
  echo -e "\n${RED}Options${NO_COLOR}:
  ${BLUE}config [options]${NO_COLOR}: config bazel build environment either non-interactively (default) or interactively.
S
storypku 已提交
109
  ${BLUE}build [module]${NO_COLOR}: run build for cyber (<module> = cyber) or modules/<module>.  If <module> unspecified, build all.
L
liuxu 已提交
110 111 112 113 114
  ${BLUE}build_dbg [module]${NO_COLOR}: run debug build (default).
  ${BLUE}build_opt [module]${NO_COLOR}: run optimized build.
  ${BLUE}build_cpu [module]${NO_COLOR}: build in CPU mode. Equivalent to 'bazel build --config=cpu'
  ${BLUE}build_gpu [module]${NO_COLOR}: run build in GPU mode. Equivalent to 'bazel build --config=gpu'
  ${BLUE}build_opt_gpu [module]${NO_COLOR}: optimized build in GPU mode. Equivalent to 'bazel build --config=opt --config=gpu'
S
storypku 已提交
115 116
  ${BLUE}test [module]${NO_COLOR}: run unit tests for cyber (<module> = cyber) or modules/<module>. If <module> unspecified, unit test all.
  ${BLUE}coverage [module]${NO_COLOR}: run bazel coverage test for cyber (<module> = cyber) or modules/<module>. If <module> unspecified, coverage test all.
L
liuxu 已提交
117 118 119 120 121 122 123
  ${BLUE}lint${NO_COLOR}: run code style check
  ${BLUE}buildify${NO_COLOR}: run 'buildifier' to fix style of bazel files
  ${BLUE}check${NO_COLOR}: run build, test and lint on all modules. Make sure check pass before checking in new code
  ${BLUE}build_fe${NO_COLOR}: compile dreamland frontend javascript. Requires all the node_modules installed already
  ${BLUE}build_teleop${NO_COLOR}: run build with teleop enabled.
  ${BLUE}build_prof [module]${NO_COLOR}: build with perf profiling support. Not implemented yet.
  ${BLUE}doc${NO_COLOR}: generate doxygen document
S
storypku 已提交
124
  ${BLUE}clean${NO_COLOR}: cleanup bazel output and log/coredump files
125
  ${BLUE}format${NO_COLOR}: format C++/Python/Bazel/Shell files
L
liuxu 已提交
126 127
  ${BLUE}usage${NO_COLOR}: show this message
  "
S
storypku 已提交
128 129
}

S
siyangy 已提交
130
function main() {
131 132
    apollo_env_setup
    if [ "$#" -eq 0 ]; then
133
        _usage
134 135 136 137
        exit 0
    fi
    local build_sh="${APOLLO_ROOT_DIR}/scripts/apollo_build.sh"
    local test_sh="${APOLLO_ROOT_DIR}/scripts/apollo_test.sh"
138
    local coverage_sh="${APOLLO_ROOT_DIR}/scripts/apollo_coverage.sh"
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    local ci_sh="${APOLLO_ROOT_DIR}/scripts/apollo_ci.sh"
    local cmd="$1"; shift
    case "${cmd}" in
        config)
            env ${APOLLO_ENV} bash "${APOLLO_ROOT_DIR}/scripts/apollo_config.sh" "$@"
            ;;
        build)
            env ${APOLLO_ENV} bash "${build_sh}" "$@"
            ;;
        build_opt)
            env ${APOLLO_ENV} bash "${build_sh}" --config=opt "$@"
            ;;
        build_dbg)
            env ${APOLLO_ENV} bash "${build_sh}" --config=dbg "$@"
            ;;
        build_cpu)
            env ${APOLLO_ENV} bash "${build_sh}" --config=cpu "$@"
            ;;
        build_gpu)
            env ${APOLLO_ENV} bash "${build_sh}" --config=gpu "$@"
            ;;
        build_opt_gpu)
            env ${APOLLO_ENV} bash "${build_sh}" --config=opt_gpu "$@"
            ;;
        build_prof)
            env ${APOLLO_ENV} bash "${build_sh}" --config=cpu_prof "$@"
            ;;
        build_teleop)
167
            env ${APOLLO_ENV} bash "${build_sh}" --config=teleop "$@"
168 169 170 171 172 173 174
            ;;
        build_fe)
            build_dreamview_frontend
            ;;
        test)
            env ${APOLLO_ENV} bash "${test_sh}" --config=unit_test "$@"
            ;;
175 176 177
        coverage)
            env ${APOLLO_ENV} bash "${coverage_sh}" "$@"
            ;;
178 179 180 181 182 183
        cibuild)
            env ${APOLLO_ENV} bash "${ci_sh}" "build"
            ;;
        citest)
            env ${APOLLO_ENV} bash "${ci_sh}" "test"
            ;;
L
liuxu 已提交
184 185 186
        cilint)
            env ${APOLLO_ENV} bash "${ci_sh}" "lint"
            ;;
187 188 189 190 191 192 193 194 195 196 197
        check)
            build_test_and_lint
            ;;
        buildify)
            env ${APOLLO_ENV} bash "${APOLLO_ROOT_DIR}/scripts/apollo_buildify.sh"
            ;;
        lint)
            # FIXME(all): apollo_lint.sh "$@" when bash/python scripts are ready.
            env ${APOLLO_ENV} bash "${APOLLO_ROOT_DIR}/scripts/apollo_lint.sh" cpp
            ;;
        clean)
S
storypku 已提交
198
            env ${APOLLO_ENV} bash "${APOLLO_ROOT_DIR}/scripts/apollo_clean.sh" "-a"
199 200 201 202
            ;;
        doc)
            env ${APOLLO_ENV} bash "${APOLLO_ROOT_DIR}/scripts/apollo_docs.sh" "$@"
            ;;
203 204 205
        format)
            env ${APOLLO_ENV} bash "${APOLLO_ROOT_DIR}/scripts/apollo_format.sh" "$@"
            ;;
206 207 208 209 210 211 212 213 214 215 216 217 218
        usage)
            _usage
            ;;
        -h|--help)
            _usage
            ;;
        *)
            _usage
            ;;
    esac
}

main "$@"
219