From 911c5a6c0b6735458568ae302f2516d32236568e Mon Sep 17 00:00:00 2001 From: storypku Date: Tue, 23 Jun 2020 16:06:29 +0800 Subject: [PATCH] Bazel: refactored apollo_build.sh --- apollo6.sh | 2 +- scripts/apollo_build.sh | 109 ++++++++++++++++----------------------- scripts/apollo_config.sh | 1 + 3 files changed, 46 insertions(+), 66 deletions(-) diff --git a/apollo6.sh b/apollo6.sh index 54f350697b..cc1e3e9076 100755 --- a/apollo6.sh +++ b/apollo6.sh @@ -65,7 +65,7 @@ function apollo_env_setup() { check_architecture_support check_platform_support check_minimal_memory_requirement - determine_gpu_use + # determine_gpu_use # work done by scripts/apollo.bashrc determine_esdcan_use APOLLO_ENV="${APOLLO_ENV} STAGE=${STAGE}" diff --git a/scripts/apollo_build.sh b/scripts/apollo_build.sh index 3ae1e81824..84a2b96218 100755 --- a/scripts/apollo_build.sh +++ b/scripts/apollo_build.sh @@ -6,8 +6,6 @@ source "${TOP_DIR}/scripts/apollo.bashrc" ARCH="$(uname -m)" -# STAGE="${STAGE:-dev}" -: ${STAGE:=dev} : ${USE_ESD_CAN:=false} COMMAND_LINE_OPTIONS= @@ -42,7 +40,7 @@ function determine_targets() { exceptions="$(determine_disabled_targets ${compo})" fi if [ -z "${compo}" ]; then - targets="//... ${exceptions}" + targets="//modules/... union //cyber/... ${exceptions}" else targets="//modules/drivers/... ${exceptions}" fi @@ -61,43 +59,53 @@ function determine_targets() { echo "${targets}" } -function parse_cmdline_options() { - local build_mode="build" - local compilation_mode="fastbuild" - local args_to_pass_on="" +function _arg_parse() { + local __retopts="$1" + local __retargs="$2" + shift 2 + local known_options="" + local remained_args="" while [ "$#" -gt 0 ]; do - local option="$1" - case "${option}" in - --mode) - build_mode="$(_check_build_mode $2)"; shift 2 + local opt="$1" + local optarg + + case "${opt}" in + --config=*) + optarg="${opt#*=}" + known_options="${known_options} ${opt}" + shift + ;; + --config) + optarg="${2}"; shift 2 + # check here + known_options="${known_options} ${opt} ${optarg}" ;; - -c|--compilation_mode) - compilation_mode="$(_check_compilation_mode $2)"; shift 2 + -c) + optarg="$2"; shift 2 + # check here + known_options="${known_options} ${opt} ${optarg}" ;; *) - # Pass arguments we don't handle to bazel - args_to_pass_on="${args_to_pass_on} ${option}"; shift + remained_args="${remained_args} ${opt}" + shift ;; esac done + eval ${__retopts}="'${known_options}'" + eval ${__retargs}="'${remained_args}'" +} - local myopts="" - if [ "${compilation_mode}" != "fastbuild" ]; then - myopts="${myopts} -c ${compilation_mode}" - fi - # TODO(all): - # Corner cases: build_gpu if env USE_GPU=0, build_cpu if env USE_GPU=1 - # And, interaction with apollo.bazelrc - if [ "${build_mode}" == "build_cpu" ]; then - myopts="${myopts} --cxxopt=-DCPU_ONLY" - elif [ "${USE_GPU}" -eq 1 ]; then - myopts="${myopts} --config=gpu" - fi - myopts="${myopts} --cxxopt=-DUSE_ESD_CAN=${USE_ESD_CAN}" +function parse_cmdline_options() { + local remained_args="" + local known_options="" + _arg_parse known_options remained_args + + # FIXME(all): Use "--define USE_ESD_CAN=${USE_ESD_CAN}" instead + local myopts="${known_options} --cxxopt=-DUSE_ESD_CAN=${USE_ESD_CAN}" local targets - targets="$(determine_targets ${args_to_pass_on})" + targets="$(determine_targets ${remained_args})" COMMAND_LINE_OPTIONS="${myopts}" BUILD_TARGETS="${targets}" @@ -107,52 +115,23 @@ function parse_cmdline_options() { info "${TAB}Build Targets: ${GREEN}${BUILD_TARGETS}${NO_COLOR}" } -function _check_build_mode() { - local supported_modes=" build build_cpu build_gpu " - local mode="$1" - - if ! optarg_check_for_opt "--mode" "${mode}" ; then - exit 1 - fi - - if [[ "${supported_modes}" != *" ${mode} "* ]]; then - error "Unknown build mode: ${mode}. Supported values:" - error " ${supported_modes}" - exit 1 - fi - - echo "${mode}" -} - -function _check_compilation_mode() { - local supported_modes=" fastbuild dbg opt " - local mode="$1" - - if ! optarg_check_for_opt "-c" "${mode}" ; then - exit 1 - fi - - if [[ "${supported_modes}" != *" ${mode} "* ]]; then - error "Unknown compilation mode: ${mode}. Supported values:" - error " ${supported_modes}" - exit 1 - fi - echo "${mode}" +function _run_bazel_build_impl() { + local job_args="--jobs=$(nproc)" + bazel build --distdir="${APOLLO_CACHE_DIR}/distdir" "${job_args}" $@ } function bazel_build() { - parse_cmdline_options "$@" - if ! "${APOLLO_IN_DOCKER}" ; then error "The build operation must be run from within docker container" - exit 1 + # exit 1 fi - run bazel build --distdir="${APOLLO_CACHE_DIR}" "${COMMAND_LINE_OPTIONS}" "${BUILD_TARGETS}" + parse_cmdline_options "$@" + _run_bazel_build_impl "${COMMAND_LINE_OPTIONS}" "$(bazel query ${BUILD_TARGETS})" } function main() { - bazel_build "$@" + bazel_build $@ } main "$@" diff --git a/scripts/apollo_config.sh b/scripts/apollo_config.sh index 1e4fc90704..bd8fd58515 100755 --- a/scripts/apollo_config.sh +++ b/scripts/apollo_config.sh @@ -8,6 +8,7 @@ source "${TOP_DIR}/scripts/apollo.bashrc" IFS='' read -r -d '' STARTUP_TXT << EOF startup --output_user_root="${APOLLO_CACHE_DIR}/bazel" +common --distdir="${APOLLO_CACHE_DIR}/distdir" EOF set -e -- GitLab